View difference between Paste ID: 7uxdAgPK and 1ba2T2k1
SHOW: | | - or go back to the newest paste.
1
import java.util.Scanner;
2
 
3
public class VendingMachine {
4
    public static void main(String[] args) {
5
 
6
        Scanner scanner = new Scanner(System.in);
7
        double sum = 0;
8
        int total = 0;
9
 
10
        String textInput = "";
11
        while (true) {
12
            textInput = scanner.nextLine();
13
            if (textInput.equals("Start")) {
14
                break;
15
            }
16
            double coin = Double.parseDouble(textInput);
17
            if (coin == 0.1 || coin == 0.2 || coin == 0.5 || coin == 1 || coin == 2) {
18
                sum += coin;
19
            } else {
20
                System.out.println("Cannot accept " + coin);
21
            }
22
        }
23
 
24
 
25
        double price = 0;
26
        String input ;
27
        boolean flag = false;
28
        while (true) {
29
            input = scanner.nextLine();
30
            switch (input.toLowerCase()) {
31
                case "nuts":
32
                    price = 2;
33
                    flag = true;
34
                    break;
35
                case "water":
36
                    price = 0.7;
37
                    flag = true;
38
                    break;
39
                case "crisps":
40
                    price = 1.5;
41
                    flag = true;
42
                    break;
43
                case "soda":
44
                    price = 0.8;
45
                    flag = true;
46
                    break;
47
                case "coke":
48
                    price = 1;
49
                    flag = true;
50
                    break;
51
            }
52
            if (input.equals("End")) {
53
                break;
54
            }
55
            if (sum < price) {
56
                System.out.println("Sorry, not enough money");
57
            } else if (flag) {
58
                System.out.println("Purchased " + input);
59
                sum -= price;
60
            } else {
61
                System.out.println("Invalid product");
62
            }
63
 
64
        }
65
        System.out.printf("Change: %.2f", sum);
66
 
67
 
68
    }
69
}