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.
1
u/syob Jun 28 '18
Sorry, still dont get it. Is it mistake or i dont understand this $operation->person->type->calculate()? I image it was suppose to be just $operation->person->calculate()?
Posible i dont understand what you meant at all. But if it was a mistake, then this is how i understand further. So each person type will have its own class, which calculates. But then i need to check what type of operation person is calculating. Because for natural person cash out charge fee is 0.2% but for another could be different.
So i would do this:
In this case, when new operation type would be introduced, i would need to add new case to every person class. Which i think is not the best way ?