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!
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; //}
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.
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.
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);
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);
public String getName() { return name; } public int getNumber() { return number; } public double getPrice() { return price; } public int getStock() { return stock; }
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);
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?
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);
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?
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);
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?
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.
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!
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;
public String getName() { return name; } public int getNumber() { return number; } public double getPrice() { return price; } public int getQuantity() { return quantity; }
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);