Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package SoftUniFund.BasicSyntax;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class VendingMachine2 {
  6. public static void main(String[] args) {
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. String input = scan.nextLine();
  10. double sum = 0;
  11. while (!input.equals("Start")) {
  12.  
  13. if (input.equals("0.1") || input.equals("0.2") || input.equals("0.5") || input.equals("1") || input.equals("2")) {
  14. sum += Double.parseDouble(input);
  15. } else {
  16. System.out.println(String.format("Cannot accept %.2f", Double.parseDouble(input)));
  17. }
  18. input = scan.nextLine();
  19. }
  20.  
  21.  
  22. String product = scan.nextLine();
  23. while (!product.equals("End")) {
  24. boolean isProduct = false;
  25. double price = 0;
  26. switch (product) {
  27. case "Nuts":
  28. price = 2.0;
  29. isProduct = true;
  30. break;
  31. case "Water":
  32. price = 0.7;
  33. isProduct = true;
  34. break;
  35. case "Crisps":
  36. price = 1.5;
  37. isProduct = true;
  38. break;
  39. case "Soda":
  40. price = 0.8;
  41. isProduct = true;
  42. break;
  43. case "Coke":
  44. price = 1.0;
  45. isProduct = true;
  46. break;
  47. }
  48. if (price > sum ){
  49. System.out.println("Sorry, not enough money");
  50. } else if (isProduct) {
  51. System.out.println("Purchased " + product);
  52. sum -= price;
  53. isProduct = false;
  54. } else {
  55. System.out.println("Invalid product");
  56. }
  57. product = scan.nextLine();
  58. }
  59. System.out.println(String.format("Change: %.2f", sum));
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement