Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bootstrap
- {
- public static function main($argv, ParserInterface $parser, PayCalculator $calculator)
- {
- $payCollection = new PayCollection();
- $data = $parser->parse($argv[1]);
- foreach ($data as $one)
- {
- if (self::isPay($one[1])) {
- $payCollection->add(new Pay($one[9], $one[8]));
- }
- }
- $result = $calculator->sumSameCurrencies($payCollection);
- echo self::render($result);
- }
- public static function render(array $result): string
- {
- $output = "Totals\n";
- foreach ($result as $one)
- {
- $output .= $one->getCurrencyName() . ' ' . number_format ( (float) $one->getAmount() , $decimals = 2 , $dec_point = "." , $thousands_sep = "," ) . "\n";
- }
- return $output;
- }
- private static function isPay(string $string): bool
- {
- if (strpos($string,'PAY') !== false) {
- return true;
- }
- else {
- return false;
- }
- }
- }
- class CsvParser implements ParserInterface
- {
- public function parse(string $filePath): array
- {
- $csvFile = file($filePath);
- $data = [];
- foreach ($csvFile as $line) {
- $data[] = str_getcsv($line);
- }
- return $data;
- }
- }
- interface ParserInterface
- {
- public function parse(string $filePath): array;
- }
- class ParserFactory
- {
- public function getParserForFormat(string $format)
- {
- switch ($format)
- {
- case 'csv':
- return new CsvParser();
- break;
- }
- }
- }
- class Pay
- {
- private $currencyName;
- private $amount;
- public function __construct(string $currencyName, string $amount)
- {
- $this->currencyName = $currencyName;
- $this->amount = $amount;
- }
- public function getAmount(): string
- {
- return $this->amount;
- }
- public function getCurrencyName(): string
- {
- return $this->currencyName;
- }
- public function setAmount(string $amount): void
- {
- $this->amount = $amount;
- }
- }
- class PayCalculator
- {
- private $currencies = [];
- public function sumSameCurrencies(PayCollection $payCollection): array
- {
- $pays = $payCollection->all();
- $checkedElements = [];
- foreach ($pays as $key => $value)
- {
- foreach ($pays as $_key => $_value)
- {
- if ($key === $_key) {
- $checkedElements[] = $key;
- continue;
- }
- if(in_array($key, $checkedElements)) {
- continue;
- }
- if ($_value->getCurrencyName() === $value->getCurrencyName()) {
- $sum = $_value->getAmount() + $value->getAmount();
- $value->setAmount($sum);
- unset($pays[$_key]);
- }
- }
- }
- return $pays;
- }
- }
- class PayCollection
- {
- private $items = [];
- public function add(Pay $pay): void
- {
- $this->items[] = $pay;
- }
- public function all(): array
- {
- return $this->items;
- }
- }
- $fileObject = new SplFileInfo($argv[1]);
- $fileExtension = $fileObject->getExtension();
- $parser = (new ParserFactory())->getParserForFormat($fileExtension);
- $payCalculator = new PayCalculator();
- Bootstrap::main($argv, $parser, $payCalculator);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement