Ifs - Le Bon-Bon
- Cut and paste this code into java. Run it.
- Answer the questions on your sheet.
public class leBonBon
{
public static void main (String args [])
{
new leBonBon ();
}
public leBonBon ()
{
double total = 0;
System.out.println ("Welcome to Le Bon-Bon!\n\n");
System.out.println ("Here we sell chocolates and candy to satisfy any sweet tooth.");
//Place 1: Caramels
System.out.println ("\nLet's begin with your first choice: C A R A M E L S.");
int caram = IBIO.inputInt ("How many caramels would you like? ");
if (caram < 1)
{
System.out.println ("Won't you even try one?");
caram = IBIO.inputInt ("Really, how many caramels would you like? ");
}
total = caram * .75;
System.out.println ("Your total so far is $" + total);
//Place 2: Gumdrops
System.out.println ("\nYour second choice is : G U M D R O P S");
int gum = IBIO.inputInt ("How many gum-drops would you like? ");
if (gum < 1)
System.out.println ("You are missing out on some great gum-drops.");
else if (gum < 5)
System.out.println ("A nice starting amount, next time try more!");
else
System.out.println ("You are going to love them!");
total = total + gum * .25;
System.out.println ("Your total so far is $" + total);
//Place 3: Jelly Beans
System.out.println ("\nYour third choice is : J E L L Y B E A N S");
int jelly = IBIO.inputInt ("How many jelly beans would you like? ");
char yesORno = IBIO.inputChar ("You entered " + jelly + ", is that correct? (y/n) ");
if (yesORno == 'n')
jelly = IBIO.inputInt ("Please enter the correct number of jelly beans: ");
total = total + jelly * .05;
System.out.println ("Your total so far is $" + total);
//Place 4: Chocolate Covered Almonds
System.out.println ("\nWarning: Allergy Alert!");
yesORno = IBIO.inputChar ("Are you allergic to nuts? (y/n) ");
if (yesORno == 'n' || yesORno == 'N')
{
int almond = IBIO.inputInt ("How many chocolate covered almonds would you like? ");
total = total + almond * .25;
System.out.println ("Your total so far is $" + total);
}
//Place 5: Truffles
System.out.println ("\nYour fifth choice is : T R U F F L E S");
System.out.println ("We have:");
System.out.println ("(c) caramel truffles");
System.out.println ("(r) orange flavoured truffles");
System.out.println ("(d) dark chocolate truffles");
System.out.println ("(m) milk chocolate truffles");
char type = IBIO.inputChar ("What type of truffle would you like? (c/r/d/m) ");
int truff = IBIO.inputInt ("How many truffles would you like? ");
double costof1 = .5;
if (type == 'm' && truff < 5)
costof1 = .75;
else if (type == 'm')
costof1 = .55;
else if (type == 'd' && truff < 6)
costof1 = .85;
else if (type == 'd')
costof1 = .45;
else if (type == 'r')
costof1 = .65;
else
costof1 = .9;
total = total + costof1 * truff;
System.out.println ("Your total so far is $" + total);
//Place 6: Discount Game
System.out.println ("\nIf you unscramble this secret word, you will get a 10% discount!");
System.out.println ("The secret word is: tcoehloac");
String ans = IBIO.inputString ("What is your guess? ");
if (ans.equals ("chocolate"))
{
System.out.println ("You got it!");
total = total * .9;
System.out.println ("Your discounted total is $" + total);
}
else
System.out.println ("I'm sorry. You didn't guess it. Better luck next time.");
//Place 7: Payment options
System.out.println ("\nHow will you be paying for your purchases today?");
String pay = IBIO.inputString ("debit, credit or cash? ");
if (pay.equals ("DEBIT") || pay.equals ("d") || pay.equals ("debit") || pay.equals ("Debit"))
System.out.println ("Sadly, our debit machine is down today. It will have to be cash.");
else if (pay.equals ("CREDIT") || pay.equals ("c") || pay.equals ("credit") || pay.equals ("Credit"))
System.out.println ("Sadly, your credit card is maxed out. It will have to be cash. ");
//Place 8: Calculate change
double amt = IBIO.inputDouble ("\nWhat was the amount of cash paid? $");
if (amt < total)
System.out.println ("That is not enough. We keep the candy. Nice try!");
else if (amt == total)
System.out.println ("We love exact change. Thanks!");
else
System.out.println ("Your change is $" + (amt - total) + ". Thanks!");
}
}