Welcome to Dream.In.Code
Become a Java Expert!

Join 149,513 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,355 people online right now. Registration is fast and FREE... Join Now!




Inventory 2 Program

 
Reply to this topicStart new topic

Inventory 2 Program, Having problems getting array program to compile

Mardie513
15 Aug, 2007 - 02:45 PM
Post #1

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 9


My Contributions
Hello guys and gals. I am having a hard time with this assignment. It’s called “Inventory Program Part 2”
I must create a product class the hold and item #, Name, # of units, and price of each unit. It must then display all this information plus the results of the stock multiplied by the price. I must also use an array to store multiple items and display them one product at a time and the value of the entire inventory.
I haven’t gotten to the display portion of my assignment yet, because I’m still having problems getting the array to work.
I have been looking through “bigbluesky’s” post and I still can’t figure it out.
Could someone please help?

OH Yeah My latest error is "Incompatible Types"

CODE

import java.util.Scanner;

public class DVDArray

{
    public static void main( String args[] )//Begin Program
    {
    Scanner input = new Scanner ( System.in );
    int i;


    Product myProduct = new Product[ 5 ];//Initiate DVD Array

    for (i=0; i<5; ++i)
        myProduct = new Product();


    for (i=0; i<5; ++i) //Enter 5 DVD's
    {

        System.out.println( "Enter the DVD Title: ");
        myProduct.setDVDName(input.next()); //read DVD Title

        System.out.print( "Enter the DVD's Product #: ");
        myProduct.setItemNumber(input.nextDouble());//read Product Number
            while( myProduct.getItemNumber() <= 0.0 )//While loop requesting a positive number
                {
                    System.out.println("Try Again");
                    System.out.println("Please Enter Only Positive Product #'s");
                    System.out.println( "Enter the DVD's Product #: ");
                    myProduct.setItemNumber(input.nextDouble());//read Product Number Again
                }

        System.out.print( "Enter the # of DVD's in Stock: ");
        myProduct.setDVDStock(input.nextDouble());//read Stock #
            while( myProduct.getDVDStock() <= 0.0 )//While loop requesting a positive number
                {
                    System.out.println("Try Again");
                    System.out.println("Please Enter Only Positive Product #'s");
                    System.out.println("Enter the # of DVD's in Stock: ");
                    myProduct.setDVDStock(input.nextDouble());//read Product Number Again
                }

        System.out.print( "Enter the DVD's Price: ");
        myProduct.setDVDPrice(input.nextDouble());//read Product Number
            while( myProduct.getDVDPrice() <= 0.0 )//While loop requesting a positive number
                {
                    System.out.println("Try Again");
                    System.out.println("Please Enter Only Positive Product #'s");
                    System.out.println( "Enter the DVD's Price: ");
                    myProduct.setDVDPrice(input.nextDouble());//read Product Number Again
                }

    }//End For


}

}

CODE

import java.util.Scanner; // Scanne class will be used in the program

class Product

{
     private double itemNumber; // class variable that stores the Item Number
     private double dvdStock; //class variable that stores the DVD's in stock
     private double dvdPrice; //class variable that stores the DVD's price
     private String dvdName; //class variable for the DVD Name

     //public Product () // Constructor for Poduct class
     {

         itemNumber = 0.0;
         dvdStock = 0.0;
         dvdPrice = 0.0;
         dvdName = "";
     }
     public void setItemNumber (double number) //Method to set Item Number
     {
         itemNumber = number;
     }
     public double getItemNumber () //Method to get Item Number
     {
         return itemNumber;
     }
     public void setDVDStock (double stock) //Method to set DVD's in Stock
     {
        dvdStock = stock;
     }
     public double getDVDStock () //Method to get DVD's in Stock
     {
        return dvdStock;
     }
     public void setDVDPrice (double price) //Method to set DVD's Price
     {
        dvdPrice = price;
     }
     public double getDVDPrice () //Method to get DVD's Price
     {
        return dvdPrice;
     }
     public void setDVDName (String name) //Method to set DVD's Name
     {
        dvdName = name;
     }
     public String getDVDName () //Method to get DVD's Name
     {
        return dvdName;
     }
     //public double calculateStockValue() //Method to calculate The value of the stock
     //}
     //    return dvdStock * dvdPrice;
     //}

}//end Product Class

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Inventory 2 Program
15 Aug, 2007 - 03:04 PM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE

CODE
    Product myProduct = new Product[ 5 ];//Initiate DVD Array

    for (i=0; i<5; ++i)
        myProduct = new Product();



Should be:
CODE
    Product[] myProduct = new Product[ 5 ];//Initiate DVD Array

    for (i=0; i<5; ++i)
        myProduct[i] = new Product();

Because you access an element of an array with the construct: arrayname[index]
And an array is declared by Type[] name

And so on, everytime you want to access an element of myproduct, you must do it with myproduct[elementindex] .
Actually, it would be better to call myproduct myproducts, so it shows that it represents (usually) multiple elements.

User is offlineProfile CardPM
+Quote Post

Mardie513
RE: Inventory 2 Program
16 Aug, 2007 - 07:23 AM
Post #3

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 9


My Contributions
QUOTE(1lacca @ 15 Aug, 2007 - 04:04 PM) *

QUOTE

CODE
    Product myProduct = new Product[ 5 ];//Initiate DVD Array

    for (i=0; i<5; ++i)
        myProduct = new Product();



Should be:
CODE
    Product[] myProduct = new Product[ 5 ];//Initiate DVD Array

    for (i=0; i<5; ++i)
        myProduct[i] = new Product();

Because you access an element of an array with the construct: arrayname[index]
And an array is declared by Type[] name

And so on, everytime you want to access an element of myproduct, you must do it with myproduct[elementindex] .
Actually, it would be better to call myproduct myproducts, so it shows that it represents (usually) multiple elements.


Thank You, my instructor told me that I could "Hard Code" the objects in the array. So now I think im on the right track.
User is offlineProfile CardPM
+Quote Post

Mardie513
RE: Inventory 2 Program
16 Aug, 2007 - 12:59 PM
Post #4

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 9


My Contributions
CODE

import java.util.Arrays;

public class Inventory
{
   public static void main(String args[])
   {
      int product [] = new Movie[10];

                product[0] = new Movie(0, "Shrek",26, 12.99);
                product[1] = new Movie(1, "Are We There Yet",15, 15.89);
                product[2] = new Movie(2, "Men in Black",8, 5.00);
                product[3] = new Movie(3, "Ring",20, 21.15);
                product[4] = new Movie(4, "Drum Line",17, 5.99);
                product[5] = new Movie(5, "Blade",7, 15.25);
                product[6] = new Movie(6, "Crash",35, 15.99);
                product[7] = new Movie(7, "Boyz in the Hood",7, 3.99);
                product[8] = new Movie(8, "Antz", 4, 25.99);
                product[9] = new Movie(9, "Shrek 2",10, 29.99);

                for(int i = 0; i < product.length; i++) {
                    System.out.println("Number:     " + product[i].getNumber());
                    System.out.println("Name:       " + product[i].getName());
                    System.out.println("Stock:   " + product[i].getStock());
                    System.out.println("Price:      " + product[i].getPrice());
                    System.out.println("Item Value: " + product[i].getStock() * product[i].getPrice());


              }
       }
}



Could someone help me understand why this file will not compile? I't one of three files that I am using for my inventory 2 Program.

There are 17 errors, the first one being:

Inventory.java:8: cannot find symbol
Symbol: class product
location: class Inventory
int product [] = new product[10];
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory 2 Program
16 Aug, 2007 - 01:09 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Got to remember, Java is case sensitive. wink2.gif

Product with a capital "P"






Another reference for Skyhawk and his short circuit quotes "Well, if you gotta go, don't squeeze the Charmin."
User is offlineProfile CardPM
+Quote Post

Mardie513
RE: Inventory 2 Program
16 Aug, 2007 - 01:50 PM
Post #6

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 9


My Contributions
Thank You,
I changed all of the products to "P", and it still will not compile. Does this file depend on another file to compile successfully before it will compile. I have three files and so far only my product files is compiling. I am not even sure I need them all, but I noticed to other members taking the same class that I am have three so I went with it.

Here are the three files that I am working with:
CODE

import java.util.Arrays;

public class Inventory
{
   public static void main(String args[])
   {
      int Product [] = new Movie[10];

                Product[0] = new Movie(0, "Shrek",26, 12.99);
                Product[1] = new Movie(1, "Are We There Yet",15, 15.89);
                Product[2] = new Movie(2, "Men in Black",8, 5.00);
                Product[3] = new Movie(3, "Ring",20, 21.15);
                Product[4] = new Movie(4, "Drum Line",17, 5.99);
                Product[5] = new Movie(5, "Blade",7, 15.25);
                Product[6] = new Movie(6, "Crash",35, 15.99);
                Product[7] = new Movie(7, "Boyz in the Hood",7, 3.99);
                Product[8] = new Movie(8, "Antz", 4, 25.99);
                Product[9] = new Movie(9, "Shrek 2",10, 29.99);

                for(int i = 0; i < Product.length; i++) {
                    System.out.println("Number:     " + Product[i].getNumber());
                    System.out.println("Name:       " + Product[i].getName());
                    System.out.println("Stock:   " + Product[i].getStock());
                    System.out.println("Price:      " + Product[i].getPrice());
                    System.out.println("Item Value: " + Product[i].getStock() * Product[i].getPrice());


              }
       }
}



CODE

public class Movie extends Product {

        public Movie(int number, String name, int stock, double price) {
                super(number, name, stock, price);
        }


    public double getValue() {
        double inventoryvalue = super.getValue();
        return (inventoryvalue * 1.05);
    }
}


CODE

public class Product {

    private String name = "";
    private int number = 0;
    private double price = 0;
    private int stock = 0;

    // Constructor
    public Product(int number, String name, int stock, double price) {
        this.number = number;
        this.name = name;
        this.stock = stock;
        this.price = price;
    }


    public String getName() { return name; }
    public int getNumber() { return number; }
    public double getPrice() { return price; }
    public int getStock() { return stock; }


    public double getValue() {
         return (double) stock * price;
    }
}



User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory 2 Program
16 Aug, 2007 - 03:19 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yeah I didn't mean that capitalizing your "P" in product would solve all your problems, but it was just one of a few errors. As for your question about the files yeah you will need three. One to define your product class, one to define your extended class (in your case Movie which extends from Product) and one for your main inventory class to manage all the products/movies.

Your product class is right, your movie class is right and will compile and the only change you need to do in your Inventory class is change how you define your product array. Remember it is an array of type "Product" not of type int. You should also provide an array name to go with that. I have chosen "Products" notice the "s" on the end.

CODE


import java.util.Arrays;

public class Inventory
{
   public static void main(String args[])
   {
                Product Products[] = new Movie[10];

                Products[0] = new Movie(0, "Shrek",26, 12.99);
                Products[1] = new Movie(1, "Are We There Yet",15, 15.89);
                Products[2] = new Movie(2, "Men in Black",8, 5.00);
                Products[3] = new Movie(3, "Ring",20, 21.15);
                Products[4] = new Movie(4, "Drum Line",17, 5.99);
                Products[5] = new Movie(5, "Blade",7, 15.25);
                Products[6] = new Movie(6, "Crash",35, 15.99);
                Products[7] = new Movie(7, "Boyz in the Hood",7, 3.99);
                Products[8] = new Movie(8, "Antz", 4, 25.99);
                Products[9] = new Movie(9, "Shrek 2",10, 29.99);

                for(int i = 0; i < Products.length; i++) {
                    System.out.println("Number:     " + Products[i].getNumber());
                    System.out.println("Name:       " + Products[i].getName());
                    System.out.println("Stock:   " + Products[i].getStock());
                    System.out.println("Price:      " + Products[i].getPrice());
                    System.out.println("Item Value: " + Products[i].getStock() * Products[i].getPrice());
              }
       }
}


This class above should compile just fine. You will need to make one more slight change. Where you have Item Value there, remember you have to call the product's overridden function to add that restocking fee. Right now it is just recalculating the stock * price. Remember your movie function's getValue() function? wink2.gif

Enjoy!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory 2 Program
16 Aug, 2007 - 03:34 PM
Post #8

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yeah I didn't mean that capitalizing your "P" in product would solve all your problems, but it was just one of a few errors. As for your question about the files yeah you will need three. One to define your product class, one to define your extended class (in your case Movie which extends from Product) and one for your main inventory class to manage all the products/movies.

Your product class is right, your movie class is right and will compile and the only change you need to do in your Inventory class is change how you define your product array. Remember it is an array of type "Product" not of type int. You should also provide an array name to go with that. I have chosen "Products" notice the "s" on the end.

CODE


import java.util.Arrays;

public class Inventory
{
   public static void main(String args[])
   {
                Product Products[] = new Movie[10];

                Products[0] = new Movie(0, "Shrek",26, 12.99);
                Products[1] = new Movie(1, "Are We There Yet",15, 15.89);
                Products[2] = new Movie(2, "Men in Black",8, 5.00);
                Products[3] = new Movie(3, "Ring",20, 21.15);
                Products[4] = new Movie(4, "Drum Line",17, 5.99);
                Products[5] = new Movie(5, "Blade",7, 15.25);
                Products[6] = new Movie(6, "Crash",35, 15.99);
                Products[7] = new Movie(7, "Boyz in the Hood",7, 3.99);
                Products[8] = new Movie(8, "Antz", 4, 25.99);
                Products[9] = new Movie(9, "Shrek 2",10, 29.99);

                for(int i = 0; i < Products.length; i++) {
                    System.out.println("Number:     " + Products[i].getNumber());
                    System.out.println("Name:       " + Products[i].getName());
                    System.out.println("Stock:   " + Products[i].getStock());
                    System.out.println("Price:      " + Products[i].getPrice());
                    System.out.println("Item Value: " + Products[i].getStock() * Products[i].getPrice());
              }
       }
}


This class above should compile just fine. You will need to make one more slight change. Where you have Item Value there, remember you have to call the product's overridden function to add that restocking fee. Right now it is just recalculating the stock * price. Remember your movie function's getValue() function? wink2.gif

Enjoy!

Yeah I didn't mean that capitalizing your "P" in product would solve all your problems, but it was just one of a few errors. As for your question about the files yeah you will need three. One to define your product class, one to define your extended class (in your case Movie which extends from Product) and one for your main inventory class to manage all the products/movies.

Your product class is right, your movie class is right and will compile and the only change you need to do in your Inventory class is change how you define your product array. Remember it is an array of type "Product" not of type int. You should also provide an array name to go with that. I have chosen "Products" notice the "s" on the end.

CODE


import java.util.Arrays;

public class Inventory
{
   public static void main(String args[])
   {
                Product Products[] = new Movie[10];

                Products[0] = new Movie(0, "Shrek",26, 12.99);
                Products[1] = new Movie(1, "Are We There Yet",15, 15.89);
                Products[2] = new Movie(2, "Men in Black",8, 5.00);
                Products[3] = new Movie(3, "Ring",20, 21.15);
                Products[4] = new Movie(4, "Drum Line",17, 5.99);
                Products[5] = new Movie(5, "Blade",7, 15.25);
                Products[6] = new Movie(6, "Crash",35, 15.99);
                Products[7] = new Movie(7, "Boyz in the Hood",7, 3.99);
                Products[8] = new Movie(8, "Antz", 4, 25.99);
                Products[9] = new Movie(9, "Shrek 2",10, 29.99);

                for(int i = 0; i < Products.length; i++) {
                    System.out.println("Number:     " + Products[i].getNumber());
                    System.out.println("Name:       " + Products[i].getName());
                    System.out.println("Stock:   " + Products[i].getStock());
                    System.out.println("Price:      " + Products[i].getPrice());
                    System.out.println("Item Value: " + Products[i].getStock() * Products[i].getPrice());
              }
       }
}


This class above should compile just fine. You will need to make one more slight change. Where you have Item Value there, remember you have to call the product's overridden function to add that restocking fee. Right now it is just recalculating the stock * price. Remember your movie function's getValue() function? wink2.gif

Enjoy!

Sorry for the double post. Ignore post #8.


User is offlineProfile CardPM
+Quote Post

Mardie513
RE: Inventory 2 Program
16 Aug, 2007 - 05:05 PM
Post #9

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 9


My Contributions
Thanks Martyr2,
You have been a great. Is there a book that you could reccomed, the breaks Java down in beginners terms? Other than "Java for Dummies", I think that book is confusing me even more.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory 2 Program
16 Aug, 2007 - 07:01 PM
Post #10

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well if you really want to learn Java thoroughly and have tons of examples, questions, in code highlighting etc I would recommend a book called Java - How to Program by Deitel. The book is a monster, but covers everything. Some of the topics they even overkill by explaining it three different ways. So if you got it the first time you may find some subjects dry. But hey, at least you learn it!

Java How to Program (Amazon.com)
User is offlineProfile CardPM
+Quote Post

Mardie513
RE: Inventory 2 Program
19 Aug, 2007 - 05:31 PM
Post #11

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 9


My Contributions
I haven't been able to figure out how to:
1. Calculate the value of the entire inventory
2. Create a method to sort the array items by the name of the product

The assignment deadline for this program has expired, but I need to figure this out before I can move on to my next assignment.

Please Help




CODE

public class Product {

    private String name = "";
    private int number = 0;
    private double price = 0;
    private int quantity = 0;

    // Constructor
    public Product(int number, String name, int quantity, double price) {
        this.number = number;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }


    public String getName() { return name; }
    public int getNumber() { return number; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }


    public double getValue() {
         return (double) quantity * price;
    }
}


CODE

public class Movie {

    public Movie() {}

    public static void main(String[] args) {
        new Inventory();
    }
}


CODE

public class Inventory {

    public static final int MAXIMUM_ITEMS = 10;
    Product product[] = new Product[MAXIMUM_ITEMS];

    public Inventory() {
        buildInventory();
        getInventory();
    }

    public void buildInventory() {


                product[0] = new Product(0, "Shrek",26, 12.99);
                product[1] = new Product(1, "Are We There Yet",15, 15.89);
                product[2] = new Product(2, "Men in Black",8, 5.00);
                product[3] = new Product(3, "Ring",20, 21.15);
                product[4] = new Product(4, "Drum Line",17, 5.99);
                product[5] = new Product(5, "Blade",7, 15.25);
                product[6] = new Product(6, "Crash",35, 15.99);
                product[7] = new Product(7, "Boyz in the Hood",7, 3.99);
                product[8] = new Product(8, "Antz", 4, 25.99);
                product[9] = new Product(9, "Shrek 2",10, 29.99);

    }

        public void getInventory() {
            for(int i = 0; i < product.length; i++) {
                System.out.println("Number:     " + product[i].getNumber());
                System.out.println("Name:       " + product[i].getName());
                System.out.println("Quantity:   " + product[i].getQuantity());
                System.out.println("Price:      " + product[i].getPrice());
                System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());

                System.out.println();


                //total += product.getQuantity() * product.getPrice();

                //System.out.println("Restock Fee:" + product.getQuantity() * product.getPrice() );
                System.out.println();
              }

                //System.out.println("Grand Total:" + total);
}
}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter