public class FruitStore {
public static void main(String[] args) {
// 定义流水集合
ArrayList<Double> flowData = new ArrayList<>();
// 定义水果集合
ArrayList<Fruit> list = new ArrayList<>();
// 初始化数据
init(list);
// 用户输入作为下面选择功能号
while (true) {
// 显示功能操作界面
mainMenu();
System.out.println("请输入要操作的功能数");
int choose = enterNumber();
switch (choose) {
case 1:
// 购买
buyFruit(list);
//购买结束后添加一次流水账单
flowDatas(flowData,list);
break;
case 2:
// 打印小票
printReceipt(list);
break;
case 3:
// 重置数据
resetNumber(list);
break;
case 4:
// 流水查询
showDatas(flowData);
break;
case 5:
// 总收入查询方法
totalMoneys(flowData);
break;
case 6:
// 查询货物详情
showFruit(list);
break;
case 7:
// 添加货物方法
addFruit(list);
break;
case 8:
// 删除货物
delFruit(list);
break;
case 9:
// 修改货物
updateFruit(list);
break;
default:
System.out.println("输入功能格式不正确");
}
}
}
// 初始化
public static void init(ArrayList<Fruit> list) {
// 创建一个种类的水果对象
Fruit item1 = new Fruit();
item1.name = "苹果";
item1.id = 1100;
item1.price = 22.2;
item1.count = 22;
item1.unit = "公斤";
item1.money = item1.price * item1.count;
Fruit item2 = new Fruit();
item2.name = "桃子";
item2.id = 1101;
item2.price = 32.2;
item2.count = 32;
item2.unit = "千克";
item2.money = item1.price * item2.count;
list.add(item1);
list.add(item2);
}
// 显示功能菜单
public static void mainMenu() {
System.out.println("==========水果超市==========");
System.out.println("1: 购物 2: 打印小票 3: 重置数据 4: 流水查询 5: 总收入查询");
System.out.println("6: 查询货物 7: 添加货物 8: 删除货物 9:修改货物 10: 退出");
}
//1: 购买商品
public static void buyFruit(ArrayList<Fruit> list) {
// 显示商品列表
System.out.println("---------欢迎购物--------------");
for (int i = 0; i < list.size(); i++) {
System.out.println("名字 编号 单价 计价单位 数量 金额");
Fruit item = list.get(i);
System.out.println(item.name + "\t" + item.id + "\t" + item.price
+ "\t" + item.unit + "\t" + item.count + "\t" + item.money);
}
// 显示打折信息
System.out.println("打折为金额为: "+ totaldazhe(list));
// 是否继续购物
System.out.println("=============继续购物=============");
while (true) {
System.out.println("1: 继续购物 0: 结束购物");
System.out.println("请输入选择功能菜单");
int choose = enterNumber();
switch(choose) {
case 0:
System.out.println("购物结束");
return;
case 1:
System.out.println("请输入商品编号");
int enterID = enterNumber();
// 遍历集合,判断输入编号是否存在
for (int j = 0; j < list.size(); j++) {
if (enterID == list.get(j).id) {
System.out.println("请输入商品数量");
//商品原本数量 + 购买数量
list.get(j).count += enterNumber();
break;
}
}
break;
default:
System.out.println("请输入操作有误.请重新输入");
}
//购物完毕统计打折后一次流水的金额
System.out.println("本次消费折后应收: " + totaldazhe(list));
System.out.println("该种类购物完毕,请继续选");
}
}
//2: 打印小票
public static void printReceipt(ArrayList<Fruit> list) {
while (true) {
// 飘头
System.out.println("========商品小票======");
// 票体
for (int i = 0; i < list.size(); i++) {
Fruit item = list.get(i);
// 打印数量不为0 的
if (item.count != 0) {
System.out.println("名字 编号 单价 计价单位 数量 金额");
System.out.println(item.name + "\t" + item.id + "\t"
+ item.price + "\t" + item.unit + "\t" + item.count
+ "\t" + item.money);
}
}
//显示商品种类
System.out.println("共添加购物车: "+list.size()+ " 种商品");
return;
}
}
//3: 重置数据
public static void resetNumber(ArrayList<Fruit> list) {
for (int i = 0; i < list.size(); i++) {
Fruit setFruit = list.get(i);
setFruit.count = 0;
setFruit.money = 0;
}
}
//5: 展示流水
public static void showDatas(ArrayList<Double> flowData) {
// 判断是否存在流水长度
if (flowData.size() <= 0) {
System.out.println("今天还没开张!");
return;
}
// 遍历并打印流水元素
for (int i = 0; i < flowData.size(); i++) {
System.out.println(flowData.get(i));
}
System.out.println("老板今天共 " + flowData.size() + " 个订单");
}
//6: 查询总收入 -------- 遍历流水集合得到折后总额 . 并返回所有折后金额之和
public static void totalMoneys(ArrayList<Double> flowData) {
double totalMoney = 0;
for (int i = 0; i < flowData.size(); i++) {
totalMoney += flowData.get(i);
}
System.out.println("老板今天销售额度为: "+totalMoney);
}
//7: 查询货物
public static void showFruit(ArrayList<Fruit> list) {
System.out.println("==========物品清单============");
System.out.println("名称 编号 单价 计价单位 数量 金额");
for (int j = 0; j < list.size(); j++) {
Fruit fruit = list.get(j);
System.out.println(fruit.name + "\t" + fruit.id + "\t"
+ fruit.price + "\t" + fruit.unit + "\t" + fruit.count
+ "\t" + fruit.money);
}
}
//8: 添加商品
public static void addFruit(ArrayList<Fruit> list) {
// 输入物品编号
int enterID = enterNumber();
for (int i = 0; i < list.size(); i++) {
Fruit fruit = list.get(i);
if (enterID == fruit.id) {
System.out.println("商品已经存在,无法添加");
return;
}
System.out.println("编号没重复,可以进行天健");
fruit.id = enterID;
System.out.println("请输入商品名称");
fruit.name = enterString();
System.out.println("请输入价格");
fruit.price = enterDouble();
System.out.println("请输入计价单位");
fruit.unit = enterString();
System.out.println("请输入商品数量");
fruit.count = enterNumber();
System.out.println("请输入商品金额");
fruit.money = enterDouble();
list.add(fruit);
System.out.println("添加完毕");
}
}
//9: 删除商品
public static void delFruit(ArrayList<Fruit> list) {
// 输入要删除商品编号
System.out.println("请输入商品编号");
int enterID = enterNumber();
for (int i = 0; i < list.size(); i++) {
Fruit fruit = list.get(i);
if (enterID == fruit.id) {
System.out.println("输入的编号不存在");
return;
}
list.remove(i);
System.out.println("删除完毕");
}
}
//10: 修改货物
public static void updateFruit(ArrayList<Fruit> list) {
int enterID = enterNumber();
for (int i = 0; i < list.size(); i++) {
Fruit fruit = list.get(i);
if (enterID == fruit.id) {
fruit.id = enterID;
System.out.println("请输入商品名称");
fruit.name = enterString();
System.out.println("请输入价格");
fruit.price = enterDouble();
System.out.println("请输入计价单位");
fruit.unit = enterString();
System.out.println("请输入商品数量");
fruit.count = enterNumber();
System.out.println("请输入商品金额");
fruit.money = enterDouble();
return;
}
System.out.println("输入商品部存在");
}
}
//流水账单-------------------添加单笔折后金额到flowData 流水集合中
public static void flowDatas(ArrayList<Double> flowData,ArrayList<Fruit> list) {
double total = totaldazhe(list);
flowData.add(total);
}
//打折信息 ----------返回单笔打折总额
public static double totaldazhe(ArrayList<Fruit> list) {
//单笔交易的折前金额
double money = totalEvery(list);
//调用打折方法,返回单笔交易折后金额
double total = doDisCount(money);
return total;
}
//单笔交易的折前金额
public static double totalEvery(ArrayList<Fruit> list){
double sum = 0 ;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i).money;
}
return sum;
}
// 打折金额方法
public static double doDisCount(double totalMoney) {
double dodis = totalMoney;
// 打折逻辑
if (totalMoney < 300) {
dodis = totalMoney * 0.9;
} else if (totalMoney >= 300 && totalMoney < 1000) {
dodis = totalMoney * 0.85;
} else {
dodis = totalMoney * 0.8;
}
return dodis;
}
// 键盘录入
public static int enterNumber() {
return new Scanner(System.in).nextInt();
}
public static String enterString() {
return new Scanner(System.in).next();
}
public static Double enterDouble() {
return new Scanner(System.in).nextDouble();
}
} |
|