Reputation: 21
i have an ArrayList called e.g. Fridge
List<Food> fridge = new ArrayList<Food>();
An simple explanation: Because i want to keep my fridge organized i want to put my food into seperate drawers. I already have the Classes Meat, Fruit and Bread.
The point i am struggling with is to create new ArrayList's which picture the different drawers.
My goal is to have the fridge List a parent list and meat,fruit,bread as children of this list.
fridge (Parent)
---meat (Child)
---fruit (Child)
---bread (Child)
Do you have any ideas how to realize this ? I'm glad for any help
EDIT: The Objects i want to add to thesublists contain informations such as String obj.name; double obj.weight; double obj.price; i want to add the objects to the sublists and be able to read the sublists out again.
Upvotes: 2
Views: 683
Reputation: 717
I don´t get 100%ly what you want but I guess you just want to store your Food data.
1.) Just make an List of Lists:
ArrayList<ArrayList<Food>> fridge;
where every inner List presents an drawer. Meat, Fruit, etc. will be an Subclass of Food.
2.) make a Map with an key as the "name" of the drawer
HashMap<String, ArrayList<Food>> fridge;
With that you can easily get the "drawers" content. However that doesn´t change much to the above.
3.)
Make an Fridge class which stores all your stuff. And since an Fridge can hold more than just Food make an interface which defines what properties an Thing has which can be stored.
interface Storable{
String getName(); int getId(); String getType(); void use();
}....//whatever everything which will be stored has.
class Fridge{
HashMap<String, ArrayList<Storable>> contents;
void add(Storable food){
this.contents.put(food.getType, food); //or instead of #getType use the class of the food.
}
and then your Food, Meat, etc:
class Food implements Storable{
//implement the inherited Methods
}
Then you have again some choices, like make for each type an class (Meat, Bread, etc) or just make in Food an field "type" which describes what it is. It highly depends on your "mental model".
Upvotes: 1
Reputation: 140631
You are getting your "mental" model wrong. A fridge is much more than just a List that contains other lists.
Just imagine the "real fridge" in your kitchen. There is much more to it than just some lists (sorted by content)!
Thus you should first try to figure for yourself what the purpose of a "fridge" within your mental model is. Which behavior (interface) should it offer to users of that class?!
Example: is it really relevant that a fridge knows its content by "type"? Or would it matter more how entries in that fridge are physically ordered?
So besides the excellent answer from Hovercraft you might want to step back even further in order to really define what the terms you are using should mean within your program!
Upvotes: 0
Reputation: 285460
"fridge" shouldn't be a simple ArrayList since it needs to have its own more complex state and behaviors. Instead it should be a full fledged class and should contain 3 (or more) ArrayLists: meatList, fruitList, and breadList, as well as methods to put items in each of the lists, as well as methods of retrieving items, and methods for printing or displaying items.
Likewise Food can have subclasses for Meat, Fruit and Bread (although per Good Housekeeping, one shouldn't put bread in a refrigerator -- it causes condensation and sogginess).
Upvotes: 5