By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

My Java Inventory Program will not compile and run

P: 2
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.ArrayList;
import java.util.Iterator;
import java.util.Collections;

public class Inventory
{

// list of products
private ArrayList<Product> products;

// Construct an empty list of products
public Inventory()
{
products = new ArrayList<Product>();
}

// add a new product's to inventory
public void addProduct(String 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.iterator();
}


public void printInventory()
{

Iterator pi = products.iterator();

// Use the Iterator pi to print the product entries one
// at a time.
// Continue until the Iterator pi has no more entries

System.out.println("Item # $ qty. inv.$ total");

while ( pi.hasNext() )
{

Product p = (Product)pi.next();

System.out.println(p);
}
}
public double totalValue()
{

double total = 0.0;

Iterator pi = products.iterator();

// 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.next();

total += p.getTotalPrice();
}

return total;
}

// sorts the products currently added to the inventory
// according to the Product's compareTo method.
public void sortByName()
{
Collections.sort( products );
}

public static void main(String args[])
{

//Print title
System.out.printf( "----------------------" );
System.out.printf( "Inventory" );
System.out.printf( "----------------------\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.printInventory();

// display the total value of the inventory
System.out.println("--------------------------------------");
System.out.println("Inventory Total Value: " + inv.totalValue());
System.out.println("--------------------------------------");
}

}

//End Inventory
Aug 22 '07 #1
Share this Question
Share on Google+
9 Replies


Nepomuk
Expert 2.5K+
P: 3,112
Well, there are a few points, which make this difficult:
  1. Please use the [code] area (the # symbol in the editor) for posting any code - it does make life easier.
  2. Please post all relevant code (for example, the Product class).
  3. Please post the error given by the compiler and possibly mark the lines, in which it find's the error.
    Example: The compiler gives an error with something like
    ... at Inventory.printInventory(Inventory.java:81) -> line 81 is relevant
  4. Please don't only write the actual question in the title, but repeat it somewhere in the posting.
That way, it will be much easier to help you.
Aug 22 '07 #2

Nepomuk
Expert 2.5K+
P: 3,112
So, here's the code you've given in a more readable format:
Expand|Select|Wrap|Line Numbers
  1. // 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.
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.Collections;
  6.  
  7. public class Inventory
  8. {
  9.  
  10.     // list of products
  11.     private ArrayList<Product> products;
  12.  
  13.     // Construct an empty list of products
  14.     public Inventory()
  15.     {
  16.         products = new ArrayList<Product>();
  17.     }
  18.  
  19.     // add a new product's to inventory
  20.     public void addProduct(String name, int number, double price, int quantity)
  21.     {
  22.         Product p = new Product(name, number, price, quantity);
  23.         products.add(p);
  24.     }
  25.  
  26.     // return the number of products in the inventory
  27.     public int size()
  28.     {
  29.         return products.size();
  30.     }
  31.  
  32.     public Iterator iterator()
  33.     {
  34.         return products.iterator();
  35.     }
  36.  
  37.  
  38.     public void printInventory()
  39.     {
  40.         Iterator pi = products.iterator();
  41.  
  42.         // Use the Iterator pi to print the product entries one
  43.         // at a time.
  44.         // Continue until the Iterator pi has no more entries
  45.  
  46.         System.out.println("Item # $ qty. inv.$ total");
  47.  
  48.         while ( pi.hasNext() )
  49.         {
  50.             Product p = (Product)pi.next();
  51.  
  52.             System.out.println(p);
  53.         }
  54.     }
  55.  
  56.     public double totalValue()
  57.     {
  58.         double total = 0.0;
  59.  
  60.         Iterator pi = products.iterator();
  61.  
  62.         // Use the Iterator pi to calculate the value of each product's inventory
  63.         // add up the value of all products.
  64.         // Continue until the Iterator pi has no more entries
  65.  
  66.         while ( pi.hasNext() )
  67.         {
  68.             Product p = (Product)pi.next();
  69.  
  70.             total += p.getTotalPrice();
  71.         }
  72.  
  73.         return total;
  74.     }
  75.  
  76.     // sorts the products currently added to the inventory
  77.     // according to the Product's compareTo method.
  78.     public void sortByName()
  79.     {
  80.         Collections.sort( products );
  81.     }
  82.  
  83.     public static void main(String args[])
  84.     {
  85.         //Print title
  86.         System.out.printf( "----------------------" );
  87.         System.out.printf( "Inventory" );
  88.         System.out.printf( "----------------------\n\n" );
  89.  
  90.         // create a new inventory object of items.
  91.         Inventory inv = new Inventory();
  92.  
  93.         // add each product to the inventory
  94.         inv.addProduct("speakers", 21, 28.00, 20);
  95.         inv.addProduct("plugs", 22, 2.00, 37);
  96.         inv.addProduct("glare guards", 23, 14.50, 15);
  97.         inv.addProduct("paper",24, 4.25, 73);
  98.  
  99.         //sort the products in the inventory by name
  100.         inv.sortByName();
  101.  
  102.         // display the inventory on the screen
  103.         inv.printInventory();
  104.  
  105.         // display the total value of the inventory
  106.         System.out.println("--------------------------------------");
  107.         System.out.println("Inventory Total Value: " + inv.totalValue());
  108.         System.out.println("--------------------------------------");
  109.     }
  110. } //End Inventory
  111.  
Aug 22 '07 #3

P: 2
When I put that very code in the JCreator and try to compile it, this is the error code that I keep getting:
--------------------Configuration: InventoryProgramPart2 - <Default> - <Default>--------------------
Error : Invalid path, \bin\javac.exe -classpath "C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\InventoryProgramP art2\classes" -d C:\Program" Files\Xinox "Software\JCreatorV4LE\MyProjects\InventoryProgram Part2\classes @src_inventoryprogrampart2.txt"

Process completed.

So, here's the code you've given in a more readable format:
Expand|Select|Wrap|Line Numbers
  1. // 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.
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.Collections;
  6.  
  7. public class Inventory
  8. {
  9.  
  10.     // list of products
  11.     private ArrayList<Product> products;
  12.  
  13.     // Construct an empty list of products
  14.     public Inventory()
  15.     {
  16.         products = new ArrayList<Product>();
  17.     }
  18.  
  19.     // add a new product's to inventory
  20.     public void addProduct(String name, int number, double price, int quantity)
  21.     {
  22.         Product p = new Product(name, number, price, quantity);
  23.         products.add(p);
  24.     }
  25.  
  26.     // return the number of products in the inventory
  27.     public int size()
  28.     {
  29.         return products.size();
  30.     }
  31.  
  32.     public Iterator iterator()
  33.     {
  34.         return products.iterator();
  35.     }
  36.  
  37.  
  38.     public void printInventory()
  39.     {
  40.         Iterator pi = products.iterator();
  41.  
  42.         // Use the Iterator pi to print the product entries one
  43.         // at a time.
  44.         // Continue until the Iterator pi has no more entries
  45.  
  46.         System.out.println("Item # $ qty. inv.$ total");
  47.  
  48.         while ( pi.hasNext() )
  49.         {
  50.             Product p = (Product)pi.next();
  51.  
  52.             System.out.println(p);
  53.         }
  54.     }
  55.  
  56.     public double totalValue()
  57.     {
  58.         double total = 0.0;
  59.  
  60.         Iterator pi = products.iterator();
  61.  
  62.         // Use the Iterator pi to calculate the value of each product's inventory
  63.         // add up the value of all products.
  64.         // Continue until the Iterator pi has no more entries
  65.  
  66.         while ( pi.hasNext() )
  67.         {
  68.             Product p = (Product)pi.next();
  69.  
  70.             total += p.getTotalPrice();
  71.         }
  72.  
  73.         return total;
  74.     }
  75.  
  76.     // sorts the products currently added to the inventory
  77.     // according to the Product's compareTo method.
  78.     public void sortByName()
  79.     {
  80.         Collections.sort( products );
  81.     }
  82.  
  83.     public static void main(String args[])
  84.     {
  85.         //Print title
  86.         System.out.printf( "----------------------" );
  87.         System.out.printf( "Inventory" );
  88.         System.out.printf( "----------------------\n\n" );
  89.  
  90.         // create a new inventory object of items.
  91.         Inventory inv = new Inventory();
  92.  
  93.         // add each product to the inventory
  94.         inv.addProduct("speakers", 21, 28.00, 20);
  95.         inv.addProduct("plugs", 22, 2.00, 37);
  96.         inv.addProduct("glare guards", 23, 14.50, 15);
  97.         inv.addProduct("paper",24, 4.25, 73);
  98.  
  99.         //sort the products in the inventory by name
  100.         inv.sortByName();
  101.  
  102.         // display the inventory on the screen
  103.         inv.printInventory();
  104.  
  105.         // display the total value of the inventory
  106.         System.out.println("--------------------------------------");
  107.         System.out.println("Inventory Total Value: " + inv.totalValue());
  108.         System.out.println("--------------------------------------");
  109.     }
  110. } //End Inventory
  111.  
Aug 22 '07 #4

P: 4
Maybe I did Not read far enough but what do you need to do ? I am currently taking a java class and had to do a inventory program too in fact there are 5 parts to it.
Aug 23 '07 #5

Nepomuk
Expert 2.5K+
P: 3,112
Maybe I did Not read far enough but what do you need to do ? I am currently taking a java class and had to do a inventory program too in fact there are 5 parts to it.
First of all: Post the Product class! Without that, this piece of code won't compile for anybody! Then you can be helped.
By the way, the code I posted is the same as yours (at least it should be), only formated differently and in a [code] environment (which is only something in this forum and has nothing to do with Java itself).
Aug 23 '07 #6

10K+
P: 13,264
Maybe I did Not read far enough but what do you need to do ? I am currently taking a java class and had to do a inventory program too in fact there are 5 parts to it.
I suggest you do some reading first before you start writing the program. The articles index page has links to some basics threads including Sun's own tutorial.
Aug 23 '07 #7

P: 3
I got this message after changing the arraylist<Products>()
to arraylist(Products);

C:\Users\End User\Documents\Inventory.java:10: invalid method declaration; return type required
private ArrayList(Product);
^
C:\Users\End User\Documents\Inventory.java:10: <identifier> expected
private ArrayList(Product);
^
C:\Users\End User\Documents\Inventory.java:10: ')' expected
private ArrayList(Product);
^
3 errors

Tool completed with exit code 1

Can you explain these errors?
Feb 19 '08 #8

Expert 100+
P: 849
Yep. You are trying to call a constructor of the ArrayList class with Products as a parameter, which is meaningless. You were close with what you had, ArrayList<Products>. The actual syntax is ArrayList<classname>, so it would be ArrayList<Product>.
Feb 19 '08 #9

10K+
P: 13,264
I got this message after changing the arraylist<Products>()
to arraylist(Products);

C:\Users\End User\Documents\Inventory.java:10: invalid method declaration; return type required
private ArrayList(Product);
^
C:\Users\End User\Documents\Inventory.java:10: <identifier> expected
private ArrayList(Product);
^
C:\Users\End User\Documents\Inventory.java:10: ')' expected
private ArrayList(Product);
^
3 errors

Tool completed with exit code 1

Can you explain these errors?
Read the Generics section in Sun's Java tutorial.
Feb 19 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.