第1次任务-面向对象设计(购物车)

本次大作业Gitee码库链接

一、目标

  • 学会使用讲故事的方式找到系统中可能包含的类与属性
  • 理解封装

二、前期调查

网购,被称作是中国“新四大发明”之一,作为一个中国的大学生,网购已然变成我们日常生活的一个重要部分,而中国的网购平台更是数不胜数

在网购之中,有一个非常人性化的功能,那就是购物车。购物过程中看到心仪的商品,可以先加入购物车,等到购物结束时再一起付款结账

当我们使用购物车时,只需要用鼠标轻轻一点就可以享受到它所带来的便利,不过,作为计算机学院的学生,我们要去利用所学的知识,来分析背后的原理,并运用已经学到的知识,来尝试实现购物车功能。其大致流程如下:

  • 顾客浏览商城,添加心仪的商品进购物车
  • 购物车存储商品信息(数量、总价等)
  • 顾客对商品信息进行更改
  • 顾客结算购物车

成员任务分配

任务 组员
代码命名规范与UML制作 游俊臻
功能设计 吴龙飞、王柏鸿
面向对象设计 王柏鸿、吴龙飞
前期调查和博客制作 张洸洋

三、系统功能结构图

四、UML类图

五、代码运行演示

初始菜单页面:

进入商城页面:

商品一览:

展示商城内现有的商品,供客户选择

查找商品:

输入商品的ID来搜索商品信息

购买商品:

输入商品的ID和数量来加入到购物单里

进入购物车:

购物车商品一览:

查看当前以及放入购物车的商品

查找商品(购物车内):

通过输入ID来查找已放入购物车内的商品

当查找的商品不存在时:

删除商品:

输入ID和数量,从购物车里删除相应的商品

当输入商品的商品数量大于实际的数量时,出现提示

清空购物车:

将购物车内的商品完全移出购物车

结算:

结算商品,算出购物车内商品的总价

五、部分代码展示

1、Main函数

  • 实现功能:实现各个函数的调用
package shopping_System;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner cin = new Scanner(System.in);
		int choice;//输入的选项
		int id;//输入的商品编号
		int num;//输入的商品数量

		while (true) {
			Menu.show_main_menu();
			choice = cin.nextInt();
			switch (choice) {
			case 1:// 进入商城进行操作
				while (true) {
					if (choice == 4)
						break;
					Menu.show_shelf();;
					choice = cin.nextInt();
					switch (choice) {
					case 1:// 商城菜单
						Shelf_lmpl.showshelf();;
						break;
					case 2://查找商品
						System.out.println("请输入要查找商品的id: ");
						id=cin.nextInt();
						if (Shelf_lmpl.search_shelf(id))
							;
						else
							System.out.println("不存在该商品!");
						break;
					case 3:// 购买商品
						System.out.println("请输入商品ID和数量:");
						id = cin.nextInt();
						num = cin.nextInt();
						Shelf_lmpl.shop(id, num);;
						System.out.println("商品添加成功!");
						break;
					case 4:
						break;
					default:
						System.out.println("请重新输入: ");
					}
				}
				break;
			case 2:// 进入购物车进行操作
				while (true) {
					if(choice==6)
						break;
					Menu.show_cart();
					choice = cin.nextInt();
					switch (choice) {
					case 1:// 购物车一览
						Cart_lmpl.showcart();
						break;
					case 2://查找商品
						System.out.println("请输入要查找商品的id: ");
						id=cin.nextInt();
						if (Cart_lmpl.search_cart(id))
							;
						else
							System.out.println("不存在该商品!");
						break;
					case 3://删除商品
						System.out.println("请输入要删除的商品Id和数量: ");
						id=cin.nextInt();
						num=cin.nextInt();
						Cart_lmpl.reduceGood(id, num);
						System.out.println("删除成功!");
						break;
					case 4://清空购物车
						Cart_lmpl.clearCart();;
						 System.out.println("清空购物车成功!");
						break;
					case 5://结算
						System.out.println("本次共消费: "+Cart_lmpl.totalAllMoney()+"元");
						break;
					case 6:// 返回主菜单
					default:
						System.out.println("请重新输入: ");
					}
				}
				break;
			case 3:
				System.out.println("退出成功");
				cin.close();
				System.exit(0);
			default:
				System.out.println("请重新输入: ");
			}
		}
	}
}

2、DAO接口

package shopping_System;

import commodities.commodity;

public interface DAO {
		public interface shelfDao {
			public static void showshelf() {//展示商品
			}
			public static commodity search(int id) {//查找货架上的商品
				return null;
			}
			public static void shop(int id,int num) {//购买商品
			}
		}
		public interface cartDao {
			public static void showcart() {//展示购物车
			} 
			public static commodity search(int id) {//查找购物车里的商品
				return null;
			}
			public static boolean reduceGood(int goodId,int count) {//删除商品
				return false;
			}
			public static void clearCart() {//清空购物车
			}
			public static int totalAllMoney() {//结算
				return 0;
			}
		}
}

3、购物车类

package shopping_System;

import commodities.commodity_message;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class Cart_lmpl implements DAO.cartDao{

	public static void showcart() {//展示购物车
		if (Shelf_lmpl.mapcart.isEmpty()) {//判断购物车是否为空
			System.out.println("购物车为空!");
		}
		else {
			Set keySet=Shelf_lmpl.mapcart.keySet();
			Iterator it=keySet.iterator();
			while(it.hasNext())	{//打印购物车
				Object key=it.next();
				Object value=Shelf_lmpl.mapcart.get(key);
				System.out.println("商品编号:"+key+" "+value);
		}
		}
	}
	public static boolean search_cart(int id) {
		if (Shelf_lmpl.mapcart.containsKey(id)) {
			System.out.println(Shelf_lmpl.mapcart.get(id).getGood());
			return true;
		} else
			return false;
	}
	public static boolean reduceGood(int goodId,int count) {//删除商品
		commodity_message goodItem = Shelf_lmpl.mapcart.get(goodId);
		if (Shelf_lmpl.mapcart.containsKey(goodId)) {
			if(count>goodItem.getCount()) {//输入数量>已有数量
				System.out.println("您的购物车里只有"+goodItem.getCount()+"件该商品,请不要输入超过该数量。");
				return false;
			}else if (count == goodItem.getCount()) {//输入数量=已有数量
				Shelf_lmpl.mapcart.remove(goodId);//直接将该商品移除map
				return true;
			} else if (count < goodItem.getCount()) {//输入数量<已有数量
					goodItem.setCount(goodItem.getCount()-count);
					return true;
			}
		}
		else System.out.println("购物车中没有该货物");
		return false;
	}
	public static void clearCart() {// 清空购物车
		Shelf_lmpl.mapcart.clear();
	}
	public static int totalAllMoney() {// 结算
		int totalmoney = 0;
		Collection<commodity_message> GoodItems = Shelf_lmpl.mapcart.values();
		Iterator<commodity_message> iterator = GoodItems.iterator();
		while (iterator.hasNext()) {
			commodity_message GoodItem = iterator.next();
			int money = GoodItem.totalMoney();
			totalmoney += money;
		}
		return totalmoney;
	}
}

4、商城类

package shopping_System;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import commodities.books;
import commodities.commodity;
import commodities.commodity_message;
import commodities.electronics;
import commodities.shoes;

public class Shelf_lmpl implements DAO.shelfDao{

	public static Map<Integer, commodity> mapshelf = new LinkedHashMap<Integer, commodity>();
	public static Map<Integer, commodity_message> mapcart = new LinkedHashMap<Integer, commodity_message>();

		static {
		electronics good1 = new electronics(1, "小米10", "电子产品", 3999, "白色", "8G+128G");
		electronics good2 = new electronics(2, "联想y7000p", "电子产品", 8499, "黑色", "16G+512G");
		electronics good3 = new electronics(3, "iPadPro", "电子产品", 6229, "灰色", "128G");
		electronics good4 = new electronics(4, "switch", "电子产品", 2996, "红蓝色", "32G");
		electronics good5 = new electronics(5, "kindle第4代", "电子产品", 998, "墨黑色", "8G");
		shoes good6 = new shoes(6, "Nike Kyrie 7", "鞋类", 624, "黑色", "38");
		shoes good7 = new shoes(7, "Air Jordan 1", "鞋类", 965, "红色", "39");
		shoes good8 = new shoes(8, "李宁韦德之道", "鞋类", 468, "黑色", "40");
		shoes good9 = new shoes(9, "匹克闪现1代", "鞋类", 279, "黑白", "41");
		shoes good10 = new shoes(10, "安踏KT6", "鞋类", 456, "水墨", "42");
		books good11 = new books(11, "《嫌疑人X的献身》", "书籍类", 24, "东野圭吾", "南海出版社");
		books good12 = new books(12, "《三体》1-3", "书籍类", 83, "刘慈欣", "重庆出版社");
		books good13 = new books(13, "《Java学习笔记》", "书籍类", 98, "林信良", "清华大学出版社");
		books good14 = new books(14, "《围城》", "书籍类", 28, "钱钟书", "人民文学出版社");
		books good15 = new books(15, "《活着》", "书籍类", 19, "余华", "作家出版社");
		mapshelf.put(good1.getId(), good1);
		mapshelf.put(good2.getId(), good2);
		mapshelf.put(good3.getId(), good3);
		mapshelf.put(good4.getId(), good4);
		mapshelf.put(good5.getId(), good5);
		mapshelf.put(good6.getId(), good6);
		mapshelf.put(good7.getId(), good7);
		mapshelf.put(good8.getId(), good8);
		mapshelf.put(good9.getId(), good9);
		mapshelf.put(good10.getId(), good10);
		mapshelf.put(good11.getId(), good11);
		mapshelf.put(good12.getId(), good12);
		mapshelf.put(good13.getId(), good13);
		mapshelf.put(good14.getId(), good14);
		mapshelf.put(good15.getId(), good15);
	}

	// 商品一览
	public static void showshelf() {
		Set keySet = mapshelf.keySet();
		Iterator it = keySet.iterator();
		while (it.hasNext()) {
			Object key = it.next();
			Object value = mapshelf.get(key);
			System.out.println(key + ":" + value);
		}
	}

	public static boolean search_shelf(int id) {
		if (mapshelf.containsKey(id)) {
			System.out.println(mapshelf.get(id));
			return true;
		} else
			return false;
	}
	//添加购物车
	public static void shop(int id, int num) {
		if (mapcart.containsKey(id)) {
			commodity_message goodItem = mapcart.get(id);
			goodItem.setCount(goodItem.getCount() + num);
		} else
			mapcart.put(id, new commodity_message(mapshelf.get(id), num));
	}
}


5、商品类

package commodities;

public class commodity {//商品类(父类)
	private Integer id;//商品编号
	private String name;//商品名称
	private String category;//商品种类
	private Integer price;//商品价钱
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "commodity [id=" + id + ", name=" + name + ", category=" + category + ", price=" + price + "]";
	}

	public commodity(Integer id,String name,String category,Integer price) {
		this.id =id;
		this.name =name;
		this.category=category;
		this.price=price;
	}
	
}


6、书

package commodities;

public class books extends commodity {//书籍类(继承于商品类)
	private String author;//作者
	private String press;//出版社
	
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	
	public books(Integer id,String name,String category,Integer price,String author,String press) {		
		super(id,name,category,price);
		this.author=author;
		this.press=press;
	}
	@Override
	public String toString() {
		return books.super.getName()+" "+"书籍类"+" "+"单价:"+books.super.getPrice()+"元 "+books.this.author+" "+books.this.press;
	}
}

7、商品清单

package commodities;

public class commodity_message {//商品信息类
	private commodity good;//要购买的商品
	private int count;//要购买的数量
	
	public commodity_message(commodity good,int count) {
		this.good=good;
		this.count=count;
	}

	public commodity getGood() {
		return good;
	}

	public void setGood(commodity good) {
		this.good = good;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	public int totalMoney() {
		int  price = good.getPrice();
		return price * count; 
	}

	@Override
	public String toString() {
		return this.good.toString()+" 数量: "+this.count+" 小计: "+this.totalMoney()+"元";
	}
}

原文地址:https://www.cnblogs.com/Guangyang/p/13967234.html