Tip Calculator

/** * Calculates tip given the bill. * @author Josh Wang * @version 19 September 2017 */ import java.util.*; import java.text.DecimalFormat; public class tipCalc{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); DecimalFormat format = new DecimalFormat("0.00"); //format for dollar.cents boolean againPlease = true; while (againPlease){ boolean num; double bill; double tip; int count; String in = ""; String test = ""; System.out.println(); do{ System.out.println("Enter cost of the bill."); in = sc.nextLine(); num = in.matches("[$\\d\\.]+"); //checks if input is $, digits, and periods boolean exit = in.matches("exit"); //loop escape if (exit) System.exit(1); int index = in.indexOf("."); test = in.substring(index+1); index = test.indexOf("."); //tests for more than one period if (index >= 0) num = false; index = in.indexOf("$"); test = in.substring(index+1); index = test.indexOf("$"); //tests for more than one $ if (index >= 0) num = false; test = in.substring(1); index = test.indexOf("$"); //tests for noninitial $ if (index >= 0) num = false; if (num == false) System.out.println("\nInvalid input!"); }while (num == false); if (in.substring(0,1).matches("[$]") == true){ //tests for initial $ bill = (Double.parseDouble(in.substring(1))); } else{ bill = (Double.parseDouble(in)); } System.out.println(); do{ System.out.println("How much would you like to tip (percent)?"); in = sc.nextLine(); num = in.matches("[%\\d\\.]+"); //checks if input is %, digits, and periods boolean exit = in.matches("exit"); //loop escape if (exit) System.exit(1); int index = in.indexOf("."); test = in.substring(index+1); index = test.indexOf("."); //tests for more than one period if (index >= 0) num = false; index = in.indexOf("%"); test = in.substring(index+1); index = test.indexOf("%"); //tests for more than one % if (index >= 0) num = false; test = in.substring(0,in.length()-1); index = test.indexOf("%"); //tests for nonfinal % if (index >= 0) num = false; if (num == false) System.out.println("\nInvalid input!"); }while (num == false); if (in.substring(in.length()-1).matches("[%]") == true){ //tests for final % tip = (Double.parseDouble(in.substring(0,in.length()-1))); } else{ tip = (Double.parseDouble(in)); } System.out.println("\nTip: $" + format.format(tip/100.0*bill)); System.out.println("Total: $" + format.format(bill+tip/100.0*bill)); System.out.println(); do{ System.out.println("How many people will be splitting the bill?"); in = sc.nextLine(); num = in.matches("[\\d]+"); //checks if input is integer //try{ // count = sc.nextInt(); //}catch(ArithmeticException e){System.out.println("Cannot divide by zero!"); //}catch(InputMismatchException e){System.out.println("Must be an integer!"); //}finally{num = false;} boolean exit = in.matches("exit"); //loop escape if (exit) System.exit(1); if (num == false) System.out.println("\nInvalid input!"); if (Double.parseDouble(in) < 1){ System.out.println("\nSomeone has to pay!"); num = false; } }while (num == false); double out = (Math.ceil(100*((bill+tip/100.0*bill)/Double.parseDouble(in))))/100.0; //rounds up each person's cost to next cent System.out.println("\nEach person will pay $" + format.format(out) + "."); if(out > (bill+tip/100.0*bill)/Double.parseDouble(in)){ System.out.println("You will pay an extra $" + format.format(((out*Double.parseDouble(in))-(bill+tip/100.0*bill))) + "."); } System.out.println("To calculate again, press 1. Press any other key to exit."); int proceed = sc.nextInt(); if(proceed != 1) againPlease = false; } } }

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.