Need help on Arrays and objects

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?

(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

[8692 byte] By [Zerofurya] at [2007-11-27 9:19:52]
# 1

That's a lot of code. When you post a lot of code (or even a little code), please for the love of god, learn to use code tags. You probably pasted your code into the message box as you were creating this message. Before clicking the "Post" button, highlight your code and click the "code" button. This helps format your code so that it can be readable. Right now it's impossible to read.

One suggestion: consider changing this:

public static void main(String args[])

{

int choice = 0; // initialize variable

Scanner input = new Scanner(System.in);

public ArrayList<Item> stock;

to this:

public static void main(String args[])

{

int choice = 0; // initialize variable

Scanner input = new Scanner(System.in);

List<Item> stock = new ArrayList<Item>();

Also, you are using a lot of inner or nested classes here. That could potentially make your code less extensible and harder to debug. Any particular reason that you are doing this? Is there a good design reason behind this?

I'm reading a little of your code as I go, and trying to make suggestions... I now see that you seem to have classes nested within inner classes -- multiple levels of nesting. Yikes! For now, consider getting all of them out into their own files.

.... possibly more to come.....

Message was edited by:

petes1234

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 2

Ahhh.... you have a core misconception here. You are creating different classes where you should be creating different objects. You must learn the difference between a class and an object. For instance, Item is a basic class. I see nothing wrong with it. WindGem, a subclass of Item (and all your other subclasses of Item, I think) shouldn't even exist at all. You should create Item Objects that have the WindGem data within them.

Before you do any further coding, please stop and read your textbook about this distinction. This is critical to your future development as a coder.

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 3

For instance, don't do this:

public class Foo

{

private String name;

private int number;

public Foo(String name, int number)

{

this.name = name;

this.number = number;

}

public String getName()

{

return this.name;

}

public int getNumber()

{

return this.number;

}

public void setName(String name)

{

this.name = name;

}

public void setNumber(int number)

{

this.number = number;

}

@Override

public String toString()

{

return name + ", " + String.valueOf(number);

}

//***********************************************************

//************* this stuff below is wrong *****************

//***********************************************************

public class subFoo1 extends Foo

{

public subFoo1(String name, int number)

{

super(name, number);

}

public void setInfo(String name, int number)

{

setName("subFoo1");

setNumber(1);

}

}

public class subFoo2 extends Foo

{

public subFoo2(String name, int number)

{

super(name, number);

}

public void setInfo(String name, int number)

{

setName("subFoo2");

setNumber(2);

}

}

}

Instead, create a basic class as you are doing:

public class Foo

{

private String name;

private int number;

public Foo(String name, int number)

{

this.name = name;

this.number = number;

}

public String getName()

{

return this.name;

}

public int getNumber()

{

return this.number;

}

public void setName(String name)

{

this.name = name;

}

public void setNumber(int number)

{

this.number = number;

}

@Override

public String toString()

{

return name + ", " + String.valueOf(number);

}

}

And then create objects of Foo (or in your case, Item) with the data inserted correctly:

public class UseFoo

{

private List<Foo> myFoos;

public UseFoo()

{

myFoos = new ArrayList<Foo>();

myFoos.add(new Foo("subFoo1", 1)); // here I'm creating new Foo's

myFoos.add(new Foo("subFoo2", 2)); // but putting different info in them.

myFoos.add(new Foo("petes1234", 1234));

}

public void showFoos()

{

for (int i = 0; i < myFoos.size(); i++)

{

System.out.println(myFoos.get(i).toString());

}

}

public static void main(String[] args)

{

UseFoo myUseFoo = new UseFoo();

myUseFoo.showFoos();

}

}

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 4
Pete your code isn't compiling.I'm getting a bunch of ";" expected error messages...
Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 5

> Pete your code isn't compiling.

> I'm getting a bunch of ";" expected error messages...

You know of course that you have to put my code into two files, right? A file for Foo and another code file for UseFoo. But even if it doesn't compile for you, it's the general point that I want you to see. Do you understand what I am getting at?

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 6
it's mainly saying symbol not found and pointing at List and ArrayList.
Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 7

I'm getting this little by little, but i really need a little more explaining. i don't suppose you have MSN? add paladinwannab1@hotmail.com

I can explain better what i'm trying to achieve. my big problem is i'm on a time crunch, because my teacher hasn't been replying at all lately.

Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 8

> I'm getting this little by little, but i really need

> a little more explaining. i don't suppose you have

> MSN? add paladinwannab1@hotmail.com

>

> I can explain better what i'm trying to achieve. my

> big problem is i'm on a time crunch, because my

> teacher hasn't been replying at all lately.

It's better to do everything here. One thing I forgot in my code is to import List and ArrayList in the UseFoo class. this goes here:

import java.util.ArrayList;

import java.util.List;

public class UseFoo

{

private List<Foo> myFoos;

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 9

YES that's what i needed to know.

Now here's what i have to do next.. i have to create a menu that allows the user to do a couple things. View individual items, view the entire value of the stock (of the entire inventory), sort them by alphabetical by name, and i also have to integrate it into a GUI. can we take this one step at a time? I've been wracking my brain on this for a week and a half now, am NOT enjoying my vacation (had o take school with me, and teacher is useless), and lost about 10% of my total grade because i couldn't figure the previous two assignments out (due to getting no help).

I don't really have anyone to mentor or work with me...

Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 10
that should have been obvious, the import command... i'm still getting used to that.
Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 11

We are volunteers here and can help you as time allows. The best way to get help here is to try to do something yourself, find a specific error and then ask a specific question. The poor way is to ask general questions, and a terrible way is to post the assignment, do none of the work and ask someone to do it for you. If someone does that, they get flamed to death. Just to warn you in advance.

So again the key is you have to put out effort. Good luck.

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 12
So at this point, first i'm going to create a menu, and then use case statements and plug the show code into case for "View item" correct? and then modify it so that it only shows the selected item.Next i'm going to plug it into the view all option
Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 13
ah not trying to get someone else to do it for me, but if i have code i can take apart... heheit makes it easier to learn it.
Zerofurya at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 14

> So at this point, first i'm going to create a menu,

> and then use case statements and plug the show code

> into case for "View item" correct? and then modify it

> so that it only shows the selected item.

> Next i'm going to plug it into the view all option

The best bet here is to try to create a small app that does this and test it out. You'll see if it works or not.

petes1234a at 2007-7-12 22:12:32 > top of Java-index,Java Essentials,Java Programming...
# 15
I'm just also gonna say....THANK YOU PETE! you're a lifesaver!
Zerofurya at 2007-7-21 22:58:50 > top of Java-index,Java Essentials,Java Programming...
# 16
Question. what is this @Override, and what does it REALLY do?
Zerofurya at 2007-7-21 22:58:50 > top of Java-index,Java Essentials,Java Programming...
# 17

> Question. what is this @Override, and what does it

> REALLY do?

Short answer: you may want to ignore it at this point and not use it.

Longer answer: You will have to forgive me if I don't get this totally correct since I've only been doing Java for about 6 months, but as far as I know, @Override tells the compiler that the method that follows is going to override a method that is present in one of the class's ancestors. For instance, I am telling the compiler that the Foo class has a method, toString, that override's it's parent's toString (the parent is the base class, Object).

Override isn't necessary, but I put it in as a fail-safe, because if I mess up my code and type in a method that doesn't correctly override the parent's method, the compiler gives me either an error or a warning (can't remember which).

Again, at this point in your training, I would just skip using this. It isn't necessary.

Addendum: it's an error, not a warning.

Message was edited by:

petes1234

petes1234a at 2007-7-21 22:58:50 > top of Java-index,Java Essentials,Java Programming...
# 18
Question:Can i have you as my replacement programming teacher? :)
Zerofurya at 2007-7-21 22:58:50 > top of Java-index,Java Essentials,Java Programming...