wood burning stoves*
The moose likes Beginning Java and the fly likes Sales Program help neo-noob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sales Program help neo-noob" Watch "Sales Program help neo-noob" New topic
Author

Sales Program help neo-noob

Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
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

Joined: Jun 09, 2003
Posts: 4632
in calculateTotal()
if (rate >= 9)

probably should be
if (sales >= 9)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
And welcome to the Ranch
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
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

Joined: Feb 07, 2013
Posts: 12
hahaha it was an ad at the bottom. so awesome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
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

Joined: Oct 13, 2005
Posts: 38483
    
  23
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

Joined: Mar 17, 2011
Posts: 7702
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sales Program help neo-noob