java复杂题目,请java大神给出答案?
(1)编写一个商品类Product,定义属性描述商品的编号(String)、名称(String)、单价(float)、数量(int)等信息,定义4个参数的构造方法用于对商品对象的4个属性进行初始化,定义一个方法printProduct(),用于输出商品对象的四个属性值。
(2)创建一个测试类,在main方法中创建一个HashMap集合对象,要求使用泛型,键类型为String,值类型为Product。向Map对象中添加3个元素,value为Product对象,key为该对象的商品编号。之后对这个Map对象遍历,调用每个Product对象的printProduct()方法。 展开
Product.java
public class Product {
private String id;
private String name;
private float price;
private int num;
public Product(String id, String name, float price, int num) {
this.id = id;
this.name = name;
this.price = price;
this.num = num;
}
public void printProduct() {
System.out.println("商品编号:" + this.id);
System.out.println("商品名称:" + this.name);
System.out.println("商品单价:" + this.price);
System.out.println("商品数量:" + this.num);
}
}
TestMain.java
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class TestMain {
public static void main(String[] args) {
Map<String, Product> map = new HashMap<String, Product>();
Product p1 = new Product("P01", "文具", 5.87f, 12);
Product p2 = new Product("P02", "服装", 67.45f, 8);
Product p3 = new Product("P03", "百货", 17.62f, 35);
map.put("P01", p1);
map.put("P02", p2);
map.put("P03", p3);
for (Entry<String, Product> entry : map.entrySet()) {
entry.getValue().printProduct();
}
}
}
执行结果:
2020-05-07
public static void main(String[] args) {
Map<String, Product> map = new HashMap<>();
for (int i = 0; i < 3; i++) {
map.put(String.valueOf(i), new Product(String.valueOf(i), "名称" + i, i, i));
}
for (Map.Entry<String, Product> entry : map.entrySet()) {
entry.getValue().printProduct();
}
}
}
class Product {
private String productNo;
private String productName;
private float productPrice;
private int productAmount;
public Product(String productNo, String productName, float productPrice, int productAmount) {
this.productNo = productNo;
this.productName = productName;
this.productPrice = productPrice;
this.productAmount = productAmount;
}
public void printProduct() {
System.out.println("商品的编号: " + productNo + " ,名称: " + productName + " ,单价: " + productPrice + ", 数量: " + productAmount);
}
}
private String id; //编号
private String name; //名称
private float price; //单价
private int num; //数量
public static void main(String[] args) throws ParseException {
Map<String,Product> map = new HashMap<>();
Product product1 = new Product();
product1.setId("1");
product1.setName("商品1");
product1.setPrice(10);
product1.setNum(100);
Product product2 = new Product();
product2.setId("2");
product2.setName("商品2");
product2.setPrice(20);
product2.setNum(100);
Product product3 = new Product();
product3.setId("3");
product3.setName("商品3");
product3.setPrice(5);
product3.setNum(20);
map.put("1",product1);
map.put("2",product2);
map.put("3",product3);
for(String key : map.keySet()){
System.out.println(map.get(key).printProduct());
}
}
public String printProduct() {
return "Product{" +
"id='" + this.id + '\'' +
", name='" + this.name + '\'' +
", price=" + this.price +
", num=" + this.num +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}