wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Best way to approach ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best way to approach ?" Watch "Best way to approach ?" New topic
Author

Best way to approach ?

Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
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
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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

Joined: Sep 20, 2010
Posts: 3605
    
  14

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

Joined: Mar 17, 2011
Posts: 7553
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to approach ?
 
Similar Threads
Rounding BigDecimal Number
A mystery...
if statements and errors
urgent help needed for assignment
Clumsy code