import java.util.ArrayList;
import java.util.Scanner;
public class Supermarket {
public static void main(String[] args) {
ArrayList<Goodsitem> list = new ArrayList<Goodsitem>();
//以下为各方法调用
init(list);
while (true) {
mainMenu();//此处为显示主菜单
int n = chooseFunction();//此处为录入客户的输入序号
switch (n) {
case 1://1 选择货物清单,此处调用
goodsList(list);
break;
case 2://2 添加新货物
addGoods(list);
break;
case 3://3 删除货物
deleteGoods(list);
break;
case 4://修改货物
modifyGoods(list);
break;
case 5://退出
return ;
default:
System.out.println("您输入的序号不存在!!");
break;
}
}
}
//方法1,此处需要存储三种商品的信息
public static void init(ArrayList<Goodsitem> list) {
Goodsitem g1 = new Goodsitem();
g1.name = "五仁月饼";
g1.ID = 1001;
g1.price = 15.6;
Goodsitem g2 = new Goodsitem();
g2.name = "糖水罐头";
g2.ID = 1002;
g2.price = 9.9;
Goodsitem g3 = new Goodsitem();
g3.name = "东北大米";
g3.ID = 1003;
g3.price = 39.9;
list.add(g1); //将商品信息添加到集合中
list.add(g2);
list.add(g3);
}
//方法2:此处设置选择菜单
public static void mainMenu() {
System.out.println("=====欢迎光临itCast超市=====");
System.out.println("1.货物清单 2.添加新货物 3.删除货物 4.修改货物 5.退出系统");
System.out.println("请输入您要选择的功能序号");
}
//方法3:录入客户的输入请求
public static int chooseFunction() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
return n;
}
//方法4:此处需要打印欢迎界面以及货物清单
public static void goodsList(ArrayList<Goodsitem> list) {
System.out.println("=====商品库存管理=====");
System.out.println("商品编号 商品名称 商品单价");
//此处需要打印获取清单,即遍历集合
for (int i = 0; i < list.size(); i++) {
Goodsitem good = list.get(i); //获取集合中索引为i的元素信息
System.out.println(good.ID+good.name+good.price);
}
}
//方法5:添加新货物
public static void addGoods(ArrayList<Goodsitem> list) {
System.out.println("请输入货物的ID");
Scanner sc = new Scanner(System.in);//键盘录入多个数据,创建一个键盘录入实例即可
int id1 = sc.nextInt();
System.out.println("请输入货物的name");
String name1 = sc.next();
System.out.println("请输入货物的price");
Double price1 = sc.nextDouble();
Goodsitem goods = new Goodsitem();
goods.ID = id1;
goods.name = name1;
goods.price = price1;
list.add(goods);//保存goods商品到集合
}
//方法6:删除货物
public static void deleteGoods(ArrayList<Goodsitem> list) {
System.out.println("请输入要删除的货物ID");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//此处需要遍历集合,找到ID为num的元素
for (int i = 0; i < list.size(); i++) {
Goodsitem deteleGoods = list.get(i);
if (deteleGoods.ID==num) {
list.remove(i); //删除索引为i的元素
System.out.println("删除货物成功!");
return; //此处如果是break,后面的没有找打ID,依然会执行。
}
}
System.out.println("警告:没有找到此ID");//词句如果加在if后的else内部,会输出三遍
}
//方法7:修改货物信息
public static void modifyGoods(ArrayList<Goodsitem> list) {
System.out.println("请输入要修改货物的ID");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
Goodsitem modifyGoods = list.get(i);
if (modifyGoods.ID==num) {
System.out.println("请输入新货物ID");
// modifyGoods.ID =sc.nextInt();
int ID = sc.nextInt();
System.out.println("请输入新货物name");
// modifyGoods.name =sc.next();
String name = sc.next();
System.out.println("请输入新货物price");
// modifyGoods.price =sc.nextDouble();
Double price = sc.nextDouble();
modifyGoods.ID = ID;
modifyGoods.name = name;
modifyGoods.price = price;
System.out.println("商品信息更新完成");
return ;
}
}
System.out.println("输入的编号不存在");
}
}
|
|