![]() |
![]() |
|
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
They still offer a bulk discount, although fritters have a different threshold and discount amount than donuts do.
Notes:
Remember: To compare twoString s in java, do not use== . Instead, useObject120.equals(·,·) (e.g.equals(item, "donut") or whatever).
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
/** 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.
Go ahead and add one more case:
Right now, your code might be structured so that there is an
Task:
Can you factor out the inner
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
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |