Java version of the fruit management system source code, java management system source code
The Java version of the fruit management system is shared with you.
Main class FruitsDemo
/*** Function: * 1. view All fruits * 2. add a new fruit (check whether the fruit name is repeated when added) * 3. sort all fruits (price and inventory) * 4. delete the specified fruit * 5. exit the system ** Note: * 1. each fruit must have a Fruit id, fruit name, fruit quantity, and fruit price * 2. when adding a fruit, you must enter the fruit name, quantity, and price * 3. when deleting fruit, you must confirm the score twice. ** basis: functional implementation, code normalization (naming and format ), design rationality * @ author yj **/public class FruitsDemo {public static void main (String [] args) {int select = 0; // choose boolean isStart = true for the main menu function; // while (isStart) {System. out. Println ("******************** fruit Management System **************** ** \ n enter the following sequence number to select the corresponding function: \ n 1. view All fruits \ t2. add new fruits \ n 3. sort all fruits (price sorting and stock sorting) \ n 4. delete fruit \ t5. exit system "); select = Calculation. inputIsInt (); switch (select) {case 1: // 1. view All fruit Calculation. seeAllFruits (); break; case 2: // 2. add a new fruit Calculation. add (); break; case 3: // 3. order all fruits (price sorting and inventory sorting) Calculation. sort (); break; case 4: // 4. delete the fruit System. out. println ("Enter you Fruit to be deleted "); String index = Calculation. inputIsString (); System. out. println (" Secondary confirmation !!! 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 System isStart = false; break; default: System. out. println ("input error, please enter again"); break ;}} System. out. println ("the program has exited. Welcome !!! ");}}
Fruits class
/*** Fruit class ** @ author yj **/public class Fruits {// each fruit must have a Fruit id, fruit name, fruit quantity, and 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. using list; import java. util. compute;/*** computing class, the function for storing the computing and processing data ** @ author yj **/public class Calculation {static vertex list <Fruits> list = new vertex list <Fruits> (); static pipeline SC = new pipeline (System. in); static int id = 1;/*** add fruit get () */public static void add () {int nums; String name; dou Ble price; System. out. print ("Enter the name, quantity (unit: quantity), and price (unit: RMB) \ n"); name = Calculation. inputIsString (); nums = Calculation. inputIsInt (); price = Calculation. inputIsDouble (); if (cals (name, nums, price) {list. add (new Fruits (id, name, nums, price); id ++ ;}}/*** view 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 () ;}}}/*** Delete fruit remove (String index) ** @ param index * Name of the fruit 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 cals is repeated (String name, int nums, double price) ** @ param name * fruit name * @ param nums * fruit quantity * @ 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 to the original base with" + nums + ". The price has been updated to" + price); return false ;}} return true;}/*** Sort output Sort () */public static void Sort () {System. out. println ("1. sort by price 2. sort by inventory in ascending order "); int n = inputIsInt (); switch (n) {case 1: Collections. sort (list, new Comparator <Fruits> () {@ Override public in T 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 ("the input command is incorrect !!! "); 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 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 ("the input type does not match. Please input a decimal number (Double )!!! "); SC. nextLine (); isRight = false ;}} while (! IsRight); return select ;}}
For more information, see management system development.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.