Java簡單管理系統的實現

來源:互聯網
上載者:User

標籤:pre   檔案   stat   ati   ring   商品   nbsp   cti   turn   

檔案 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 }

 

Java簡單管理系統的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.