Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Can anyone validate my solution for the below sated problem ?

Greenhorn

Joined: Dec 20, 2010
Posts: 5
PROBLEM TWO: SALES TAXES

Basic sales tax is applicable at a rate of 10% on all goods, except books,
food, and medical products that are exempt. Import duty is an additional
sales tax applicable on all imported goods at a rate of 5%, with no
exemptions.

When I purchase items I receive a receipt which lists the name of all the
items and their price (including tax), finishing with the total cost of the
items, and the total amounts of sales taxes paid. The rounding rules for
sales tax are that for a tax rate of n%, a shelf price of p contains
(np/100 rounded up to the nearest 0.05) amount of sales tax.

Write an application that prints out the receipt details for these shopping

INPUT:

Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99
1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25

OUTPUT

Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 74.68

-----------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------

----------------------------------------------------------------

----------------------------------------------------------------------

--------------------------------------------------------------

--------------------------------------------------------------------------------

-----------------------------------------------------------------------------------

S. Nitesh
Ranch Hand

Joined: Jan 15, 2010
Posts: 31

Please post your code in code tags. You can use edit button.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 34618

13
Since he's new, I'll add the code tags, and we can see how much better it looks.

What do you want us to validate? Does the code work? Does it print the correct totals? You will doubtless have checked all those things already.

• Why are you using new BigDecimal("0.0") when there is a ready-made object available?
• Why are you using a LinkedList rather than other List implementations?
• don't use "if (somethingBoolean) return true; else return false;" Use "return somethingBoolean;"
• Why are you using a Queue inside your Iterator object?
• Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 555
What are you asking? Does your program run and give the desired answer? If so, what are you hoping to get from The Ranch? If not, what are the errors, what help do you need?

Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Greenhorn

Joined: Dec 20, 2010
Posts: 5
Could you please validate my programming approach .. And how better i can do the same
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3096

2
Look, normally I wouldn't post a working example, but it looks like you put enough effort into this, and I think that a full example with an explanation might be helpful. If the moderators think otherwise, I'm sure they will edit my post.

Most of the logic of the problem is in this one class, Item. All you need now is a class Invoice which simply maintains a list (hint: ArrayList) of orders, each order containing the Item, the quantity of the item, and whether it's imported. Note the following:

- This setup eliminates a lot of your classes. You don't really need a decorator pattern to calculate the taxes, it is much more simple and readable to let the Item calculate its own tax.
- The Item class is immutable. Always strive to make your classes immutable, unless it's impractical to do so. Note how all members are final, as well as the class itself.
- Visibility is reduced where possible. Think long and hard before you use public or protected access modifiers. If you don't need them, don't use them.
- Category is an enum. Always prefer enums over a collection of constants.
- Tax is calculated on the fly. It isn't added as a member variable of the Order or Item, because it can dynamically be calculated from the members in Item.
- Normally I would also include equals and hashCode methods, but for brevity I left them out.

Please tell me whether you understand why this code is 'better'. If someone thinks this code is worse, please tell me, so I can learn about your ideas as well.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.

subject: Can anyone validate my solution for the below sated problem ?