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