面向对象--购物车
代码预览
商品类
package ShoppingCart;
public class Product {
private int id;// 商品编号
private String name;// 商品名称
private double price;// 单价
public Product(int id, String name,double price) {
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
商品清单
package ShoppingCart;
public class ProductItem {
private Product product;//购买的商品
private int count;//商品数量
public ProductItem() {
}
public ProductItem(Product product, int count) {
this.product = product;
this.count = count;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double totalMoney(){//小计
double price=product.getPrice();//获取商品单价
return price*count;
}
}
购物车类(即购物的功能实现)
package ShoppingCart;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ShoppingCart {
private Map<Integer,ProductItem> map=new LinkedHashMap<Integer,ProductItem>();
public void addProduct(Product p){//添加商品
int productId=p.getId();
if(map.containsKey(productId)){ //判断商品编号是否存在
ProductItem productItem=map.get(productId);
productItem.setCount(productItem.getCount()+1); //数量增加
}else{
map.put(productId, new ProductItem(p,1));//新增商品
}
}
public void showAll(){//查看订单信息
Collection<ProductItem> productItems = map.values();//获取键值
Iterator<ProductItem> iterator = productItems.iterator();
while(iterator.hasNext()){
ProductItem productItem = iterator.next();
Product product = productItem.getProduct();
System.out.println("编号:"+product.getId()+",名称:"
+product.getName()+",单价:"+product.getPrice()+",数量:"+productItem.getCount()
+",小计:"+productItem.totalMoney());
}
}
public boolean deleteProduct(int productId){//删除商品
if(map.containsKey(productId)){
map.remove(productId);
return true;
}
return false;
}
public boolean modifyProduct(int productId,int count){//修改商品数量
if(map.containsKey(productId)){
if(count>=1){
ProductItem productItem = map.get(productId);
productItem.setCount(count);
return true;
}else if(count==0){//删除某商品
deleteProduct(productId);
return true;
}
}
return false;
}
public void clearCart(){//清空购物车
map.clear();
}
public double totalAllMoney(){//商品总价值
double total=0;
Collection<ProductItem> productItems = map.values();
Iterator<ProductItem> iterator = productItems.iterator();
while(iterator.hasNext()){
ProductItem productItem = iterator.next();
double money=productItem.totalMoney();
total+=money;
}
return total;
}
}
测试
package ShoppingCart;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ShoppingCart cart = new ShoppingCart();
Product p1 = new Product(1, "华硕笔记本", 4599);
Product p2 = new Product(2, "小米电视机", 2999.9);
Product p3 = new Product(3, "NIKE外套", 328);// 测试买两件商品的情况
cart.addProduct(p1);
cart.addProduct(p2);
cart.addProduct(p3);
cart.showAll();
System.out.println("------------------------");
System.out.println("请输入你要删除的商品编号:");
int id=in.nextInt();
boolean flag = cart.deleteProduct(id);
if (flag) {
System.out.println("编号为:" + id + "的商品删除成功!");
} else {
System.out.println("删除失败");
}
cart.showAll();
System.out.println("------------------------");
System.out.println("请输入你要修改的商品编号:");
int id1=in.nextInt();
System.out.println("请输入你要修改的商品数量:");
int count=in.nextInt();
boolean flag2 = cart.modifyProduct(id1, count);
if (flag2) {
System.out.println("编号为:" + id1 + "的商品修改成功!");
} else {
System.out.println("修改失败");
}
cart.showAll();
System.out.println("总价为:" + cart.totalAllMoney());
}
}