/* Lesley Hogan * July 22nd 2003 * IfLabTwo.java * More If-Else practice * Homework from: * http://chortle.ccsu.edu/cs151/Notes/chap13/progExercises13.html */ package net.bunnie.dos; import java.text.DecimalFormat; import net.bunnie.*; public class IfLabTwo { public static void main (String[] args) { // deli variables int overnight; int shipping; double price; String total; String item; // temp variables double efficiency = 0; double airT; double steamT; // microwave variables int items; double heatingTime; // general variables KeyboardReader r = new KeyboardReader(); DecimalFormat dec = new DecimalFormat("$0.00"); DecimalFormat decT = new DecimalFormat("$0,000"); DecimalFormat decS = new DecimalFormat("0.00"); boolean loop = false; boolean fullLoop = true; int switchInt = 0; // array of options String[] optionList = new String[5]; String optionString = ""; int options = optionList.length - 1; optionList[0] = "0: View this menu"; optionList[1] = "1: Order from Sam and Ella's Delicatessen"; optionList[2] = "2: Calculate steam engine efficiency"; optionList[3] = "3: Calculate heating time"; optionList[options] = "" + options + ": Press " + options + " to escape"; int i = 0; do { optionString += optionList[i]; optionString += "\n"; i++; } while (i < optionList.length); // welcome message System.out.print("-----------------------------------\n"); System.out.print(" I F - E L S E L A B T W O \n"); System.out.print(" L . H O G A N \n"); System.out.print(" _______ \n"); System.out.print(" | | | | \n"); System.out.print(" | | | | \n"); System.out.print(" |____| | | \n"); System.out.print(" | | | | \n"); System.out.print(" | | | \n"); System.out.print(" | | ___|___ + \n\n"); System.out.print("-----------------------------------\n\n\n"); // print out list of options and let user choose // what they want to do in the program do { switch(switchInt) { case 0: // show menu System.out.print("What would you like to do today?\n"); System.out.print(optionString + "\n"); switchInt = r.readInt(); break; case 1: // buy eggs System.out.print("Enter the item: "); item = r.readLine(); System.out.print("Enter the price: "); price = r.readDouble(); System.out.print("Overnight (1 for yes, 2 for no): "); overnight = r.readInt(); if (price < 10) { shipping = 2; } else { shipping = 3; } if (overnight == 1) { shipping = shipping + 5; } total = dec.format(shipping + price); System.out.print("\nInvoice: \n" + item + ": " + dec.format(price) + "\nShipping: " + dec.format(shipping) + "\nTotal: " + total + "\n\n"); switchInt = 0; break; case 2: System.out.print("Enter the air temperature: "); airT = r.readDouble(); System.out.print("Enter the steam temperature: "); steamT = r.readDouble(); if (steamT < 373) { efficiency = 0; } else { efficiency = (1.0 - (airT / steamT)); } System.out.print("\nEfficiency: " + decS.format(efficiency) + "\n\n"); switchInt = 0; break; case 3: System.out.print("Enter the number of items: "); items = r.readInt(); System.out.print("Enter the heating time for one item: "); heatingTime = r.readDouble(); if (items <= 0) { System.out.print("\nWhy would you want to heat no items?\n\n"); } //Finished 8/12/04 -.-;; else if (items > 3) { Echo.e("\nThe microwave won't heat more than 3 items at a time.\n\n"); } else { if (items == 2) { heatingTime = heatingTime / 2 + heatingTime; } Echo.e("\nHeat for " + decS.format(heatingTime) + "\n\n"); } switchInt = 0; break; default: fullLoop = false; // exit program break; } } while (fullLoop == true); } }