import java.io.BufferedReader; import java.io.InputStreamReader; import java.uti
ID: 3741543 • Letter: I
Question
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class YogurtBillCalculator {
/**
* @param args the command line arguments
*/
private static final float YOGURT_COST = 0.44F;
private static final float WAFFLE_CONE_COST = 0.61F;
private static final float TOPPING_COST = 0.53F;
private static final int NO_ORDER_FOR_DISCOUNT = 10;
private static final int DISCOUNT_RATE = 5;
public static void main(String[] args) throws NumberFormatException, Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int noOfYogurt = 0;
int noOfToppings = 0;
int cupChoice = 0;
float yogurtCost, coneCost, toppingCost;
char extraToppings;
System.out.println("Yogurt Bill Calculator");
System.out.println("");
System.out.println("Enter yogurt weight (whole ounces):");
noOfYogurt = Integer.parseInt(br.readLine());
System.out.println("Do you want toppings (Y or N)?");
extraToppings = br.readLine().charAt(0);
if (extraToppings == 'y' || extraToppings =='Y'){
System.out.println("How many toppings?");
noOfToppings = Integer.parseInt(br.readLine());
}
System.out.println("Enter 1 for a plastic cup or 2 for a waffle cone:");
cupChoice = Integer.parseInt(br.readLine());
String coneOrCup = (cupChoice == 1 ? " cup" : "cone");
coneCost = (cupChoice == 1 ? 0 : WAFFLE_CONE_COST);
System.out.println(); System.out.println(); // Display 2 blank lines after reading all inputs
System.out.println("YOGURT ORDER");
if(noOfYogurt < 10) {
System.out.printf("%-24s%d ","Weight (oz):", noOfYogurt);
}
if(noOfYogurt > 10) {
System.out.printf("%-23s%d ","Weight (oz):", noOfYogurt);
}
System.out.printf("%-21s%s ","Container:", coneOrCup);
System.out.printf("%-24s%d ","Number Toppings:", noOfToppings);
System.out.println(""); // Display a blank line
System.out.println("YOGURT BILL");
yogurtCost = noOfYogurt * YOGURT_COST;
toppingCost = noOfToppings * TOPPING_COST;
System.out.printf("%-20s %.2f ","Yogurt Cost:",yogurtCost);
if (coneCost > 0){
System.out.printf("%-20s %.2f ","Cone cost:",coneCost);
}
System.out.printf("%-18s+ %.2f ","Topping cost:",toppingCost); //plus sign
System.out.printf("%-17s %s ","","-------");
float totalCost = yogurtCost + toppingCost + coneCost;
if(totalCost < 10) {
System.out.printf("%-21s%.2f ","",totalCost);
}
if(totalCost > 10) {
System.out.printf("%-20s%.2f ","",totalCost);
}
// Check if Discount is possible
if (noOfYogurt > NO_ORDER_FOR_DISCOUNT){
float discount = (DISCOUNT_RATE/100.00F) * totalCost;
System.out.printf("%-18s- %.2f ","Discount:",discount);
System.out.printf("%-17s %s ","","-------");
if(totalCost - discount < 10 ) {
System.out.printf("%-21s%.2f ","",totalCost - discount);
}
if(totalCost - discount > 10) {
System.out.printf("%-20s%.2f ","",totalCost - discount);
}
}
}
}
I need to extend my current program to read like the example below: Thanks for the help!
example:
Frozen Treat Bill Calculator
Enter 1 for frozen yogurt or 2 for ice cream:
1
Enter frozen yogurt weight (whole ounces):
8
How many toppings?
0
Container options are
C - plastic cup
S - sugar cone
W - waffle cone
B - souvenir bowl
Enter choice:
c
ORDER
Treat type: frozen yogurt
Weight (oz): 8
Container: plastic cup
Number Toppings: 0
BILL
Treat cost: 3.52
Explanation / Answer
in the below program add choice for icecream and fixed the rate for the containercups (used switch case)
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class YogurtBillCalculator {
private static final float YOGURT_COST = 0.44F;
private static final float ICECREAM_COST = 0.45F;
private static final float WAFFLE_CONE_COST = 0.61F;
private static final float PLASTIC_CUP_COST = 0.50F;
private static final float SUGAR_CONE_COST = 0.63F;
private static final float SOUVENIR_BOWL_COST = 0.61F;
private static final float TOPPING_COST = 0.53F;
private static final int NO_ORDER_FOR_DISCOUNT = 10;
private static final int DISCOUNT_RATE = 5;
public static void main(String[] args) throws NumberFormatException, Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int noOfItems;
int item;
int noOfToppings = 0;
float ItemCost = 0;
float ContainerCost = 0;
char extraToppings;
String itemName = null;
String ContainerName = null;
char ContainerOption;
System.out.println("Frozen Treat Bill Calculator");
System.out.print(" Enter 1 for frozen Yogurt or 2 for ice cream :");
item = Integer.parseInt(br.readLine());
//assign the name to the item name
if (item == 1)
itemName = "Frozen Yogurt";
if (item == 2)
itemName = "Ice Cream";
//Prompt the user to enter the item weight
System.out.print(" Enter " + itemName + " weight (whole ounces):");
noOfItems = Integer.parseInt(br.readLine());
//cclculate the item cost based on the weight
if (item == 1) {
ItemCost = noOfItems * YOGURT_COST;
}
if (item == 2) {
ItemCost = noOfItems * ICECREAM_COST;
}
//prompt the user for toppings
System.out.println("Do you want toppings (Y or N)?");
extraToppings = br.readLine().charAt(0);
//if the user enters y then accepts the number of toppings
if (extraToppings == 'y' || extraToppings == 'Y') {
System.out.println("How many toppings?");
noOfToppings = Integer.parseInt(br.readLine());
}
//prompt the user to enter the choice of container
System.out.println("Container options are :");
System.out.print(" C - Plastic Cup S - Sugar Cone W - Waffle Cone B - Souvenir Bowl ");
ContainerOption = br.readLine().charAt(0);
//Calculate the container cost
switch (ContainerOption) {
case 'C':
case 'c':
ContainerName = "Plastic Cup";
ContainerCost = PLASTIC_CUP_COST;
break;
case 'S':
case 's':
ContainerName = "Sugar Cone";
ContainerCost = SUGAR_CONE_COST;
break;
case 'W':
case 'w':
ContainerName = "Waffle Cone";
ContainerCost = WAFFLE_CONE_COST;
break;
case 'B':
case 'b':
ContainerName = "Souvenir Bowl";
ContainerCost = SOUVENIR_BOWL_COST;
break;
default:
break;
}
//Display the order done
System.out.println(" ORDER");
System.out.println("Treat Type : " + itemName);
if (noOfItems < 10) {
System.out.println("Weight (oz):" + noOfItems);
}
if (noOfItems > 10) {
System.out.println("Weight (oz): " + noOfItems);
}
System.out.println("Container : " + ContainerName);
System.out.printf("%-24s%d ", "Number Toppings:", noOfToppings);
//display the bill if the there is discount calculate the discount and display
System.out.println(" BILL");
float toppingCost = noOfToppings * TOPPING_COST;
float totalCost = ItemCost + toppingCost + ContainerCost;
if (totalCost < 10) {
System.out.printf("%-20s%.2f ", "Treat Cost", totalCost);
}
if (totalCost > 10) {
System.out.printf("%-19s%.2f ", "Treat Cost", totalCost);
}
// Check if Discount is possible
if (noOfItems >= NO_ORDER_FOR_DISCOUNT) {
float discount = (DISCOUNT_RATE / 100.00F) * totalCost;
System.out.printf("%-18s- %.2f ", " Discount:", discount);
System.out.printf("%-17s %s ", "", "-------");
if (totalCost - discount < 10) {
System.out.printf("%-20s%.2f ", "", totalCost - discount);
}
if (totalCost - discount > 10) {
System.out.printf("%-19s%.2f ", "", totalCost - discount);
}
}
}
}
Sample Run:
Frozen Treat Bill Calculator
Enter 1 for frozen Yogurt or 2 for ice cream :1
Enter Frozen Yogurt weight (whole ounces):8
Do you want toppings (Y or N)?
y
How many toppings?
2
Container options are :
C - Plastic Cup
S - Sugar Cone
W - Waffle Cone
B - Souvenir Bowl
c
ORDER
Treat Type : Frozen Yogurt
Weight (oz):8
Container : Plastic Cup
Number Toppings: 2
BILL
Treat Cost 5.08
BUILD SUCCESSFUL (total time: 29 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.