Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super class and Sub class inheritance not working

 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Well, looks like I've got another mess here. Part 3 of the Inventory program requirements are as follows:

- Modify the Inventory Program by creating a subclass of the product class that uses one additional unique attribute of the product you chose (for example, if Movie is your product, you can create a subclass named ForiegnMovie with “country” as a unique attribute).
- In the subclass, override the method to calculate the value of the inventory of a product with the same name and return type as the method previously created for the product class. The subclass override should add a 5% restocking fee to the value of the product.
- Modify the output to display this additional attribute you have chosen and the restocking fee.

This is what I have for code (please be merciful...):



OUTPUT:


I'm sure it's something simple I missed, but being a complete novice, I can't see it. Any help would be greatly appreciated...
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose this is the method that calculates mentioned price in your superclass:

You didn't override it in your subclass. Instead, you've actually implemented another method:

If your intention is to override superclass method in a subclass, overriden method needs to have the same signature as the one in superclass.

Furthermore, I notice that your setters are not implemented correctly. Instead of what you have implemented, each setter in your class should look like this:

Notice that it has an argument of the same type as field you are setting value of. You should also use Camel notation for naming your methods as well as fields (with exception of constants, which you are not using).

Also, your subclass constructor shouldn't look like this:

Once you invoke superclass constructor, there is no need to set the same fields again, because super has already done that, so what you should set are actually just fields specific to your subclass.
 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal,
Thanks for pointing those things out. The problem I'm having is getting the data in the sub-class back to the Inventory class. I'm at my boiling point right now so I have to step away for a while. Thanks again...
 
Tony Docherty
Bartender
Posts: 2952
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do what Kemal has already suggested it will work.

BTW what's the purpose of the itemClass instance variable which is set to "Training". You know the object is a training book because it's an instance of the TrainingBook class.
If you ever have an instance variable that is hard coded to a value and is never changed then you have a design problem.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the return type for those methods really Double? Don’t you mean double?

Actually, you ought never to use double arithmetic for money. Use what is described in this thread: BigDecimal.
 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: OK, the code below is almost there. The last thing I need to do is populate "Restock price" with the result of the "(InStock * PriceEach) * 1.05" formula in the TrainingBook class. I need to generate separate instances of Java 101, C#, and Python. Basically I need to have the value of each book with a 5% restocking fee on each "Restock Price" line and I'm done. Any ideas? Do I need to create a for loop for each instance? Maybe a new ArrayList for NewTotal? Not quite sure what to do at this point. Also, I realize the Restock Price is null because it's not set. Code follows:



OUTPUT


 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If what you wrote is the formula for calculating restock price in your application, you already have it in your gettotal() method. So just change toString() and add that method call at the end of it (instead of NewTotal).

Note that you didn't follow instructions you got here:
  • Campbell told you about the types you use.
  • Tony told you about the design.
  • I told you about setters (those you implemented still don't work as you expect them, since you are assigning value to itself in them) and use of Camel notation.
  •  
    Dan D'Apice
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kemal,
    I had a feeling it was something simple I wasn't seeing (again). Thanks again for the assist. Here is what the output looks like:

    Now if I can just trim that output to only 2 places after the decimal point to represent a dollars and cents format. Any ideas or suggestions would be appreciated...
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Formatting Numeric Print Output
     
    Dan D'Apice
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks again, everyone. I apologize for not incorporating the information you all gave me at first. I am going to take what great information has be given to me here and mull it over again and sort out this last hurdle of my program. I promise I wont ask any more questions about this application... :-D
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dan D'Apice wrote:I promise I wont ask any more questions about this application... :-D

    Nobody told you not to ask questions. Discussion on any topic is what this forum is about.

    Cheers!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic