一、项目准备
1、开发工具:JDK、Eclipse
2、需求分析:
包括商品管理和购物车管理。
1)商品管理主要功能
-
商品信息导入
-
显示所有商品信息
2)购物车主要功能
-
添加商品到购物车
-
修改购物车中的商品数量
-
显示购物车中的所有商品信息
-
结算
4、详细设计
包括如下类的设计:
-
商品类(Goods)
-
商品管理类(GoodsManage)
-
购物车中的商品信息类(GoodsInCart)
-
购物车类(ShoppingCart)
-
测试类 (TestDemo)
-
下面分别对各个类进行介绍:
1)商品类(Goods),包括如下属性和方法
方法:
-构造方法
-getter和setter方法
-hashCode()和equals()方法
-toString()方法
2)商品管理类(GoodsManage)
属性:存放商品的容器(goodsSet):Set类型
方法:
-构造方法
-getter和setter方法
-商品信息导入:public void importGoods();
-显示所有商品信息:public void displayAllGoods();
3)购物车中的商品信息类(GoodsInCart)
属性:
- 商品信息(goods):Goods类型
- 商品数量(num):int类型
方法:
-构造方法
-getter和setter方法
4)购物车类(ShoppingCart)
属性:
- 购物车(shoppingCart):Map类型,其中key为String类型,value为GoodsInCart类型
方法:
-添加商品到购物车:
public void addGoodsToCart(GoodsManage gm);
-修改购物车中的商品数量:public void updateNumInCart();如果购物车中没有商品,需要给出提示信息
- 显示购物车中的所有商品:public void displayAllInCart();如果购物车中没有商品,需要给出提示信息
- 结算:public void settleAccounts();结算后购物车要清空。
- 构造方法及getter和setter方法
5)测试类(TestDemo)
- 根据需求设计完成主流程实现
代码实现:
-
商品类(Goods):
package pers.szl.shopping; public class Goods { private String goodsId; private String goodsName; private double price; private String goodDes; @Override public boolean equals(Object o) { if(o==this){ return true; } if(o instanceof Goods){ Goods goods=(Goods) o; if(this.getGoodsId().equals(goods.goodsId)&&this.goodsName.equals(goods.goodsName)&&this.price==goods.price&&this.goodDes.equals(goods.goodDes)) { return true; } } return false; } @Override public int hashCode() { return (goodsId.hashCode()+goodsName.hashCode()+goodDes.hashCode()+(int)price)*31; } public Goods() { } public Goods(String goodsId, String goodsName, double price, String goodDes) { this.goodsId = goodsId; this.goodsName = goodsName; this.price = price; this.goodDes = goodDes; } /** * 获取 * @return goodsId */ public String getGoodsId() { return goodsId; } /** * 设置 * @param */ public void setGoodsId(String goodsId) { this.goodsId = goodsId; } /** * 获取 * @return goodsName */ public String getGoodsName() { return goodsName; } /** * 设置 * @param */ public void setGoodsName(String goodsName) { this.goodsName = goodsName; } /** * 获取 * @return price */ public double getPrice() { return price; } /** * 设置 * @param price */ public void setPrice(double price) { this.price = price; } /** * 获取 * @return goodDes */ public String getGoodDes() { return goodDes; } /** * 设置 * @param goodDes */ public void setGoodDes(String goodDes) { this.goodDes = goodDes; } public String toString() { return "商品编号:" + goodsId + ",商品名称:" + goodsName + ",商品单价:" + price+"元" + ",商品描述:" + goodDes; } }
商品管理类(GoodsManage):
package pers.szl.shopping; import java.util.HashSet; public class GoodManage { HashSet<Goods> goodsSet = new HashSet<>(); public GoodManage() { } public void importGoods() {//商品信息导入 Goods g1 = new Goods("goods001", "水杯", 56.0, "不锈钢"); Goods g2 = new Goods("goods002", "电脑", 5556.0, "笔记本电脑"); Goods g3 = new Goods("goods003", "手机", 2556.0, "智能手机"); Goods g4 = new Goods("goods004", "饮水机", 556.0, "智能饮水机"); Goods g5 = new Goods("goods005", "篮球", 256.0, "专业篮球"); goodsSet.add(g1); goodsSet.add(g2); goodsSet.add(g3); goodsSet.add(g4); goodsSet.add(g5); System.out.println("商品信息导入成功!"); } public void displayAllGoods() {//显示所有商品信息 System.out.println("显示所有商品信息:"); for (Object object : goodsSet) { System.out.println(object); } } }
购物车中的商品信息类(GoodsInCart):
package pers.szl.shopping; public class GoodsInCart { private Goods goods; private int num; public GoodsInCart() { } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String toString() { return goods + ",商品数量:" + num +";"; } }
购物车类(ShoppingCart):
-
package pers.szl.shopping; import java.util.*; public class ShoppingCart { Map<String, GoodsInCart> shoppingCart = new HashMap<>(); Scanner sc=new Scanner(System.in); public ShoppingCart() { } public void addGoodsToCart(GoodManage gm ){//添加商品到购物车 GoodsInCart goods=new GoodsInCart(); if(gm.goodsSet.isEmpty()){ System.out.println("商品信息未导入,请先导入商品信息!"); return ; } gm.displayAllGoods(); System.out.println("请输入商品编号:"); String goodId=sc.next(); Set <String> keys=shoppingCart.keySet(); for(String key:keys){ if(key.equals(goodId)) { System.out.println("该商品已添加到购物车,请重新输入商品编号!"); addGoodsToCart(gm ); } } for (Goods object : gm.goodsSet) { if(object.getGoodsId().equals(goodId)){ goods.setGoods( object); System.out.println("请输入商品数量:"); int num=sc.nextInt(); while(num<=0){ System.out.println("商品数量不能为负,请重新输入商品数量:"); num=sc.nextInt(); } goods.setNum(num); shoppingCart.put(goods.getGoods().getGoodsId(),goods); System.out.println("添加商品到购物车成功!"); return; } } System.out.println("商品信息中不存在该编号的商品,请重新输入商品编号!"); addGoodsToCart( gm ); } public void updateNumInCart(){//修改购物车中的商品数量 GoodsInCart goods=new GoodsInCart(); if(shoppingCart!=null&&shoppingCart.isEmpty()){ System.out.println("购物车没有添加商品,请先进行添加商品操作!"); return; } System.out.println("请输入修改商品编号:"); String goodId=sc.next(); Set <String> keys=shoppingCart.keySet(); for(String key:keys){ if(key.equals(goodId)) { Goods goods1= shoppingCart.get(key).getGoods(); goods.setGoods(goods1); System.out.println("请输入修改商品数量:"); int num=sc.nextInt(); if(num==0){ shoppingCart.remove(key); System.out.println("商品数量为零,已从购物车中删除!"); return; } while(num<0){ System.out.println("商品数量不能为负,请重新输入商品数量:"); num=sc.nextInt(); } goods.setNum(num); goods.getGoods().setGoodsId(goodId); shoppingCart.put(goods.getGoods().getGoodsId(),goods); System.out.println("修改购物车中的商品数量成功!"); return; } } System.out.println("购物车中不存在该编号的商品,请重新输入商品编号!"); updateNumInCart(); } public void displayAllInCart() {//显示购物车中的商品信息 if(shoppingCart!=null&&shoppingCart.isEmpty()){ System.out.println("购物车没有添加商品,请先进行添加商品操作!"); return; } System.out.println("购物车商品信息如下:"); Set <String> keys=shoppingCart.keySet(); for(String key:keys){ System.out.println(shoppingCart.get(key).getGoods()+",商品数量:"+shoppingCart.get(key).getNum()); } } public void settleAccounts(){//结算 if(shoppingCart!=null&&shoppingCart.isEmpty()){ System.out.println("购物车没有添加商品,请先进行添加商品操作!"); return; } Set <String> keys=shoppingCart.keySet(); double sumPrice=0; for(String key:keys){ GoodsInCart value=shoppingCart.get(key); sumPrice+= value.getGoods().getPrice()*value.getNum(); } System.out.println("结算成功,您一共需要支付"+sumPrice+"元。"); displayAllInCart(); shoppingCart.clear(); System.out.println("支付成功,购物车已清空!"); } }
测试类(TestDemo):
package pers.szl.shopping; import java.util.Scanner; public class TestDemo { public static void main(String[]args){ Menu menu=new Menu(); Scanner input= new Scanner(System.in); while(true){ menu.home(); switch(input.next()){ case "1": menu.shoppingHome(); break; case "2": menu.goodsHome(); break; case "0": System.out.println("成功退出系统!"); System.exit(0); break; default: System.out.println("没有此操作,请重新输入!"); } } } }
菜单类Menu:
-
package pers.szl.shopping; import java.util.Scanner; public class Menu { Scanner input = new Scanner(System.in); ShoppingCart shoppingCart = new ShoppingCart(); GoodManage gm = new GoodManage(); public void home() { System.out.println(" ************************"); System.out.println(" **主菜单**"); System.out.println(" 1--商品管理"); System.out.println(" 2--购物车"); System.out.println(" 0--退出"); System.out.println(" ************************"); System.out.println("请输入对应数字进行操作:"); } public void shoppingHome() { boolean flag = true; while (flag) { System.out.println(" **************************"); System.out.println(" **商品管理**"); System.out.println(" 1--商品信息导入"); System.out.println(" 2--显示所有商品信息"); System.out.println(" 9--返回上一级菜单"); System.out.println(" **************************"); System.out.println("请输入对应数字对商品进行管理:"); switch (input.next()) { case "1": gm.importGoods(); break; case "2": gm.displayAllGoods(); break; case "9": System.out.println("成功返回上一级菜单!"); flag = false; break; default: System.out.println("输入错误,请重新输入!"); } } } public void goodsHome() { boolean flag = true; while (flag) { System.out.println(" ************************"); System.out.println(" **购物车管理**"); System.out.println(" 1--添加商品到购物车"); System.out.println(" 2--修改购物车中的商品数量"); System.out.println(" 3--显示购物车中的所有商品"); System.out.println(" 4--结算"); System.out.println(" 9--返回上一级菜单"); System.out.println(" ***********************"); System.out.println("请输入对应的数字对购物车进行管理:"); switch (input.next()) { case "1": shoppingCart.addGoodsToCart(gm); break; case "2": shoppingCart.updateNumInCart(); break; case "3": shoppingCart.displayAllInCart(); break; case "4": shoppingCart.settleAccounts(); break; case "9": System.out.println("成功返回上一级菜单!"); flag = false; break; default: System.out.println("输入错误,请重新输入!"); } } } }