desislava_topuzakova

07.

Sep 22nd, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. package BasicSyntax;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class VendingMachine_07 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String command = scanner.nextLine();
  9. double sum = 0;
  10. //with 0.1, 0.2, 0.5, 1, and 2
  11. while(!command.equals("Start")) {
  12. double coin = Double.parseDouble(command);
  13. if(coin != 0.1 && coin != 0.2 && coin != 0.5 && coin != 1 && coin != 2) {
  14. System.out.printf("Cannot accept %.2f%n", coin);
  15. } else {
  16. sum += coin;
  17. }
  18.  
  19. command = scanner.nextLine();
  20. }
  21.  
  22. String product = scanner.nextLine();
  23.  
  24. //"Nuts", "Water", "Crisps", "Soda", "Coke". The prices are: 2.0, 0.7, 1.5, 0.8, 1.0
  25. while(!product.equals("End")) {
  26. boolean hasMoney = true;
  27. switch(product) {
  28. case "Nuts":
  29. if(sum < 2) {
  30. System.out.println("Sorry, not enough money");
  31. hasMoney = false;
  32. } else {
  33. sum -= 2;
  34. }
  35. break;
  36. case "Water":
  37. if(sum < 0.7) {
  38. System.out.println("Sorry, not enough money");
  39. hasMoney = false;
  40. } else {
  41. sum -= 0.7;
  42. }
  43. break;
  44. case "Crisps":
  45. if(sum < 1.5) {
  46. System.out.println("Sorry, not enough money");
  47. hasMoney = false;
  48. } else {
  49. sum -= 1.5;
  50. }
  51. break;
  52. case "Soda":
  53. if(sum < 0.8) {
  54. System.out.println("Sorry, not enough money");
  55. hasMoney = false;
  56. } else {
  57. sum -= 0.8;
  58. }
  59.  
  60. break;
  61. case "Coke":
  62. if(sum < 1) {
  63. System.out.println("Sorry, not enough money");
  64. hasMoney = false;
  65. } else {
  66. sum -= 1;
  67. }
  68. break;
  69. default:
  70. System.out.println("Invalid product");
  71. hasMoney = false;
  72. break;
  73. }
  74. if(!hasMoney) {
  75. product = scanner.nextLine();
  76. continue;
  77. }
  78. System.out.printf("Purchased %s%n", product);
  79.  
  80.  
  81. product = scanner.nextLine();
  82. }
  83.  
  84. System.out.printf("Change: %.2f", sum);
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment