Java版水果管理系統(tǒng)源碼
更新時間:2018年01月15日 13:42:50 作者:叁念
這篇文章主要為大家詳細(xì)介紹了Java版水果管理系統(tǒng)源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
水果管理系統(tǒng)Java版分享給大家。
主類 FruitsDemo
/** * 功能: * 1. 查看所有的水果 * 2. 添加新的水果(添加的時候判斷水果名是否有重復(fù)) * 3. 對所有的水果進(jìn)行排序(價格排序、庫存排序) * 4. 刪除指定的水果 * 5. 退出系統(tǒng) * * 注意: * 1. 每種水果都必須有水果id,水果名,水果數(shù)量,水果價格 * 2. 添加水果時,要由用戶輸入水果名、數(shù)量和價格 * 3. 刪除水果時要二次確認(rèn) * * 評分依據(jù): 功能實現(xiàn)的情況,代碼規(guī)范性(命名規(guī)范、格式規(guī)范),設(shè)計的合理性 * @author yj * */ public class FruitsDemo { public static void main(String[] args) { int select = 0; // 主菜單功能選擇 boolean isStart = true;// 程序運行標(biāo)志位 while (isStart) { System.out.println("******************水果管理系統(tǒng)******************\n請輸入下列序號選擇相應(yīng)功能:\n\n 1.查看所有的水果 \t2. 添加新的水果 \n 3.對所有的水果進(jìn)行排序查看(價格排序、庫存排序) \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.對所有的水果進(jìn)行排序查看(價格排序、庫存排序) Calculation.Sort(); break; case 4:// 4.刪除水果 System.out.println("請輸入你要刪除的水果"); String index = Calculation.inputIsString(); System.out.println("二次確認(rèn)?。?!請再次輸入你要刪除的水果"); 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("輸入錯誤,請重新輸入"); break; } } System.out.println("程序已退出,歡迎使用?。?!"); } }
Fruits 類
/** * 水果類 * @author yj * */ public class Fruits { // 每種水果都必須有水果id,水果名,水果數(shù)量,水果價格 private int id;//ID private int nums;//數(shù)量(庫存) private String name;//水果名 private double price;//水果價格 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; /** * 計算類,存放關(guān)于計算處理數(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("請輸入你要添加的水果名、數(shù)量(單位:個)和價格(單位:元)\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水果價格 -> " + temp.getPrice()); } } } /** * 刪除水果 remove(String index) * * @param index * 你要刪除的水果名 */ 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 * 水果價格 * @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+",價格已更新為 "+price); return false; } } return true; } /** * 排序輸出 Sort() */ public static void Sort() { System.out.println("1.按照價格升序 2.按照庫存升序"); 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("輸入指令錯誤?。?!"); 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("輸入類型不匹配,請輸入一個整數(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("輸入類型不匹配,請輸入一個字符串(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("輸入類型不匹配,請輸入一個小數(shù)(Double)!!!"); sc.nextLine(); isRight = false; } } while (!isRight); return select; } }
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
啟動SpringBoot報JavaMail加載錯誤的原因分析和解決
這篇文章給大家介紹了啟動SpringBoot報JavaMail加載錯誤的原因分析和解決,文中通過代碼示例給出了詳細(xì)的原因分析和解決方法,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01圖解Java經(jīng)典算法歸并排序的原理與實現(xiàn)
歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide?and?Conquer)的一個非常典型的應(yīng)用。本文將通過動圖詳解歸并排序的原理及實現(xiàn),需要的可以參考一下2022-09-09