java實現(xiàn)超市庫存管理系統(tǒng)
本文實例為大家分享了java庫存管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
模擬真實的庫存管理邏輯,完成超市管理系統(tǒng)的日常功能實現(xiàn)。
經(jīng)過分析,首先需要一個功能菜單,然后輸入所選的功能后,調(diào)用序號對應(yīng)的功能方法,實現(xiàn)想要的操作。
具體的步驟如下:
1.完成超市商品初始化。創(chuàng)建商品,將商品添加到集合
2.顯示來到超市能做的操作,也就是顯示主菜單
3.根據(jù)接收到的功能選項,執(zhí)行對應(yīng)的功能
3.1.庫存貨物查詢
3.2.添加新貨物
3.3.刪除貨物
3.4.修改貨物
3.5.退出系統(tǒng),結(jié)束main方法的運行
4.循環(huán),回到 2.顯示主菜單
具體的代碼實現(xiàn)步驟為:
1.每種庫存商品都擁有多項商品信息,為了方便管理每種商品的信息,對商品信息進(jìn)行封裝,編寫FruitItem.java文件
public class FruitItem { // 商品號 int ID; // 商品名字 String name; // 單價 double price; // 數(shù)量 int number; // 總金額 double money; }
2.編寫Shopp.java,完成如下功能:
①主方法 主要實現(xiàn)集合創(chuàng)建、調(diào)用商品初始化、調(diào)用菜單方法、調(diào)用序號選擇方法。
public static void main(String[] args) { //創(chuàng)建ArrayList集合,存儲商品類型,存儲數(shù)據(jù)類型FruitItem類型 ArrayList<FruitItem> array = new ArrayList<FruitItem>(); //調(diào)用商品初始化方法,傳遞集合 init(array); while(true){ //調(diào)用菜單方法 mainMenu(); //調(diào)用用戶選擇序號方法 int choose = chooseFunction(); switch (choose) { case 1: //調(diào)用1: 貨物 清單 showFruitList(array); break; case 2: //2: 添加貨物 addFruit(array); break; case 3: //3: 刪除貨物 deleteFruit(array); break; case 4: //4: 修改貨物 updateFruit(array); break; case 5: return ; default: System.out.println("輸入的序號沒有"); break; } } }
②商品初始化方法。創(chuàng)建方法,將商品添加到集合里去。
public static void init(ArrayList<FruitItem> array){ //創(chuàng)建出多個FruitItem類型,并且屬性賦值 FruitItem f1 = new FruitItem(); f1.ID = 9527; f1.name = "少林寺酥餅核桃"; f1.price = 12.7; FruitItem f2 = new FruitItem(); f2.ID = 9008; f2.name = "尚康雜糧牡丹餅"; f2.price = 5.6; FruitItem f3 = new FruitItem(); f3.ID = 9879; f3.name = "新疆原產(chǎn)哈密瓜"; f3.price = 599.6; //創(chuàng)建的3個FruitItem類型變量,存儲到集合中 array.add(f1); array.add(f2); array.add(f3); }
③菜單顯示方法,也就是顯示具體能完成哪些操作。
public static void mainMenu(){ System.out.println(); System.out.println("============歡迎光臨ItCast超市============"); System.out.println("1: 貨物 清單 2: 添加貨物 3: 刪除貨物 4: 修改貨物 5: 退出"); System.out.println("請您輸入要操作的功能序號"); }
④序號選擇方法。 根據(jù)接收到的功能選項,執(zhí)行對應(yīng)的操作。
public static int chooseFunction(){ Scanner sc = new Scanner(System.in); return sc.nextInt(); }
⑤庫存貨物查詢
public static void showFruitList(ArrayList<FruitItem> array){ System.out.println(); System.out.println("================商品庫存清單================"); System.out.println("商品編號 商品名稱 商品單價"); //遍歷集合 for(int i = 0 ; i < array.size(); i++){ //集合get方法,獲取出每個FruitItem變量,可以使用FruitItem接受get結(jié)果 FruitItem item = array.get(i); //變量item調(diào)用類中屬性 System.out.println(item.ID+" "+item.name+" "+item.price); } }
⑥添加新貨物
public static void addFruit(ArrayList<FruitItem> array){ System.out.println("選擇的是添加商品功能"); //創(chuàng)建Scanner變量 Scanner sc = new Scanner(System.in); System.out.println("請輸入商品的編號"); //輸入商品的編號 int ID = sc.nextInt(); //輸入商品的名字 System.out.println("請輸入商品的名字"); String name = sc.next(); //輸入商品的單價 System.out.println("輸入商品的單價"); double price = sc.nextDouble(); //創(chuàng)建FruitItem變量 FruitItem item = new FruitItem(); //item.屬性賦值 item.ID = ID; item.name = name; item.price = price; array.add(item); System.out.println("商品添加成功"); }
⑦刪除貨物
public static void deleteFruit(ArrayList<FruitItem> array){ System.out.println("選擇的是刪除功能"); System.out.println("請輸入商品的編號"); Scanner sc = new Scanner(System.in); int ID = sc.nextInt(); //遍歷集合 for(int i = 0 ; i < array.size(); i++){ //獲取到每個FruitItem變量 FruitItem item = array.get(i); //變量,調(diào)用屬性ID,和用戶輸入的編號比較 if( item.ID == ID){ //移除集合中的元素 //集合的方法remove實現(xiàn) array.remove(i); System.out.println("刪除成功"); return; } } System.out.println("你輸入的編號不存在"); }
⑧修改貨物
public static void updateFruit(ArrayList<FruitItem> array){ System.out.println("選擇的是修改功能"); System.out.println("請輸入商品的編號"); Scanner sc = new Scanner(System.in); int ID = sc.nextInt(); //遍歷集合,獲取每個FruitItem變量 for(int i = 0 ; i < array.size(); i++){ FruitItem item = array.get(i); //獲取FruitItem的屬性ID,和用戶輸入的ID比較 if(item.ID == ID){ System.out.println("輸入新的商品編號"); item.ID = sc.nextInt(); System.out.println("輸入新的商品名字"); item.name = sc.next(); System.out.println("輸入新的商品價格"); item.price = sc.nextDouble(); System.out.println("商品修改成功"); return ; } } System.out.println("輸入的編號不存在"); }
至此,基本上完成了各項功能,大概的運行結(jié)果為:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot項目的搭建教程(分離出common父依賴)
這篇文章主要介紹了Springboot項目的搭建教程(分離出common父依賴),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot整合Mybatis Plus實現(xiàn)基本CRUD的示例代碼
Mybatis Plus是在Mybatis的基礎(chǔ)上的增強,使得我們對一些基本的CRUD使用起來更方便,本文主要介紹了SpringBoot整合Mybatis Plus實現(xiàn)基本CRUD的示例代碼,具有一定的參考價值,感興趣的可以了解一下2023-05-05如何利用java中String類的substring()字符串截取最后一個字符
Java中的String是不可變的類型,因此substring()方法并不會改變原字符串,而是返回了一個新的字符串,這篇文章主要介紹了如何利用java中String類的substring()字符串截取最后一個字符,需要的朋友可以參考下2023-11-11SpringBoot統(tǒng)一響應(yīng)格式及統(tǒng)一異常處理
在我們開發(fā)SpringBoot后端服務(wù)時,一般需要給前端統(tǒng)一響應(yīng)格式,本文主要介紹了SpringBoot統(tǒng)一響應(yīng)格式及統(tǒng)一異常處理2023-05-05SpringBoot配置連接兩個或多個數(shù)據(jù)庫的常用方法
在Spring Boot應(yīng)用中連接多個數(shù)據(jù)庫或數(shù)據(jù)源可以使用多種方式,本文講給大家介紹兩種常用的方法:使用Spring Boot官方支持的多數(shù)據(jù)源配置和使用第三方庫實現(xiàn)多數(shù)據(jù)源,文章通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08SpringBoot?+?layui?框架實現(xiàn)一周免登陸功能示例詳解
這篇文章主要介紹了SpringBoot+layui框架實現(xiàn)一周免登陸功能,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08