package com.oracle.demo02;
import java.util.ArrayList;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner dd = new Scanner(System.in);
ArrayList list = new ArrayList();
while (true) {
mainMenu();
// 让用户输入选择
int choose = dd.nextInt();
// 对用户选择进行判断
switch (choose) {
case 1:// 货物清单
getGoods(list);
break;
case 2:deleteGoods(list);
break;
case 3:
addGoods(list);
break;
case 4:ubdateGoods(list);
break;
case 5:
return;
default:
System.out.println("您的输入有误,请重新输入");
}
}
}
public static void mainMenu() {
System.out.println("=======欢迎光临=======");
System.out.println("1.货物清单");
System.out.println("2.删除货物");
System.out.println("3.添加货物");
System.out.println("4.修改货物");
System.out.println("5.退出");
System.out.println("请输入您的选择");
}
// 添加货物页面
public static void addGoods(ArrayList list) {
System.out.println("请输新水果的编号");
Scanner sc = new Scanner(System.in);
int gid = sc.nextInt();
System.out.println("请输新水果的名称:");
String gname = sc.next();
System.out.println("请输新水果的价格:");
double price = sc.nextDouble();
Goods goods = new Goods();
// 把属性封装
goods.gid = gid;
goods.gname = gname;
goods.price = price;
// 将对象存入集合
list.add(goods);
}
public static void getGoods(ArrayList list) {
System.out.println("=======库存商品清单=====");
System.out.println("商品编号\t商品名称\t商品单价");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).gid + "\t" + list.get(i).gname + "\t" + list.get(i).price);
}
}
// 修改頁面
public static void ubdateGoods(ArrayList list) {
// 給用戶展示
getGoods(list);
System.out.println("请输入您要修改的编码");
Scanner sc = new Scanner(System.in);
int gid = sc.nextInt();
System.out.println("請輸入您要修改的名稱");
String gname = sc.next();
System.out.println("請輸入您要修改的價格");
double price = sc.nextDouble();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).gid == gid) {
list.get(i).gname = gname;
list.get(i).price = price;
}
}
}
//删除页面
public static void deleteGoods(ArrayList list) {
// 給用戶展示
getGoods(list);
System.out.println("请输入您要删除的编码");
Scanner sc = new Scanner(System.in);
int gid = sc.nextInt();
//找到用户输入的gid对应的商品
for (int i = 0; i < list.size(); i++) {
if (list.get(i).gid == gid) {
//删除商品对象
list.remove(i);}
}
}
}