Inventory case – Set + method

The code is as follows:

Commodity category:

/** define and describe the class of the commodity * commodity: 4 attributes (commodity name string, size double, price double, inventory int) * / / / define the class, class name goods / / this type of variable is stored in the collection. Public class goods {@ h_403_16 @ string brand; double size; double price; int count;

}

Test demo class:

import java. util.*; /* * Case of realizing inventory management: * 1 Store commodity information: create a collection and store commodity type variables; Store variables of item type in the collection * 2 View inventory list: traverse the collection, get the goods type variables stored in the collection, and output the attributes of each goods type * calculate and sum: total inventory, total amount * 3 Modify the inventory of goods: traverse the collection, Retrieve the goods type variable stored in the collection. The * variable calls the count value of the goods class attribute to modify (enter) * / public class demo {@ h_403_16 @ public static void main (string [] args) {@ h_403_16 @ / / create an ArrayList collection to store goods type ArrayList < goods > array = new ArrayList < goods > (); / / call the method addgoods (array) to add goods information ; // Enter an endless loop while (true) {@ h_403_16 @ / / call the method of selecting a function and get the SN entered by the user int number = choosefunction(); / / judge the SN switch (number) {@ h_403_16 @ case 1: printstore (array); break; case 2: update (array); break; case 3: return; default: system.out.println ("no such function!"); break; } } } /* * Define methods and store the information of goods in the collection * the collection is the shared data of all methods, Parameter passing * / public static void addgoods (ArrayList < goods > array) {@ h_403_16 @ / / create commodity type variable goods G1 = new goods(); goods G2 = new goods(); G1. Brand = "MacBook"; G1. Size = 13.3; G1. Price = 9999; G1. Count = 3; G2. Brand = "ThinkPad"; G2. Size = 15.6; G2. Price = 6999; G2. Count = 1; array. Add (G1); array. Add (G2);} / ** Define the method, view the inventory list, Traverse the collection * / public static void printstore (ArrayList < goods > array) {@ h_403_16 @ / / output the header system.out.println ("----------- shopping mall inventory list -----------"); system.out.println ("brand model \ tsize \ tprice \ tinventory"); / / define variables to save the total inventory and total amount, int totalcount = 0; double totalmoney = 0; / / traverse the collection for (int i=0;i
array) {@ h_403_16 @ scanner SC = new scanner (system. In); / / traverse the collection and obtain each element in the collection for (int i = 0; I < array. Size(); I + +) {@ h_403_16 @ / / the collection method get obtains the elements of the collection, the type is goods goods g = array.get (I); system.out.println ("please enter" + g.brand+ "Inventory of:"; g.count=sc.nextInt(); } } // Define the method to realize the selection menu, The user selects the menu public static int choosefunction() {@ h_403_16 @ system.out.println ("------------------ inventory management ------------"); system.out.println ("1. View inventory list"); system.out.println ("2. Modify commodity inventory quantity"); system.out.println ("3. Exit"); system.out.println ("please enter the operation serial number to be performed:") ; Scanner sc=new Scanner(system.in); int num= sc.nextInt(); return num; } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>