1 Package Demo02; 2 3 Public class Chaoshi {4 int ID; 5 String name; 6 Double Price ; 7 }
1 PackageDemo02;2 Importjava.util.ArrayList;3 ImportJava.util.Scanner;4 Public classTest {5 Public Static voidMain (string[] args) {6arraylist<chaoshi> arr =NewArraylist<chaoshi> ();//Import Supermarket class7Chaoshi arr1 =NewChaoshi ();//Create a new object, assign a value to his property8Arr1.id = 9001;9Arr1.name = "Walnut cake";TenArr1.price = 120.0; One Arr.add (arr1); AChaoshi arr2 =NewChaoshi (); -Arr2.id = 9002; -Arr2.name = "Peony Cake"; theArr2.price = 20.0; - Arr.add (ARR2); -Chaoshi ARR3 =NewChaoshi (); -Arr3.id = 9002; +Arr3.name = "Hami melon"; -Arr3.price = 7.0; + Arr.add (ARR3); A while(true){//the while comes to a dead loop, then the process statement switch executes every time it goes through the first method at Switch(Shouye ()) { - Case1: - A (arr); - Break; - Case2: - B (arr); in Break; - Case3: to C (arr); + Break; - Case4 : the d (arr); * Break; $ Case5:Panax Notoginseng return; - } the } + A } the Public Static intShouye () { +SYSTEM.OUT.PRINTLN ("========= Welcome Oracle Supermarket =========="); -System.out.println ("1. List of goods"); $System.out.println ("2. Add New goods"); $System.out.println ("3. Delete goods"); -System.out.println ("4. Modify the goods"); -System.out.println ("Exit system"); theScanner sc =NewScanner (system.in); -System.out.println ("Please enter the number to be manipulated");Wuyi intnum =sc.nextint (); the returnnum; - } Wu - Public Static voidA (arraylist<chaoshi>arr) { AboutSYSTEM.OUT.PRINTLN ("======= Commodity inventory List =========="); $System.out.print ("Commodity Ticket"); -System.out.print ("Product name")); -SYSTEM.OUT.PRINTLN ("Commodity price"); - for(inti = 0;i<arr.size (); i++){ ASystem.out.println (Arr.get (i). id+ "\ T" +arr.get (i). name+ "\ T" +Arr.get (i). Price); + } the } - Public Static voidB (arraylist<chaoshi>arr) { $Scanner sc =NewScanner (system.in); theSystem.out.println ("Please enter the name of the new fruit"); theString A =Sc.next (); theSystem.out.println ("Please enter the number of the new fruit"); the intb =sc.nextint (); -System.out.println ("Please enter the price of the new fruit"); in Doublec =sc.nextdouble (); theChaoshi ARR4 =NewChaoshi (); theArr4.name =A; AboutArr4.id =b; theArr4.price =C; the Arr.add (ARR4); the } + Public Static voidC (arraylist<chaoshi>arr) { -System.out.println ("Please enter the number you want to delete"); theScanner sc =NewScanner (system.in);Bayi intA =sc.nextint (); the for(intI= 0;i<arr.size (); i++){ the if(Arr.get (i). id==a) { - Arr.remove (i); - } the } theSystem.out.println ("Delete Complete"); the } the Public Static voidD (arraylist<chaoshi>arr) { -System.out.println ("Please enter the fruit number to be modified.")); theScanner sc =NewScanner (system.in); the intA =sc.nextint (); theSystem.out.println ("Please enter a new fruit number");94 intb =sc.nextint (); theSystem.out.println ("Please enter a new fruit name"); theString C =Sc.next (); theSystem.out.println ("Please enter a new fruit price");98 DoubleD =sc.nextdouble (); About for(intI= 0;i<arr.size (); i++){ - if(Arr.get (i). id==a) {101Arr.get (i). id =b;102Arr.get (i). Name =C;103Arr.get (i). Price =D;104SYSTEM.OUT.PRINTLN ("Modified"); the } 106 }107 }108}
Java Collection Small practice supermarket inventory management system