/*******************************************************************************
*
* ComputeTip program
*
* Purpose: This program asks the user to enter a meal price and a satisfaction
* rating (1=Good, 2=Great and 3=Extraordinary) and then calculates
* an appropriate tip amount for the meal based on the service.
*
* @author Thomas Rogers
*
* @version 1.0 (Februrary 15, 2018)
*
******************************************************************************/
import java.util.Scanner;
public class ComputeTip {
/**=========================================================================
* CLASS CONSTANTS
*
* Constants used throughout this program and their comments.
*========================================================================*/
public static final double MIN_TIP = 0.15;
public static final double GOOD_TIP = 0.20;
public static final double MAX_TIP = 0.25;
/**=========================================================================
* CLASS METHODS
*
* Program methods are listed below in alphabetical order or the order in
* which they are used in main below.
*========================================================================*/
/**
* Calculates the tip given meal price and tip percentage
* @param price The price of the meal
* @param prct The tip percentage desired
* @return The return tip amount.
*/
public static double calculateTip(double price, double prct) {
return price*prct;
}
/**
* Displays a tip, tip percentage and meal amounts in an informative and formatted manner.
* @param price The price of the meal
* @param prct The tip percentage
* @param tip The amount of the tip
*/
public static void displayTip(double price, double prct, double tip) {
System.out.println("\nAn $" + price + " meal at " + prct*100 + "% gratuity is: $" + tip);
}
/**
* Calculates and displays a tip given a meal price and tip percentage
* @param price The price of the meal
* @param prct The tip percentage desired
*/
public static void calcAndDisplayTip(double price, double prct) {
double tip = calculateTip(price, prct);
displayTip(price, prct, tip);
}
/**=========================================================================
* CLASS main
*
* @param args - The command-line arguments passed to the running program.
*========================================================================*/
public static void main(String[] args) {
Scanner io = new Scanner(System.in); // Declare instance of Scanner for use in getting user input
double mealPrice; // Meal price
int satNumber; // Satisfaction rating (1, 2 or 3 for OK, Great and Extraordinary, respectively)
System.out.print("\nPlease enter your total meal price including tax: "); // Prompt the user for meal price
mealPrice = io.nextDouble(); // Read a double from the user's input
// Prompt for service satisfaction (1, 2, or 3) and read user's input.
// Don't leave the infinite while loop until 1, 2 or 3 is entered.
while (true) {
System.out.print("\nHow was your service:\n" +
"\t1 = OK\n" +
"\t2 = Great\n" +
"\t3 = Extraordinary\n" +
"\nPlease enter 1, 2 or 3: ");
satNumber = io.nextInt();
if (satNumber>=1 && satNumber<=3) {
break;
}
}
// Depending on the satisfaction entered calculate compensatory tip
if (satNumber == 1) {
calcAndDisplayTip(mealPrice, MIN_TIP);
} else {
if (satNumber == 2) {
calcAndDisplayTip(mealPrice, GOOD_TIP);
} else {
calcAndDisplayTip(mealPrice, MAX_TIP);
}
}
}
}