Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Backpack | |
100.00% |
8 / 8 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCost | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMaxCost | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getItems | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addItem | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Algorithm\Knapsack |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Algorithm\Knapsack; |
| 16 | |
| 17 | /** |
| 18 | * Backpack for the Knapsack problem |
| 19 | * |
| 20 | * @package phpOMS\Algorithm\Knapsack |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Backpack implements BackpackInterface |
| 26 | { |
| 27 | /** |
| 28 | * Maximum amount of cost this backpack can hold |
| 29 | * |
| 30 | * @var float |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | private float $maxCost = 0.0; |
| 34 | |
| 35 | /** |
| 36 | * Current value |
| 37 | * |
| 38 | * @var float |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | private float $value = 0.0; |
| 42 | |
| 43 | /** |
| 44 | * Current cost |
| 45 | * |
| 46 | * @var float |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | private float $cost = 0.0; |
| 50 | |
| 51 | /** |
| 52 | * Items inside the backpack |
| 53 | * |
| 54 | * @var array<int, array{item:ItemInterface, quantity:int|float}> |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | private array $items = []; |
| 58 | |
| 59 | /** |
| 60 | * Constructor. |
| 61 | * |
| 62 | * @param float $maxCost Maximum amount of costs the backpack can hold |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | public function __construct(float $maxCost) |
| 67 | { |
| 68 | $this->maxCost = $maxCost; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function getValue() : float |
| 75 | { |
| 76 | return $this->value; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * {@inheritdoc} |
| 81 | */ |
| 82 | public function getCost() : float |
| 83 | { |
| 84 | return $this->cost; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | public function getMaxCost() : float |
| 91 | { |
| 92 | return $this->maxCost; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * {@inheritdoc} |
| 97 | */ |
| 98 | public function getItems() : array |
| 99 | { |
| 100 | return $this->items; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * {@inheritdoc} |
| 105 | */ |
| 106 | public function addItem(ItemInterface $item, int | float $quantity = 1) : void |
| 107 | { |
| 108 | $this->items[] = ['item' => $item, 'quantity' => $quantity]; |
| 109 | $this->value += $item->getValue() * $quantity; |
| 110 | $this->cost += $item->getCost() * $quantity; |
| 111 | } |
| 112 | } |