Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class VendingApplication
  5. {
  6. static Scanner input = new Scanner(System.in);
  7. static DecimalFormat currency = new DecimalFormat("0.00");
  8. static int qtyCoke = 10;
  9. static int qty7up = 10;
  10. static int qtyFanta = 2;
  11. static double totalCash = 0;
  12. static int numTransaction = 0;
  13. static int pin = 1234;
  14.  
  15.  
  16. public static void main(String[] args)
  17. {
  18. menu();
  19. }
  20.  
  21. public static void menu()
  22. {
  23. System.out.println("Welcome to the BFEI vending Machine");
  24. System.out.println("-----------------------------------");
  25. System.out.println("Press 1 for Coke");
  26. System.out.println("Press 2 for 7up");
  27. System.out.println("Press 3 for Fanta");
  28. System.out.println("-----------------------------------");
  29.  
  30. String choice = input.next();
  31.  
  32. switch(choice)
  33. {
  34. case "1":
  35. {
  36. if(qtyCoke==0)
  37. {
  38. System.out.println("Item out of stock. Please choose another.");
  39. menu();
  40. }
  41.  
  42. String product = "Coke";
  43. double price = 1.20;
  44. payment(product, price);
  45. break;
  46. }
  47. case "2":
  48. {
  49. if(qty7up==0)
  50. {
  51. System.out.println("Item out of stock. Please choose another.");
  52. menu();
  53. }
  54.  
  55. String product = "7up";
  56. double price = 1.10;
  57. payment(product, price);
  58. break;
  59. }
  60. case "3":
  61. {
  62. if(qtyFanta==0)
  63. {
  64. System.out.println("Item out of stock. Please choose another.");
  65. menu();
  66. }
  67.  
  68. String product = "Fanta";
  69. double price = 1;
  70. payment(product, price);
  71. break;
  72. }
  73. case "a":
  74. {
  75. System.out.println("Please enter pin number");
  76. int enteredPin = input.nextInt();
  77.  
  78. if(enteredPin==pin)
  79. {
  80. System.out.println("Correc Pin. Loading Admin Mode");
  81. admin();
  82. }
  83. else
  84. {
  85. System.out.println("Incorrect Pin. Try again");
  86. }
  87. break;
  88. }
  89. case "x":
  90. {
  91. System.out.println("System closing down");
  92. System.exit(0);
  93. break;
  94. }
  95. default:
  96. {
  97. System.out.println("Invalid choice. Please choose again.");
  98. break;
  99. }
  100. }
  101.  
  102. menu();
  103.  
  104. }
  105.  
  106. public static void payment(String productChoice, double transactionPrice)
  107. {
  108. System.out.println("Enter Payment. Balance is " + currency.format(transactionPrice));
  109.  
  110. double moneyEntered = input.nextDouble(); //User enters payment
  111.  
  112. while(moneyEntered<transactionPrice)
  113. {
  114. System.out.println("Enter payment. Balance remaining is " + currency.format((transactionPrice-moneyEntered)));
  115. moneyEntered = moneyEntered + input.nextDouble();//User tops up payment and overwrites existing amount
  116. }
  117. //Product has been purchased at this point
  118. if(moneyEntered>transactionPrice)
  119. {
  120. System.out.println("Changedue is " + currency.format((moneyEntered-transactionPrice)));
  121. }
  122. System.out.println("Trasaction Succesful. Please take your product");
  123.  
  124. numTransaction++;
  125. totalCash = totalCash + transactionPrice; // Update total takings with value of the transaction
  126.  
  127. if(productChoice.equals("Coke"))
  128. {
  129. qtyCoke--;
  130. }
  131. else if(productChoice.equals("Fanta"))
  132. {
  133. qtyFanta--;
  134. }
  135. {
  136. qty7up--;
  137. }
  138.  
  139. }
  140.  
  141. public static void admin()
  142. {
  143. System.out.println("Total Takings: €" + currency.format(totalCash));
  144. System.out.println("Number of Transactions: " + numTransaction);
  145. System.out.println("Average Value of a Transaction" + currency.format((totalCash/numTransaction)));
  146. System.out.println("Coke Stock Remaining" + qtyCoke);
  147. System.out.println("7up Stock Remaining" + qty7up);
  148. System.out.println("Fanta Stock Remaining" + qtyFanta);
  149.  
  150. System.out.println("Press r to restock or any other key to exit");
  151.  
  152. String choice = input.next();
  153.  
  154. if(choice.equals("r"))
  155. {
  156. qtyCoke=10;
  157. qty7up=10;
  158. qtyFanta=2;
  159. }
  160.  
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement