Advertisement
Guest User

Untitled

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