标签:创建 类型 ice ott 例子 选择 ini print ann
使用ide来进行编程!
对于java来说,最好的idle 是intellijidea(IDEA)
通过一个例子练习 idea !
使用ide来进行编程,java最好的ide 是idea
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Shop { 5 public static void main(String[] args) { 6 ArrayList<Fruit> array = new ArrayList<Fruit>(); 7 init(array); 8 while (true) { 9 mainMenu(); 10 int choose = chooseFunc(); 11 switch (choose) { 12 case 1: 13 //货物清单 14 showFruit(array); 15 break; 16 case 2: 17 //添加货物 18 addItem(array); 19 break; 20 case 3: 21 //删除货物 22 delItem(array); 23 break; 24 case 4: 25 //修改货物 26 modifyItem(array); 27 break; 28 case 5: 29 //退出 30 return; 31 32 } 33 34 } 35 } 36 37 //初始化列表 38 public static void init(ArrayList<Fruit> arr) { 39 //创建多个Fruit 类型,并且为属性赋值 40 Fruit f1 = new Fruit(); 41 f1.ID = 111111; 42 f1.name = "苹果"; 43 f1.price = 12; 44 45 Fruit f2 = new Fruit(); 46 f2.ID = 22222; 47 f2.name = "句子"; 48 f2.price = 31; 49 50 Fruit f3 = new Fruit(); 51 f3.ID = 333333; 52 f3.name = "香蕉"; 53 f3.price = 42; 54 55 arr.add(f1); 56 arr.add(f2); 57 arr.add(f3); 58 } 59 60 //提示用户做选择,让选择序号 61 public static void mainMenu() { 62 System.out.println(); 63 System.out.println("=======欢迎光临超市========="); 64 System.out.println("1:货物清单 2:添加货物 3:删除货物 4:修改货物 5:退出"); 65 System.out.println("请你输入要操作的功能序号>>>"); 66 } 67 68 //接收用户的键盘输入 69 public static int chooseFunc() { 70 Scanner scan = new Scanner(System.in); 71 int num = scan.nextInt(); 72 return num; 73 } 74 75 public static void showFruit(ArrayList<Fruit> arr) { 76 System.out.println(); 77 System.out.println("========商品库存清单========="); 78 System.out.println("商品编号 商品名称 商品单价"); 79 80 for (int i = 0; i < arr.size(); i++) { 81 Fruit item = arr.get(i); 82 System.out.println(item.ID + " " + item.name + " " + item.price); 83 } 84 85 } 86 87 public static void addItem(ArrayList<Fruit> arr) { 88 Scanner scan = new Scanner(System.in); 89 System.out.println("请输入商品的编号"); 90 int ID = scan.nextInt(); 91 92 System.out.println("请输入商品的名字"); 93 String name = scan.next(); 94 95 //商品单价 96 System.out.println("请输入商品单价"); 97 double price = scan.nextDouble(); 98 99 Fruit item = new Fruit(); 100 item.ID = ID; 101 item.name = name; 102 item.price = price; 103 104 arr.add(item); 105 } 106 107 public static void delItem(ArrayList<Fruit> arr) { 108 System.out.println("请输入要删除的商品编号"); 109 110 Scanner scan = new Scanner(System.in); 111 int id = scan.nextInt(); 112 113 for (int i = 0; i < arr.size(); i++) { 114 if (arr.get(i).ID == id) { 115 arr.remove(i); 116 System.out.println("删除成功! "); 117 return; //此处不用break 因为最后一句话 118 } 119 } 120 System.out.println("您输入的id 不存在"); 121 } 122 public static void modifyItem(ArrayList<Fruit> arr ){ 123 System.out.println("请输入要删除的商品ID"); 124 125 Scanner scan = new Scanner(System.in); 126 int id = scan.nextInt(); 127 128 for (int i = 0; i <arr.size() ; i++) { 129 if(arr.get(i).ID == id){ 130 Fruit item = arr.get(i); 131 System.out.println("请输入新的商品名字"); 132 item.name = scan.next(); 133 134 System.out.println("请输入新的商品价格"); 135 item.price = scan.nextInt(); 136 } 137 } 138 139 } 140 141 }