JAVA map编写购物车的问题
本人是初学者,老师安排了这个作业。第三个编写ShopingCart类,怎么写呢?HashMap<Integer,ProductItem>items=newHashMap<...
本人是初学者,老师安排了这个作业。第三个编写ShopingCart类,怎么写呢?
HashMap<Integer, ProductItem> items = new HashMap<Integer,ProductItem>();
//增加购物车物品
public void produceItemAdd(int i, ProductItem p ){
items.put(i, p);
}
感觉这样写不对,求大神帮忙解答下。谢谢!号上只有15分了,全部给了,希望大神不吝赐教。 展开
HashMap<Integer, ProductItem> items = new HashMap<Integer,ProductItem>();
//增加购物车物品
public void produceItemAdd(int i, ProductItem p ){
items.put(i, p);
}
感觉这样写不对,求大神帮忙解答下。谢谢!号上只有15分了,全部给了,希望大神不吝赐教。 展开
2个回答
展开全部
public class Product {
private Integer productid;
private String productname;
private String category;
private float price;
private String picture;
public Product(Integer productid, String productname, String category, float price, String picture) {
this.productid = productid;
this.productname = productname;
this.category = category;
this.price = price;
this.picture = picture;
}
public Integer getProductid() {
return productid;
}
public void setProductid(Integer productid) {
this.productid = productid;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
}
public class ProductItem {
private Product product;
private Integer count;
public ProductItem(Product product, Integer count) {
this.product = product;
this.count = count;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public float getTotalPrice() {
return this.product.getPrice()*count;
}
}
public class ShoppingCart {
private HashMap<Integer, ProductItem> items = new HashMap<Integer, ProductItem>();
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
System.out.println("初始化产品");
Product banana = new Product(10000, "香蕉", "水果", 2.50F, "banana.jpg");
Product apple = new Product(10001, "苹果", "水果", 3.99F, "apple.jpg");
Product orange = new Product(10002, "桔子", "水果", 1.80F, "orange.jpg");
Product pen = new Product(10003, "钢笔", "文具", 12.00F, "pen.jpg");
Product pencil = new Product(10004, "铅笔", "文具", 1.00F, "pencil.jpg");
System.out.println("###############################################");
System.out.println("添加苹果");
cart.addToCart(apple);
System.out.println(cart.showAll());
System.out.println("当前购物车总价:" + cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("添加香蕉");
cart.addToCart(banana);
System.out.println(cart.showAll());
System.out.println("当前购物车总价:" + cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("修改香蕉数量");
cart.modifyCart(banana, 5);
System.out.println(cart.showAll());
System.out.println("当前购物车总价:" + cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("删除苹果");
cart.delFromCart(apple);
System.out.println(cart.showAll());
System.out.println("当前购物车总价:" + cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("添加铅笔");
cart.addToCart(pencil);
System.out.println(cart.showAll());
System.out.println("当前购物车总价:" + cart.getTotalPrice());
System.out.println("***********************************************");
}
public void addToCart(Product product) {
this.items.put(product.getProductid(), new ProductItem(product, 1));
}
public void delFromCart(Product product) {
this.items.remove(product.getProductid());
}
public String showAll() {
String detail = "";
for (Integer key : this.items.keySet()) {
detail += this.items.get(key).getProduct().getProductname() + ":" + this.items.get(key).getCount() + "件,单价 " + this.items.get(key).getProduct().getPrice() + ",小计" + this.items.get(key).getTotalPrice() + "\n";
}
return detail;
}
public boolean modifyCart(Product product, Integer count) {
if (this.items.containsKey(product.getProductid())) {
this.items.get(product.getProductid()).setCount(count);
return true;
}
return false;
}
public float getTotalPrice() {
float total = 0;
for (Integer key : this.items.keySet()) {
total += this.items.get(key).getTotalPrice();
}
return total;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询