Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off <strong>good heavens do not do financial calculations in double</strong>. Do financial calculations in <strong>decimal</strong>; that is what it is for. Use double to solve <em>physics</em> problems, not <em>financial</em> problems. </p> <p>The major design flaw in your program is that <em>policy is in the wrong place</em>. Who is in charge of computing the taxes? You've put <em>the product</em> in charge of computing the taxes, but when you buy an apple or a book or a washing machine, <em>the thing you are about to buy</em> is not responsible for telling you how much tax you're going to pay on it. <em>Government policy</em> is responsible for telling you that. Your design massively violates the basic OO design principle that <strong>objects should be responsible for their own concerns</strong>, and not anyone else's. The concern of a washing machine is washing your clothes, not charging the right import duty. If the tax laws change, you don't want to change <em>the washing machine object</em>, you want to change <em>the policy object</em>.</p> <p>So, how to approach these sorts of problems in the future?</p> <p>I would have started by highlighting every important noun in the problem description:</p> <blockquote> <p><strong>Basic sales tax</strong> is applicable at a <strong>rate</strong> of 10% on all <strong>goods</strong>, except <strong>books</strong>, <strong>food</strong>, and <strong>medical products</strong> that are exempt. <strong>Import duty</strong> is an additional <strong>sales tax</strong> applicable on all <strong>imported goods</strong> at a <strong>rate</strong> of 5%, with no <strong>exemptions</strong>. When I purchase <strong>items</strong> I receive a <strong>receipt</strong> which lists the <strong>name</strong> of all the <strong>items</strong> and their <strong>price</strong> (including <strong>tax</strong>), finishing with the <strong>total cost</strong> of the items, and the total amounts of <strong>sales taxes</strong> paid. The rounding rules for <strong>sales tax</strong> are that for a tax rate of n%, a <strong>shelf price</strong> of p contains (np/100 rounded up to the nearest 0.05) amount of <strong>sales tax</strong>.</p> </blockquote> <p>Now, what are the relationships between all those nouns?</p> <ul> <li>Basic Sales Tax is a kind of Sales Tax</li> <li>Import Duty is a kind of Sales Tax</li> <li>A Sales Tax has a Rate which is a Decimal</li> <li>Books are a kind of Item</li> <li>Food is a kind of Item</li> <li>Medical Products are a kind of Item</li> <li>Items may be Imported Goods</li> <li>An Item has a Name which is a String</li> <li>An Item has a Shelf Price which is a Decimal. (Note: does an item really have a price? two identical washing machines might be for sale for different prices at different stores, or at the same store at different times. A better design might be to say that a Pricing Policy relates an Item to its Price.)</li> <li>A Sales Tax Exemption Policy describes the conditions under which a Sales Tax is inapplicable on an Item.</li> <li>A Receipt has a list of Items, their prices and their taxes.</li> <li>A Receipt has a total</li> <li>A Receipt has a total tax</li> </ul> <p>... and so on. Once you have all the relationships between all the nouns worked out, then you can start designing a class hierarchy. There is an abstract base class Item. Book inherits from it. There is an abstract class SalesTax; BasicSalesTax inherits from it. And so on.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload