第1次任务:购物车程序的面向对象设计
2021/10/10 22:47:04
本文主要是介绍第1次任务:购物车程序的面向对象设计,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、人员分工
任务 | 姓名 |
---|---|
编码规范,面向对象设计 | 王奥运 |
前期调查与功能设计,PPT制作或博客制作 | 陈伟盛 |
二、前期调查
前期调查以京东为例,
购物车中展示的项目有:商品名称,商品单价,商品数量,商品总价。其中商品数量可选,商品可删除。
进入结算前,可选多种商品。结算时有一次确认订单,可返回购物车修改。
三、系统功能结构图
四、系统描述:一段用来描述系统功能与流程的文字,用红色字代表可能的对象(名词)或属性,用蓝色字代表可能的方法(动词)
五、UML类图:类的关键属性与方法、类与类之间的关系。每个类的功能描述。可使用ProcessOn绘制。
六、本系统哪里体现了面向对象的封装性。可选:哪里体现了继承与多态。
商品的名称单价数量总价等属性不可直接修改不可访问,只能通过对外接口如查询商品信息,修改数量,删除商品等。
七、项目包结构与关键代码:项目的包结构(为什么要这样设计包结构),主要功能(如网购物车添加、删除商品)的流程图与关键代码。
购物车类
package com.jmu.shoppingcart; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Cart { private BigDecimal totalPrice = new BigDecimal("1"); Map<Commodity, Integer> cart = new HashMap<>(); public Cart() { } @Override public String toString() { return cart + ""; } public void updateCart() { System.out.print("请输入要修改的商品的名称:"); Scanner Sc = new Scanner(System.in); String name = Sc.nextLine(); Commodity commodity = findCommodity(name); if (commodity != null) { System.out.print("请输入您要修改的数量:"); int number = Integer.valueOf(Sc.nextLine()); int addnumber = cart.get(commodity) - number; if (!updateNumber(commodity, number)) { System.out.println("您输入的数量有误,请重新输入"); updateCart(); } // else { // heixin.commoditys.put(commodity, heixin.commoditys.get(commodity) + addnumber); // } } else { System.out.println("购物车内没有该商品,请重新输入"); updateCart(); } } public boolean updateNumber(Commodity commodity, int number) { if (cart.get(commodity) < number) { return false; } else if (number == 0) { cart.remove(commodity); return true; } cart.put(commodity, number); return true; } public void add(Commodity goods, int number) { if(cart.containsKey(goods)){ cart.put(goods, cart.get(goods)+number); }else { cart.put(goods, number); } } public BigDecimal price() { if (cart.isEmpty()) { return new BigDecimal("0"); } totalPrice = new BigDecimal("0"); cart.forEach((K, V) -> { BigDecimal k = new BigDecimal(K.getPrice() + ""); BigDecimal v = new BigDecimal(V + ""); BigDecimal temp1 = k.multiply(v); totalPrice = totalPrice.add(temp1); //System.out.println(totalPrice); }); return totalPrice; } public Commodity findCommodity(String name) { for (Commodity commodity : cart.keySet()) { if (commodity.getName().equals(name)) { return commodity; } } return null; } public void showCart() { System.out.println("===============您的购物车============="); System.out.println("ID\t\t\tname\t\t\t\tprice\t\t\t\tnumber"); cart.forEach((K,V)->{ System.out.println(K+"\t"+V); }); } }
商品类
package com.jmu.shoppingcart; public class Commodity { private String name; private double price; private int ID; public Commodity(String name, double price, int ID) { this.name = name; this.price = price; 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; } public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } @Override public String toString() { return ID+"\t\t\t"+name+"\t\t\t\t"+price+"\t\t\t\t"; } }
商店类
package com.jmu.shoppingcart; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class Store { private static int ID=0; Map<Commodity,Integer> commoditys= new LinkedHashMap<>(); public void add(String name,double price,int number){ Commodity commodity=new Commodity(name,price,ID++); commoditys.put(commodity,number); } public void showStory(){ System.out.println("ID\t\t\tname\t\t\t\tprice\t\t\t\tnumber"); commoditys.forEach((K,V)->{ System.out.println(K+"\t"+V); }); } public Commodity findCommodity(String name) { for (Commodity commodity : commoditys.keySet()) { if(commodity.getName().equals(name)){ return commodity; } } return null; } public int getRepertory(Commodity commodity) { return commoditys.get(commodity); } public void update(Commodity commodity,int number) { commoditys.put(commodity,commoditys.get(commodity)-number); } }
这篇关于第1次任务:购物车程序的面向对象设计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?