java 集合小练习 超市库存管理系统
1 package Demo02; 2 3 public class chaoshi { 4 int id; 5 String name; 6 double price; 7 }
1 package Demo02; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 public class test { 5 public static void main(String[] args) { 6 ArrayList<chaoshi> arr = new ArrayList<chaoshi>(); //导入超市类 7 chaoshi arr1 = new chaoshi(); //创建一个新对象,对他的属性赋值 8 arr1.id = 9001; 9 arr1.name = "核桃饼"; 10 arr1.price = 120.0; 11 arr.add(arr1); 12 chaoshi arr2 = new chaoshi(); 13 arr2.id = 9002; 14 arr2.name = "牡丹饼"; 15 arr2.price = 20.0; 16 arr.add(arr2); 17 chaoshi arr3 = new chaoshi(); 18 arr3.id = 9002; 19 arr3.name = "哈蜜瓜"; 20 arr3.price = 7.0; 21 arr.add(arr3); 22 while(true){ //while来个死循环,然后流程语句switch每次执行都走一遍第一个方法 23 switch(shouye()){ 24 case 1: 25 a(arr); 26 break; 27 case 2: 28 b(arr); 29 break; 30 case 3: 31 c(arr); 32 break; 33 case 4 : 34 d(arr); 35 break; 36 case 5: 37 return; 38 } 39 } 40 41 } 42 public static int shouye(){ 43 System.out.println("=========欢迎oracle超市=========="); 44 System.out.println("1.货物清单"); 45 System.out.println("2.添加新货物"); 46 System.out.println("3.删除货物"); 47 System.out.println("4.修改货物"); 48 System.out.println("退出系统"); 49 Scanner sc = new Scanner(System.in); 50 System.out.println("请输入要操作的编号"); 51 int num = sc.nextInt(); 52 return num; 53 } 54 55 public static void a(ArrayList<chaoshi> arr){ 56 System.out.println("=======商品库存清单=========="); 57 System.out.print("商品单号"); 58 System.out.print("商品名称"); 59 System.out.println("商品价格"); 60 for(int i = 0;i<arr.size();i++){ 61 System.out.println(arr.get(i).id+"\t"+arr.get(i).name+"\t"+arr.get(i).price); 62 } 63 } 64 public static void b(ArrayList<chaoshi> arr){ 65 Scanner sc = new Scanner(System.in); 66 System.out.println("请输入新水果的名称"); 67 String a = sc.next(); 68 System.out.println("请输入新水果的编号"); 69 int b = sc.nextInt(); 70 System.out.println("请输入新水果的单价"); 71 double c = sc.nextDouble(); 72 chaoshi arr4 = new chaoshi(); 73 arr4.name = a; 74 arr4.id = b; 75 arr4.price = c; 76 arr.add(arr4); 77 } 78 public static void c(ArrayList<chaoshi> arr){ 79 System.out.println("请输入要删除的编号"); 80 Scanner sc = new Scanner(System.in); 81 int a = sc.nextInt(); 82 for(int i= 0;i<arr.size();i++){ 83 if(arr.get(i).id== a){ 84 arr.remove(i); 85 } 86 } 87 System.out.println("删除完毕"); 88 } 89 public static void d(ArrayList<chaoshi> arr){ 90 System.out.println("请输入要修改的水果编号"); 91 Scanner sc = new Scanner(System.in); 92 int a = sc.nextInt(); 93 System.out.println("请输入要新的水果编号"); 94 int b = sc.nextInt(); 95 System.out.println("请输入要新的水果名称"); 96 String c = sc.next(); 97 System.out.println("请输入要新的水果单价"); 98 double d = sc.nextDouble(); 99 for(int i= 0;i<arr.size();i++){ 100 if(arr.get(i).id== a){ 101 arr.get(i).id = b; 102 arr.get(i).name = c; 103 arr.get(i).price = d; 104 System.out.println("修改完毕"); 105 } 106 } 107 } 108 }