package com.tedu.day05; import java.util.Scanner; public class Demo { /** * 管理員能夠進行的操作有3項(查看、修改、退出),我們可以采用(switch)菜單的方式來完成。 * -------------庫存管理------------ * 1.查看庫存清單 * 2.修改商品庫存數量 * 3.退出 請輸入要執行的操作序號: * 每一項功能操作,我們采用方法進行封裝,這樣,可使程序的可讀性增強。 * 選擇“1.查看庫存清單”功能,則控制台打印庫存清單 * 選擇“2.修改商品庫存數量”功能,則對每種商品庫存數進行更新 * 選擇“3.退出”功能,則退出庫存管理,程序結束 */ public static void main(String[] args) { String[] brand = {"MacBook","ThinkPad"}; double[] size = {13.3,14.7}; double[] price = {7988.88,6999.88}; int[] count = {0,0}; while(true){ printMenu(); System.out.println("請輸入菜單選項:"); Scanner scan =new Scanner(System.in); int number = scan.nextInt(); switch(number){ case 1: printStock(brand, size, price, count); break; case 2: alterCount(brand, count); break; case 3: System.out.println("歡迎下次再來!!!"); return; default: System.out.println("輸入格式或字符錯誤,請重新輸入"); } } } //數組遍歷查看庫存的方法 public static void printStock(String[] brand, double[] size, double[] price,int[] count){ int totalCount = 0; double totalPrice = 0; System.out.println("--------庫存管理--------"); System.out.println("品名----------尺寸----------價格----------數量"); for(int i=0;i<brand.length;i++){ System.out.println(brand[i]+"------"+size[i]+"---------"+price[i]+"------"+count[i]+"\t"); totalCount += count[i]; totalPrice = count[i]*price[i]; } System.out.println("----------------------------"); System.out.println("庫存總量:"+totalCount); System.out.println("庫存總金額:"+totalPrice); } //修改商品庫存的方法 public static void alterCount(String[] brand,int[] count){ for(int i=0;i<count.length;i++){ System.out.println("請輸入"+brand[i]+"的數量"); Scanner scan = new Scanner(System.in); int newCount = scan.nextInt(); count[i] = newCount; } } //顯示功能菜單的方法 public static void printMenu(){ System.out.println("1.查看庫存清單"); System.out.println("2.修改庫存數量"); System.out.println("3.退出"); } }
功能還能拓展。。。