Advertisement
feagans

Untitled

Mar 2nd, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class retail {
  4. public static void main (String[] args){
  5.  
  6. //compute the discount on retail sales depending on the day
  7.  
  8. //declares all variables
  9. double discount = 0;
  10. double discount_amount = 0;
  11. double retail_price = 0;
  12. double total_retail_price =0;
  13. double retail_price_after_discount = 0;
  14. double total_retail_price_after_discount = 0;
  15. double total_discount = 0;
  16. int total_items = -1;
  17. int day = 0;
  18.  
  19. boolean tryAgain = true;
  20. //try catch loop to check for the input for the day is between 1 and 7 and is a number
  21. do {
  22. try {
  23. day = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number according to the day of purchase: \n 1: Sunday\n 2: Monday\n 3: Tuesday\n 4: Wednesday\n 5: Thursday\n 6: Friday\n 7: Saturday"));
  24.  
  25. if (day >= 1 && day <= 7){
  26. JOptionPane.showMessageDialog(null, "Day chosen.");
  27. tryAgain = false;
  28. }
  29. else if (day < 1 || day > 7){
  30. throw new NumberFormatException("Integer is out of range.");
  31. }
  32. }
  33. catch (NumberFormatException e1) { // Range check
  34. JOptionPane.showMessageDialog(null, "The number you entered is not between 1 and 6. Try again.");
  35. }
  36. }
  37. while(tryAgain);
  38. //prompt for day to control selection
  39. // day = Integer.parseInt(JOptionPane.showInputDialog
  40. //("Please enter the number according to the day of purchase: \n 1: Sunday\n 2: Monday\n 3: Tuesday\n 4: Wednesday\n 5: Thursday\n 6: Friday\n 7: Saturday"));
  41.  
  42. do {
  43. //try and catch to ensure the user input is only a numeric and is only 1-6
  44. try
  45. {
  46. retail_price = Double.parseDouble(JOptionPane.showInputDialog("Please enter the retail price of the item or a -1 if you have no more items"));
  47.  
  48. }
  49. catch (NumberFormatException e)
  50. {
  51. JOptionPane.showMessageDialog(null, "ERROR! The number for the day must be numeric");
  52. }
  53. //if statements depending on the day input to run in the loop to adjust the prices
  54. if (day == 1 || day == 2){
  55. discount = 0;
  56. discount_amount = retail_price * discount;
  57. retail_price_after_discount = retail_price - discount_amount;
  58. total_retail_price = total_retail_price + retail_price;
  59. total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
  60. total_discount = total_discount + discount_amount;
  61. total_items++;
  62. }
  63. else if(day == 3){
  64. discount = .08;
  65. discount_amount = retail_price * discount;
  66. retail_price_after_discount = retail_price - discount_amount;
  67. total_retail_price = total_retail_price + retail_price;
  68. total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
  69. total_discount = total_discount + discount_amount;
  70. total_items++;
  71. }
  72. else if(day == 4){
  73. discount = .16;
  74. discount_amount = retail_price * discount;
  75. retail_price_after_discount = retail_price - discount_amount;
  76. total_retail_price = total_retail_price + retail_price;
  77. total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
  78. total_discount = total_discount + discount_amount;
  79. total_items++;
  80. }
  81. else if(day == 5){
  82. discount = .24;
  83. discount_amount = retail_price * discount;
  84. retail_price_after_discount = retail_price - discount_amount;
  85. total_retail_price = total_retail_price + retail_price;
  86. total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
  87. total_discount = total_discount + discount_amount;
  88. total_items++;
  89. }
  90. else if(day == 6){
  91. discount = .32;
  92. discount_amount = retail_price * discount;
  93. retail_price_after_discount = retail_price - discount_amount;
  94. total_retail_price = total_retail_price + retail_price;
  95. total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
  96. total_discount = total_discount + discount_amount;
  97. total_items++;
  98. }
  99. else if(day == 7){
  100. discount = .40;
  101. discount_amount = retail_price * discount;
  102. retail_price_after_discount = retail_price - discount_amount;
  103. total_retail_price = total_retail_price + retail_price;
  104. total_retail_price_after_discount = total_retail_price_after_discount + retail_price_after_discount;
  105. total_discount = total_discount + discount_amount;
  106. total_items++;
  107. }
  108. else {
  109. JOptionPane.showMessageDialog(null, "No items were entered");
  110. }
  111. //loop until the user input is -1
  112. } while(retail_price != -1);
  113. //output receipt
  114. JOptionPane.showMessageDialog
  115. (null, "Total number of items is: " + total_items + "\n" + "Total price without discount is: " + total_retail_price + "\n" + "Total price with the discount is: " + total_retail_price_after_discount + "\n" + "Total discount saved: " + total_discount);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement