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

nested if-else
donuts and fritters

If you didn't check off lab03a last time, the peer instructor will come around and check you off at the start of class.

The bakery is happy with your work from lab03a (donutOrder). They are adding fritters to their menu, and they want you to add to your existing solution, making a new function orderPrice which takes in two pieces of information: What type of pastry is being ordered (fritter or donut), and (of course) how many of that pastry.

They still offer a bulk discount, although fritters have a different threshold and discount amount than donuts do.

Notes:

It pays to buy in bulk, at Bulk-o-Mart's bakery!

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


  /** Return the cost for multiple bakery items, in cents.
   * @param itemName The product being ordered; must be either "donut" or "fritter".
   * @param count The number items being ordered.
   * @return the list price for `count` `itemName`s, including any discounts, *in cents*.
   */

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



    /** Test the above function. */
    static void testPrices() {
        System.out.println( "0 donuts:" );
        System.out.println( "Actual:  " + donutOrder( 0 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "1 donut:" );
        System.out.println( "Actual:  " + donutOrder( 1 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "2 donuts:" );
        System.out.println( "Actual:  " + donutOrder( 2 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "10 donuts:" );
        System.out.println( "Actual:  " + donutOrder( 10 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "100 donuts:" );
        System.out.println( "Actual:  " + donutOrder(100 ) );
        System.out.println( "Desired: " + 6175 );  // 6500¢ less 5% (325¢)

        System.out.println( "0 donuts:" );
        System.out.println( "Actual:  " + orderPrice( "donuts", 0 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "1 donut:" );
        System.out.println( "Actual:  " + orderPrice( "donuts", 1 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "2 donuts:" );
        System.out.println( "Actual:  " + orderPrice( "donuts", 2 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "10 donuts:" );
        System.out.println( "Actual:  " + orderPrice( "donuts", 10 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "100 donuts:" );
        System.out.println( "Actual:  " + orderPrice( "donuts", 100 ) );
        System.out.println( "Desired: " + 6175 );  // 6500¢ less 5% (325¢)

        System.out.println( "0 fritters:" );
        System.out.println( "Actual:  " + orderPrice( "fritter", 0 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "1 fritter:" );
        System.out.println( "Actual:  " + orderPrice( "fritter", 1 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "2 fritters:" );
        System.out.println( "Actual:  " + orderPrice( "fritter", 2 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "4 fritters:" );
        System.out.println( "Actual:  " + orderPrice( "fritter", 4 ) );
        System.out.println( "Desired: " + … );
        System.out.println( "100 fritters:" );
        System.out.println( "Actual:  " + orderPrice( "fritter", 100 ) );
        System.out.println( "Desired: " + … );
    }

}

Still include the function donutOrder you wrote last time — no need to remove good, useful code! It's more satisfying if you use the version you wrote last time, but if you don't have that available, you can paste in this version we talked about in lecture yesterday:

    /** Return the cost of a donut order, in cents.
     * @param numDonuts The number of donuts being ordered.
     * @return the list price for `numDonuts` donuts, including any discounts, *in cents*.
     */
    static int donutOrder( int numDonuts ) {
        int unitPrice;
        unitPrice = 65;

        double discount;
        if (numDonuts >= 10) {
            discount = 5.0 / 100.0;
        }
        else {
            discount = 0;
        }

        return toInt( (unitPrice*numDonuts)*(1-discount) );
    }

Submit this code above (after optionally completing some of the tasks below) to D2L, as lab03b.

If you finish…

Go ahead and add one more case:

Where will you fit a third case into the if-else statement? (There are several possible ways; we'll also talk about if-else-if statements in lecture soon.)

Further Challenge

Right now, your code might be structured so that there is an if-else for the pastry-type, and inside each of these is another if-else determining the discount.

Task: Can you factor out the inner if-else so it only occurs once (instead of once-for-each-type-of-pastry)?

Hint: Look at the repeated parts of code, and try to make a variable which holds the details/numbers that differ (e.g. 10 and 4 and 6).

Better yet: We already had the code for donutOrder; can you make your general-purpose orderPrice call donutOrder? Does that simplify the number of if-statements-inside-if-statements? Can we have three helpers, one for each type of pastry, with orderPrice being a “master switch” to call the right helper? Does the code feel cleaner or dirtier, having four separate functions instead of one?


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.