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