/**
* This calculates the tip.
*
* @Zman
* @1.0
*/
import java.util.*;
import java.lang.Integer.*;
import java.lang.Double.*;
public class TipCalculator
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in); //initializing variables
String billS = ""; //variables with an S at the end are the String versions (user inputted)
String tipPercentS = ""; //need a String to check if it's a number in method
String peopleS = "";
String choiceS = "";
int people = 0;
double bill = 0.0;
double tipPercent = 0.0;
double tip = 0.0;
double total = 0.0;
double splitBill = 0.0;
int choice = 1;
while (choice == 1) { //while loop to play again or not
System.out.print("Enter the value of the bill (don't enter the dollar sign): "); //prompts for bill
billS = s.nextLine();
while (!checkIfStringIsDouble(billS)) { //checks if bill price inputted is a double
System.out.print("That is not a valid answer. Enter the value of the bill (don't enter the dollar sign): "); //re-prompts for bill
billS = s.nextLine();
}
System.out.print("Enter the percentage you would like to tip (don't enter the percent sign): "); //prompts for tip percentage
tipPercentS = s.nextLine();
while (!checkIfStringIsDouble(tipPercentS)) { //checks if percent inputted is a double
System.out.print("That is not a valid answer. Enter the percentage you would like to tip (don't enter the percent sign): "); //re-prompts for tip
tipPercentS = s.nextLine();
}
bill = Double.parseDouble(billS); //converts string input to a double
tipPercent = Double.parseDouble(tipPercentS);
tip = (tipPercent/100) * bill; //calculates tip
tip = Math.round(tip * 100.0)/100.0; //rounds tip (if needed)
total = tip + bill;
System.out.println("The tip is $" + tip + "."); //outputs tip
System.out.println("The total price of the bill is $" + total + "."); //outputs total price
System.out.print("Enter the number of people you're splitting the bill with: ");
peopleS = s.nextLine();
while (!checkIfStringIsInt(peopleS)) { //checks if number of people inputted is an int
System.out.print("That is not a valid answer. Enter the number of people you're splitting the bill with: ");
peopleS = s.nextLine();
}
people = Integer.parseInt(peopleS);
splitBill = total/people;
splitBill = Math.round(splitBill * 100.0)/100.0; //rounds the bill to the nearest hundreth
System.out.println("Each person has to pay $" + splitBill + ".");
System.out.print("Would you like to calculate another tip? Enter 1 to do it again, enter 2 to stop: "); //keep going feature
choiceS = s.nextLine();
while (!choiceS.equals("1") && !choiceS.equals("2")) { //checks if the user input is a one or a two
System.out.print("That is not a valid answer. Would you like to calculate another tip? Enter 1 to do it again, enter 2 to stop: ");
choiceS = s.nextLine();
}
choice = Integer.parseInt(choiceS);
}
System.out.println("Program terminated.");
}
public static boolean checkIfStringIsDouble(String s) {
try {
Double.parseDouble(s);
} catch (Exception e) { //evaluates to false if not a Double
return false;
}
return true;
}
public static boolean checkIfStringIsInt(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) { //evaluates to false if not an Int
return false;
}
return true;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.