• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class design problem

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?




 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the inventory class is merely responsible for keep track of numbers of items, why does it need anything other than integer counters?
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not quite understand. Does the following code describe the situation:

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jian Shen, welcome to the Ranch
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Jian Shen, welcome to the Ranch



Thanks Campbell.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic