• 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

Best way to approach ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, this **is** a homework assignment but I'm not looking for the answer just an opinion on the way that I am approaching it.
I have to get the user to input their taxable income and filing status (0 to 3). No problem there.
Then depending on these two things apply the correct equation to determine their taxable income.
I originally thought 'switch' but can switch handle two variables? Therefore I turned my thoughts to if-then.
Am I going down the right track? If not, what is a smarter way to go about it. (not asking for code).
Many thanks.


 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll be interested to see if any of the folks with more OOP experience offer good OOP answers to your question. For now, what I'll offer is that you should consider carefully what would happen if your user had a taxable income of any of these:

 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your values seem to indicate tax brackets, so you should be able to take a more elegant approach. Note that you can often replace a bunch of magic values with a collection (preferably read from a config file).
I wrote this from the top of my head, be prepared for bugs or errors. Note a few things:

FilingStatus is an enum. Prefer enums over numerical or String constants.

BigDecimal is used for money and rates. For money you can use integer as well, as long as you don't use floating points.

There are no magic values. Everything is stored in a map that can be supplied to the constructor.

This solution may be a bit heavy handed for what you're trying to do, but it's an idea.
 
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

Charles Angemeyer wrote:I have to get the user to input their taxable income and filing status (0 to 3). No problem there.
Then depending on these two things apply the correct equation to determine their taxable income.
I originally thought 'switch' but can switch handle two variables? Therefore I turned my thoughts to if-then...


Whoa hoss, you're overthinking this.

You're also trying to work out how you're going to do this in Java rather than analysing what needs to be done.

My advice (and it's always the same in cases like this): StopCoding (←click).

Ask yourself a few questions:
1. What are these equations?
2. How are they affected by your two parameters (filing status and income bracket)? Is there, for example, any formula that can be applied to the two together?

The answers may give you a good idea of how to set up your program, but do your thinking on paper; and don't write a line of Java code until you can describe your solution completely in English (or your native language).

Personally, I'd consider creating enums for both your parameters, for example:which provides you with a foundation for adding any calculation based on income bracket.
Note that the above is just an example; there are many other ways of doing this.

It's also worth noting that you can use enums in switch statements; but if you design your program properly, you shouldn't have to.

HIH

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic