面向对象设计(购物车)
Git源码
https://gitee.com/smyn77/caicai77.git
前期调查
系统功能结构图
系统描述
该购物车系统提供了注册登录、增减商品等功能。用户注册账号后,选择登录。登录成功后,系统显示菜单,菜单提供增加、删除、修改商品和清空购物车功能。用户通过菜单里的选项来完成相应功能。
UML类图
关键代码
商品类————描述商品的属性(编号、名称、分类、单价)
class Product {
private int productId;// 商品编号
private String productName;// 商品名称
private String category;// 商品分类
private double price;// 单价
public Product(int productId, String productName, String category, double price) {
this.productId = productId;
this.productName = productName;
this.category = category;
this.price = price;
}
public String toString() {
return "Product [productId=" + productId + ", productName=" + productName + ", category=" + category
+ ", price=" + price + "]";
}
public int getProductId() {// 获取商品编号
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {// 获取商品名字
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {// 设置种类
this.category = category;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {// 设置价格
this.price = price;
}
}
商品信息类————描述商品数量、总价
class ProductItem {
private Product product;// 购买的商品(包含商品编号,名称,分类,单价)
private int count;// 商品数量
public double totalMoney() {// 小计
double price = product.getPrice();// 获取商品单价
return price * count; // 获取这个商品的总的价格
}
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;
}
}
购物车类————描述用户的购物车内容
class ShoppingCart {// 购物车
private Map<Integer, ProductItem> map = new LinkedHashMap<Integer, ProductItem>();
// 集合
public void addProduct(Product p, int count) {// 添加商品
int productId = p.getProductId();
if (map.containsKey(productId)) {// 如果购物车存在同样的商品,则此对象的数量加一
ProductItem productItem = map.get(productId);
productItem.setCount(productItem.getCount() + count);
} else {
map.put(productId, new ProductItem(p, count));// 如果没有,则在map容器里新建一个ProductItem
}
}
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.getProductId() + ", 商品名称:" + product.getProductName() + ", 单价:"
+ 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;
}
public Map<Integer, ProductItem> getMap() {
return map;
}
public void setMap(Map<Integer, ProductItem> map) {
this.map = map;
}
}
菜单类————登录界面
class Menu {
private static Map<Integer, Product> map = new LinkedHashMap<Integer, Product>();
static {// 商品编号// 商品名称// 商品分类// 单价
Product p1 = new Product(1, "华硕笔记本", "笔记本", 4599);
Product p2 = new Product(2, "苹果", "水果", 5.9);
Product p3 = new Product(3, "彩电", "家电", 2799);
Product p4 = new Product(4, "秋裤", "服装", 128);
Product p5 = new Product(5, "华为手机", "手机", 2998);
map.put(p1.getProductId(), p1);
map.put(p2.getProductId(), p2);
map.put(p3.getProductId(), p3);
map.put(p4.getProductId(), p4);
map.put(p5.getProductId(), p5);
}
public static void showMenu() {
System.out.println("****欢迎来到青春有你514的购物系统****");
System.out.println("* [4]输出商品菜单 *");
System.out.println("* [5]输入所需商品 *");
System.out.println("* [6]删除所需商品 *");
System.out.println("* [7]修改所需商品 *");
System.out.println("* [8]清空购物车 *");
System.out.println("* [9]返回登录界面 *");
System.out.println("*************感谢光临****************");
}
public static void showOption() {
System.out.println("编号1:华硕笔记本,单价为:4599");
System.out.println("编号2:苹果,单价为:5.9");
System.out.println("编号3:彩电,单价为:2799");
System.out.println("编号4:秋裤,单价为:128");
System.out.println("编号5:华为手机,单价为:2998");
}
public static void inputOption(ShoppingCart cart) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要购买的商品编号及个数(以end为结束标志):");
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (s.equals("end")) {
break;
} else {
String[] strings = s.split(" ");
int id = Integer.parseInt(strings[0]);
int count = Integer.parseInt(strings[1]);
Product p = map.get(id);
cart.addProduct(p, count);
}
}
System.out.println("***********用户总加购的商品*****************");
cart.showAll();
System.out.println("商品总价钱为:" + cart.totalAllMoney());
System.out.println();
}
public static void deleteOption(ShoppingCart cart) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要删除的商品编号:(以end为结束标志)");
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (s.equals("end")) {
break;
} else {
int id = Integer.parseInt(s);
boolean flag = cart.deleteProduct(id);
if (flag) {
System.out.println("商品编号:" + id + "的商品删除成功!");
} else {
System.out.println("商品编号" + id + "删除失败,购物车没有该商品!!");
}
}
}
System.out.println("***********商品删除信息*****************");
cart.showAll();
System.out.println("商品总价钱为:" + cart.totalAllMoney());
System.out.println();
}
public static void modifyOption(ShoppingCart cart) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要修改的商品编号及个数(以end为结束标志):");
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (s.equals("end")) {
break;
} else {
String[] strings = s.split(" ");
int id = Integer.parseInt(strings[0]);
int count = Integer.parseInt(strings[1]);
boolean flag = cart.modifyProduct(id, count);
if (flag)
System.out.println("商品编号:" + id + "的商品修改成功!");
else
System.out.println("购物车中没有该商品,修改失败!!");
}
}
System.out.println("***********商品修改信息*****************");
cart.showAll();
System.out.println("商品总价钱为:" + cart.totalAllMoney());
System.out.println(" ");
}
public static void clearOption(ShoppingCart cart) {
cart.clearCart();
System.out.println("***********清空购物车*****************");
cart.showAll();
System.out.println("商品总价钱为:" + cart.totalAllMoney());
}
}
用户类————用户登录的账号、密码、购物车
class User {
private int userID;
private String passWord;
private ShoppingCart cart;
private static int nextId = 1;
{
this.cart = new ShoppingCart();
this.userID = nextId++;
}
@Override
public String toString() {
String s = String.valueOf(userID);
return "userId = " + s + ",passWord = " + passWord;
}
public int getUserID() {
return userID;
}
public String getPassWord() {
return passWord;
}
public ShoppingCart getCart() {
return cart;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public void setCart(ShoppingCart cart) {
this.cart = cart;
}
}