-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVending machine
132 lines (126 loc) · 5.58 KB
/
Vending machine
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
namespace ex07_vending_machine
{
class Program
{
static void Main(string[] args)
{
double currentProductValue = 0;
string productNamePrint = "";
bool purchiseSuccesful = false;
// You task is to calculate the total price of a purchase from a vending machine.Until you receive “Start” you will be
//given different coins that are being inserted in the machine. You have to sum them in order to have the total money
//inserted.There is a problem though. ??? Your vending machine only works with 0.1, 0.2, 0.5, 1, and 2 coins.If someone
//tries to insert some other coins you have to display “Cannot accept { money}” and not add it to the total money. ???? On
//the next few lines until you receive “End” you will be given products to purchase.Your machine has however only
//“Nuts”, “Water”, “Crisps”, “Soda”, “Coke”. The prices are: 2.0, 0.7, 1.5, 0.8, 1.0 respectively.If the person tries to
//purchase a not existing product print “Invalid product”. Be careful that the person may try to purchase a product
//they don’t have the money for. In that case print “Sorry, not enough money”. If the person purchases a product
//successfully print “Purchased { product name}”. After the “End” command print the money that are left formatted
//to the second decimal point in the format “Change: { money left}”.
string command = "";
double totalValueOfMoney = 0;
while ((command = Console.ReadLine()) != "Start")
{
double coinInsert = double.Parse(command);
if (coinInsert == 0.1 || coinInsert == 0.2 || coinInsert == 0.5 || coinInsert == 1 || coinInsert == 2)
{
totalValueOfMoney += coinInsert;
}
else
{
Console.WriteLine($"Cannot accept {coinInsert}"); // 1 / 1/ 0.5 / 0.6
}
}
string productName = "";
while((productName = Console.ReadLine()) != "End" || !(currentProductValue > totalValueOfMoney))
{
if(productName == "Nuts")
{
currentProductValue = 2;
productNamePrint = "nuts";
if (currentProductValue > totalValueOfMoney)
{
Console.WriteLine($"Sorry, not enough money");
purchiseSuccesful = false;
}
else
{
totalValueOfMoney -= currentProductValue;
purchiseSuccesful = true;
}
}
else if (productName == "Water")
{
currentProductValue = 0.7;
productNamePrint = "water";
if (currentProductValue > totalValueOfMoney)
{
Console.WriteLine($"Sorry, not enough money");
purchiseSuccesful = false;
}
else
{
totalValueOfMoney -= currentProductValue;
purchiseSuccesful = true;
}
}
else if (productName == "Crisps") //2.0, 0.7, 1.5, 0.8, 1.0
{
currentProductValue = 1.5;
productNamePrint = "crisps";
if (currentProductValue > totalValueOfMoney)
{
Console.WriteLine($"Sorry, not enough money");
purchiseSuccesful = false;
}
else
{
totalValueOfMoney -= currentProductValue;
purchiseSuccesful = true;
}
}
else if (productName == "Soda")
{
currentProductValue = 0.8;
productNamePrint = "soda";
if (currentProductValue > totalValueOfMoney)
{
Console.WriteLine($"Sorry, not enough money");
purchiseSuccesful = false;
}
else
{
totalValueOfMoney -= currentProductValue;
purchiseSuccesful = true;
}
}
else if (productName == "Coke")
{
currentProductValue = 1;
productNamePrint = "coke";
if (currentProductValue > totalValueOfMoney)
{
Console.WriteLine($"Sorry, not enough money");
purchiseSuccesful = false;
}
else
{
totalValueOfMoney -= currentProductValue;
purchiseSuccesful = true;
}
}
else
{
Console.WriteLine($"Invalid product");
break;
}
if (purchiseSuccesful == true)
{
Console.WriteLine($"Purchased {productNamePrint}");
}
}
Console.WriteLine($"Change: {totalValueOfMoney:f2}");
}
}
}