RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab03b
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 ) {
        double basePrice;
        basePrice = numDonuts * 65;
        if (numDonuts >= 10) {
            double discountedPrice;
            discountedPrice = basePrice * (1 - 0.05);
            return doubleToInt( discountedPrice );
        }
        else {
            return basePrice;
        }
    }

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.)


1 Unfortunately, it's not even the case that == never works for Strings, because "howdy" == "how"+"dy" is true?! Oh Java, why do you make things so confusing to learn? Ask the prof if you want a full explanation.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Sep.13 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme