Advertisement
Guest User

Untitled

a guest
May 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package BasicsSyntax;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class VendingMachine {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10. String input = scanner.nextLine();
  11. double coinSum = 0;
  12. while (!input.equals("Start")) {
  13. double coins = Double.parseDouble(input);
  14. if (coins == 0.1 || coins == 0.2 || coins == 0.5 || coins == 1 || coins == 2) {
  15. coinSum += coins;
  16. } else {
  17. System.out.printf("Cannot accept %.2f", coins);
  18. }
  19. input = scanner.nextLine();
  20.  
  21.  
  22. while (!input.equals("End")) {
  23. String product = input;
  24. double price = 0;
  25. if (product.equals("Nuts")) {
  26. price = 2.0;
  27. } else if (product.equals("Water")) {
  28. price = 0.7;
  29. } else if (product.equals("Crisps")) {
  30. price = 1.5;
  31. } else if (product.equals("Soda")) {
  32. price = 0.8;
  33. } else if (product.equals("Coke")) {
  34. price = 1.0;
  35. } else {
  36. System.out.println("Invalid product");
  37. }
  38. if (coinSum >= price) {
  39. System.out.printf("Purchased %s", input);
  40. coinSum -= price;
  41. } else {
  42. System.out.println("Sorry, not enough money");
  43. }
  44. input = product;
  45. }
  46.  
  47. }
  48. System.out.printf("Change:%.2f", coinSum);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement