Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class TipCalc {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. //Prerequisites
  7.  
  8. //Variables
  9. double cost = 0;
  10. double tipPre = 0;
  11. double tip = 0;
  12. double total;
  13. double splitPre = 0;
  14. double split = 0;
  15. boolean rerun = false;
  16. boolean actualNum = false;
  17. String goAgain = "yes";
  18. String addtext = "";
  19.  
  20. //scanner
  21. Scanner kb = new Scanner(System.in);
  22.  
  23. //Program Start
  24. while(goAgain.equals("yes")) {
  25. //is this an additional tip calculation?
  26. if(rerun == true) {
  27. addtext = "again";
  28. }
  29. else {
  30. }
  31.  
  32. System.out.println("Hello " + addtext + "! (^ー^)ノ \nPlease enter the price of the meal as a decimal(no $)");
  33.  
  34. //id10t proofing
  35. while(actualNum == false) {
  36. if(kb.hasNextDouble() == true) {
  37. cost = kb.nextDouble();
  38. if(cost <= 0) {
  39. System.out.println("The food must be pretty bad if they are giving it to you for free or if they are paying you to eat it. Please enter a number above 0");
  40. }
  41. else {
  42. actualNum = true;
  43. }
  44. }
  45. else {
  46. System.out.println("Please enter an actual number.");
  47. kb.nextLine();
  48. }
  49. }
  50.  
  51. actualNum = false;
  52.  
  53. System.out.println("Now please eneter the tip precent (no %)");
  54.  
  55. //id10t proofing
  56. while(actualNum == false) {
  57. if(kb.hasNextDouble() == true) {
  58. tipPre = kb.nextDouble();
  59. if(tipPre < 0) {
  60. System.out.println("The service must be pretty bad if you expect them to pay you. Please enter a number that is not negitive");
  61. }
  62. else if(tipPre == 0) {
  63. System.out.println("Wow, no tip.");
  64. actualNum = true;
  65. }
  66. else {
  67. actualNum = true;
  68. }
  69. }
  70. else {
  71. System.out.println("Please enter an actual number.");
  72. kb.nextLine();
  73. }
  74. }
  75.  
  76. actualNum = false;
  77.  
  78. //math to calculate tip
  79. tipPre = tipPre / 100;
  80.  
  81. tip = cost * tipPre;
  82.  
  83. //rounding
  84. tip = (tip * 100) + 0.5;
  85. tip = (int) tip;
  86. tip = (double) tip / 100;
  87.  
  88. cost = (cost * 100) + 0.5;
  89. cost = (int) cost;
  90. cost = (double) cost / 100;
  91.  
  92. //math for total cost
  93. total = cost + tip;
  94.  
  95. //converting this back into a whole number to be displayed later
  96. tipPre = tipPre * 100;
  97.  
  98. System.out.println("For a meal that costs " + cost + " and a tip of " + tipPre + "%, your tip is " + tip + " and your total cost is " + total + ".");
  99.  
  100. System.out.println("Please type how many people you are splitting the bill with. If you are alone, just type 1");
  101.  
  102. //id10t proofing
  103. while(actualNum == false) {
  104. if(kb.hasNextInt() == true) {
  105. split = kb.nextInt();
  106. if(split <= 0) {
  107. System.out.println("You must be really lonely to have no one eating. Please enter a number above 0");
  108. }
  109. else {
  110. actualNum = true;
  111. }
  112. }
  113. else {
  114. System.out.println("Please enter an actual number.");
  115. kb.nextLine();
  116. }
  117. }
  118.  
  119. actualNum = false;
  120.  
  121. //math for calculating the percentage of price per person split
  122. splitPre = total / split;
  123.  
  124. //more rounding
  125. splitPre = (splitPre * 100) + 0.5;
  126. splitPre = (int) splitPre;
  127. splitPre = (double) splitPre / 100;
  128.  
  129. System.out.println("If you are splitting the bill between " + split + " people evenly, each person pays " + splitPre + ".");
  130.  
  131. System.out.println("Would you like to calculate another tip?" // restart?
  132. + "\nif so, type \"yes\", if not type \"no\".");
  133.  
  134. //id10t proofing
  135. while(actualNum == false) {
  136. goAgain = kb.next();
  137. if(goAgain.equals("yes") == false && goAgain.equals("no") == false) {
  138. System.out.println("Please type \"yes\" or \"no\"");
  139. }
  140. else {
  141. actualNum = true;
  142. }
  143. }
  144. rerun = true;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement