文件 FruitItem.java FruitStore.java
FruitItem.java代码
1 //自定义类 2 public class FruitItem { 3 int ID; //商品编号 4 String name; //名称 5 double price; //单价 6 }
FruitStore.java代码
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 /* 5 * 超市管理系统 6 * 实现 : 7 * 1.商品数据初始化 8 * 2.菜单 9 * 3.选择功能 10 * 4.增删改查 11 */ 12 public class FruitStore { 13 public static void main(String[] args) { 14 15 //集合 16 ArrayList<FruitItem> array = new ArrayList<FruitItem>(); 17 18 //商品数据初始化 19 init(array); 20 21 while (true) { 22 //菜单选择 23 mainMenu(); 24 25 //选择功能 26 int choose = chooseFunction(); 27 28 switch (choose) { 29 case 1: 30 //查 31 showFruitList(array); 32 break; 33 case 2: 34 //增 35 addFruitItem(array); 36 break; 37 case 3: 38 delFruitItem(array); 39 break; 40 case 4: 41 updateFruitItem(array); 42 break; 43 case 5: 44 System.out.println("退出系统"); 45 return; 46 default: 47 System.out.println("没有选择的功能"); 48 break; 49 } 50 } 51 52 } 53 54 //初始化 55 public static void init(ArrayList<FruitItem> array) { 56 FruitItem item1 = new FruitItem(); 57 item1.ID = 101; 58 item1.name = "苹果"; 59 item1.price = 5.5; 60 61 FruitItem item2 = new FruitItem(); 62 item2.ID = 102; 63 item2.name = "菠萝"; 64 item2.price = 3.5; 65 66 FruitItem item3 = new FruitItem(); 67 item3.ID = 103; 68 item3.name = "甘蔗"; 69 item3.price = 1.5; 70 71 array.add(item1); 72 array.add(item2); 73 array.add(item3); 74 75 } 76 77 public static void mainMenu() { 78 System.out.println("=========================欢迎光临水果超市========================="); 79 System.out.println("1:查询货物 2:添加新货物 3:删除货物 4:修改货物 5:退出系统"); 80 System.out.println("请您输入要操作的功能序号:"); 81 } 82 83 //菜单 84 public static int chooseFunction() { 85 Scanner sc = new Scanner(System.in); 86 return sc.nextInt(); 87 } 88 89 //查询 90 public static void showFruitList(ArrayList<FruitItem> array) { 91 System.out.println("=======================商品库存清单======================="); 92 System.out.println("商品编号\t商品名称\t商品单价"); 93 for (int i = 0; i < array.size(); i++) { 94 FruitItem item = array.get(i); 95 System.out.println(item.ID+"\t"+item.name+"\t"+item.price); 96 } 97 } 98 99 //添加 100 public static void addFruitItem(ArrayList<FruitItem> array) { 101 FruitItem newItem = new FruitItem(); 102 Scanner sc = new Scanner(System.in); 103 104 System.out.println("请输入ID"); 105 newItem.ID = sc.nextInt(); 106 System.out.println("请输入名称"); 107 newItem.name = sc.next(); 108 System.out.println("输入商品的单价"); 109 newItem.price = sc.nextDouble(); 110 111 array.add(newItem); 112 System.out.println("添加成功"); 113 } 114 115 //删除 116 public static void delFruitItem(ArrayList<FruitItem> array) { 117 System.out.println("输入删除ID"); 118 Scanner sc = new Scanner(System.in); 119 int ID = sc.nextInt(); 120 121 for (int i = 0; i < array.size(); i++) { 122 if (array.get(i).ID == ID) { 123 array.remove(array.get(i)); 124 System.out.println("删除成功"); 125 return; 126 } 127 } 128 129 System.out.println("ID 不存在"); 130 } 131 132 //修改 133 public static void updateFruitItem(ArrayList<FruitItem> array) { 134 System.out.println("输入ID"); 135 Scanner sc = new Scanner(System.in); 136 int ID = sc.nextInt(); 137 138 for (int i = 0; i < array.size(); i++) { 139 FruitItem item = array.get(i); 140 if (item.ID == ID) { 141 System.out.println("请输入ID"); 142 item.ID = sc.nextInt(); 143 System.out.println("请输入名称"); 144 item.name = sc.next(); 145 System.out.println("输入商品的单价"); 146 item.price = sc.nextDouble(); 147 System.out.println("修改成功"); 148 return; 149 } 150 } 151 } 152 153 }
时间: 2024-10-12 11:35:04