This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes class design problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class design problem" Watch "class design problem" New topic
Author

class design problem

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Thank you for responding =)

fruit is an abstract class; apple and orange are concrete subclass of fruit.
apple has a special attribute of its own and orange has one as well.

there is an inventory class, it keep that keep track of the numbers of apple and oranges in stock.

I'm trying to get inventory class design right. I have create two arraylists to keep track of two different types of fruit because the constructor for apple and orange is different as they each have an unique attribute. I have been advised that I must not use undefined arraylist type (RAWTYPE). Is there a better way of getting this done? While fruit as an abstract class, I do not have any abstract methods in there, do I have a design issue? Or in this scenario, this is fine as a design approach?




Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

If the inventory class is merely responsible for keep track of numbers of items, why does it need anything other than integer counters?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Bear Bibeault wrote:If the inventory class is merely responsible for keep track of numbers of items, why does it need anything other than integer counters?


inventory are arraylisy, they hold objects; e.g. when an orange or apple object is created, it is added to inventory's arraylist
Jian Shen
Greenhorn

Joined: Dec 02, 2010
Posts: 3
I do not quite understand. Does the following code describe the situation:

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
a counter will do...


else if you like to have separate Inventory class like the previous poster suggested, you shall add that in different List and get its size.
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
John Jai wrote:a counter will do...


else if you like to have separate Inventory class like the previous poster suggested, you shall add that in different List and get its size.


Thanks! your code sample has been a great help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Beware of anything using instanceof; it can usually be done more elegantly by some other means. Have you considered overloading that putFruit method? Or, go through the Java Tutorials, where you find an example of how you use a Map as a counting device. You can set up a Map with Class<?> objects as its "K" and Integer objects as its "V", and you can count against the Class objects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Jian Shen, welcome to the Ranch
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Campbell Ritchie wrote:Beware of anything using instanceof; it can usually be done more elegantly by some other means. Have you considered overloading that putFruit method? Or, go through the Java Tutorials, where you find an example of how you use a Map as a counting device. You can set up a Map with Class<?> objects as its "K" and Integer objects as its "V", and you can count against the Class objects.


Thanks Campbell, in this case, I'm not sure how will I need to overload the putfruit method, as it seems to be rather generic, i want to "dump" all the generic methods that i will be using in the subclass onto the superclass.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Have you been through the tutorials link I gave you?
If you use that approach, there is no need for any overloading.
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Campbell Ritchie wrote:Have you been through the tutorials link I gave you?
If you use that approach, there is no need for any overloading.


Hi Campbell, i'm using arraylist to store the objects instantiated, not simply counting. I'm already making use of collections and its related API. I would appreciate if you would clarify on how the use of Map and overloading methods can apply here. I have gone through these tutorials before though I am not well-versed on them
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Did you find an example in the tutorial which counts words, or something like that? Look in the Map interface section. Have you understood how it works?

I think you can use a trick like that to avoid overloading, which will work happily with no changes if you create Banana and Lemon classes. That is the nice thing about object-orientated programming. You can create that sort of technique, and add Bananas with no change to the counting technique whatsoever.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Campbell,

I searched in the tutorial and I found the below sample program. Is this the one you pointed at? I think since String is used, we can use this to count distinct String objects. Can you explain bit more on how to count having a HashMap<Fruit,Integer> to count Apple, Orange objects. I was not able to figure out without using either a instanceof or overloading. Thank you..



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I have already made a suggestion about what you can use as a Key, so as to avoid using overloading, and to avoid using instanceof. Have a look. It's earlier in this thread.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Campbell,

Thank you lot for the information. I was able to get the count like below. Is there anything you think it should be tuned still?

Jian Shen
Greenhorn

Joined: Dec 02, 2010
Posts: 3
Campbell Ritchie wrote:Jian Shen, welcome to the Ranch


Thanks Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Well done.

Now for all the fruit you can count withYou are correct with the generics: Class<? extends Fruit> And because of the way object-orientation works, that code will work if you have a Cherry class. Maybe the method would be better called countFruit(), however.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class design problem
 
Similar Threads
Why this exception ?
Generic class declarations
Reference Casting
Using @DiscriminatorColumn for Inheritance
Why declare an object and instantiate it as instance of inherited type?