Inventory2 Arrays?

loop array for normal and in order!!!!!

Page 1 of 1

1 Replies - 1188 Views - Last Post: 04 October 2007 - 04:45 PM Rate Topic: -----

#1 tjbedgood2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-September 07

Inventory2 Arrays?

Posted 03 October 2007 - 08:22 PM

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.

I get alot of errors on the inventory2 class when I compile.

Inventory2 class
import java.util.Arrays;

public class Inventory2
{
		public static void main(String[] args) {
		
		System.out.println("\nCheckPoint - Inventory2");
		System.out.println("Thomas' DVD Collection\n");
		
		String dvd[];
		
		dvd = new DVD[4];
		
		DVD dvd = null;
	
		dvd[0]= new DVD(1, "Spiderman", 40, 14.95);
		System.out.println(dvd);

		dvd[1]= new DVD(2, "Superman", 10, 14.95);
		System.out.println(dvd);

		dvd[2]= new DVD(3, "X-Men", 20, 14.95);
		System.out.println(dvd);

		dvd[3]= new DVD(4, "Fantastic Four", 50, 14.95);
		System.out.println(dvd);
		
		for (int i=0; i<4; i++) {
			System.out.println("DVD " + i + " : " + DVD[i]);
		}
				
		Arrays.sort(DVD);
		
		for (int i=0; i<4; i++) {
			System.out.println("DVD " + i + " : " + DVD[i]);
		}

		System.out.println("\nProgram Done\n");
	}

} // end class Inventory2



C:\IT315\Inventory2\Inventory2.java:17: incompatible types
found : DVD[]
required: java.lang.String[]
dvd = new DVD[4];
^
C:\IT315\Inventory2\Inventory2.java:19: dvd is already defined in main(java.lang.String[])
DVD dvd = null;
^
C:\IT315\Inventory2\Inventory2.java:21: incompatible types
found : DVD
required: java.lang.String
dvd[0]= new DVD(1, "Spiderman", 40, 14.95);
^
C:\IT315\Inventory2\Inventory2.java:24: incompatible types
found : DVD
required: java.lang.String
dvd[1]= new DVD(2, "Superman", 10, 14.95);
^
C:\IT315\Inventory2\Inventory2.java:27: incompatible types
found : DVD
required: java.lang.String
dvd[2]= new DVD(3, "X-Men", 20, 14.95);
^
C:\IT315\Inventory2\Inventory2.java:30: incompatible types
found : DVD
required: java.lang.String
dvd[3]= new DVD(4, "Fantastic Four", 50, 14.95);
^
C:\IT315\Inventory2\Inventory2.java:34: cannot find symbol
symbol : variable DVD
location: class Inventory2
System.out.println("DVD " + i + " : " + DVD[i]);
^
C:\IT315\Inventory2\Inventory2.java:37: cannot find symbol
symbol : variable DVD
location: class Inventory2
Arrays.sort(DVD);
^
C:\IT315\Inventory2\Inventory2.java:40: cannot find symbol
symbol : variable DVD
location: class Inventory2
System.out.println("DVD " + i + " : " + DVD[i]);
^
9 errors
Exit code: 1
There were errors


My DVD class compiles good...
DVD class
public class DVD {

	private int ItemNo;		
	private String Title;
	private int Quantity;	
	private double Price;

	DVD(int itemNo, String title, int quantity, double price) {
		ItemNo   = itemNo;
		Title	= title;
		Quantity = quantity;
		Price	= price;
	} //end  constructor

	// set DVD name
	public void setTitle(String title) {
		Title = title;
	} //end method  setDvdTitle

	//return DVD Title
	public String getTitle() {
		return Title;
	} //end method getDvdTitle

	//set DVD quantity
	public void setQuantity(int quantity) {
		Quantity = quantity;
	} //end method setdvdQuantity

	//return dvdQuantity
	public double getQuantity() {
		return Quantity;
	} //end method get dvdQuantity

	public void setPrice(double price) {
		Price = price;
	} //end method setDvdPrice

	//return dvdPrice
	public double getPrice() {
		return Price;
	} //end method get Dvd Price

	public void setItemNo(int item) {
		ItemNo = item;
	} //end method setdvdItemNo

	//return DVD item
	public int getItemNo() {
		return ItemNo;
	} //end  method getdvdItemNo

	//calculate inventory value
	public double value() {
		return Price * Quantity;
	} //end method value

	public String toString() {
		return String.format("Item#=%03d  Title=%-15s  Quantity=%3d  Price=$%6.2f  Value=$%7.2f",
							  ItemNo, Title, Quantity, Price, value());
	}
  
} //end class DVD


This post has been edited by tjbedgood2: 03 October 2007 - 08:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Inventory2 Arrays?

#2 hyperionza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 15-September 07

Re: Inventory2 Arrays?

Posted 04 October 2007 - 04:45 PM

import java.util.Arrays;

public class Inventory2
{
		public static void main(String[] args) {
		
		System.out.println("\nCheckPoint - Inventory2");
		System.out.println("Thomas' DVD Collection\n");
		
		DVD dvds[];
		
		dvds = new DVD[4];
	
		dvds[0]= new DVD(1, "Spiderman", 40, 14.95);
		System.out.println(dvds[0].toString());

		dvds[1]= new DVD(2, "Superman", 10, 14.95);
		System.out.println(dvds[1].toString());

		dvds[2]= new DVD(3, "X-Men", 20, 14.95);
		System.out.println(dvds[2].toString());

		dvds[3]= new DVD(4, "Fantastic Four", 50, 14.95);
		System.out.println(dvds[3].toString());
		
		for (int i=0; i<4; i++) {
			System.out.println("DVD " + i + " : " + dvds[i].toString());
		}
				
		Arrays.sort(dvds); // this wont work btw, class DVD doesnt implement Comparable, see note after code block
		
		for (int i=0; i<4; i++) {
			System.out.println("DVD " + i + " : " + dvds[i].toString());
		}

		System.out.println("\nProgram Done\n");
	}

} // end class Inventory2



ok hope I got all of them, dont know what you were trying to do there but...

ok onto the note about the DVD class, i said it had to implement Comparable so you need to change the following line:
public class DVD {


to:
public class DVD implements Comparable {



then you need to add the following method to conform to the interface definition
int compareTo(Object otherObject)
{
DVD other = (DVD)otherObject;
if (Title < other.Title) return -1;
if (Title == other.Title) return 0;
return 1;
}



though youre going to have to come up with your own way to judge the strings thats just an eg.

good luck to you.

<why do i get the feeling ive just fixed someones homework? and that the DVD class isnt even his/hers?>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1