473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My Java Inventory Program will not compile and run

2 New Member
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
Aug 22 '07 #1
9 7612
Nepomuk
3,112 Recognized Expert Specialist
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.print Inventory(Inven tory.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 Recognized Expert Specialist
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 New Member
When I put that very code in the JCreator and try to compile it, this is the error code that I keep getting:
--------------------Configuration: InventoryProgra mPart2 - <Default> - <Default>--------------------
Error : Invalid path, \bin\javac.exe -classpath "C:\Program Files\Xinox Software\JCreat orV4LE\MyProjec ts\InventoryPro gramPart2\class es" -d C:\Program" Files\Xinox "Software\JCrea torV4LE\MyProje cts\InventoryPr ogramPart2\clas ses @src_inventoryp rogrampart2.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
twin2003
4 New Member
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 Recognized Expert Specialist
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 MVP
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 New Member
I got this message after changing the arraylist<Produ cts>()
to arraylist(Produ cts);

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

Tool completed with exit code 1

Can you explain these errors?
Feb 19 '08 #8
Laharl
849 Recognized Expert Contributor
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<Produ cts>. The actual syntax is ArrayList<class name>, so it would be ArrayList<Produ ct>.
Feb 19 '08 #9
r035198x
13,262 MVP
I got this message after changing the arraylist<Produ cts>()
to arraylist(Produ cts);

C:\Users\End User\Documents\ Inventory.java: 10: invalid method declaration; return type required
private ArrayList(Produ ct);
^
C:\Users\End User\Documents\ Inventory.java: 10: <identifier> expected
private ArrayList(Produ ct);
^
C:\Users\End User\Documents\ Inventory.java: 10: ')' expected
private ArrayList(Produ ct);
^
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
3999
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 button "Unexpected Error":3251 operation is not supported for this type of object.The demo cd has two databases, one is called inventory and the other just has the tables for the design called inventory data. When you run inventory the database works...
0
7117
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 the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
3
2136
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 Product.java, when you compile it a .class is created for every class in the file Product.java if there are no errors. In this case you are getting this error because there is no class file called Product.class so you must compile the Product class...
3
3591
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 use the previos two. I've gotten all of this code oto compile but I'm still doing something wrong. I'm about to pull my hair out! When I run my CDInventoryDisplay program it comes back with this error: C:\Documents and Settings\Sam>java...
1
2827
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 item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display. the...
11
7704
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 workplace, office supplies, music CDs, DVD movies, or software). • Create 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. • Create a Java application that displays the...
2
2543
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 the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item. Add a Save button to the GUI that saves the...
2
1366
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 product class that holds the item number, name of DVD, how many I have in stock, as well as the price per DVD. Then I need to show the value of my inventory. I am so lost, can anyone help? I have this so far: package InventoryProgram; import...
2
4447
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 reading has very little in it that helps can someone take a look at this code and point me in the right direction please. Thanks for any help that is offered. //Modify the Inventory Program to use a GUI. The GUI should display the //informationone...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.