Inventory 2 Program

~~** Need Help Sorting My Array **~~

Page 1 of 1

5 Replies - 2197 Views - Last Post: 21 May 2009 - 02:08 AM Rate Topic: -----

#1 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Inventory 2 Program

Post icon  Posted 21 May 2009 - 12:52 AM

Okay.. Here is my program .. *Finally it runs* Thanks to all the help i've gotten from you ALL!!!!!

Anyway, now all I need to do is sort the array... I realize I need to use Arrays.sort(); but what goes into the ()?? I made the product name comparable, but I am, once again, STUCK. What do I need to do to sort these Games out in ascending alphabetical order by the product name? (prodName). I have created the method to do it, just don't know what to put within it!



/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

 ***  References  ***
 Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
 */

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.

public class Inventory2 {

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

		myGame = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame);

		myGame = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame);

		myGame = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame);

		myGame = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame);

		myGame = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame);

		myGame = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame);

		myGame = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2



// Game class is used to contain data from Inventory 1,
// and store the information 

class Game implements Comparable {
	private int itemNumber;
	private String prodName;
	private int inStock;
	private float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;
	} // end Constructor
	
	///TIM - This is required...
	public String getProdName() {
		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice


	public int compareTo(Object obj) {
		return prodName.compareTo(((Game) obj).getProdName());
	}

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

} // end class Game


class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array
  
	Inventory() {
		games = new Game[20];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		++nGames;					// increment the counter
	}
	

	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}
	
		private void sort() {
		
			}

	public void display() {
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory


Is This A Good Question/Topic? 0
  • +

Replies To: Inventory 2 Program

#2 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Inventory 2 Program

Posted 21 May 2009 - 01:05 AM

Theres a whole bunch of different things to put within the (). Look at the Java API. Google Java API, click the first result, click Arrays on the left and look through all of the different sort() methods.
Was This Post Helpful? 0
  • +
  • -

#3 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Re: Inventory 2 Program

Posted 21 May 2009 - 01:21 AM

View Post333OnlyHalfEvil, on 21 May, 2009 - 12:05 AM, said:

Theres a whole bunch of different things to put within the (). Look at the Java API. Google Java API, click the first result, click Arrays on the left and look through all of the different sort() methods.



That was almost helpful! :blink: Except for everything I just read obviously made little sense to me, because it did not work. :(
Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Inventory 2 Program

Posted 21 May 2009 - 01:36 AM


import java.util.Arrays;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

***  References  ***
Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
*/

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.




// Game class is used to contain data from Inventory 1,
// and store the information

public class  Main{

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

	   Game myGame1 = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame1);

		Game myGame2 = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame2);

	   Game myGame3 = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame3);

	   Game myGame4 = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame4);

		Game myGame5 = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame5);

	   Game myGame6 = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame6);

		Game myGame7 = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame7);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2






class Game implements Comparable  {
	public int itemNumber;
	public String prodName;
	public int inStock;
	public float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;

	} // end Constructor

	///TIM - This is required...
	public String getProdName() {

		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

@Override
	


	

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

public int  compareTo(Object obj){
Game gs=(Game)obj;

return	 this.prodName.compareTo(gs.prodName);


}
}// end class Game
class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array

	Inventory() {
		games = new Game[7];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		nGames=nGames+1;
		// increment the counter
	}


	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

		

	public void display() {
Arrays.sort(games);
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory


this should work
please correct array length it should be 7 since you are adding only 7 object
and you have implement comparable interface and provide description to java as how the sorting should takes place

this should also work using single refrence

import java.util.Arrays;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

***  References  ***
Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
*/

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.




// Game class is used to contain data from Inventory 1,
// and store the information

public class  Main{

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

		myGame = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame);

		myGame = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame);

		myGame = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame);

		myGame = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame);

		 myGame = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame);

		myGame = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame);

		 myGame = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2






class Game implements Comparable  {
	public int itemNumber;
	public String prodName;
	public int inStock;
	public float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;

	} // end Constructor

	///TIM - This is required...
	public String getProdName() {

		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

@Override
	


	

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

public int  compareTo(Object obj){
Game gs=(Game)obj;

return	 this.prodName.compareTo(gs.prodName);


}
}// end class Game
class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array

	Inventory() {
		games = new Game[7];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		nGames=nGames+1;
		// increment the counter
	}


	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

		

	public void display() {
Arrays.sort(games);
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory


This post has been edited by prajayshetty: 21 May 2009 - 01:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Re: Inventory 2 Program

Posted 21 May 2009 - 01:49 AM

View Postprajayshetty, on 21 May, 2009 - 12:36 AM, said:


import java.util.Arrays;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

***  References  ***
Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
*/

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.




// Game class is used to contain data from Inventory 1,
// and store the information



Thanks for going above and beyond!  I was making a mountain out of a mole hill I guess, considering I only needed to change two lines of code around.  This stuff is very awesome to me, but very hard to teach myself.  

See you for Inventory part 3  :P 

public class  Main{

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

	   Game myGame1 = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame1);

		Game myGame2 = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame2);

	   Game myGame3 = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame3);

	   Game myGame4 = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame4);

		Game myGame5 = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame5);

	   Game myGame6 = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame6);

		Game myGame7 = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame7);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2






class Game implements Comparable  {
	public int itemNumber;
	public String prodName;
	public int inStock;
	public float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;

	} // end Constructor

	///TIM - This is required...
	public String getProdName() {

		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

@Override
	


	

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

public int  compareTo(Object obj){
Game gs=(Game)obj;

return	 this.prodName.compareTo(gs.prodName);


}
}// end class Game
class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array

	Inventory() {
		games = new Game[7];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		nGames=nGames+1;
		// increment the counter
	}


	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

		

	public void display() {
Arrays.sort(games);
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory


this should work
please correct array length it should be 7 since you are adding only 7 object
and you have implement comparable interface and provide description to java as how the sorting should takes place

this should also work using single refrence

import java.util.Arrays;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

***  References  ***
Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
*/

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.




// Game class is used to contain data from Inventory 1,
// and store the information

public class  Main{

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

		myGame = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame);

		myGame = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame);

		myGame = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame);

		myGame = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame);

		 myGame = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame);

		myGame = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame);

		 myGame = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2






class Game implements Comparable  {
	public int itemNumber;
	public String prodName;
	public int inStock;
	public float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;

	} // end Constructor

	///TIM - This is required...
	public String getProdName() {

		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

@Override
	


	

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

public int  compareTo(Object obj){
Game gs=(Game)obj;

return	 this.prodName.compareTo(gs.prodName);


}
}// end class Game
class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array

	Inventory() {
		games = new Game[7];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		nGames=nGames+1;
		// increment the counter
	}


	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

		

	public void display() {
Arrays.sort(games);
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory


Was This Post Helpful? 0
  • +
  • -

#6 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Re: Inventory 2 Program

Posted 21 May 2009 - 02:08 AM

View Postinmate188826, on 21 May, 2009 - 12:49 AM, said:

View Postprajayshetty, on 21 May, 2009 - 12:36 AM, said:


import java.util.Arrays;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

***  References  ***
Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
*/

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.




// Game class is used to contain data from Inventory 1,
// and store the information



Thanks for going above and beyond!  I was making a mountain out of a mole hill I guess, considering I only needed to change two lines of code around.  This stuff is very awesome to me, but very hard to teach myself.  

See you for Inventory part 3  :P 

public class  Main{

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

	   Game myGame1 = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame1);

		Game myGame2 = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame2);

	   Game myGame3 = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame3);

	   Game myGame4 = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame4);

		Game myGame5 = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame5);

	   Game myGame6 = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame6);

		Game myGame7 = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame7);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2






class Game implements Comparable  {
	public int itemNumber;
	public String prodName;
	public int inStock;
	public float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;

	} // end Constructor

	///TIM - This is required...
	public String getProdName() {

		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

@Override
	


	

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

public int  compareTo(Object obj){
Game gs=(Game)obj;

return	 this.prodName.compareTo(gs.prodName);


}
}// end class Game
class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array

	Inventory() {
		games = new Game[7];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		nGames=nGames+1;
		// increment the counter
	}


	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

		

	public void display() {
Arrays.sort(games);
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory


this should work
please correct array length it should be 7 since you are adding only 7 object
and you have implement comparable interface and provide description to java as how the sorting should takes place

this should also work using single refrence

import java.util.Arrays;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Timothy Torrey
   IT 215
   Week 5 Checkpoint 1
   Inventory1 due date, day 5
   5/15/2009

***  References  ***
Deitel, H.M., & Deitel, P.J,. (2005). Java: How to Program (6th ed.): Prentice Hall.
*/

// Currently the main class is used to obtain data through code.
// The Public Class Inventory1 works with Game Class to modify Inventory1 information
// and  output it accordingly.




// Game class is used to contain data from Inventory 1,
// and store the information

public class  Main{

	public static void main(String[] args) {

		Game myGame;
		Inventory inventory = new Inventory();

		// New Game Records

		myGame = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame);

		myGame = new Game(58, "Far Cry 2", 4, 7.34f);
		inventory.add(myGame);

		myGame = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame);

		myGame = new Game(30, "Lord of the Rings: Conquest", 1, 50.34f);
		inventory.add(myGame);

		 myGame = new Game(41, "Tiger Woods '08", 4, 49.99f);
		inventory.add(myGame);

		myGame = new Game(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f);
		inventory.add(myGame);

		 myGame = new Game(23, "SmackDown VS. Raw: 2009", 7, 59.44f);
		inventory.add(myGame);

		inventory.display();

		System.out.println("End of Inventory2\n");
	} // End Method Main

} // End Public Class Inventory2






class Game implements Comparable  {
	public int itemNumber;
	public String prodName;
	public int inStock;
	public float itemPrice;

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName   = prodName;
		this.inStock	= inStock;
		this.itemPrice  = itemPrice;

	} // end Constructor

	///TIM - This is required...
	public String getProdName() {

		return prodName;
	}

	// calculate totalPrice method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

@Override
	


	

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n" +
				   "Number in Stock = %3d	 Unit Price = $%.2f	 Total Price = $%.2f\n",
				   itemNumber, prodName, inStock, itemPrice, totalPrice());
	} // end method toString

public int  compareTo(Object obj){
Game gs=(Game)obj;

return	 this.prodName.compareTo(gs.prodName);


}
}// end class Game
class Inventory {
	private Game[] games;	// the array of games
	private int nGames;		// number of games in the array

	Inventory() {
		games = new Game[7];
		nGames = 0;
		 }

	public void add(Game game) {
		games[nGames] = game;		// insert the game in the array
		nGames=nGames+1;
		// increment the counter
	}


	public float total() {
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

		

	public void display() {
Arrays.sort(games);
		System.out.println("Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());
	}

} // end class Inventory







prajayshetty, you are duly noted in my citations :) Thanks a million. I can't believe it was that simple and I couldn't get it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1