Thursday, August 7, 2014

How Much to Tip

This program calculates the amount you should tip based on the bill amount and the user's rating of the waiter using different methods.

import java.util.*;
import java.text.*;

public class Arithmetic {

public static void main(String[] args) {
double price = getPrice();
int rating = rateWaiter();
double tipAmount = calculateTip(price, rating);
System.out.println("You should tip " + currencyFormatter(tipAmount)
+ " for a total of " + currencyFormatter(price + tipAmount));
}

public static int rateWaiter() {
Scanner keyboard = new Scanner(System.in);
int choice;
do {
System.out.print("Rate your waiter from 0-10: ");
choice = keyboard.nextInt();
} while (!(choice >= 0 && choice <= 10));
return choice;
}

public static String currencyFormatter(double amount) {
DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
return myFormatter.format(amount);
}

public static double getPrice() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter bill amount: ");
return keyboard.nextDouble();
}

public static double calculateTip(double price, int rating) {
return price * ((10 + rating) / 100.0);
}

}

No comments:

Post a Comment