Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package bg.home.homeworkOne;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author Chobi
  13. */
  14. public class VendingMachine_07 {
  15.  
  16. public static void main(String[] args) {
  17. Scanner inputScaner = new Scanner(System.in);
  18. String input = inputScaner.nextLine();
  19. double totalCoins = 0d;
  20. while (!"Start".equals(input)) {
  21. double currentCoin = Double.parseDouble(input);
  22. if (currentCoin == 0.1
  23. || currentCoin == 0.2
  24. || currentCoin == 0.5
  25. || currentCoin == 1
  26. || currentCoin == 2) {
  27.  
  28. totalCoins += currentCoin;
  29. } else {
  30. System.out.println(String.format("Cannot accept %.2f", currentCoin));
  31. }
  32. input = inputScaner.nextLine();
  33. }
  34. input = inputScaner.nextLine();
  35. while (!"End".equals(input)) {
  36.  
  37. double price = getPrice(input);
  38. if (price == 0d) {
  39. System.out.println("Invalid product");
  40. } else if (price > totalCoins) {
  41. System.out.println("Sorry, not enough money");
  42. } else {
  43. System.out.println(String.format("Purchased %s", input));
  44. totalCoins -= price;
  45. }
  46.  
  47. input = inputScaner.nextLine();
  48. }
  49. System.out.println(String.format("Change: %.2f", totalCoins));
  50.  
  51. }
  52.  
  53. private static double getPrice(String input) {
  54. double price = 0d;
  55. switch (input) {
  56. case "Nuts":
  57. price = 2.0;
  58. break;
  59. case "Water":
  60. price = 0.7;
  61. break;
  62. case "Crisps":
  63. price = 1.5;
  64. break;
  65. case "Soda":
  66. price = 0.8;
  67. break;
  68. case "Coke":
  69. price = 1.0;
  70. break;
  71. default:
  72. break;
  73. }
  74. return price;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement