In: Computer Science
/* What's wrong with this code? (nothing actually - but let's fix it to use a repetition structure) Copy and paste this .txt file into jGrasp, then save it, compile it, run it. Modify it! Bring your code to class at the start of Week 8, with at least 2 output runs with different data. What the program should do is take orders(input) for three (3) octoberfest guests, using a "for" loop. (+1 pt.) Each guest will choose 1 or 2 items (take in as strings) from the menu. Print (output) an itemized receipt as items are ordered, then a total bill and a "split the bill 3 ways" total. Bonus(+1 pt.) Modify the "take order part" - so a guest can order as many items as they choose, using a "while" loop. Bonus(+1 pt.) Use decimal formatter object to display output correctly as dollars and cents, i.e. $3.10 not $3.1 ) */ //import API's needed here: import java.util.Scanner; public class COdetoberFest01 { public static void main(String args[ ]) { Scanner kbd = new Scanner(System.in); float total =0; //total bill float splitTotal; //for 3 way split of bill float cost; //item cost int guest =0; //count off guests String item; int qty; //number of items a guest will order System.out.println("Will you be ordering 1 or 2 items?" ); qty =kbd.nextInt(); kbd.nextLine(); guest++; if(qty ==2) { System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel "); System.out.print("Place your order: "); item=kbd.nextLine(); if(item.charAt(0) == 'j')//jGrasp Pretzel cost = 1.50f; else if(item.charAt(0) == 's')//sub-routine Saurkraut cost = 2.75f; else if(item.charAt(0) == 'b')//boolean Bier cost = 3.90f; else cost =5.35f; // must be double schnitzel System.out.println(cost); total = total +cost; } System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel "); System.out.print("Place your order: "); item=kbd.nextLine(); if(item.charAt(0) == 'j')//jGrasp Pretzel cost = 1.50f; else if(item.charAt(0) == 's')//sub-routine Saurkraut cost = 2.75f; else if(item.charAt(0) == 'b')//boolean Bier cost = 3.90f; else cost =5.35f; // must be double schnitzel System.out.println(cost); total = total + cost; System.out.println("Will you be ordering 1 or 2 items?" ); qty =kbd.nextInt(); kbd.nextLine(); guest++; if(qty ==2) { System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel "); System.out.print("Place your order: "); item=kbd.nextLine(); if(item.charAt(0) == 'j')//jGrasp Pretzel cost = 1.50f; else if(item.charAt(0) == 's')//sub-routine Saurkraut cost = 2.75f; else if(item.charAt(0) == 'b')//boolean Bier cost = 3.90f; else cost =5.35f; // must be double schnitzel System.out.println(cost); total = total +cost; } System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel "); System.out.print("Place your order: "); item=kbd.nextLine(); if(item.charAt(0) == 'j')//jGrasp Pretzel cost = 1.50f; else if(item.charAt(0) == 's')//sub-routine Saurkraut cost = 2.75f; else if(item.charAt(0) == 'b')//boolean Bier cost = 3.90f; else cost =5.35f; // must be double schnitzel System.out.println(cost); total = total +cost; System.out.println("Will you be ordering 1 or 2 items?" ); qty =kbd.nextInt(); kbd.nextLine(); guest++; if(qty ==2) { System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel "); System.out.print("Place your order: "); item=kbd.nextLine(); if(item.charAt(0) == 'j')//jGrasp Pretzel cost = 1.50f; else if(item.charAt(0) == 's')//sub-routine Saurkraut cost = 2.75f; else if(item.charAt(0) == 'b')//boolean Bier cost = 3.90f; else cost =5.35f; // must be double schnitzel System.out.println(cost); total = total +cost; } System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel "); System.out.print("Place your order: "); item=kbd.nextLine(); if(item.charAt(0) == 'j')//jGrasp Pretzel cost = 1.50f; else if(item.charAt(0) == 's')//sub-routine Saurkraut cost = 2.75f; else if(item.charAt(0) == 'b')//boolean Bier cost = 3.90f; else cost =5.35f; // must be double schnitzel System.out.println(cost); total = total +cost; System.out.println("For " + guest + ", The total is: $" + total); System.out.println("If you split the bill " + guest + " ways, each guest must pay: $" + (total/guest)); } //closing main method } //closing class header
Below is the modified code : I have used DecimalFormat class to format the decimals. Also StringBuffer class to display the itemized bill. Also modified code to take as many items as customer want instead of 1 or 2 items.
Added for loop to iterate over the three customers.
Added while loop to iterate over the number of orders.
----------------------------------------------------
/*
*Program to take the order and provide the bill to customer.
*/
//import API's needed here:
import java.text.DecimalFormat;
import java.util.Scanner;
public class COdetoberFest01 {
public static void main(String args[]) {
Scanner kbd = new
Scanner(System.in);
float total = 0; // total
bill
float splitTotal; // for 3 way
split of bill
float cost; // item cost
int guest = 0; // count off
guests
String item;
int qty; // number of items a guest
will order
//decimal format object
DecimalFormat df = new
DecimalFormat("#.00");
//StrigBuffer to hold the items to
display in itemized bill
StringBuffer
itemized_bill = new StringBuffer();
for(int i =1; i<=3;
i++)
{
System.out.println("please enter the number of items you want to
order");
qty = kbd.nextInt();
kbd.nextLine();
guest++;
while (qty > 0)
{
System.out.println(
"CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n
$2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double
schnitzel ");
System.out.print("Place your
order: ");
item = kbd.nextLine();
if (item.charAt(0) == 'j')//
jGrasp Pretzel
{
cost =
1.50f;
itemized_bill.append("jGrasp Pretzel \n");
}
else if (item.charAt(0) ==
's')// sub-routine Saurkraut
{
cost =
2.75f;
itemized_bill.append("sub-routine Saurkraut \n");
}
else if (item.charAt(0) ==
'b')// boolean Bier
{
cost =
3.90f;
itemized_bill.append("boolean Bier \n");
}
else
{
cost =
5.35f; // must be double schnitzel
itemized_bill.append("double schnitzel \n");
}
System.out.println(df.format(cost));
total = total + cost;
qty--;
}
}
System.out.println("For " + guest +
", The total is: $" + df.format(total));
System.out.println("Itemized bill :
\n" + itemized_bill);
System.out.println("If you split
the bill " + guest + " ways, each guest must pay: $" +
df.format(total / guest));
} // closing main method
} // closing class header
------------------------------------------------------
Below is the output:
Ran the code from eclipse. Right clickon the program > Run As > Java Application:
Output of First run:
please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: jGrasp Pretzel
1.50
please enter the number of items you want to order
2
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: sub-routine Saurkraut
2.75
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: double schnitzel
5.35
please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: boolean Bier
3.90
For 3, The total is: $13.50
Itemized bill :
jGrasp Pretzel
sub-routine Saurkraut
double schnitzel
boolean Bier
If you split the bill 3 ways, each guest must pay: $4.50
-----------------------------------------------------------------------------------
2nd Run output:
please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: j
1.50
please enter the number of items you want to order
2
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: b
3.90
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: d
5.35
please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: b
3.90
For 3, The total is: $14.65
Itemized bill :
jGrasp Pretzel
boolean Bier
double schnitzel
boolean Bier
If you split the bill 3 ways, each guest must pay: $4.88
-----------------------------------------------------------------------------------