商城订单管理系统

news/2025/2/4 2:42:03/

订单管理系统

某购物商场,想做一个在线购物的程序,客户可以通过程序自助购物,要求有显示商品、选购商品、购物车、历史订单等功能。

学习目标

通过这个案例我们想达到的学习目标是 以下几点

- 1.熟练使用BufferedReader和BufferedWriter读写数据
- 2.熟练使用ArrayList集合进行增、删、改、查等操作
- 3.了解面向对象和实际场景的结合使用
- 4.了解购物类项目的业务流程

1. 需求分析

所有的商品信息以固定的格式(商品编号,商品名称,商品价格,商品库存)保存在product.txt文件中格式如下:001,小米手机,1999,10002,华为手机,3999,10003,乐事薯片,5,100004,小米锅巴,5,200005,格力空调,5000,5...1.显示商品功能:读取product.txt文件,将每一行打印在控制台即可2.选购商品功能:键盘录入购买的商品编号和数量,将商品信息添加到购物车,并提示是否继续购买。如下:录入购买的商品编号:001录入购买的商品数量5已添加到购物车,是否继续购买(Y/N)? 选择Y: 继续购物选择N: 结束购物3.购物车功能:显示购物车中每个商品的名称,数量和金额,以及购物车总金额,并提示是否生成订单。查看购物车格式如下:商品名称:小米手机, 价格:1999, 数量:3, 金额:5997商品名称:乐视薯片, 价格:5, 数量:3, 金额:15总金额:6012元---------------------------------------------是否生成订单?(Y/N)选择Y: 1)会有一个唯一的订单号,并且把订单的购物清单写到orderlist文件中2)商品库存减少相应的数量3)清空购物车选择N: 什么都不做4.历史订单功能:每一个订单信息,以如下格式保存到orderlist.txt文件中。订单编号:92a9502f-cbd8-40f3-af1d-87a387a9fada商品名称:小米手机, 价格:1999, 数量:3, 金额:5997商品名称:乐视薯片, 价格:5, 数量:3, 金额:15总金额:6012元---------------------------------------------订单编号:00785cce-394c-4788-9313-af5b3cd4a863商品名称:小米手机, 价格:1999, 数量:3, 金额:5997商品名称:乐视薯片, 价格:5, 数量:3, 金额:15总金额:6012元---------------------------------------------

2. 准备数据

在Shopping模块目录下,新建一个product.txt文件。将以下商品信息复制到文件中。每一行从左往右分别表示商品编号、商品名称、商品价格、商品库存

                   
001,小米手机,1999,10
002,华为电视,3999,10
003,乐事薯片,5,100
004,小米锅巴,5,200
005,格力空调,5000,5
006,海尔冰箱,2500,10
007,公牛插座,50,250
008,长虹电视,3500,200
009,南孚电池,5,1000
010,大桥鸡精,9000,100

3. 功能菜单

public static void main(String[] args) throws IOException {while (true) {System.out.println("**************欢迎来到黑马商城***************");System.out.println("1.查看商品  2.选购商品  3.我的购物车  4.我的订单");Scanner sc = new Scanner(System.in);String choose = sc.next();switch (choose) {case "1"://查看所有商品信息showAllProduct();break;case "2"://选购商品buyProduct(shoppingcart);break;case "3"://查看购物车showShoppingCart(shoppingcart);break;case "4"://查看订单showOrderList();break;default:System.out.println("你选择有误,请重新输入");}}
}

4. Product类

public class Product {private String id; //编号private String name; //名称private int price; //单价private int stock; //库存//自己补全get和set方法,以及构造方法...
}

5. 查看商品

查看商品的思路:1.编写方法readProductToList(),读取所有的商品信息到集合2.遍历集合中的所有数据,并打印输出
  • 读取product.txt文件到集合
- 使用BufferedReader读取product.txt文件
- 使用把每一行使用","进行切割,得到一个字符串数组
- 字符串数组中的元素分别表示:商品编号、商品名称、商品价格、商品库存注意:其中商品的价格和库存需要转换为整数//如: 把"123"转换为123int price=Integer.parseInt("123");
- 把商品信息封装为Product对象
- 把对象存储到ArrayList集合中
//读取文件到集合
public static ArrayList<Product> readProductToList() throws IOException {//创建流对象,关联day15/product.txt文件BufferedReader br =new BufferedReader(new FileReader("Shoppping/product.txt"));//创建集合,用于存储商品对象ArrayList<Product> list = new ArrayList<>();//line用来记录读取的每一行String line;//读取readLine()方法返回null,则表示读取到文件末尾while ((line = br.readLine()) != null) {//对每一行使用","进行切割,得到一个数组String[] array = line.split(",");//数组中的元素分别表示:商品编号、商品名称、商品价格、商品库存String productId = array[0]; //商品idString productName = array[1]; //商品名称String productPrice = array[2]; //商品价格String productStock = array[3]; //商品库存//每一个元素封装为Product对象Product p = new Product(productId, productName, Integer.parseInt(productPrice), Integer.parseInt(productStock));list.add(p);}//关闭流br.close();return list;
}
  • 查看商品时,遍历集合,打印集合中元素
//显示所有商品
private static void showAllProduct() throws IOException {//1.读取product.txt文件到集合ArrayList<Product> list = readProductToList();//2.打印集合System.out.println("id,名称,价格,库存");for (int i = 0; i < list.size(); i++) {Product p = list.get(i);System.out.println(p.getId() + "," + p.getName() + "," + p.getPrice() + "," + p.getStock());}
}

6. 选购商品

选购商品的思路:1.键盘录入要选购的商品编号,如果不存在给出提示,并结束选购;2.键盘录入要选购的商品数量,如果库存不足给出提示,并结束选购3.如果商品编号正确、库存也足够,就将商品添加到购物车。4.提示是否继续选购(Y/N),输入Y表示继续,N表示不继续。
  • 根据id获取Product对象,没有获取到返回null
//获取id对应的产品
private static Product getProductById(String productId) throws IOException {ArrayList<Product> list = readProductToList();for (int i = 0; i < list.size(); i++) {Product p = list.get(i);if (p.getId().equals(productId)) {return list.get(i);}}return null;
}
  • 选购的商品条目

每次选购一个商品,会在购物车中添加一个购物条目

public class BuyItem {private String id;private String name;private int price;private int number;private int money;//get和set方法自动生成,以及构造方法
}
//购物车
public static ArrayList<BuyItem> shoppingcart = new ArrayList<>();private static void buyProduct() throws IOException {//1.读取所有的商品信息ArrayList<Product> list = readProductToList();//2.键盘录入选购商品编号,判断商品编号是否存在Scanner sc = new Scanner(System.in);while (true) {System.out.println("键盘录入选购的商品编号:");String id = sc.next();//获取id对应的商品Product product = getProductById(list, id);if (product == null) {System.out.println("选购的商品不存在");return; //结束选购}//3.键盘录入选购商品数量,判断库存是否充足System.out.println("请录入选购的商品数量:");int number = sc.nextInt();//获取该商品的库存if (product.getStock() < number) {System.out.println("商品库存不足,剩余库存为:" + product.getStock());return;}//4.创建选购商品条目int money = number * product.getPrice();BuyItem item = new BuyItem(id, product.getName(), product.getPrice(), number, money);//5.将选购的商品条目添加到购物车shoppingcart.add(item);System.out.println("选购商品成功,已将" + product.getId() + "号商品添加到购物车");System.out.println("是否继续选购(Y/N)");String isContinue = sc.next();if (isContinue.equalsIgnoreCase("Y")) {continue; //继续选购} else {System.out.println("已经结束选购,请到购物车结算!");break; //结束选购}}
}

7. 查看购物车

查看购物车思路:1.遍历购物车集合,如果没有要给出提示2.打印购物车商品信息3.计算购物车总金额4.是否生成订单(Y/N),Y表示生成订单;N表示不生成订单Y: 生成订单,并产生一个唯一的订单号,把购物车清空N: 不做任何处理
private static void showShoppingCart() throws IOException {//判断购物车是否为空if (shoppingCart.isEmpty()) {System.out.println("购物车什么都没有,选购商品后再来看看吧");return;}//打印购物车清单int totalMoney = 0;for (int i = 0; i < shoppingCart.size(); i++) {BuyItem buyItem = shoppingCart.get(i);//打印清单信息//商品名称:小米手机, 价格:1999, 数量:3, 金额:5997System.out.println("商品名称" + buyItem.getName() + "," +"价格:" + buyItem.getPrice() + "," +"数量:" + buyItem.getNumber() + "," +"金额:" + buyItem.getMoney());//计算购物清单总金额totalMoney += buyItem.getMoney();}System.out.println("总金额:" + totalMoney);System.out.println("---------------------------------------------");System.out.println("是否生成订单?(Y/N)");Scanner sc = new Scanner(System.in);String confirm = sc.next();//是否生成订单if (confirm.equalsIgnoreCase("y")) {//生成订单Order order = createOrder(shoppingCart);//将订单写入到orderlist.txt文件中writeOrderToFile(order);//生成订单后,更新商品库存updateProductStock(order);//购物车清空shoppingCart.clear();}
}

8. 生成订单

  • 订单类Order
/*
订单类分析:一个订单有一个唯一的订单编号一个订单包含多选购的商品条目一个订单有一个订单金额
*/
public class Order {private String orderId; //订单编号private ArrayList<BuyItem> list; //该订单选购的商品private int money; //订单金额public Order() {}public Order(String orderId, ArrayList<BuyItem> list,int money) {this.orderId = orderId;this.list = list;this.money=money;}//自动生成get和set方法,自己补全
}
  • 生成订单
//生成订单
private static Order createOrder() throws IOException {//创建订单Order order = new Order();//设置订单IDorder.setOrderId(UUID.randomUUID().toString());//设置订单商品列表order.setList(shoppingCart);//计算订单金额,购物车的所有商品的金额相加,就是订单金额int money=0;for (int i = 0; i < shoppingCart.size(); i++) {money+=shoppingCart.get(i).getMoney();}//设置订单金额order.setMoney(money);return order;
}

9. 订单写入到文件

订单编号:92a9502f-cbd8-40f3-af1d-87a387a9fada
商品名称:小米手机, 价格:1999, 数量:3, 金额:5997
商品名称:乐视薯片, 价格:5, 数量:3, 金额:15
总金额:6012---------------------------------------------
//写入订单信息到文件
private static void writeOrderToFile(Order order) throws IOException {//创建输出流对象BufferedWriter bw = new BufferedWriter(new FileWriter("Shoppping/orderlist.txt", true));bw.write("订单号:" + order.getOrderId());bw.newLine();//获取订单商品条目ArrayList<BuyItem> list = order.getList();for (int i = 0; i < list.size(); i++) {BuyItem buyItem = list.get(i);bw.write("商品名称:" + buyItem.getName() + "," +"价格:" + buyItem.getPrice() + "," +"数量:" + buyItem.getNumber() + "," +"金额:" + buyItem.getMoney());bw.newLine();}bw.write("订单金额:" + order.getMoney());bw.newLine();bw.write("---------------------------------------------");bw.newLine();bw.close();
}

10. 更新库存

//更新商品库存
private static void updateProductStock(Order order) throws IOException {//获取订单中购买的产品条目ArrayList<BuyItem> orderItems = order.getList();//获取所有的产品ArrayList<Product> allProducts = readProductToList();//订单中每一个商品的库存for (int i = 0; i < orderItems.size(); i++) {BuyItem buyItem = orderItems.get(i);//根据id获取产品for (int j = 0; j < allProducts.size(); j++) {Product product = allProducts.get(j);if (buyItem.getId().equals(product.getId())) {product.setStock(product.getStock() - buyItem.getNumber());}}}//重新把产品信息写到product.txt文件中writeProductToFile(allProducts);
}
//重新写入产品信息到文件
private static void writeProductToFile(ArrayList<Product> allProducts) throws IOException {BufferedWriter bw = new BufferedWriter(new FileWriter("Shoppping/product.txt"));for (int i = 0; i < allProducts.size(); i++) {Product product = allProducts.get(i);bw.write(product.getId() + "," + product.getName() + "," + product.getPrice() + "," + product.getStock());bw.newLine();}bw.close();
}

11.查看订单

//查看订单
private static void showOrderList() throws IOException {BufferedReader br=new BufferedReader(new FileReader("Shopping/orderlist.txt"));String line;while((line=br.readLine())!=null){System.out.println(line);}br.close();
}

http://www.ppmy.cn/news/125474.html

相关文章

国产芯片WiFi物联网智能插座—项目简介

目录 1、项目背景 2、项目功能 3、硬件设计 随着智能电子设备的不断进步和发展&#xff0c;必然会提升智能设备的使用率&#xff0c;诸如智能移动设备、智能家居等设备发展极为迅速。物联网作为一个互联网和通信网络的信息载体&#xff0c;能够使物理对象实现网络互通&#…

微信小程序开发的学习资料收集

一、学习内容汇总&#xff1a; 官方工具&#xff1a;https://mp.weixin.qq.com/debug/w ... tml?t1476434678461简易教程&#xff1a;https://mp.weixin.qq.com/debug/wxadoc/dev/?t1476434677599设计指南&#xff1a;微信小程序设计指南 | 微信开放文档设计资源下载&#x…

2022中国智能家居产业博览会

时间&#xff1a;2022年7月8日-10日 地点&#xff1a;南昌绿地国际展览中心 主办单位&#xff1a;南昌市人民政府 中国通信工业协会 承办单位&#xff1a;南昌市商贸和会展服务中心 南昌市商务局 高新区管委会 执行承办单位&#xff1a;中国通信工业协会品控自律联盟 南昌市安…

智能门锁迈入“长尾”时代

文|智能相对论 作者|佘凯文 “智能门锁又摊上事了。” 日前&#xff0c;一女子在网上发布视频称自己用了一年的智能门锁&#xff0c;居然任意密码就能打开&#xff1f;前不久&#xff0c;在其输密码进家门时无意输错密码&#xff0c;奇怪的是家门竟然可以打开。而后女子胡乱…

【程序员讲装修】平台选择第三期

【程序员讲装修】平台选择第三期 前言 有意思&#xff0c;昨天我写了一篇博客《【程序员讲装修】平台选择后续》&#xff0c;用来描述前天写的这篇博客《程序员讲装修——平台选择》&#xff0c;其中有些内容还是有些偏颇&#xff0c;描述不准确。 不准确的地方就是对今日头…

中国电工开关产业市场调查及未来发展趋势预测报告2022-2028年

中国电工开关产业市场调查及未来发展趋势预测报告2022-2028年 详情内容请咨询鸿晟信合研究网&#xff01; 【全新修订】&#xff1a;2022年3月 【撰写单位】&#xff1a;鸿晟信合研究网 第一章 电工开关产业概述 17 第一章 电工开关产业概述 17 第一节 电工开关产业定义 17 第…

3000+价位投影仪该怎么选?双十一投影仪选购攻略

随着时代的发展和科技进步&#xff0c;年轻人目前对于娱乐的需求越来越高。而Z时代对于娱乐需求的智能化程度要求就更上一层楼了。根据最新调查显示&#xff0c;目前Z时代中对于家电的需求程度中&#xff0c;家用投影仪的需求量甚至排到了第一。 因此可以说&#xff0c;目前投…

IC卡、ID卡及车库蓝牙卡的复制说明!(小区的门禁系统)

随着科技的发展&#xff0c;各种新的技术也不断的出现&#xff0c;如现在很多的小区物业管理和其它一些关于关卡出入的管理方面都采取了门禁卡的形式&#xff0c;若是门禁卡丢失了&#xff0c;那么可能会被物业管理公司几倍的罚款&#xff0c;为了避免这种情况的出现&#xff0…