Advertisement
mitry99

Untitled

Sep 24th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 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.0) {
  33. sum -= 2.0;
  34. System.out.println("Purchased Nuts");
  35.  
  36. } else {
  37. System.out.println("Sorry, not enough money");
  38.  
  39.  
  40. }
  41. break;
  42.  
  43. case "Water":
  44. if (sum >= 0.7) {
  45. sum -= 0.7;
  46. System.out.println("Purchased Water");
  47. } else {
  48. System.out.println("Sorry, not enough money");
  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. break;
  63. case "Soda":
  64. if (sum >= 0.8) {
  65. sum -= 0.8;
  66. System.out.println("Purchased Soda");
  67. } else {
  68. System.out.println("Sorry, not enough money");
  69. }
  70. break;
  71. case "Coke":
  72. if (sum >= 1.0) {
  73. sum -= 1.0;
  74. System.out.println("Purchased Coke");
  75. } else {
  76. System.out.println("Sorry, not enough money");
  77. }
  78. break;
  79. default:
  80. System.out.println("Invalid product");
  81.  
  82. break;
  83.  
  84. }
  85. product = scanner.nextLine();
  86. }
  87.  
  88. System.out.printf("Change: %.2f", sum);
  89.  
  90.  
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement