Creating an array of classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zerofury
    New Member
    • Jun 2007
    • 9

    Creating an array of classes

    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.Scann er;

    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(Str ing name, double price, double quantity, int itemNumber, double restockFee, double stockValue, String description)

    {

    System.out.prin tf("Item Number: %d\nName:%s\nQu antity: %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.prin tln("The Magic Depot Inventory\n");
    System.out.prin tln("Please choose an option:\n");
    System.out.prin tln("[1] View an item information\n") ;
    System.out.prin tln("[2] View inventory list stock\n");
    System.out.prin tln("[3] Organize item list by alphabetical\n" );
    System.out.prin tln("[4] View overall stock value\n");
    System.out.prin tln("[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.prin tf("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.prin tf("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.prin tf("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.prin tf("You have chosen option %d\n", choice);

    //insert store stock value module ref here

    choice = 0;

    break;

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

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

    break; //allows exit of program

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

    System.out.prin tf("You have chosen option %d\n", choice);
    System.out.prin tf( "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
  • iWillLiveforever
    New Member
    • Feb 2007
    • 136

    #2
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Zerofury
      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

      Comment

      • Zerofury
        New Member
        • Jun 2007
        • 9

        #4
        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.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Zerofury
          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

          Comment

          Working...