Java版水果管理系統(tǒng)源碼
水果管理系統(tǒng)Java版分享給大家。
主類 FruitsDemo
/** * 功能: * 1. 查看所有的水果 * 2. 添加新的水果(添加的時(shí)候判斷水果名是否有重復(fù)) * 3. 對(duì)所有的水果進(jìn)行排序(價(jià)格排序、庫(kù)存排序) * 4. 刪除指定的水果 * 5. 退出系統(tǒng) * * 注意: * 1. 每種水果都必須有水果id,水果名,水果數(shù)量,水果價(jià)格 * 2. 添加水果時(shí),要由用戶輸入水果名、數(shù)量和價(jià)格 * 3. 刪除水果時(shí)要二次確認(rèn) * * 評(píng)分依據(jù): 功能實(shí)現(xiàn)的情況,代碼規(guī)范性(命名規(guī)范、格式規(guī)范),設(shè)計(jì)的合理性 * @author yj * */ public class FruitsDemo { public static void main(String[] args) { int select = 0; // 主菜單功能選擇 boolean isStart = true;// 程序運(yùn)行標(biāo)志位 while (isStart) { System.out.println("******************水果管理系統(tǒng)******************\n請(qǐng)輸入下列序號(hào)選擇相應(yīng)功能:\n\n 1.查看所有的水果 \t2. 添加新的水果 \n 3.對(duì)所有的水果進(jìn)行排序查看(價(jià)格排序、庫(kù)存排序) \n 4.刪除水果\t5. 退出系統(tǒng)"); select = Calculation.inputIsInt(); switch (select) { case 1://1.查看所有的水果 Calculation.seeAllFruits(); break; case 2://2. 添加新的水果 Calculation.add(); break; case 3://3.對(duì)所有的水果進(jìn)行排序查看(價(jià)格排序、庫(kù)存排序) Calculation.Sort(); break; case 4:// 4.刪除水果 System.out.println("請(qǐng)輸入你要?jiǎng)h除的水果"); String index = Calculation.inputIsString(); System.out.println("二次確認(rèn)?。?!請(qǐng)?jiān)俅屋斎肽阋獎(jiǎng)h除的水果"); String index1 = Calculation.inputIsString(); if(index.equals(index1)){ Calculation.remove(index); }else{ System.out.println("兩次輸入不匹配,刪除失敗?。?!"); } break; case 5://5. 退出系統(tǒng) isStart = false; break; default: System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入"); break; } } System.out.println("程序已退出,歡迎使用?。?!"); } }
Fruits 類
/** * 水果類 * @author yj * */ public class Fruits { // 每種水果都必須有水果id,水果名,水果數(shù)量,水果價(jià)格 private int id;//ID private int nums;//數(shù)量(庫(kù)存) private String name;//水果名 private double price;//水果價(jià)格 public Fruits(int id, String name, int nums, double price) { super(); this.id = id; this.nums = nums; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getNums() { return nums; } public void setNums(int nums) { this.nums = nums; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
Calculation 類
import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; /** * 計(jì)算類,存放關(guān)于計(jì)算處理數(shù)據(jù)的函數(shù) * * @author yj * */ public class Calculation { static LinkedList<Fruits> list = new LinkedList<Fruits>(); static Scanner sc = new Scanner(System.in); static int id = 1; /** * 添加水果 get() */ public static void add() { int nums; String name; double price; System.out.print("請(qǐng)輸入你要添加的水果名、數(shù)量(單位:個(gè))和價(jià)格(單位:元)\n"); name = Calculation.inputIsString(); nums = Calculation.inputIsInt(); price = Calculation.inputIsDouble(); if (cals(name, nums, price)) { list.add(new Fruits(id, name, nums, price)); id++; } } /** * 查看所有水果 seeAllFruits() */ public static void seeAllFruits() { if (list.size() == 0) { System.out.println("數(shù)據(jù)為空!??!"); } else { Iterator<Fruits> it = list.iterator(); while (it.hasNext()) { Fruits temp = it.next(); System.out.println("ID -> " + temp.getId() + "\t水果名稱 -> " + temp.getName() + "\t水果數(shù)量 -> " + temp.getNums() + "\t水果價(jià)格 -> " + temp.getPrice()); } } } /** * 刪除水果 remove(String index) * * @param index * 你要?jiǎng)h除的水果名 */ public static void remove(String index) { Iterator<Fruits> it = list.iterator(); while (it.hasNext()) { if (index.equals(it.next().getName())) { it.remove(); System.out.println(index + "已刪除"); } } } /** * 判斷是否重復(fù) cals(String name, int nums, double price) * * @param name * 水果名 * @param nums * 水果數(shù)量 * @param price * 水果價(jià)格 * @return */ public static boolean cals(String name, int nums, double price) { Iterator<Fruits> it1 = list.iterator(); while (it1.hasNext()) { Fruits temp = it1.next(); if (name.equals(temp.getName())) { temp.setNums(nums + temp.getNums()); temp.setPrice(price); System.out.println("水果——"+name+" 已存在,數(shù)量在原基礎(chǔ)上加上 "+nums+",價(jià)格已更新為 "+price); return false; } } return true; } /** * 排序輸出 Sort() */ public static void Sort() { System.out.println("1.按照價(jià)格升序 2.按照庫(kù)存升序"); int n = inputIsInt(); switch (n) { case 1: Collections.sort(list, new Comparator<Fruits>() { @Override public int compare(Fruits o1, Fruits o2) { if (o1.getPrice() > o2.getPrice()) { return 1; } else if (o1.getPrice() < o2.getPrice()) { return -1; } else { return 0; } // return (int) (o1.getPrice() * 100 - o2.getPrice() * 100); } }); break; case 2: Collections.sort(list, new Comparator<Fruits>() { @Override public int compare(Fruits o1, Fruits o2) { if (o1.getNums() > o2.getNums()) { return 1; } else if (o1.getNums() < o2.getNums()) { return -1; } else { return 0; } // return (int) (o1.getNums() - o2.getNums()); } }); break; default: System.out.println("輸入指令錯(cuò)誤?。?!"); break; } seeAllFruits(); } /** * 輸入是否是Int inputIsInt() * * @return */ public static int inputIsInt() { boolean isRight = true; int select = 0; do { try { select = sc.nextInt(); isRight = true; } catch (Exception e) { System.out.println("輸入類型不匹配,請(qǐng)輸入一個(gè)整數(shù)(Int)"); sc.nextLine(); isRight = false; } } while (!isRight); return select; } /** * 輸入是否是String inputIsString() * * @return */ public static String inputIsString() { boolean isRight = true; String select = null; do { try { select = sc.next(); isRight = true; } catch (Exception e) { System.out.println("輸入類型不匹配,請(qǐng)輸入一個(gè)字符串(String)"); sc.nextLine(); isRight = false; } } while (!isRight); return select; } /** * 輸入是否為Douuble * * @return */ public static Double inputIsDouble() { boolean isRight = true; Double select = null; do { try { select = sc.nextDouble(); isRight = true; } catch (Exception e) { System.out.println("輸入類型不匹配,請(qǐng)輸入一個(gè)小數(shù)(Double)!!!"); sc.nextLine(); isRight = false; } } while (!isRight); return select; } }
更多學(xué)習(xí)資料請(qǐng)關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決使用@ResponseBody后返回500錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決使用@ResponseBody后返回500錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09深度理解Java中volatile的內(nèi)存語(yǔ)義
前面我們已經(jīng)講過(guò)了volatile的作用、底層實(shí)現(xiàn)與內(nèi)存屏障,下面就總結(jié)一下整個(gè)流程,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下2021-06-06啟動(dòng)SpringBoot報(bào)JavaMail加載錯(cuò)誤的原因分析和解決
這篇文章給大家介紹了啟動(dòng)SpringBoot報(bào)JavaMail加載錯(cuò)誤的原因分析和解決,文中通過(guò)代碼示例給出了詳細(xì)的原因分析和解決方法,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01圖解Java經(jīng)典算法歸并排序的原理與實(shí)現(xiàn)
歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide?and?Conquer)的一個(gè)非常典型的應(yīng)用。本文將通過(guò)動(dòng)圖詳解歸并排序的原理及實(shí)現(xiàn),需要的可以參考一下2022-09-09Java_異常類(錯(cuò)誤和異常,兩者的區(qū)別介紹)
下面小編就為大家?guī)?lái)一篇Java_異常類(錯(cuò)誤和異常,兩者的區(qū)別介紹) 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09