商品类
public class Goods {
private String id;
private String name;
private double price;
public Goods() {
super();
}
public Goods(String id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
商品项
package IWillPassYou_ST2;
public class ShopItem {
private Goods goods;
private int amount;
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public ShopItem(){
super();
}
public ShopItem(Goods goods,int amount){
super();
this.goods=goods;
this.amount=amount;
}
public double singleSum() {
return goods.getPrice()*amount;
}
@Override
public String toString() {
return "[ 购物项 | 商品编号 :"+goods.getId()+"| 商品名称:"+goods.getName()+"| 商品单价:"+goods.getPrice()
+"| 购买数量:"+amount+"| 小计:"+singleSum()+" ]";
}
}
购物车
package IWillPassYou_ST2;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
public class ShopCar {
private HashMap<String, ShopItem> car = null;
public ShopCar() {
car = new HashMap<>();
}
public void addItem(ShopItem si) {
Set<String> keys = car.keySet();
if (si != null) {
for (String key : keys) {
String cid = car.get(key).getGoods().getId();
String id = si.getGoods().getId();
if (cid.equals(id)) {
car.get(key).setAmount(car.get(key).getAmount() + si.getAmount());
return;
}
}
car.put(si.getGoods().getId(), si);
}
}
public boolean delItem(String id) {
boolean flag = false;
if (car.get(id) != null) {
flag = true;
car.remove(id);
}
return flag;
}
public boolean updateItem(String id, int amount) {
boolean flag = false;
if (car.get(id) != null) {
if (amount <= 0) {
car.get(id).setAmount(0);
} else {
car.get(id).setAmount(amount);
;
}
flag = true;
}
return flag;
}
public double getTotalPrice() {
double total = 0;
Collection<ShopItem> items = car.values();
for (ShopItem item : items) {
total += item.singleSum();
}
return total;
}
public void showCar() {
Collection<ShopItem> items = car.values();
ShopItem[] citems = items.toArray(new ShopItem[items.size()]);
Arrays.sort(citems, new Comparator<ShopItem>() {
@Override
public int compare(ShopItem o1, ShopItem o2) {
if (o1.singleSum() > o2.singleSum()) {
return 1;
} else if (o1.singleSum() < o2.singleSum()) {
return -1;
} else {
return 0;
}
}
});
for (int i = 0; i < citems.length; i++) {
System.out.println(citems[i].toString());
}
System.out.println("\t\t\t\t\t总计:" + getTotalPrice());
}
}
商铺
package IWillPassYou_ST2;
import java.util.Scanner;
public class TaoBao {
private ShopCar shopCar = null;
public TaoBao() {
shopCar = new ShopCar();
}
public void mainMenu() {
Scanner input = new Scanner(System.in);
System.out.println("********浪矢杂货铺*********");
System.out.println("1.添加商品");
System.out.println("2.删除商品");
System.out.println("3.修改商品");
System.out.println("4.查看购物车");
System.out.println("0.结束本次服务");
System.out.println("请选择:");
int choice = input.nextInt();
switch (choice) {
case 0:
System.out.println("欢迎下次光临");
System.exit(0);
break;
case 1:
addProduct();
mainMenu();
break;
case 2:
delProduct();
mainMenu();
break;
case 3:
updateProduct();
mainMenu();
break;
case 4:
showProduct();
mainMenu();
break;
default:
System.out.println("输入错误!");
System.exit(0);
break;
}
input.close();
}
private void updateProduct() {
System.out.println("*******更新购物车商品*******");
shopCar.showCar();
Scanner input = new Scanner(System.in);
System.out.println("请选择要更新的商品编号");
String id = input.next();
System.out.println("请输入要更新的商品数量");
int amount = input.nextInt();
boolean flag = shopCar.updateItem(id, amount);
if (flag) {
System.out.println("更新商品成功");
shopCar.showCar();
} else {
System.out.println("更新商品失败");
shopCar.showCar();
}
}
private void delProduct() {
System.out.println("*******删除商品*******");
shopCar.showCar();
Scanner input = new Scanner(System.in);
System.out.println("请选择要删除的商品编号");
String id = input.next();
boolean flag = shopCar.delItem(id);
if (flag) {
System.out.println("删除商品成功");
shopCar.showCar();
} else {
System.out.println("删除商品失败");
shopCar.showCar();
}
input.close();
}
private void showProduct() {
System.out.println("*******购物车清单*******");
shopCar.showCar();
}
private void addProduct() {
Scanner input = new Scanner(System.in);
System.out.println("**********浪矢杂货铺************");
System.out.println("编号\t商品名字\t商品单价");
System.out.println("1.\t香蕉\t¥10");
System.out.println("2.\t苹果\t¥20");
System.out.println("3.\t大鸭梨\t¥5");
System.out.println("4.\t榴莲\t¥50");
System.out.println("请输入要购买的商品编号");
int id = input.nextInt();
System.out.println("请输入要购买的商品数量");
int amount = input.nextInt();
ShopItem item = null;
if (id == 1) {
item = new ShopItem(new Goods(String.valueOf(id), "香蕉", 10), amount);
} else if (id == 2) {
item = new ShopItem(new Goods(String.valueOf(id), "苹果", 20), amount);
} else if (id == 3) {
item = new ShopItem(new Goods(String.valueOf(id), "大鸭梨",5), amount);
} else if (id == 4) {
item = new ShopItem(new Goods(String.valueOf(id), "榴莲", 50), amount);
}
shopCar.addItem(item);
System.out.println("已添加至购物车");
}
}
测试类
package IWillPassYou_ST2;
public class Test {
public static void main(String[] args) {
TaoBao b = new TaoBao();
b.mainMenu();
}
}