首页 > 编程语言 >购物车程序的面向对象设计(一)

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

时间:2022-09-25 10:55:21浏览次数:45  
标签:sc 程序 System 购物车 面向对象 String println out

一、人员分工

1.网络2113 202121333063 徐彬晶 负责商品类、商城类、仓库类、菜单类(普通用户菜单)、参与讨论博客的编写
2.网络2113 202121333064 黄坤 负责商品类、购物车类、菜单类(商城管理人员菜单、仓库管理人员菜单)、用户类,编写博客

二、前期调查

我们在搜索商品保温杯时,体验从淘宝中搜索,并通过设置一定价格范围内,将搜索出来的商品进行升、降价排序后,挑选心仪的保温杯,之后加入购物车,从操作购物车,对保温杯的数量进行增加、删减,直至下单、付款全过程。
在该过程中,我们思考其中可能包含的类有:商品类(commodity)、商城类(ShoppingMall)、购物车类(ShoppingCar)、用户类(Person)、仓库类(WareHouse)、菜单类(Menu)。

三、系统功能结构图

四、系统描述

进入登录界面,输入账号、密码,判断用户身份:普通用户(user)、商城管理人员(productManage)、仓库管理人员(warehouseMan),同时判断用户账号、密码是否匹配正确,然后进入菜单界面:(括号内是方法,字体加粗是属性)
1.普通用户界面
(1)【查看】所有商品信息(价格【升序】)
(2)【查看】所有商品信息(价格【降序】)
(3)【查看】一定价格范围的商品
(4)将商品【加入】购物车
(5)将商品【移出】购物车
(6)【查看】购物车内容
(7)【结算】
(8)【退出】商城
2.商城管理人员界面
(1)【查看】仓库商品信息
(2)【查看】商城商品信息
(3)【增加】商品种类
(4)【修改】商品价格
(5)【修改】商品数量
(6)【删除】商品
(7)【退出】商城管理系统
3.仓库管理人员界面
(1)【查看】所有商品信息
(2)【增加】商品种类
(3)【修改】商品数量
(4)【删除】商品
(5)【退出】仓库管理系统

五、类设计说明

  1. 商品类
    属性:商品的名称、数量、价格
  2. 商城类、仓库类
    方法:
    (1)实现对商品的增删改查
    (2)写入、读取、修改、清空对应的txt
    (3)按价格升、降序排列商品
    (4)展示商品
  3. 购物车类
    方法:
    (1)展示购物车中的商品信息
    (2)继承商城类的方法,实现购物车中对商品的增删
    (3)结算购物车商品
  4. 用户类
    属性:账号、密码
    方法:
    (1)判定用户身份
    (2)判定用户账户和密码是否与txt中保存的内容相匹配
    (3)创建新账户
  5. 菜单类
    方法:展示不同身份的用户的主界面

    6.类与类之间的关系
    (1)功能关系
    a.使用Menu类的登录界面,通过Person类的匹配功能对输入的账号、密码进行判别,随后进入相应的界面
    b.普通用户在购物车内完成对商品的购买后,该商品的数量在商城和仓库同步减少
    c.商品/仓库管理人员可以对商品进行相应的增删改操作,修改后的信息可以在Menu类中展示
    (2)代码关系
    WareHouse类、ShoppingCar类、Person类都继承(extends)ShoppingMall类

六、本系统哪里体现了面向对象的封装性?可选:哪里体现了继承与多态?

(1)Commodity类中的name、price、number都是private的
(2)WareHouse类、ShoppingCar类、Person类都继承(extends)ShoppingMall类中的多数方法

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

(1)流程图

  • ShoppingMall类
  • Person类识别功能
  • Menu类showUser方法中的将商品增加至购物车功能
  • Menu类showUser方法中,根据商品名称/编号删除功能
  • Menu类showUser方法中的结算功能

    (2)关键代码
  • ShoppingMall类
public class ShoppingMall {
	 ArrayList<Commodity> list = new ArrayList<>();	 
		// 将文本信息录入list
		public void inputList() throws FileNotFoundException {
			String fileName = "D:\\Users\\HP\\eclipse-workspace\\learnjava\\ShoppingMall.txt";
			try (Scanner sc = new Scanner(new FileReader(fileName))) {
				while (sc.hasNextLine()) { // 按行读取字符串
					String line = new String(sc.nextLine());
					String[] strArr = line.split(" ");
					Commodity commodity = new Commodity();
					commodity.setName(strArr[0]);
					commodity.setPrice(Double.valueOf(strArr[1].toString()));
					commodity.setNumber(Integer.valueOf(strArr[2].toString()));
					//commodity.setId(Integer.valueOf(strArr[3].toString()));
					list.add(commodity);
				}
			}
		} 
		//增加商品,写入列表
		 public void addToList(Commodity commodity,int number) {
		   int flag1=0;
		   for (Commodity e : list){
		    if (e.getName().contains(commodity.getName())) {//如果items包含p.name
		     int num=e.getNumber()+number;//增加数量
		     e.setNumber(num);
		     flag1=1;
		    } 
		  }
		   if(flag1==0) {
		    list.add(commodity);
		    }
		 }
		       //清空列表数据
			public void clearList(){
				list.clear();
			}
			// 将列表数据存入文本
			public void inputTxt() {
				try {
					File file = new File("ShoppingMall.txt");
					if ((!file.exists())) {
						file.createNewFile();
					}
					FileWriter fileWritter = new FileWriter(file.getName(), true);
					for (Commodity e : list) {
						fileWritter.write(e.getName() + " " + e.getPrice() + " " + e.getNumber() + "\n");
					}
					fileWritter.close();
				} catch (IOException a) {
					a.printStackTrace();
				}
			}
	        //展示商品
		public void dispList() throws IOException {
			for (Commodity e : list)
				System.out.println(e);
		}	
	// 删除商品
	public String deletList(String name) {
		for (Commodity e : list) {
			if (e.getName().equals(name)) {
				list.remove(e);
				return e.getName() + "已下架";
			}
		}
		return "查无此商品";
	}
	// 查找商品
	public Commodity searchList(String name){
		for (Commodity e : list) {
			if(e.getName().equals(name)) {
				return e;	
			}
		}		
		return null;
	}
	// 清空文本内容
	public static void clearTxt() {
		File file = new File("D:\\Users\\HP\\eclipse-workspace\\learnjava\\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 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 (Commodity e : list) {
			if (e.getPrice() >= min && e.getPrice() <= max)
				System.out.println(e);
		}
	}
	//修改商品价格
	public void changeListPrice(String name, double price) {
		int flag = 1;
		if (price < 0) {
			System.out.println("输入不符合规范!\n");
			return;
		}
		for (Commodity e : list)
			if (e.getName().equals(name)) {
				e.setPrice(price);
				flag = 0;
			}
		if (flag == 1) {
			System.out.println("查无此商品");
		}
	}
	//修改商品数量
	public void changeListNum(String name, int num) {
		int flag = 1;
		if (num < 0) {
			System.out.println("输入不符合规范!\n");
			return;
		}
		for (Commodity e : list)
			if (e.getName().equals(name)) {
				e.setNumber(num);
				flag = 0;
			}
		if (flag == 1) {
			System.out.println("查无此商品");
		}
	}	
	        //商品数量减少
		public Commodity reduceNum(String name,int number){
			for (Commodity e : list){
				if(e.getName().equals(name)){
					int numNew=e.getNumber()-number;
					e.setNumber(numNew);
					return e;
				}
			}
			return null;
		}			
		//显示list的长度
		public int size(){
			int size=list.size();
			return size;
		}
		// 返回指定位置的元素
		public Commodity get(int index) {
			return list.get(index);
		}
}
  • Person类中的识别身份功能
public void Identify() throws IOException {// 识别
		Menu m = new Menu();//定义菜单类
		Person p = new Person();//定义person类
		
		Scanner sc = new Scanner(System.in);
		String account = sc.next();//输入账号
		String code = sc.next();//输入密码
		
		if (!account.startsWith("x") && !account.endsWith("h") ) {// 判定用户
			p.setUser(account);//将account赋给Person
			if (code.length() == 6) {//如果密码长度为6
				p.setPassword(code);//将password赋给Person
				p.readUser(p);//读取User的txt判断是否存在该用户
			} else {
				do {
					System.out.println("密码错误请重新输入:");
					code = sc.next();
				}while (code.length() == 6) ;
				p.setPassword(code);
				p.readUser(p);//读取User的txt判断是否存在该用户
			}
			m.showUser();//展示用户的使用界面
		} else {
			if (account.startsWith("xh") && account.endsWith("xk")) {//判定仓库管理人员
				p.setWarehouseMan(account);
				if (code.length() == 5) {
				  p.setPassword(code);
				  p.readWarehouseMan(p);//读取WarehouseMan的txt判断是否存在该仓库管理人员
			} else {
				do {
					System.out.println("密码错误请重新输入:");
					code = sc.next();
				}while (code.length() == 5) ;
				p.setPassword(code);
				p.readWarehouseMan(p);//读取WarehouseMan的txt判断是否存在该仓库管理人员
				}
			System.out.println("进入仓库管理系统...");
			//进入仓库
			m.showWarehouseMan();//展示仓库管理人员的使用界面
			} 
			else if (account.startsWith("x") && account.endsWith("k") ){//判定商城管理人员
			p.setProductManage(account);
			if (code.length() == 5) {
				p.setPassword(code);
				p.readProductManage(p);//读取ProductManage的txt判断是否存在该商城管理人员
			} else {
				do {
					System.out.println("密码错误请重新输入:");
					code = sc.next();
				}while (code.length() != 5) ;
				p.setPassword(code);
				p.readProductManage(p);//读取ProductManage的txt判断是否存在该商城管理人员
			}
			System.out.println("进入商品管理系统...");
			//进入商城界面
			m.showProductManage();//展示商城管理人员的使用界面
		}
	}
}
	
	public void readUser(Person person) throws IOException {//读取User的txt判断是否存在该用户
		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 + "的到来!");
				}
			}
		}
	}
	
	public void readProductManage(Person person) throws IOException {//读取ProductManage的txt判断是否存在该商城管理人员
		String fileName = "D:\\Users\\HP\\eclipse-workspace\\learnjava\\Caretaker.txt";
		try (Scanner sc = new Scanner(new FileReader(fileName))) {
			while (sc.hasNextLine()) {//以行为读取格式
				String line = sc.nextLine();
				String name = person.getProductManage();//name赋值给person
				String password = person.getPassword();//password赋值给person
				String a = name+password;//将name和password合并 
				if (a.equals(line)) {//使用equals进行判断
					System.out.println("登录成功!欢迎管理员" + name + "!");
				}
			}
		}
	}
	
	public void readWarehouseMan(Person person) throws IOException {//读取WarehouseMan的txt判断是否存在该仓库管理人员
		String fileName = "D:\\Users\\HP\\eclipse-workspace\\learnjava\\Warehouseman.txt";
		try (Scanner sc = new Scanner(new FileReader(fileName))) {
			while (sc.hasNextLine()) {//以行为读取格式
				String line = sc.nextLine();
				String name = person.getWarehouseMan();
				String password = person.getPassword();
				String a = name+password;
				if (a.equals(line)) {
					System.out.println("登录成功!欢迎管理员" + name + "!");
				}
			}
		}
	}
  • Menu类showUser方法中的将商品增加至购物车功能
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 = new Commodity(commodityNam,
								shoppingMall.searchList(commodityNam).getPrice(),
								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 > commodity.getNumber())
								System.out.println("商品库存不足,仅余" + commodity.getNumber());
							else {
								commodity.setNumber(number);
								cart.addToList(commodity, number);
								System.out.println("该商品已加入购物车");
							}
						}
					}
					System.out.println("是否继续向购物车添加商品:(Y/N)");
				} while (sc.next().equals("Y"));
				break;
  • Menu类showUser方法中,根据商品名称/编号删除功能
case 5:
				do {
					System.out.println("***********************************");
					System.out.println("*********5、将商品加入购物车********");
					System.out.println("1.输入商品名称删除商品:");
					System.out.println("2.输入购物车内商品序号删除商品:");
					System.out.printf("请输入您的选择:");
					if (sc.nextInt() == 1) {
						System.out.printf("输入需要删除的商品名称:");
						String name = sc.next();
						if (cart.searchList(name) == null){
                                                   System.out.println("购物车内无此商品");
                                                }
						else{
                                                   cart.deleteName(name);
                                                }
					} else {
						System.out.printf("输入需要删除的商品id:");
						int id = sc.nextInt();
						cart.deleteOrd(id);
					}
					System.out.printf("是否继续删除?(Y/N)");
				} while (sc.next().equals("Y"));
				break;
  • Menu类showUser方法中的结算功能
case 7:
				System.out.println("***********************************");
				System.out.println("**************7、结算**************");

				cart.show();// 展示购物车
				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.size(); i++) {
						Commodity c = cart.get(i);
						shoppingMall.reduceNum(c.getName(), c.getNumber());
					}
					cart.Clear();
					shoppingMall.clearTxt();
					shoppingMall.inputTxt();
				}
				break;

标签:sc,程序,System,购物车,面向对象,String,println,out
From: https://www.cnblogs.com/APTXkn-4869/p/16726881.html

相关文章