-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor.php
105 lines (90 loc) · 2.96 KB
/
vendor.php
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
<?php
$product = new stdClass();
$product->name = "Coffee";
$product->price = 350;
$person = new stdClass();
$person->tenCents = (int)readline("Enter amount of 10 cents: ");
$person->twentyCents = (int)readline("Enter amount of 20 cents: ");
$person->fiftyCents = (int)readline("Enter amount of 50 cents: ");
$person->oneEuro = (int)readline("Enter amount of 1 euros: ");
$person->twoEuro = (int)readline("Enter amount of 2 euros: ");
$total = (10 * $person->tenCents) + (20 * $person->twentyCents) + (50 * $person->fiftyCents) + (100 * $person->oneEuro) + (200 * $person->twoEuro);
if ($total < $product->price) {
echo "You don't have enough money for {$product->name}" . PHP_EOL;
}
echo "Please put in coins to receive {$product->name}" . PHP_EOL;
while ($product->price > 0) {
echo "Total left: {$product->price}" . PHP_EOL;
$inputCoin = (float) readline("Input coins in vendor: ") * 100;
if ($inputCoin != 10 && $inputCoin != 20 && $inputCoin != 50 && $inputCoin != 100 && $inputCoin != 200) {
echo "Vendor accepts only (10,20,50) cents and (1,2) euro coins";
continue;
}
if ($inputCoin == 10 && $person->tenCents == 0) {
echo "You don't have 10 cent coins" . PHP_EOL;
continue;
}
if ($inputCoin == 20 && $person->twentyCents == 0) {
echo "You don't have 20 cent coins" . PHP_EOL;
continue;
}
if ($inputCoin == 50 && $person->fiftyCents == 0) {
echo "You don't have 50 cent coins" . PHP_EOL;
continue;
}
if ($inputCoin == 100 && $person->oneEuro == 0) {
echo "You don't have 1 euro coins" . PHP_EOL;
continue;
}
if ($inputCoin == 200 && $person->twoEuro == 0) {
echo "You don't have 2 euro coins" . PHP_EOL;
continue;
}
if ($inputCoin == 10) {
$person->tenCents -= 1;
}
if ($inputCoin == 20) {
$person->twentyCents -= 1;
}
if ($inputCoin == 50) {
$person->fiftyCents -= 1;
}
if ($inputCoin == 100) {
$person->oneEuro -= 1;
}
if ($inputCoin == 200) {
$person->twoEuro -= 1;
}
$product->price -= $inputCoin;
}
$change = 0 - $product->price;
var_dump($change);
$receivedCoins = [];
while ($change != 0){
if ($change >= 200){
$person->twoEuro += 1;
$change -= 200;
$receivedCoins[] = 200;
} else if ($change >= 100){
$person->oneEuro += 1;
$change -= 100;
$receivedCoins[] = 100;
} else if ($change >= 50) {
$person->fiftyCents += 1;
$change -= 50;
$receivedCoins[] = 50;
} else if ($change >= 20){
$person->twentyCents += 1;
$change -= 20;
$receivedCoins[] = 20;
} else {
$person->tenCents += 1;
$change -= 10;
$receivedCoins[] = 10;
}
}
$outputCoins = array_count_values($receivedCoins);
echo "Your change is: " . PHP_EOL;
foreach ($outputCoins as $coin => $count){
echo "$count - " . $coin / 100 . " euro coins" . PHP_EOL;
}