• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sales Program help neo-noob

 
Ben Calvin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know the culture of this community and how to ask for help here. So please bear with me. I've given code below and the discount is not working properly. I'm sure it has to do with the if/else statement in calculateTotal(), but I don't know how to do it outside of that. The problem is applying the discount or not, so I thought the if/else statement would fit the bill, guess not. As I have seen in other posts, I am not looking for the answer. Answer being typed code, showing the fix, but direction. Thanks again.



 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in calculateTotal()
if (rate >= 9)

probably should be
if (sales >= 9)
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ben Calvin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the extremely late reply. Thank you for the replies and helping me out. I'll check out the link in the last post. I am stuck AGAIN with creating a class. Seems I don't understand the books translation of what a constructor does and how to appropriately use it.
 
Ben Calvin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahaha it was an ad at the bottom. so awesome.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is called immediately after the class has been instantiated and is intended for setting all fields to their correct initial values.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get confusion with initialisers (I don’t like initialisers because they can cause confusion).
You can get confusion if you don’t write a constructor because the compiler adds a default constructor which might not necessarily do what you want.
You can get confusion because the superclass’ constructor has to be called first; if you haven’t specified otherwise the compiler looks for a superclass constructor with no arguments as the first line of every constructor.. If you haven’t specified a superclass, then it uses Object, which has got a no‑arguments constructor and no fields, so the confusion does not arise.

A mistake a lot of people make is to write too many constructors. If you want a particular combination of arguments to instantiate your class, give it a constructor with those arguments and no others.
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:...probably should be
if (sales >= 9)...

@Ben: And actually it should be
if (sales >= 9.0)

Or, more likely, sales should be an int (or possibly a long).

Can you have sales of 3.5? If not, then don't allow it.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic