I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
• Create a method to calculate the value of the entire inventory.
• Create another method to sort the array items by the name of the product.
This is what I have so far:
//A product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
import java.util.Array List;
import java.util.Itera tor;
import java.util.Colle ctions;
public class Inventory
{
// list of products
private ArrayList<Produ ct> products;
// Construct an empty list of products
public Inventory()
{
products = new ArrayList<Produ ct>();
}
// add a new product's to inventory
public void addProduct(Stri ng name, int number, double price, int quantity)
{
Product p = new Product(name, number, price, quantity);
products.add(p) ;
}
// return the number of products in the inventory
public int size()
{
return products.size() ;
}
public Iterator iterator()
{
return products.iterat or();
}
public void printInventory( )
{
Iterator pi = products.iterat or();
// Use the Iterator pi to print the product entries one
// at a time.
// Continue until the Iterator pi has no more entries
System.out.prin tln("Item # $ qty. inv.$ total");
while ( pi.hasNext() )
{
Product p = (Product)pi.nex t();
System.out.prin tln(p);
}
}
public double totalValue()
{
double total = 0.0;
Iterator pi = products.iterat or();
// Use the Iterator pi to calculate the value of each product's inventory
// add up the value of all products.
// Continue until the Iterator pi has no more entries
while ( pi.hasNext() )
{
Product p = (Product)pi.nex t();
total += p.getTotalPrice ();
}
return total;
}
// sorts the products currently added to the inventory
// according to the Product's compareTo method.
public void sortByName()
{
Collections.sor t( products );
}
public static void main(String args[])
{
//Print title
System.out.prin tf( "----------------------" );
System.out.prin tf( "Inventory" );
System.out.prin tf( "----------------------\n\n" );
// create a new inventory object of items.
Inventory inv = new Inventory();
// add each product to the inventory
inv.addProduct( "speakers", 21, 28.00, 20);
inv.addProduct( "plugs", 22, 2.00, 37);
inv.addProduct( "glare guards", 23, 14.50, 15);
inv.addProduct( "paper",24, 4.25, 73);
//sort the products in the inventory by name
inv.sortByName( );
// display the inventory on the screen
inv.printInvent ory();
// display the total value of the inventory
System.out.prin tln("--------------------------------------");
System.out.prin tln("Inventory Total Value: " + inv.totalValue( ));
System.out.prin tln("--------------------------------------");
}
}
//End Inventory
• Create a method to calculate the value of the entire inventory.
• Create another method to sort the array items by the name of the product.
This is what I have so far:
//A product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
import java.util.Array List;
import java.util.Itera tor;
import java.util.Colle ctions;
public class Inventory
{
// list of products
private ArrayList<Produ ct> products;
// Construct an empty list of products
public Inventory()
{
products = new ArrayList<Produ ct>();
}
// add a new product's to inventory
public void addProduct(Stri ng name, int number, double price, int quantity)
{
Product p = new Product(name, number, price, quantity);
products.add(p) ;
}
// return the number of products in the inventory
public int size()
{
return products.size() ;
}
public Iterator iterator()
{
return products.iterat or();
}
public void printInventory( )
{
Iterator pi = products.iterat or();
// Use the Iterator pi to print the product entries one
// at a time.
// Continue until the Iterator pi has no more entries
System.out.prin tln("Item # $ qty. inv.$ total");
while ( pi.hasNext() )
{
Product p = (Product)pi.nex t();
System.out.prin tln(p);
}
}
public double totalValue()
{
double total = 0.0;
Iterator pi = products.iterat or();
// Use the Iterator pi to calculate the value of each product's inventory
// add up the value of all products.
// Continue until the Iterator pi has no more entries
while ( pi.hasNext() )
{
Product p = (Product)pi.nex t();
total += p.getTotalPrice ();
}
return total;
}
// sorts the products currently added to the inventory
// according to the Product's compareTo method.
public void sortByName()
{
Collections.sor t( products );
}
public static void main(String args[])
{
//Print title
System.out.prin tf( "----------------------" );
System.out.prin tf( "Inventory" );
System.out.prin tf( "----------------------\n\n" );
// create a new inventory object of items.
Inventory inv = new Inventory();
// add each product to the inventory
inv.addProduct( "speakers", 21, 28.00, 20);
inv.addProduct( "plugs", 22, 2.00, 37);
inv.addProduct( "glare guards", 23, 14.50, 15);
inv.addProduct( "paper",24, 4.25, 73);
//sort the products in the inventory by name
inv.sortByName( );
// display the inventory on the screen
inv.printInvent ory();
// display the total value of the inventory
System.out.prin tln("--------------------------------------");
System.out.prin tln("Inventory Total Value: " + inv.totalValue( ));
System.out.prin tln("--------------------------------------");
}
}
//End Inventory
Comment