/* Lesley Hogan * July 21st 2003 * IfLab.java * Practice using If-Else statements * Homework from: * http://teachertech.rice.edu/Participants/marilynt/Lessons/Exeter/Programming/IfLab.html */ package net.bunnie.dos; import java.text.DecimalFormat; import net.bunnie.*; public class IfLab { public static void main (String[] args) { // eggs variables int eggs; int eggsDozen; int eggsExtra; double pricePerDozen; double eggsCostDouble; String eggsCost; String eggCost; // deli variables int hoursWorked; double hourlyRate; double overForty; String grossWages; // triangle int sideOne; int sideTwo; int sideThree; boolean isTriangle; // acorn int yearsWorked; int acornSalary; String acornBonus; String acornName; // general variables KeyboardReader r = new KeyboardReader(); DecimalFormat dec = new DecimalFormat("$0.00"); DecimalFormat decT = new DecimalFormat("$0,000"); boolean loop = false; boolean fullLoop = true; int switchInt = 0; // array of options String[] optionList = new String[6]; String optionString = ""; int options = optionList.length - 1; optionList[0] = "0: View this menu"; optionList[1] = "1: Buy eggs"; optionList[2] = "2: Calculate salary at Arturo's Deli"; optionList[3] = "3: Determine triangles"; optionList[4] = "4: Calculate salary at The Rocky J. Squirrel Acorn Company"; 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 \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 number of eggs: "); eggs = r.readInt(); eggsDozen = eggs / 12; eggsExtra = eggs % 12; if (eggsDozen < 4) { pricePerDozen = .50; } else if ((eggsDozen > 3) && (eggsDozen < 6)) { pricePerDozen = .45; } else if ((eggsDozen > 5) && (eggsDozen < 11)) { pricePerDozen = .40; } else { pricePerDozen = .35; } eggsCostDouble = (eggsDozen * pricePerDozen) + (eggsExtra * (pricePerDozen / 12)); eggsCost = dec.format(eggsCostDouble); eggCost = dec.format((eggsCostDouble / eggs)); System.out.print("Your cost is " + dec.format(pricePerDozen) + " per dozen or " + eggCost + " per egg.\nYour bill comes to " + eggsCost + "\n\n"); switchInt = 0; break; case 2: System.out.print("Enter hours worked: "); hoursWorked = r.readInt(); System.out.print("Enter hourly rate: "); hourlyRate = r.readDouble(); if (hoursWorked > 40) { overForty = ((hourlyRate / 2) * (hoursWorked - 40)); } else { overForty = 0; } grossWages = dec.format((overForty + (hoursWorked * hourlyRate))); System.out.print("Gross Wages: " + grossWages + "\n\n"); switchInt = 0; break; case 3: System.out.print("Given a triange ABC,\nplease enter the lengths of the sides\nto determine what type of triangle it is.\nAB = "); sideOne = r.readInt(); System.out.print("BC = "); sideTwo = r.readInt(); System.out.print("AC = "); sideThree = r.readInt(); if ((sideOne >= sideTwo) && (sideOne >= sideThree)) { isTriangle = (sideTwo + sideThree > sideOne); } else if ((sideTwo >= sideOne) && (sideTwo >= sideThree)) { isTriangle = (sideOne + sideThree > sideTwo); } else { isTriangle = (sideOne + sideTwo > sideThree); } if (isTriangle == false) { System.out.print("\nThat is not a triangle!\n\n"); } else { if ((sideOne == sideTwo)) { if((sideOne == sideThree)) { System.out.print("\nThis is an equilateral triangle."); } else { System.out.print("\nThis is an isosceles triangle."); } } else if ((sideTwo == sideThree) || (sideOne == sideThree)) { System.out.print("\nThis is an isosceles triangle."); } else { System.out.print("\nThis is a scalene triangle."); } System.out.print("\n\n"); } switchInt = 0; break; case 4: System.out.print("Rocky J. Squirrel Acorn Company\nEmployee: "); acornName = r.readLine(); System.out.print("Years of service: "); yearsWorked = r.readInt(); System.out.print("Annual Salary: "); acornSalary = r.readInt(); if (yearsWorked >= 10) { acornBonus = decT.format((acornSalary * .12)); } else if ((yearsWorked < 10) && (yearsWorked >= 5)) { acornBonus = decT.format((acornSalary * .0575)); } else { acornBonus = "$0"; } System.out.print("\nBonus earned: " + acornBonus + "\n\n"); switchInt = 0; break; default: // exit program fullLoop = false; break; } } while (fullLoop == true); } }