-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCashMachine.php
More file actions
156 lines (127 loc) · 3.31 KB
/
Copy pathCashMachine.php
File metadata and controls
156 lines (127 loc) · 3.31 KB
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
<?php
namespace XirisApps;
/**
* Class CashMachineException
*
* @package XirisApps
* @author Christopher Silva
*/
class CashMachineException extends \Exception{}
/**
* Class ValidateMoney
* Just an example of validation class
*
* @package XirisApps
* @author Christopher Silva
*/
class ValidateMoney
{
const VALIDATE_INTEGER = false;
const VALIDATE_EMPTY = true;
/**
* @param $money
* @return bool
* @throws CashMachineException
*/
public static function validate($money)
{
if (self::VALIDATE_EMPTY && empty($money))
throw new CashMachineException('Please type some value, ok?');
return true;
}
/**
* @param $value
* @param $min
* @return bool
*/
public static function isTooLower($value, $min)
{
if ($value < $min)
return true;
return false;
}
}
/**
* Class CashMachine
*
* @package Xiris
* @author Christopher Silva
*/
class CashMachine
{
/**
* @var array
* @access private
*/
private $availableNotes = [10, 20, 50, 100];
private $countNotes = [];
public function getNotesByValue($value)
{
$validate = \XirisApps\ValidateMoney::validate($value);
if (in_array($value, $this->getAvailableNotes())) {
return $value;
}
// ...
}
/**
* @param $value
* @return bool
*/
public function isTooLower($value)
{
// use ValidateMoney::isTooLower in case we want to put more validations
return \XirisApps\ValidateMoney::isTooLower($value, min($this->getAvailableNotes()));
}
/**
* @param $key
* @param $value
*/
public function setCountNotes($key, $value)
{
$this->countNotes[$key] += $value;
}
/**
* @param null $key
* @return array
*/
public function getCountNotes($key = null)
{
if (is_null($key))
return $this->countNotes;
return $this->countNotes[$key];
}
/**
* @return array
*/
public function getAvailableNotes()
{
return $this->availableNotes;
}
}
/**
* Interactive PHP Client
*/
echo "********************************************\n";
echo "** **\n";
echo "** WELCOME TO XIRIS INTERNATIONAL BANK :D **\n";
echo "** **\n";
echo "********************************************\n\n";
fwrite(STDOUT, "- Please, type how much money do you want baby: ");
// casting money, i dont want to have problems =/
$money = (int) trim(fgets(STDIN));
$cashMachine = new CashMachine();
if (!empty($money)) {
// verify minimal inputed value
while ($cashMachine->isTooLower($money)) {
fwrite(STDOUT, "- Hey, this money doesn't pay my coffee =/ ... \n\n");
fwrite(STDOUT, "- Please, type how much money do you want baby: ");
// casting money, i dont want to have problems =/
$money = (int) trim(fgets(STDIN));
}
$moneyback = $cashMachine->getNotesByValue($money);
fwrite(STDOUT, "- Ohh God, \"\${$moneyback}\" bucks.\n");
fwrite(STDOUT, "- Please, wait a moment ...\n");
} else {
fwrite(STDOUT, "\n- I think you dont have money, han? =/\n");
}
fwrite(STDOUT, "\n\nThanks to use and abuse! :D\n\n");