Inventory 2 Program

Having problems getting array program to compile

Page 1 of 1

10 Replies - 3043 Views - Last Post: 19 August 2007 - 06:31 PM Rate Topic: -----

#1 Mardie513  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 07

Inventory 2 Program

Posted 15 August 2007 - 03:45 PM

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"

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


}

}


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



Is This A Good Question/Topic? 0
  • +

Replies To: Inventory 2 Program

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Inventory 2 Program

Posted 15 August 2007 - 04:04 PM

Quote

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

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



Should be:
	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.
Was This Post Helpful? 0
  • +
  • -

#3 Mardie513  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 07

Re: Inventory 2 Program

Posted 16 August 2007 - 08:23 AM

View Post1lacca, on 15 Aug, 2007 - 04:04 PM, said:

Quote

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

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



Should be:
	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.
Was This Post Helpful? 0
  • +
  • -

#4 Mardie513  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 07

Re: Inventory 2 Program

Posted 16 August 2007 - 01:59 PM

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];
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4325
  • View blog
  • Posts: 12,106
  • Joined: 18-April 07

Re: Inventory 2 Program

Posted 16 August 2007 - 02:09 PM

Got to remember, Java is case sensitive. ;)

Product with a capital "P"






Another reference for Skyhawk and his short circuit quotes "Well, if you gotta go, don't squeeze the Charmin."
Was This Post Helpful? 0
  • +
  • -

#6 Mardie513  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 07

Re: Inventory 2 Program

Posted 16 August 2007 - 02:50 PM

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:
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());


			  }
	   }
}




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);
	}
}



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;
	}
}



Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4325
  • View blog
  • Posts: 12,106
  • Joined: 18-April 07

Re: Inventory 2 Program

Posted 16 August 2007 - 04:19 PM

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.


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? ;)

Enjoy!
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4325
  • View blog
  • Posts: 12,106
  • Joined: 18-April 07

Re: Inventory 2 Program

Posted 16 August 2007 - 04:34 PM

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.


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? ;)

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.


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? ;)

Enjoy!

Sorry for the double post. Ignore post #8.
Was This Post Helpful? 0
  • +
  • -

#9 Mardie513  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 07

Re: Inventory 2 Program

Posted 16 August 2007 - 06:05 PM

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.
Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4325
  • View blog
  • Posts: 12,106
  • Joined: 18-April 07

Re: Inventory 2 Program

Posted 16 August 2007 - 08:01 PM

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)
Was This Post Helpful? 0
  • +
  • -

#11 Mardie513  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 07

Re: Inventory 2 Program

Posted 19 August 2007 - 06:31 PM

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




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;
	}
}



public class Movie {

	public Movie() {}

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



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);
}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1