购物车系统改进----java编写
大家好! 这是我第一次使用 CSDN博客 来记录分享自己的旅途。欢迎大家指点我的不足,我是个菜鸟新人**^ - ^**
本人 @kingsirvince
Java 基础–集合联系
今天集合内容学完了 ,练习题是购物车系统:
- 添加商品;
- ** 删除**商品;
- 清空购物车;
- 显示每类价格,总价格;
分析
- 创建类:商品类,购物车类,测试类
- 商品类Product:包含变量:id, name, price. 无参构造,有参构造,toString, get set, hashCode equals .
- 购物车类ShopCart:包含成员变量 productMap,(用Map的 key和value对应关系来记录购买的商品名称和数量),总价格BigDecimal totalPrice (涉及金融的用BigDecimal,保证运算精度),
- 购物车方法:添加add,删减remove,清空clear,显示总价print
- 测试类Exercise06: 写入商品详细信息,商品类赋值;Scanner收集键盘输入,判断购物车动作,调用动作
商品类
// 商品类 Product
package com.king.ShopCart;
import java.math.BigDecimal;
public class Product {
private int id;
private String name;
private BigDecimal price;
public Product(){
}
public Product(int id, String name, BigDecimal price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (</