国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java版水果管理系統源碼

2024-07-13 10:16:41
字體:
來源:轉載
供稿:網友

水果管理系統Java版分享給大家。

Java,水果,管理系統,Java版管理系統,Java水果管理系統

主類 FruitsDemo

/** * 功能:  * 1. 查看所有的水果  * 2. 添加新的水果(添加的時候判斷水果名是否有重復)  * 3. 對所有的水果進行排序(價格排序、庫存排序)  * 4. 刪除指定的水果 * 5. 退出系統 *  * 注意:  * 1. 每種水果都必須有水果id,水果名,水果數量,水果價格  * 2. 添加水果時,要由用戶輸入水果名、數量和價格  * 3. 刪除水果時要二次確認 *  * 評分依據: 功能實現的情況,代碼規范性(命名規范、格式規范),設計的合理性 * @author yj * */public class FruitsDemo { public static void main(String[] args) { int select = 0; // 主菜單功能選擇 boolean isStart = true;// 程序運行標志位 while (isStart) {  System.out.println("******************水果管理系統******************/n請輸入下列序號選擇相應功能:/n/n 1.查看所有的水果 /t2. 添加新的水果 /n 3.對所有的水果進行排序查看(價格排序、庫存排序) /n 4.刪除水果/t5. 退出系統");  select = Calculation.inputIsInt();  switch (select) {  case 1://1.查看所有的水果  Calculation.seeAllFruits();  break;  case 2://2. 添加新的水果  Calculation.add();  break;  case 3://3.對所有的水果進行排序查看(價格排序、庫存排序)  Calculation.Sort();  break;  case 4:// 4.刪除水果  System.out.println("請輸入你要刪除的水果");  String index = Calculation.inputIsString();  System.out.println("二次確認?。。≌堅俅屋斎肽阋獎h除的水果");  String index1 = Calculation.inputIsString();  if(index.equals(index1)){   Calculation.remove(index);  }else{   System.out.println("兩次輸入不匹配,刪除失?。。?!");  }  break;  case 5://5. 退出系統  isStart = false;  break;  default:  System.out.println("輸入錯誤,請重新輸入");  break;  } } System.out.println("程序已退出,歡迎使用?。。?quot;); }}

Fruits 類

/** * 水果類 * @author yj * */public class Fruits { // 每種水果都必須有水果id,水果名,水果數量,水果價格 private int id;//ID private int nums;//數量(庫存) 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;/** * 計算類,存放關于計算處理數據的函數 *  * @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("請輸入你要添加的水果名、數量(單位:個)和價格(單位:元)/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("數據為空?。?!"); } else {  Iterator<Fruits> it = list.iterator();  while (it.hasNext()) {  Fruits temp = it.next();  System.out.println("ID -> " + temp.getId() + "/t水果名稱 -> " + temp.getName() + "/t水果數量 -> "   + 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 + "已刪除");  } } } /** * 判斷是否重復 cals(String name, int nums, double price) *  * @param name *  水果名 * @param nums *  水果數量 * @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+" 已存在,數量在原基礎上加上 "+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("輸入類型不匹配,請輸入一個整數(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("輸入類型不匹配,請輸入一個小數(Double)!!!");  sc.nextLine();  isRight = false;  } } while (!isRight); return select; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浑源县| 吴川市| 莱芜市| 桂阳县| 增城市| 瑞安市| 青龙| 新津县| 大石桥市| 舟曲县| 兴国县| 府谷县| 石嘴山市| 吴堡县| 齐河县| 达孜县| 万宁市| 顺义区| 彩票| 子洲县| 鄱阳县| 集安市| 阿坝县| 乡宁县| 瓮安县| 香港| 阿克| 湾仔区| 资中县| 安庆市| 石渠县| 浦北县| 太保市| 宁晋县| 海兴县| 红桥区| 昭觉县| 巴马| 确山县| 盐源县| 垫江县|