/
P07_VendingMachine.cs
97 lines (92 loc) · 3.17 KB
/
P07_VendingMachine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace P07_VendingMachine
{
using System;
class P07_VendingMachine
{
static void Main(string[] args)
{
string command = Console.ReadLine();
double coins = 0.0;
while (command != "Start")
{
double coin = double.Parse(command);
if (coin == 0.1 || coin == 0.2 || coin == 0.5 || coin == 1 || coin == 2)
{
coins += coin;
}
else
{
Console.WriteLine($"Cannot accept {coin}");
}
command = Console.ReadLine();
}
string product = Console.ReadLine().ToLower();
while (product != "end")
{
switch (product)
{
case "nuts":
if (coins >= 2.0)
{
Console.WriteLine($"Purchased {product}");
coins -= 2.0;
}
else
{
Console.WriteLine("Sorry, not enough money");
}
break;
case "water":
if (coins >= 0.7)
{
Console.WriteLine($"Purchased {product}");
coins -= 0.7;
}
else
{
Console.WriteLine("Sorry, not enough money");
}
break;
case "crisps":
if (coins >= 1.5)
{
Console.WriteLine($"Purchased {product}");
coins -= 1.5;
}
else
{
Console.WriteLine("Sorry, not enough money");
}
break;
case "soda":
if (coins >= 0.8)
{
Console.WriteLine($"Purchased {product}");
coins -= 0.8;
}
else
{
Console.WriteLine("Sorry, not enough money");
}
break;
case "coke":
if (coins >= 1.0)
{
Console.WriteLine($"Purchased {product}");
coins -= 1.0;
}
else
{
Console.WriteLine("Sorry, not enough money");
}
break;
default:
Console.WriteLine("Invalid product");
break;
}
product = Console.ReadLine().ToLower();
}
Console.WriteLine($"Change: {coins:F2}");
}
}
}