My Java Inventory Program will not compile and run

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xxplod
    New Member
    • Aug 2007
    • 2

    #1

    My Java Inventory Program will not compile and run

    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
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    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.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      So, here's the code you've given in a more readable format:
      [CODE=java]
      // 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
      [/CODE]

      Comment

      • xxplod
        New Member
        • Aug 2007
        • 2

        #4
        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.

        Originally posted by nepomuk
        So, here's the code you've given in a more readable format:
        [CODE=java]
        // 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
        [/CODE]

        Comment

        • twin2003
          New Member
          • Aug 2007
          • 4

          #5
          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.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by twin2003
            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).

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by twin2003
              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.

              Comment

              • dlb53
                New Member
                • Feb 2008
                • 3

                #8
                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?

                Comment

                • Laharl
                  Recognized Expert Contributor
                  • Sep 2007
                  • 849

                  #9
                  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>.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by dlb53
                    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.

                    Comment

                    Working...