/*
* CoffeePriceCalculator3.java
*
* Computer Science S-111
*
* A structured solution to the coffee-price problem.
* It uses procedural decomposition to capture the structure of
* the program and to reduce code duplication. It also factors out
* code common to both types of drinks.
*
* See CoffeePriceCalculator1.java for the original unstructured version.
* See CoffeePriceCalculator2.java for an unstructured version that
* factors out some code that was repeated in the main if-else-if
* statement.
*/
import java.util.*;
public class CoffeePriceCalculator3 {
/* Class constants */
public static final double TAX_RATE = 0.0625; // sales tax
public static final int BREWED = 1; // These two make the main
public static final int LATTE = 2; // method more readable.
/*
* getChoice - uses the Scanner passed in as a parameter
* to determine the user's choice for type of drink.
* Returns the number of the choice.
*/
public static int getChoice(Scanner console) {
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();
return choice;
}
/*
* getSize - uses the Scanner passed in as a parameter
* to determine the size of drink that the user wants.
* Returns the String that the user enters.
*/
public static String getSize(Scanner console) {
System.out.print("What size (tiny, medio, gigundo)? ");
String size = console.next();
return size;
}
/*
* brewedPrice - uses the Scanner passed in as a parameter
* to determine the size and thereby the price of a brewed coffee.
* Returns the price.
*/
public static double brewedPrice(Scanner console) {
String size = getSize(console);
if (size.equals("tiny")) {
return 1.60;
} else if (size.equals("medio")) {
return 1.80;
} else { // must be gigundo
return 2.00;
}
}
/*
* lattePrice - uses the Scanner passed in as a parameter
* to determine the details (size & syrup) and thereby
* the price of a latte. Returns the price.
*/
public static double lattePrice(Scanner console) {
String size = getSize(console);
double price;
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;
}
return price;
}
/*
* computeTax - uses the Scanner and price passed in as parameters
* to determine and return the tax associated with the price
*/
public static double computeTax(Scanner console, double price) {
System.out.print("Are you a student (yes or no)? ");
String reply = console.next();
if (reply.equals("no")) {
return price * TAX_RATE;
} else {
return 0.0;
}
}
/*
* printPrices - prints the prices (base price, tax, and
* total price) based on the price and tax passed in as
* parameters.
*/
public static void printPrices(double price, double tax) {
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);
}
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();
int choice = getChoice(console);
double price = 0.0;
if (choice == BREWED) {
price = brewedPrice(console);
} else if (choice == LATTE) {
price = lattePrice(console);
}
double tax = computeTax(console, price);
printPrices(price, tax);
}
}