超市管理系统简易版

时间:2022-10-20 13:17:27
  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Shopp {
  5     
  6     public static void mainMenu()//菜单
  7     {
  8         System.out.println("==============欢迎光临超市==============");
  9         System.out.println("1:货物清单"+ " " + "2:添加新货物" + " " + "3:删除货物" + "4:修改货物" + "5:退出系统");
 10         System.out.println("请输入要操作的功能序号");
 11     }
 12     public static void init(ArrayList<FruitItem> array)//超市商品初始化
 13     {
 14         FruitItem fi1= new FruitItem();
 15         fi1.ID = 10;
 16         fi1.name = "小鸡腿";
 17         fi1.price = 3;
 18         FruitItem fi2= new FruitItem();
 19         fi2.ID = 11;
 20         fi2.name = "老干妈";
 21         fi2.price = 13.5;
 22         FruitItem fi3= new FruitItem();
 23         fi3.ID = 12;
 24         fi3.name = "王老吉";
 25         fi3.price = 4;
 26         array.add(fi1);
 27         array.add(fi2);
 28         array.add(fi3);
 29     }
 30     public static void showFruitList(ArrayList<FruitItem> array)//库存货物查询
 31     {
 32         System.out.println();
 33         System.out.println("============商品清单============");
 34         System.out.println("商品编号"+ "  "+"商品名称" + "  "+"商品单价");
 35         for(int i = 0; i < array.size(); i++)
 36         {
 37             FruitItem fi = array.get(i);
 38             System.out.println(fi.ID + "    "+ fi.name + "       "+fi.price);
 39         }
 40         
 41     }
 42     //添加新货物
 43     public static void addFruitItem(ArrayList<FruitItem> array)
 44     {
 45         FruitItem fi = new FruitItem();
 46         Scanner sc = new Scanner(System.in);
 47         System.out.println("请输入新的商品编号");
 48         fi.ID = sc.nextInt();
 49         System.out.println("请输入新的商品名称");
 50         fi.name = sc.next();
 51         System.out.println("输入新商品的价格");
 52         fi.price = sc.nextDouble();
 53         array.add(fi);
 54     }
 55     public static void delFruitItem(ArrayList<FruitItem> array)//删除商品
 56     {
 57         System.out.println("选择的是删除商品功能");
 58         System.out.println("请输入商品编号");
 59         Scanner sc = new Scanner(System.in);
 60         int ID = sc.nextInt();
 61         for(int i = 0; i < array.size(); i++)
 62         {
 63             FruitItem fi = array.get(i);
 64             if(fi.ID == ID)
 65             {
 66                 array.remove(i);
 67                 System.out.println("删除成功");
 68                 return;//当fi.ID == ID时,就删除了,没必要再循环比下去了,直接结束return
 69             }
 70         }
 71         System.out.println("未找到该商品!");
 72     }
 73     public static void updateFruitItem(ArrayList<FruitItem> array)
 74     {
 75         System.out.println("这是修改功能");
 76         System.out.println("请输入需要修改的商品编号");
 77         Scanner sc = new Scanner(System.in);
 78         int ID = sc.nextInt();
 79         System.out.println("您修改的商品编号为:" + ID);
 80         for(int i = 0; i < array.size();i++)
 81         {
 82             if(array.get(i).ID == ID)
 83             {
 84                 System.out.println("请输入新的商品编号");
 85                 array.get(i).ID = sc.nextInt();
 86                 System.out.println("请输入新的商品名称");
 87                 array.get(i).name = sc.next();
 88                 System.out.append("请输入新的商品单价");
 89                 array.get(i).price = sc.nextDouble();
 90                 System.out.println("修改成功");
 91                 return;//当编号相等,改商品数据成功后,没必要继续执行下面的输出语句
 92             }
 93         }
 94         System.out.println("您输入的编号不存在");
 95     }
 96     
 97     public static void main(String[] args) {
 98         ArrayList<FruitItem> array = new ArrayList<FruitItem>();
 99         init(array);
100         Scanner sc = new Scanner(System.in);
101         
102         while(true)//死循环,一定要保证循环条件有变成false的时候
103         {
104             mainMenu();
105             int i = sc.nextInt();
106             switch(i)
107             {
108             case 1:
109                 showFruitList(array);//货物清单
110                 break;
111             case 2:
112                 addFruitItem(array);//添加新货物
113                 break;
114             case 3:
115                 delFruitItem(array);//删除商品
116             break;
117             case 4:
118             updateFruitItem(array);//修改商品
119             break;
120             case 5:
121             System.out.println("退出系统");
122             return;
123             default:
124             System.out.println("暂无此功能");
125             break;    
126         }
127         
128     }
129     }
130 }