r/PHPhelp • u/syob • Jun 27 '18
Php Object Oriented Design. Need help
I have a list of operations and i need to calculate bank commission for every operation.
Operation types for now: cash in, cash out.
Person types can be: natural or legal. Not sure if there can be more types, but i thinking with in mind, that its possible.
Commission size depends on operation type and what person type made that operation:
Cash in commission for everyone, is 0.07% but not more than 4.00 Eur.
Cash out commission for legal person is 0.2% , but not less than 1.00 Eur.
Cash out commission for natural person is 0.2%, but person can cash out 1000 Eur in a week for free.
Input is csv file. With lines like this:
2015-12-31,1,natural,cash_out,1000.00,EUR
2016-01-05,2,legal,cash_in,200.00,EUR
2016-02-01,1,natural,cash_out,1000.00,EUR
(Second value is user id).
I want to do this using all good oop practices, solid principles etc. So far im doing it like this:
$calculator = new Calculator();
foreach ($data as $row) {
$operation = OperationFactory::create($data); //Returns instance of class CashIn, class CashOut etc
$calculator->addOperation($operation);
}
$results = $calculator->calculate();And now it becomes very confusing to me. If tax price would only depend on operation type, i would do something like this:
class Calculator
{
public function addOperation(Operations\Operation $operation)
{
$this->operations[] = $operation;
}
public function calculate()
{
foreach ($this->operations as $operation) {
$result = $this->operation->amount * $operation->charge / 100;
if ($result < $operation->min) {
$result = $operation->min;
} elseif ($result > $operation->max) {
$result = $operation->max;
}
$results[] = $result;
}
return $result;
}
}I want to make this, that i could easily extend system with new operation types and new person types if neccesary. And this complicates the task for me. Escpecialy that part : "Cash out tax price for natural person is 0.2%, but person can cashout 1000 Eur in a week for free". In which place should i track all of this?
Im doing all this for learning purposes. Any help and tips appreciated.
2
u/cryptichorizon77 Jun 27 '18 edited Jun 27 '18
To be able to calculate "person can cashout 1000 Eur in a week for free", your code needs to know how much each person cashed out so far, for at least one week. So that information is in the csv data, but you need an accumulation of it for the current week so far.
I can't see in your current model that you store the data anywhere. So either each csv file always represents one whole week's worth of data, or you will need to write something that stores the data (in a db perhaps) as it imports it. You can then uses that "historical" data to re-calculate the current weekly cach-out total for each person when you process a new csv file.
Make sense?