diff --git a/homeworks/09-2020/narvesen/Narvesen.php b/homeworks/09-2020/narvesen/Narvesen.php new file mode 100644 index 0000000..3ffdcdb --- /dev/null +++ b/homeworks/09-2020/narvesen/Narvesen.php @@ -0,0 +1,20 @@ +name = $name; + $this->products = $products; + } + + public function getAllProducts(): array + { + return $this->products; + } +} + + diff --git a/homeworks/09-2020/narvesen/Person.php b/homeworks/09-2020/narvesen/Person.php new file mode 100644 index 0000000..48969a5 --- /dev/null +++ b/homeworks/09-2020/narvesen/Person.php @@ -0,0 +1,29 @@ +name = $name; + $this->money = $money; + } + + public function getName(): string + { + return $this->name; + } + + public function getMoney(): int + { + return $this->money; + } + + public function removeMoney(int $price): int + { + $this->money -= $price; + } + +} diff --git a/homeworks/09-2020/narvesen/PersonProducts.php b/homeworks/09-2020/narvesen/PersonProducts.php new file mode 100644 index 0000000..26b0a2c --- /dev/null +++ b/homeworks/09-2020/narvesen/PersonProducts.php @@ -0,0 +1,33 @@ +path = $path; + } + + public function load(): array + { + $content = file_get_contents($this->path); + $rows = array_filter((array)explode('|', $content)); + $products = []; + + foreach ($rows as $row) { + $productData = (array)explode(';', $row); + $products[] = new Product( + trim($productData[0]), + (int)$productData[1], + (int)$productData[2], + ); + } + + return $products; + } + + public function productStore(array $product): void + { + file_put_contents('./products_bought.txt', $product); + } +} \ No newline at end of file diff --git a/homeworks/09-2020/narvesen/PriceFormatter.php b/homeworks/09-2020/narvesen/PriceFormatter.php new file mode 100644 index 0000000..62fb05a --- /dev/null +++ b/homeworks/09-2020/narvesen/PriceFormatter.php @@ -0,0 +1,10 @@ +name = $name; + $this->quantity = $quantity; + $this->price = $price; + } + + public function getName(): string + { + return $this->name; + } + + + public function getQuantity(): int + { + return $this->quantity; + } + + + public function getPrice(): int + { + return $this->price; + } + +} diff --git a/homeworks/09-2020/narvesen/ProductCollection.php b/homeworks/09-2020/narvesen/ProductCollection.php new file mode 100644 index 0000000..f9fb680 --- /dev/null +++ b/homeworks/09-2020/narvesen/ProductCollection.php @@ -0,0 +1,31 @@ +path = $path; + } + + public function load(): array + { + $content = file_get_contents($this->path); + $rows = array_filter((array)explode('|', $content)); + $products = []; + + foreach ($rows as $row) { + $productData = (array)explode(';', $row); + $products[] = new Product( + trim($productData[0]), + (int)$productData[1], + (int)$productData[2], + ); + } + + return $products; + } +} + diff --git a/homeworks/09-2020/narvesen/ProductFormatter.php b/homeworks/09-2020/narvesen/ProductFormatter.php new file mode 100644 index 0000000..1c13efb --- /dev/null +++ b/homeworks/09-2020/narvesen/ProductFormatter.php @@ -0,0 +1,9 @@ + ' . '::'; + } +} \ No newline at end of file diff --git a/homeworks/09-2020/narvesen/QuantityFormatter.php b/homeworks/09-2020/narvesen/QuantityFormatter.php new file mode 100644 index 0000000..1e8def1 --- /dev/null +++ b/homeworks/09-2020/narvesen/QuantityFormatter.php @@ -0,0 +1,11 @@ +load()); + +foreach ($narvesen->getAllProducts() as $product) { + echo ProductFormatter::name($product->getName()) . ' '; + echo QuantityFormatter::quantity($product->getQuantity()) . ' '; + echo PriceFormatter::price($product->getPrice()) . ' '; + echo "\n"; +} + +echo "\n"; +echo 'Customer: ' . $person->getName() . ' | Money: ' . PriceFormatter::price($person->getMoney()) . PHP_EOL; + +$product = $narvesen->getAllProducts()[1]; +$cashback = $person->getMoney() - $product->getPrice(); + + +do { + + if ($person->getMoney() <= $product->getPrice()) { + echo 'Sorry, you dont have enough pesos for ' . $product->getName() . ' :: ' . PriceFormatter::price($product->getPrice()) . PHP_EOL; + + } elseif ($person->getMoney() >= $product->getPrice()) { + echo 'You take: Product - ' . $product->getName() . ' ::' . ' Price: ' . PriceFormatter::price($product->getPrice()) . PHP_EOL; + } else { + break; + } + + if ($product->getQuantity() <= 0) { + echo 'Sorry, your preferred product are out of stock!' . PHP_EOL; + break; + } else { + echo 'The product is available!' . PHP_EOL; + + } + + if ($person->getMoney() >= $product->getPrice()) { + echo 'Your cashback: ' . PriceFormatter::price($cashback) . PHP_EOL; + break; + } +} while ($product->getQuantity() >= 0); + + diff --git a/homeworks/09-2020/narvesen/products.txt b/homeworks/09-2020/narvesen/products.txt new file mode 100644 index 0000000..d351567 --- /dev/null +++ b/homeworks/09-2020/narvesen/products.txt @@ -0,0 +1,2 @@ +HotDogs;0;150;| +Burgers;6;200;| \ No newline at end of file diff --git a/homeworks/09-2020/narvesen/products_bought.txt b/homeworks/09-2020/narvesen/products_bought.txt new file mode 100644 index 0000000..e69de29