Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. class Bootstrap
  2. {
  3.     public static function main($argv, ParserInterface $parser, PayCalculator $calculator)
  4.     {
  5.         $payCollection = new PayCollection();
  6.         $data = $parser->parse($argv[1]);
  7.  
  8.         foreach ($data as $one)
  9.         {
  10.             if (self::isPay($one[1])) {
  11.                 $payCollection->add(new Pay($one[9], $one[8]));
  12.             }
  13.         }
  14.  
  15.         $result = $calculator->sumSameCurrencies($payCollection);
  16.  
  17.         echo self::render($result);
  18.     }
  19.    
  20.     public static function render(array $result): string
  21.     {
  22.         $output = "Totals\n";
  23.         foreach ($result as $one)
  24.         {
  25.             $output .= $one->getCurrencyName() . ' ' . number_format ( (float) $one->getAmount() , $decimals = 2 , $dec_point = "." , $thousands_sep = "," )  . "\n";
  26.         }
  27.  
  28.         return $output;
  29.     }
  30.  
  31.     private static function isPay(string $string): bool
  32.     {
  33.         if (strpos($string,'PAY') !== false) {
  34.             return true;
  35.         }
  36.         else {
  37.             return false;
  38.         }
  39.     }
  40. }
  41.  
  42. class CsvParser implements ParserInterface
  43. {
  44.     public function parse(string $filePath): array
  45.     {
  46.         $csvFile = file($filePath);
  47.         $data = [];
  48.         foreach ($csvFile as $line) {
  49.             $data[] = str_getcsv($line);
  50.         }
  51.  
  52.         return $data;
  53.     }
  54.  
  55. }
  56.  
  57. interface ParserInterface
  58. {
  59.     public function parse(string $filePath): array;
  60. }
  61.  
  62. class ParserFactory
  63. {
  64.     public function getParserForFormat(string $format)
  65.     {
  66.         switch ($format)
  67.         {
  68.             case 'csv':
  69.                 return new CsvParser();
  70.                 break;
  71.         }
  72.     }
  73.    
  74. }
  75.  
  76. class Pay
  77. {
  78.     private $currencyName;
  79.  
  80.     private $amount;
  81.  
  82.     public function __construct(string $currencyName, string $amount)
  83.     {
  84.         $this->currencyName = $currencyName;
  85.         $this->amount = $amount;
  86.     }
  87.  
  88.     public function getAmount(): string
  89.     {
  90.         return $this->amount;
  91.     }
  92.  
  93.     public function getCurrencyName(): string
  94.     {
  95.         return $this->currencyName;
  96.     }
  97.  
  98.     public function setAmount(string $amount): void
  99.     {
  100.         $this->amount = $amount;
  101.     }
  102. }
  103.  
  104. class PayCalculator
  105. {
  106.     private $currencies = [];
  107.  
  108.     public function sumSameCurrencies(PayCollection $payCollection): array
  109.     {
  110.         $pays = $payCollection->all();
  111.  
  112.         $checkedElements = [];
  113.         foreach ($pays as $key => $value)
  114.         {
  115.             foreach ($pays as $_key => $_value)
  116.             {
  117.                 if ($key === $_key) {
  118.                     $checkedElements[] = $key;
  119.                     continue;
  120.                 }
  121.  
  122.                 if(in_array($key, $checkedElements)) {
  123.                     continue;
  124.                 }
  125.  
  126.                 if ($_value->getCurrencyName() === $value->getCurrencyName()) {
  127.                     $sum = $_value->getAmount() + $value->getAmount();
  128.                     $value->setAmount($sum);
  129.                     unset($pays[$_key]);
  130.                 }
  131.             }
  132.         }
  133.  
  134.         return $pays;
  135.     }
  136. }
  137.  
  138. class PayCollection
  139. {
  140.     private $items = [];
  141.  
  142.     public function add(Pay $pay): void
  143.     {
  144.         $this->items[] = $pay;
  145.     }
  146.  
  147.     public function all(): array
  148.     {
  149.         return $this->items;
  150.     }
  151. }
  152.  
  153. $fileObject = new SplFileInfo($argv[1]);
  154. $fileExtension = $fileObject->getExtension();
  155.  
  156. $parser = (new ParserFactory())->getParserForFormat($fileExtension);
  157. $payCalculator = new PayCalculator();
  158. Bootstrap::main($argv, $parser, $payCalculator);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement