Product类代码发一下
import java.util.Scanner;
public class Generator {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("输入产品信息:");
String str=in.next();
Product product=createProduct(str,"-");
System.out.println(product);
}
public static Product createProduct(String str,String deli){
Product product=new Product();
String[] split = str.split(deli);
product.setName(split[0]);
product.setQuantity(Integer.parseInt(split[1]));
product.setPrice(Float.parseFloat(split[2]));
return product;
}
}
一个product类,一个测试类,一会给你个代码
Generatorl.java
public class Generator {
public static Product createProduct(String str,String deli){
Product pro = new Product();
int index = str.indexOf(deli);
String name = str.substring(0, index);
int index2 = str.indexOf(deli, index+1);
String nmb = str.substring(index+1,index2);
String price = str.substring(index2+1,str.length());
pro.setName(name);
pro.setQuantity(Integer.parseInt(nmb));
pro.setPrice(Float.parseFloat(price));
return pro;
}
public static void main(String[] args){
Product p = createProduct("monitor-100-999.9","-");
System.out.println(p.toString());
}
}
Product.java
public class Product {
private String name;
private int quantity;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Product [name=" + name + ", quantity=" + quantity + ", price="
+ price + "]";
}
}
你得用题干中的Product,替换我这个就行,不能用我的
import java.math.BigDecimal;
public class Product {
public String name;
public Integer quantity;
public BigDecimal price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", quantity=" + quantity +
", price=" + price +
'}';
}
}
import java.math.BigDecimal;
import java.util.Scanner;
public class Generator {
public static Product createProduct(String str, String deli) {
String[] split = str.split(deli);
Product product = new Product();
product.setName(split[0]);
product.setQuantity(Integer.valueOf(split[1]));
product.setPrice(new BigDecimal(split[2]));
return product;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String nextLine = scanner.nextLine();
Product product = createProduct(nextLine, "-");
System.out.println(product.toString());
}
}
}
import java.util.Scanner;
class Product {
private String _name;
private int _quantity;
private double _price;
public Product() {
_quantity = 0;
_price = 0.0;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public int getQuantity() {
return _quantity;
}
public void setQuantity(int quantity) {
_quantity = quantity;
}
public double getPrice() {
return _price;
}
public void setPrice(double price) {
_price = price;
}
public String toString() {
return "name: " + _name + ", quantity: " + _quantity + ", price: " + _price;
}
}
class Generator {
public static Product createProduct(String str, String deli) {
Product product = new Product();
String[] s = str.split(deli);
product.setName(s[0]);
product.setQuantity(Integer.parseInt(s[1]));
product.setPrice(Double.parseDouble(s[2]));
return product;
}
public static void main(String[] args) {
System.out.println("Enter product info: ");
Scanner scanner = new Scanner(System.in);
Product product = Generator.createProduct(scanner.nextLine(), "-");
System.out.println(product.toString());
}
}