Advertisement
mitry99

Untitled

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