473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.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
4 2202
iWillLiveforever
136 100+
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
JosAH
11,448 Expert 8TB
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
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
r035198x
13,262 8TB
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

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

Similar topics

4
by: Gonzalo Aguirre | last post by:
i have a classes diagram like this ------------ * ---------- | Vehicles | ------------------------- | Parking| ------------ vehicles ---------- ^ |...
3
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown below and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you...
4
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to...
4
by: mirek | last post by:
Hello, is there a way how to create data view (via datagrid) of a 2dim. array? I've data which I've got via SOAP call - it is a customer list, I want to simply display it in an datagrid. Can...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
2
by: nickburton | last post by:
Hi there, I am new to Java and the concept of requiring a class/object to do everything.. I am creating a menu based program and hence need to re-use segments of code. If I was writing in C, I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.