Skip to content

Adding new un-finished homework (Narvesen) #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions homeworks/09-2020/narvesen/Narvesen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

class Narvesen
{
private string $name;
private array $products = [];

public function __construct(string $name, array $products)
{
$this->name = $name;
$this->products = $products;
}

public function getAllProducts(): array
{
return $this->products;
}
}


29 changes: 29 additions & 0 deletions homeworks/09-2020/narvesen/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

class Person
{
private string $name;
private int $money;

public function __construct(string $name, int $money)
{
$this->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;
}

}
33 changes: 33 additions & 0 deletions homeworks/09-2020/narvesen/PersonProducts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
class PersonProducts
{
private string $path;

public function __construct($path)
{
$this->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);
}
}
10 changes: 10 additions & 0 deletions homeworks/09-2020/narvesen/PriceFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

class PriceFormatter
{
public static function price(int $price): string
{
return '€' . number_format($price / 100, 2);
}

}
33 changes: 33 additions & 0 deletions homeworks/09-2020/narvesen/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

class Product
{
private string $name;
private int $quantity;
private int $price;

function __construct(string $name, int $quantity, int $price)
{
$this->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;
}

}
31 changes: 31 additions & 0 deletions homeworks/09-2020/narvesen/ProductCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

class ProductCollection
{

private string $path;

public function __construct($path)
{
$this->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;
}
}

9 changes: 9 additions & 0 deletions homeworks/09-2020/narvesen/ProductFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

class ProductFormatter
{
public static function name($name)
{
return '<' . $name . '> ' . '::';
}
}
11 changes: 11 additions & 0 deletions homeworks/09-2020/narvesen/QuantityFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

class QuantityFormatter
{
public static function quantity($quantity): string
{

return 'Available pieces: [' . $quantity . '] ' . '::' ;
}

}
55 changes: 55 additions & 0 deletions homeworks/09-2020/narvesen/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

require_once 'Product.php';
require_once 'ProductCollection.php';
require_once 'Narvesen.php';
require_once 'ProductFormatter.php';
require_once 'PriceFormatter.php';
require_once 'QuantityFormatter.php';
require_once 'Person.php';
require_once 'PersonProducts.php';

$person = new Person('Chris', 1500);
$storage = new ProductCollection('./products.txt');
$narvesen = new Narvesen('Narvesen Saldus', $storage->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);


2 changes: 2 additions & 0 deletions homeworks/09-2020/narvesen/products.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
HotDogs;0;150;|
Burgers;6;200;|
Empty file.