Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class TipCalculator {
  4.  
  5.  
  6.  
  7. public static void main(String[] args) {
  8. // declare variables
  9.  
  10. boolean calculate = true;
  11.  
  12. //begin program
  13.  
  14. while(calculate==true){
  15. Scanner input1 = new Scanner(System.in);
  16. System.out.println("Please enter value of bill");
  17. double bill=0;
  18. boolean allGood = true;
  19.  
  20.  
  21.  
  22. try
  23. {
  24. bill = input1.nextDouble();
  25. }
  26. catch(Exception e)
  27. {
  28. allGood = false;
  29. // System.out.println("Please enter a valid bill amount in dollars and cents.");
  30. }
  31.  
  32. // double roundedBill = (((int)(100 * bill + .05) )/100); // rounds to hundredths place
  33. if (allGood == true){
  34. Scanner input2 = new Scanner(System.in);
  35. System.out.println("Please enter the percentage you would like to tip");
  36. double tip = input2.nextDouble();
  37.  
  38. double tipAmount = bill * tip;
  39. double totalAmount = bill * (tip + 1);
  40.  
  41. double roundedTip = (((int)(100 * tipAmount) )/100.0);
  42.  
  43. double roundedTotal = (((int)(100 * totalAmount + .05) )/100.0);
  44.  
  45. System.out.println("Your tip amount is " + roundedTip + " and your total bill is " + roundedTotal);
  46.  
  47. Scanner input3 = new Scanner(System.in);
  48. System.out.println("How many people are you splitting the bill between?");
  49. double numPeople = input3.nextDouble();
  50. double amountPerPerson = totalAmount / numPeople;
  51. double roundedAmtPP = (int)(100 * amountPerPerson + .05) / 100.0;
  52. System.out.println("The amount each person should pay is " + roundedAmtPP);
  53.  
  54. System.out.print("Would you like to calculate another tip? Enter 1 for yes, enter 2 to leave.");
  55. Scanner input4 = new Scanner(System.in);
  56. int calculateAgain = input4.nextInt();
  57.  
  58. if(calculateAgain==1){
  59. calculate = true;
  60. }
  61.  
  62. if(calculateAgain==2){
  63. calculate = false;
  64. }
  65. }//end of all good
  66.  
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement