The Java version of the fruit management system is shared with everyone.
Main class FruitsDemo
/** * Function: * 1. View all fruits* 2. Add new fruits (judging whether the fruit name is duplicated when adding) * 3. Sort all fruits (price sort, inventory sort) * 4. Delete the specified fruit* 5. Exit the system* * Note: * 1. Each fruit must have a fruit id, fruit name, fruit quantity, fruit price* 2. When adding fruits, the user must enter the fruit name, quantity and price* 3. When deleting fruits, you must confirm it twice* * Scoring basis: Function implementation, code normativeness (naming specifications, format specifications), and design rationality* @author yj * */public class FruitsDemo { public static void main(String[] args) { int select = 0; // main menu function selection boolean isStart = true;// program run flag while (isStart) { System.out.println("*************************** Fruit Management System**********************/n Please enter the following number to select the corresponding function: /n/n 1. View all fruits/t2. Add new fruits/n 3. Sort all fruits (price sort, inventory sort) /n 4. Delete fruits/t5. Exit the system"); select = Calculation.inputIsInt(); switch (select) { case 1://1.View all fruits Calculation.seeAllFruits(); break; case 2://2. Add new fruit Calculation.add(); break; case 3://3.Sort all fruits (price sort, inventory sort) Calculation.Sort(); break; case 4:// 4.Delete the fruit System.out.println("Please enter the fruit you want to delete"); String index = Calculation.inputIsString(); System.out.println("Second confirmation!!! Please enter the fruit you want to delete again"); String index1 = Calculation.inputIsString(); if(index.equals(index1)){ Calculation.remove(index); }else{ System.out.println("Two inputs do not match, deletion failed!!!"); } break; case 5://5. Exit the system isStart = false; break; default: System.out.println("Input error, please re-enter"); break; } } System.out.println("Program has been exited, welcome to use!!!"); }}Fruits Class
/** * Fruit class* @author yj * */public class Fruits { // Each fruit must have a fruit id, fruit name, fruit quantity, fruit price private int id;//ID private int nums;//Quantity (inventory) private String name;//Fruit name private double price;//Fruit 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 class
import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.LinkedList;import java.util.Scanner;/** * Computation class, storing functions for computing and processing data* * @author yj * */public class Calculation { static LinkedList<Fruits> list = new LinkedList<Fruits>(); static Scanner sc = new Scanner(System.in); static int id = 1; /** * Add fruit get() */ public static void add() { int nums; String name; double price; System.out.print("Please enter the name, quantity (unit: unit) and price (unit: yuan)/n"); name = Calculation.inputIsString(); nums = Calculation.inputIsInt(); price = Calculation.inputIsDouble(); if (cals(name, nums, price)) { list.add(new Fruits(id, name, nums, price)); id++; } } /** * See all fruits seeAllFruits() */ public static void seeAllFruits() { if (list.size() == 0) { System.out.println("Data is empty!!!"); } else { Iterator<Fruits> it = list.iterator(); while (it.hasNext()) { Fruits temp = it.next(); System.out.println("ID -> " + temp.getId() + "/t fruit name-> " + temp.getName() + "/t fruit quantity-> " + temp.getNums() + "/t fruit price-> " + temp.getPrice()); } } } /** * Remove fruit remove(String index) * * @param index * The fruit name you want to delete */ 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 + "Deleted"); } } } /** * Determine whether to repeat cals(String name, int nums, double price) * * @param name * Fruit name* @param nums * Number of fruits* @param price * Fruit 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("Fruit-"+name+" already exists, the quantity is added with "+nums+" on the original basis, and the price has been updated to "+price); return false; } } return true; } /** * Sort output Sort() */ public static void Sort() { System.out.println("1. Ascending order according to price 2. Ascending order according to inventory"); 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("Input instruction error!!!"); break; } seeAllFruits(); } /** * Whether the input is 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("The input type does not match, please enter an integer (Int)"); sc.nextLine(); isRight = false; } } while (!isRight); return select; } /** * Whether the input is 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("The input type does not match, please enter a string (String)"); sc.nextLine(); isRight = false; } } while (!isRight); return select; } /** * Whether the input is Double * * @return */ public static Double inputIsDouble() { boolean isRight = true; Double select = null; do { try { select = sc.nextDouble(); isRight = true; } catch (Exception e) { System.out.println("The input type does not match, please enter a decimal (Double)!!!"); sc.nextLine(); isRight = false; } } while (!isRight); return select; }}For more learning materials, please pay attention to the special topic "Management System Development".
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.