/*
* CoffeePriceCalculator2.java
*
* Computer Science S-111
*
* This version is still unstructured, but it reduces some
* code duplication by factoring out the code for computing
* the tax and printing the prices. This code used to appear in
* the code for both choices, but it now appears only once,
* AFTER the if-else-if statement.
*
* See CoffeePriceCalculator1.java for the original unstructured version
* See CoffeePriceCalculator3.java for the fully structured version.
*/
import java.util.*;
public class CoffeePriceCalculator2 {
/* Class constants */
public static final double TAX_RATE = 0.0625; // sales tax
public static void main(String[] args) {
Scanner console = new Scanner(System.in); // only create this once!
System.out.println("Welcome to Javabucks!");
System.out.println();
System.out.println("What type of drink would you like?");
System.out.println("1) brewed coffee");
System.out.println("2) latte");
System.out.print("Enter your choice (1-2): ");
int choice = console.nextInt();
double price = 0.0; /**** WE MUST NOW DECLARE PRICE HERE. ****/
if (choice == 1) {
System.out.print("What size (tiny, medio, gigundo)? ");
String size = console.next();
if (size.equals("tiny")) {
price = 1.60;
} else if (size.equals("medio")) {
price = 1.80;
} else { // must be gigundo
price = 2.00;
}
} else if (choice == 2) {
System.out.print("What size (tiny, medio, gigundo)? ");
String size = console.next();
if (size.equals("tiny")) {
price = 2.80;
} else if (size.equals("medio")) {
price = 3.20;
} else { // must be gigundo
price = 3.60;
}
System.out.print("Flavored syrup (yes or no)? ");
String reply = console.next();
if (reply.equals("yes")) {
price += 0.50;
}
}
/***
* THE FOLLOWING CODE WAS FACTORED OUT OF THE TWO BLOCKS
* FROM THE if-else-if ABOVE.
***/
double tax;
System.out.print("Are you a student (yes or no)? ");
String reply = console.next();
if (reply.equals("no")) {
tax = price * TAX_RATE;
} else {
tax = 0.0;
}
System.out.println();
System.out.printf(" base price: $%.2f\n", price);
System.out.printf(" tax: $%.2f\n", tax);
System.out.printf("total price: $%.2f\n", price + tax);
}
}