Win a copy of High Performance Python for Data Analytics this week in the Python forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

Can anyone validate my solution for the below sated problem ?

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
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

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

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

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

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

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

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

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

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

Ranch Hand
Posts: 36
1
• Number of slices to send:
Optional 'thank-you' note:
Please post your code in code tags. You can use edit button.

Marshal
Posts: 71769
312
• Number of slices to send:
Optional 'thank-you' note:
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?
•
Bartender
Posts: 563
• Number of slices to send:
Optional 'thank-you' note:
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?

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Could you please validate my programming approach .. And how better i can do the same

Saloon Keeper
Posts: 12631
274
• Number of slices to send:
Optional 'thank-you' note:
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.

 A wop bop a lu bob a womp bam boom. Tutti frutti ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton