By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,781 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,781 IT Pros & Developers. It's quick & easy.

Creating an array of classes

P: 9
Okay here's what i'm trying to do. I want to create an array and fill it with objects, so that when i create a method to alphabetize them, all of the pieces (the price, name, ect) don't get all out of order and correlate to each other.
Here is my code so far... any ideas?
Just need to create an array of classes. How do i get all these items i created into it? Please just give me a quick explanation of the syntax, maybe an example... don't refer me to an article. I have trouble on those, and need this explained.
Thanks guys!

(Just the section by Main)

import java.util.Scanner;

public class NewInventory

{
//------------------------------------------------------
public class Item //create basic item class

{

public String name;
public double price;
public double quantity;
public int itemNumber;
public double restockFee;
public double stockValue;
public String description;

//constructor

public Item(String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription)

{

name = cName;
price = cPrice;
quantity = cQuantity;
itemNumber = cItemNumber;
restockFee = cRestockFee;
stockValue = cStockValue;
description = cDescription;

} //end constructor

public void displayInfo(String name, double price, double quantity, int itemNumber, double restockFee, double stockValue, String description)

{

System.out.printf("Item Number: %d\nName:%s\nQuantity: %d\nPrice: $%.2f Restock Fee: $%.2f Item Description: %s\n Stock Value: $%.2f\n", itemNumber, name, quantity, price, restockFee, description, stockValue);

} //allows the user to display item information



//-------------------------------------------------
public class FireGem extends Item

{

public FireGem (String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription) //begin firegem constructor

{

super (cName, cPrice, cQuantity, cItemNumber, cRestockFee, cStockValue, cDescription);

} //end firegem constructor

public void setInfo(int itemNumber, String name, double price, double quantity, double restockFee, double stockValue, String description)

{

itemNumber = 1;
name = "Fire Gem";
price = 50.00;
quantity = 6;
restockFee = price * 0.05;
description = "Self-contained fire attack spell.";
stockValue = price * quantity;

} // sets the specs for fire gem

} //end firegem subclass
//-------------------------------------------------

public class WaterGem extends Item

{

public WaterGem (String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription) //begin constructor

{

super (cName, cPrice, cQuantity, cItemNumber, cRestockFee, cStockValue, cDescription);

} //end water gem constructor

public void setInfo(int itemNumber, String name, double price, double quantity, double restockFee, double stockValue, String description)

{

itemNumber = 2;
name = "Water Gem";
price = 50.00;
quantity = 8;
restockFee = price * 0.05;
description = "Self-contained water attack spell.";
stockValue = price * quantity;

} // sets the specs for water gem

} //end water gem subclass

//-----------------------------------------------------

public class ThunderGem extends Item

{

public ThunderGem (String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription) //begin constructor

{

super (cName, cPrice, cQuantity, cItemNumber, cRestockFee, cStockValue, cDescription);

} //end thunder gem constructor

public void setInfo(int itemNumber, String name, double price, double quantity, double restockFee, double stockValue, String description)

{

itemNumber = 3;
name = "Thunder Gem";
price = 50.00;
quantity = 4;
restockFee = price * 0.05;
description = "Self-contained lightning attack spell.";
stockValue = price * quantity;

} // sets the specs for thunder gem

} //end water gem subclass

//-----------------------------------------------------

public class EarthGem extends Item

{

public EarthGem (String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription) //begin firegem constructor

{

super (cName, cPrice, cQuantity, cItemNumber, cRestockFee, cStockValue, cDescription);

} //end thunder gem constructor

public void setInfo(int itemNumber, String name, double price, double quantity, double restockFee, double stockValue, String description)

{

itemNumber = 4;
name = "Earth Gem";
price = 50.00;
quantity = 7;
restockFee = price * 0.05;
description = "Self-contained earth attack spell.";
stockValue = price * quantity;

} // sets the specs for earth gem

} //end earth gem subclass

//-----------------------------------------------------

public class WindGem extends Item

{

public WindGem (String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription) //begin firegem constructor

{

super (cName, cPrice, cQuantity, cItemNumber, cRestockFee, cStockValue, cDescription);

} //end wind gem constructor

public void setInfo(int itemNumber, String name, double price, double quantity, double restockFee, double stockValue, String description)

{

itemNumber = 5;
name = "Wind Gem";
price = 50.00;
quantity = 11;
restockFee = price * 0.05;
description = "Self-contained wind attack spell.";
stockValue = price * quantity;

} // sets the specs for wind gem

} //end wind gem subclass

//-----------------------------------------------------


public class RocFeather extends Item

{

public RocFeather (String cName, double cPrice, double cQuantity, int cItemNumber, double cRestockFee, double cStockValue, String cDescription) //begin firegem constructor

{

super (cName, cPrice, cQuantity, cItemNumber, cRestockFee, cStockValue, cDescription);

} //end roc feather constructor

public void setInfo(int itemNumber, String name, double price, double quantity, double restockFee, double stockValue, String description)

{

itemNumber = 6;
name = "Roc Feather";
price = 10.00;
quantity = 50;
restockFee = price * 0.05;
description = "A regeant for the 'Feather Fall' spell.";
stockValue = price * quantity;

} // sets the specs for roc feather

} //end roc feather subclass

} // end item class

//--------------------------------------------------------------

public static void main( String args[] )

{

int choice = 0; //initialize variable

Scanner input = new Scanner( System.in);

public ArrayList<Item>stock;

stock.add(new Item(

do

{

System.out.println("The Magic Depot Inventory\n");
System.out.println("Please choose an option:\n");
System.out.println("[1] View an item information\n");
System.out.println("[2] View inventory list stock\n");
System.out.println("[3] Organize item list by alphabetical\n");
System.out.println("[4] View overall stock value\n");
System.out.println("[5] Exit\n");

choice = input.nextInt();

switch (choice) // check user input to open correct menu

{
//-------------------------
case 1: // user chooses option 1 to view an item

System.out.printf("You have chosen option %d\n", choice);

// insert viewItemInfo ref here

choice = 0; // causes menu to repeat

break;
//-------------------------
case 2: // user chooses option 2 to view the entire list of items

System.out.printf("You have chosen option %d\n", choice);

// insert listItems module ref here

choice = 0; // causes menu to repeat

break;

//-------------------------
case 3: //user wants to sort and list items by alpha

System.out.printf("You have chosen option %d\n", choice);

//insert alpha listing module ref here

choice = 0;

break;

//-------------------------
case 4: // view value of store stock

System.out.printf("You have chosen option %d\n", choice);

//insert store stock value module ref here

choice = 0;

break;

//-------------------------
case 5: // exit option

System.out.printf("You have chosen option %d\n", choice);

break; //allows exit of program

//-------------------------
default: // user enters anything not accepted

System.out.printf("You have chosen option %d\n", choice);
System.out.printf( "That is not a valid option. Please re-enter\n.");

choice = 0; // causes loop to repeat

break;

} // end switch

} while (choice == 0);

} // end main

//----------------------------------------------------------------------



} //end inventory class
Jun 30 '07 #1
Share this Question
Share on Google+
4 Replies


iWillLiveforever
100+
P: 136
I donít know if this will help you at all but why donít you make an array of lists so that the price, name, etc. are a list that stays together then sort those list according to your specifications. Then you can sort the various data field of the list without running the risk of mixing them up.
Jul 1 '07 #2

Expert 10K+
P: 11,448
don't refer me to an article. I have trouble on those, and need this explained.
Thanks guys!
What's giving you trouble in that article? The article explains a sorting method
implementation that just needs a 'Sortable'. A Sortable can tell how many items
need to be sorted, it must be able to compare two elements given their indexes
and it must be able two swap two elements given their indexes; that's all there
is to it.

Your Sortable can compare anything you like and you can 'manage' any number
of arrays you like, as long as you can tell the number of items to be sorted
(which should be easy: the length of one of your arrays) and it must be able
to swap elements in the arrays. The sorting method does the difficult parts.

All the source code as well as examples are presented in that article.

kind regards,

Jos
Jul 1 '07 #3

P: 9
Ah, i just have trouble with articles. I'm one of those people who asks "why?" or "what does this represent exactly?" or "can i change this for this?" a lot, so i tend to be more interactive in my learning style.

By the way, what does everyone think of Kaplan university online? I was told they have excellent IT programs, and they have messenger usage with teachers, AND online tutoring available. Anyone know how employers see degrees from this place? I know some online colleges aren't actually recognized, even though they are accredited.
Jul 2 '07 #4

10K+
P: 13,264
Ah, i just have trouble with articles. I'm one of those people who asks "why?" or "what does this represent exactly?" or "can i change this for this?" a lot, so i tend to be more interactive in my learning style.

By the way, what does everyone think of Kaplan university online? I was told they have excellent IT programs, and they have messenger usage with teachers, AND online tutoring available. Anyone know how employers see degrees from this place? I know some online colleges aren't actually recognized, even though they are accredited.
You could start a discussion about it in the Miscellaneous discusions forum if anyone knows about it
Jul 2 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.