RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

if-else
donuts prices

Delicious Sugarbomb Donuts cost 65¢ each; if you order 10 or more you get a 5% discount on all your donuts. Their motto: It pays to buy in bulk, at Bulk-o-Mart's bakery!

$ & ¢

First, one point: we will calculate the price of donuts not in dollars (e.g. two donuts cost 1.6 dollars), but in cents (e.g. two donuts cost 165 cents). (Think pennies if it helps.)

Note: Be sure not to confuse $0.65 with 0.65¢ — one of those is much more than a penny, one is less than a single penny! Just like 65 is more than 1, and 0.65 is less than one. (You knew that!)
Why do we do this? The fundamental reason is that we can never charge a customer a fraction of a cent, so we shouldn't choose a data-type that allows a fraction of a cent. Using ints, and measuring in cents instead of dollars, lets us use the right type.

(Another justification, if it helps: Suppose we were opening a bakery off in the exotic kingdom of Barlandia. The smallest coin in Barlandia is the thunk; it can't be subdivided. One donut costs 65 thunks. In this case, which is more appropriate: ints or doubles?)

By the way, this is all step 1 of the design recipe — decide which type best represents the data in our program!

Starting code

/** Some bakery price-calculating functions.
 * @author ???
 * @see http://www.radford.edu/~itec120/2019fall-ibarland/Labs/lab03a.html
 */
class Bakery extends Object120 {


    /** Return the cost of a donut order, in cents.
     * @param count The number of donuts being ordered.
     * @return the list price for `count` donuts, including any discounts, *in cents*.
     */

     // (Add your code here, *after* filling in the test cases below.)



    /** Test the above function. */
    static void testDonutOrder() {
        System.out.println( "== donutOrder ==" );

        System.out.println( "Actual:  " + donutOrder( 0 ) );
        System.out.println( "Desired: " +  );

        System.out.println( "Actual:  " + donutOrder( 1 ) );
        System.out.println( "Desired: " +  );

        System.out.println( "Actual:  " + donutOrder( 2 ) );
        System.out.println( "Desired: " +  );

        System.out.println( "Actual:  " + donutOrder( 9 ) );
        System.out.println( "Desired: " +  );

        System.out.println( "Actual:  " + donutOrder( 10 ) );
        System.out.println( "Desired: " +  );

        System.out.println( "Actual:  " + donutOrder(100 ) );
        System.out.println( "Desired: " + 6175 );  // 6500¢ less 5% (325¢)
    }
  
    static void testAll() {
        testDonutOrder();
    }

}
Notes:
logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.