473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

My Java Inventory Program will not compile and run

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
9 7589
Nepomuk
3,112 Expert 2GB
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
3,112 Expert 2GB
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
xxplod
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
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
3,112 Expert 2GB
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
r035198x
13,262 8TB
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
dlb53
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
Laharl
849 Expert 512MB
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
r035198x
13,262 8TB
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

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

Similar topics

13
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
3
by: ITQUEST | last post by:
The moderator posted this in another thread: (I am starting a new one not to confuse the other subject at hand) ".java classes are compiled to into .class files. If you have a .java file called...
3
sammyboy78
by: sammyboy78 | last post by:
Hello again, This time I'm creating a program that creates an array of CD objects and then displays the information. I've created a CD class, a CDInventory class and then a CDInventoryDisplay to...
1
by: twin2003 | last post by:
need help with inventory part 5 here is what I have to do Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
11
by: hamiltongreg | last post by:
I am new to Java and am having problems getting my program to compile correctly. My assignment is as follows; Choose a product that lends itself to an inventory (for example, products at your...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
2
by: kajukenbo55 | last post by:
Hi, I am not "getting it" with Java nearly as quickly as I should. I am writing an inventory program and I need to create a DVD inventory program that will display the following; I need to create a...
2
by: blitz1989 | last post by:
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.