Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to approach ?

 
Charles Angemeyer
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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.


 
Stevens Miller
Bartender
Posts: 1255
24
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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:

 
Stephan van Hulst
Bartender
Pie
Posts: 6106
72
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic