-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.php
162 lines (140 loc) · 4.57 KB
/
machine.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
class machine{
private $typeOfMoneyAndState = [
["id"=> 0, "name" => "dollar" , "value" => 1, "quantity" => 20],
["id"=> 1, "name" => "quarter" ,"value" => 0.25,"quantity" => 20],
["id"=> 2, "name" => "dime" , "value" => 0.1,"quantity" => 20],
["id"=> 3, "name" => "nickiel" ,"value" => 0.05,"quantity" => 20],
];
//budget for the purchase of the item
private $cash = 0;
//item list
private $items = ["item0" => 0.65,
"item1" => 1,
"item2" => 1.5];
//function add money
public function addMoney($i){
foreach($this->typeOfMoneyAndState as &$type){
if($type["id"] == $i){
$type["quantity"] = $type["quantity"] + 1;
$this->update($type["value"]);
}
}
}
private function update($i){
$this->cash = $this->cash + $i;
}
public function viewCash(){
echo $this->cash . " ";
}
public function buyItem($i){
if($this->cash >= $this->items['item'.$i]){
echo "You bought the item <br />";
$this->cash = $this->cash - $this->items['item'.$i];
echo "return money <br />";
}else{
echo "More money <br />";
}
}
public function returnMoney(){
$returnCash = $this->cash;
foreach($this->typeOfMoneyAndState as $type){
//sprawdza ile bedzie do wydanai z określonego bilonu
$howMuchMoneyOfThisType = floor($returnCash / $type["value"] + 0.004);
if($howMuchMoneyOfThisType <= $type["quantity"]){
$returnCash = $returnCash - $howMuchMoneyOfThisType * $type["value"];
$type["quantity"] = $type["quantity"] - $howMuchMoneyOfThisType;
echo $howMuchMoneyOfThisType . " x " . $type["name"] . " <br />";
}elseif($type["quantity"] < $howMuchMoneyOfThisType && $type["quantity"] > 0 ){
//how much can you give away coins
$cacheCash = $howMuchMoneyOfThisType - $type["quantity"];
$returnCash = $returnCash - $cacheCash * $type["value"];
$type["quantity"] = $type["quantity"] - $cacheCash * $type["value"];
echo $cacheCash . " x " . $type["name"] . " <br /> ";
}
}
}
public function addMoneyServis($i){
foreach($this->typeOfMoneyAndState as &$type){
if($type["id"] == $i){
$type["quantity"] = $type["quantity"] + 1;
}
}
}
public function deleteMoneyServis($i){
foreach($this->typeOfMoneyAndState as &$type){
if($type["id"] == $i){
if($type["quantity"] > 0){
$type["quantity"] = $type["quantity"] - 1;
}else{
echo "<br />
no coin
<br />";
}
}
}
}
public function addOrChangeItemServis($i, $j){
$name = "item".$i;
$this->items[$name] = $j;
}
public function deleteItemServis($i){
$name = "item".$i;
if(in_array($name, array_keys($this->items), true)){
unset($this->items[$name]);
echo "
<br />
Delete item ". $name ."
<br />";
}else{
echo "
<br />
There is no item
<br />";
}
}
public function stateView(){
var_dump($this->items);
echo "<br />";
print_r($this->typeOfMoneyAndState);
}
public function stateItem(){
echo "<table>
<tr>
<th>name</th>
<th>price</th>
</tr>
<tr>
";
foreach($this->items as $item => $price){
echo "<tr>";
echo "<td>" .$item. "</td>";
echo "<td>" .$price. "</td>";
echo "</tr>";
}
echo "</table>
</tr>
";
}
public function stateCoin(){
echo "<table>
<tr>
<th>id</th>
<th>name</th>
<th>value</th>
<th>quantity</th>
</tr>
<tr>
";
foreach($this->typeOfMoneyAndState as $id => $coins){
echo "<tr>";
foreach($coins as $coin){
echo "<td>" .$coin. "</td>";
}
echo "</tr>";
}
echo "</table>
</tr>
";
}
}