• 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

Sales Program help neo-noob

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in calculateTotal()
if (rate >= 9)

probably should be
if (sales >= 9)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ben Calvin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hahaha it was an ad at the bottom. so awesome.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic