购物车程序的面向对象设计(一)

选题:购物车系统

一、人员分工

1.网络2113 202121333063 徐彬晶 负责商城类、菜单类、参与讨论博客的编写
2.网络2113 202121333064 黄坤 负责商品类、购物车类、用户类、注册与登录类、编写博客

二、前期调查

我们在搜索商品保温杯时,体验从淘宝中搜索,并通过设置一定价格范围内,将搜索出来的商品进行升、降价排序后,挑选心仪的保温杯,之后加入购物车,从操作购物车,对保温杯的数量进行增加、删减,直至下单、付款全过程。




在该过程中,我们思考其中可能包含的类有:商品类(Commodity)、商城类(ShoppingMall)、购物车类(ShoppingCart)、菜单类(UserMenu)、用户类(UserData)、用户注册与登录类(RegisterAndLogin)。

三、系统功能结构图

四、系统描述

进入登录界面,输入账号、密码,判断是否存在用户身份,即判断用户账号、密码是否与txt中的数据匹配正确,然后进入菜单界面:(括号内是方法,字体加粗是属性)
1.普通用户界面
(1)【查看】所有商品信息(价格【升序】)
(2)【查看】所有商品信息(价格【降序】)
(3)【查看】一定价格范围的商品
(4)将商品【加入】购物车
(5)将商品【移出】购物车
(6)【查看】购物车内容
(7)【结算】
(8)【退出】商城

五、类设计说明

  1. 商品类
    属性:商品的名称、价格

  2. 商城类

(1)商城类里面有一个静态嵌套类MallCommodity,里面有
属性:商城商品数量
方法:比较商品价格

(2)商城类方法:

  • 对list、txt进行录入、读取、删除操作
  • 按价格升、降序排列商品
  • 展示商城商品信息,或展示一定范围内的商品
  • 实现对商品的删改查
  1. 购物车类

(1)购物车类里面有一个静态嵌套类CartCommodity,里面有
属性:购物车内各个商品的数量

(2)购物车方法:

  • 展示购物车中的商品
  • 获得购物车商品数量
  • 实现购物车中对商品的增删
  • 结算购物车商品
  • 清空购物车
  1. 用户类
    属性:账号、密码

  2. 注册与登录类
    方法:
    (1)判定用户身份,即判定用户账户和密码是否与txt中保存的内容相匹配,相符则普通用户跳转“商城界面”
    (2)注册,即创建新账户

  3. 菜单类
    方法:展示主界面,让用户进行选择

  4. 类与类之间的关系

(1)功能关系
a.使用UserMenu类的登录界面,通过RegisterAndLogin类的匹配功能对输入的账号、密码进行判别,随后进入相应的界面
b.普通用户在购物车内完成对商品的购买后,该商品的数量在商城同步减少

六、本系统哪里体现了面向对象的封装性?

(1)Commodity类中的name、price,UserData类中user、password,MallCommodity类中number,CartCommodity类number是private的

七、项目包结构(package的划分)与关键代码:项目的包结构(为什么要这样设计包结构),主要功能(如往购物车添加、删除商品)的流程图与关键代码

  1. 项目包结构(package的划分)

(1)包结构导图

(2)各个package之间的关系

  • ui包则放Main类,是显示界面(虽然Menu类也是用户可以看到的界面,但是因为具体功能都与商城有关,所以不放入ui包)
  • shopping包放有Commodity类、UserMenu类、ShoppingCart类、ShoppingMall类等与购物有关的类
  • user包存放UserData类、RegisterAndLogin类
  1. 流程图

(1)RegisterAndLogin类的识别功能

(2)UserMenu类showUser方法中的将商品增加至购物车功能

(3)UserMenu类showUser方法中,根据商品名称删除功能

(4)UserMenu类showUser方法中的结算功能

3. 关键代码

(1) Commodity类

package shopping;

public class Commodity{
	private String name;
	private double price;

	public Commodity(){
	}
	public Commodity(String name, double price) {
		this.name=name;
		this.price=price;
	}
	
	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;
	}

	@Override
	public String toString() {
		return "Commodity [name=" + name + ", price=" + price +  ",";
	}
}

(2) ShoppingMall类

package shopping;

public class ShoppingMall {
	// 定义数量
	public static class MallCommodity implements Comparable<MallCommodity> {
		private int number;
		private Commodity commodity;

		public int getNumber() {
			return number;
		}

		public void setNumber(int number) {
			this.number = number;
		}

		// 无参构造
		public MallCommodity() {
		}

		// 有参构造
		public MallCommodity(String name, double price, int number) {
			this.setCommodity(new Commodity(name, price));
			this.number = number;
		}

		@Override
		public String toString() {
			return "MallCommodity [commodity=" + getCommodity() + ", number=" + number + "]";
		}

		@Override
		public int compareTo(MallCommodity o) {
			double num = this.getCommodity().getPrice() - o.getCommodity().getPrice();
			if (num > 0)
				return 1;
			else if (num == 0)
				return 0;
			else
				return -1;
		}

		public Commodity getCommodity() {
			return commodity;
		}

		public void setCommodity(Commodity commodity) {
			this.commodity = commodity;
		}
	}

	ArrayList<MallCommodity> list = new ArrayList<>();

	// 将文本信息录入list
	public void inputList() throws FileNotFoundException {
		String fileName = "C:\\Users\\lin'juan\\eclipse-workspace\\Shop1.1\\ShoppingMall.txt";
		try (Scanner sc = new Scanner(new FileReader(fileName))) {
			while (sc.hasNextLine()) {
				// 按行读取字符串
				String line = new String(sc.nextLine());
				String[] strArr = line.split(" ");
				MallCommodity m = new MallCommodity();
				Commodity commodity = new Commodity();
				commodity.setName(strArr[0]);
				commodity.setPrice(Double.valueOf(strArr[1].toString()));
				m.setCommodity(commodity);
				m.setNumber(Integer.valueOf(strArr[2].toString()));
				list.add(m);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 清空文本内容
	public static void clearTxt() {
		File file = new File("C:\\Users\\lin'juan\\eclipse-workspace\\Shop1.1\\ShoppingMall.txt");
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file);
			fileWriter.write(""); // 写入空
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 将列表数据存入文本
	public void inputTxt() {
		try {
			File file = new File("ShoppingMall.txt");
			if ((!file.exists()))
				file.createNewFile();
			FileWriter fileWritter = new FileWriter(file.getName(), true);
			for (MallCommodity e : list)
				fileWritter.write(
						e.getCommodity().getName() + " " + e.getCommodity().getPrice() + " " + e.getNumber() + "\n");
			fileWritter.close();
		} catch (IOException a) {
			a.printStackTrace();
		}
	}

	// 查找商品
	public MallCommodity searchList(String name) {
		for (MallCommodity e : list)
			if (e.getCommodity().getName().equals(name))
				return e;
		return null;
	}

	// 价格升序
	public void ascend() {
		Collections.sort(list);
	}

	// 价格降序
	public void descend() {
		Collections.sort(list, Collections.reverseOrder());
	}

	// 范围内的数
	public void rangeList(double min, double max) {
		if (min > max) {
			System.out.println("输入错误!");
			return;
		}
		for (MallCommodity e : list)
			if (e.getCommodity().getPrice() >= min && e.getCommodity().getPrice() <= max)
				System.out.println(e);
	}

	// 展示商品
	public void dispList() throws IOException {
		for (MallCommodity e : list)
			System.out.println(e.toString());

	}

	// 商品数量减少
	public MallCommodity reduceNum(Commodity m, int number) {
		for (MallCommodity e : list) {
			if (e.getCommodity().getName().equals(m.getName())) {
				int numNew = e.getNumber() - number;
				e.setNumber(numNew);
				return e;
			}
		}
		return null;
	}
}

(3) RegisterAndLogin类中的用户身份识别功能

package user;

public void identify() throws IOException {// 识别 
		Menu m = new Menu();
		UserData p = new UserData();
		
		Scanner sc = new Scanner(System.in);
		String account = sc.next();
		String code = sc.next();
		int len = code.length();
		
		if (!account.startsWith("x") && !account.endsWith("h") ) {// 用户账号、密码输入
			p.setUser(account);
			if (code.length() == 6) {
				p.setPassword(code);
				readUser(p);
			} 
			if(code.length() != 6) {
				System.out.println("密码错误!");
				return;
			}
			System.out.println("正在进入大商场,请稍等...");
			System.out.println("加载成功!");
			m.showUser();
		} 

        //方法:用户账号、密码比对txt内容
	public static void readUser(UserData person) throws IOException {
		String fileName = "D:\\Users\\HP\\eclipse-workspace\\learnjava\\User.txt";
		try (Scanner sc = new Scanner(new FileReader(fileName))) {
			while (sc.hasNextLine()) {
				String line = sc.nextLine();
				String name = person.getUser();
				String password = person.getPassword();
				String a = name+password;
				if (a.equals(line)) {
					System.out.println("登录成功!欢迎" + name + "的到来!");
				}
			}
		}
	}
}

(4)UserMenu类中将商品加入购物车功能

package shopping;

import shopping.ShoppingCart.CartCommodity;

case 4:
				int Id = 0;
				do {
					System.out.println("***********************************");
					System.out.println("*********4、将商品加入购物车********");
					System.out.println("请输入您要查找的商品名称:");
					String commodityNam = new String(sc.next());

					// 商城中是否有该商品
					if (shoppingMall.searchList(commodityNam) == null)
						System.out.println("查无此商品");
					else {
						Commodity commodity = shoppingMall.searchList(commodityNam).getCommodity();
						int comNum = shoppingMall.searchList(commodityNam).getNumber();
						System.out.println(shoppingMall.searchList(commodityNam));

						System.out.printf("是否加入购物车(Y/N):");
						if (sc.next().equals("Y")) {
							System.out.printf("请输入你所需的数量:");
							int number = sc.nextInt();
							if (number > comNum)
								System.out.println("商品库存不足,仅余" + comNum);
							else {
								CartCommodity c1 = new CartCommodity(commodity, 0);
								cart.addToList(c1, number);
								System.out.println("该商品已加入购物车");
							}
						}
					}
					System.out.println("是否继续向购物车添加商品:(Y/N)");
				} while (sc.next().equals("Y"));
				break;

(5)UserMenu类中根据商品名称删除商品功能

package shopping;

import shopping.ShoppingCart.CartCommodity;

case 5:
				do {
					System.out.println("***********************************");
					System.out.println("*********5、将商品加入购物车********");
					System.out.println("输入商品名称删除商品:");
					System.out.printf("输入需要删除的商品名称:");
					String name = sc.next();
					System.out.printf("输入该商品要减少的数量:");
					int delNum = sc.nextInt();
					if (!cart.deleteName(name, 0))
						System.out.println("购物车内无此商品");
					else
						cart.deleteName(name, delNum);
					System.out.println(name + "已减少" + delNum);
					System.out.printf("是否继续删除?(Y/N)");
				} while (sc.next().equals("Y"));
				break;

(6)UserMenu类中的结算功能

package shopping;

import shopping.ShoppingCart.CartCommodity;

case 7:
				System.out.println("***********************************");
				System.out.println("**************7、结算**************");

				cart.dispList();// 展示购物车
				double y = cart.checkout();
				System.out.println("商品种类:" + cart.getQty());// 商品总数
				System.out.print("总价:");// 商品总价;
				System.out.printf("%.2f\n", y);
				System.out.printf("是否付款(Y/N):");
				if (sc.next().equals("Y")) {
					System.out.println("结算成功!");
					for (int i = 0; i < cart.getQty(); i++) {
						Commodity c = cart.get(i).commodity;
						int numCom = cart.get(i).getNumber();
						shoppingMall.reduceNum(c, numCom);
					}
					cart.Clear();
					shoppingMall.clearTxt();
					shoppingMall.inputTxt();
				}
				break;

(7)ShoppingCart类部分代码

package shopping;

public class ShoppingCart {

	// 定义数量
	public static class CartCommodity {
		private int number;
		public Commodity commodity;

		public int getNumber() {
			return number;
		}

		public void setNumber(int number) {
			this.number = number;
		}

		// 无参构造
		public CartCommodity() {
		}

		// 有参构造
		public CartCommodity(Commodity commodity, int number) {
			this.commodity = commodity;
			this.number = number;
		}

		@Override
		public String toString() {
			return "CartCommodity [commodity=" + commodity + ", number=" + number + "]";
		}
	}

	ArrayList<CartCommodity> list = new ArrayList<>();

	// 增加商品,写入列表
	public void addToList(CartCommodity c, int number) {
		boolean flag = true;
		for (CartCommodity e : list)
			if (e.commodity.getName().contains(c.commodity.getName())) // 如果items包含p.name{
				int num = e.getNumber() + number;// 增加数量
				e.setNumber(num);
				flag = false;
			}

		if (flag) {
			c.setNumber(number);
			list.add(c);
		}
	}
}

八、界面

控制台

九、数据存储

商城商品信息、仓库库存信息、购物车内容、用户信息、商城管理人员、仓库管理人员都分别存放在不同名称的txt文档中,然后实现读取等操作

十、开发工具

Eclipse

posted @ 2022-09-25 10:48  xlyh  阅读(115)  评论(0编辑  收藏  举报