예제 #1
0
<body>
';
// Render Place Order page
echo '<form method="post">' . "<br>\n";
echo "Select items:<br>\n";
foreach ($itemList as $key => $item) {
    echo '<input type="checkbox" name="items[]" id="item_' . $key . '" value="' . $key . '"> <label for="item_' . $key . '">' . $item[0] . ' - $' . $item[1] . ' - ' . $item[2] . 'g</label>';
    echo "<br>\n";
}
echo "<br>\n";
echo "Select algorithm:<br>\n";
echo '<input type="radio" name="algo" checked value="' . PackageDistributor::ALGO_BEST_PRICE . '">Best Price<br>
<input type="radio" name="algo" value="' . PackageDistributor::ALGO_EVENLY_DISTRIBUTED . '">Evenly Distributed' . "<br>\n";
echo "<br>\n";
echo '<input type="submit" value="Place Order"><br>';
echo "</form><br>\n";
if (isset($_POST['items'])) {
    $algoType = $_POST['algo'];
    $items = $_POST['items'];
    $order = new Order($items, $itemList);
    $distributor = new PackageDistributor($algoType);
    $calc = new ShippingChargeCalculator();
    $renderer = new PackageRenderer($calc);
    $distributor->distributeItems($order, _MAX_PRICE_PER_PACKAGE_);
    echo "This order has following packages:<br>";
    echo $renderer->renderPackages($order->getPackages());
}
echo '
</body>
</html>
';
 public function testBestPriceAlgoDistributeItemsBasedExampleWithExtraItem()
 {
     $itemList = [['Item 1', 10, 200], ['Item 2', 100, 20], ['Item 3', 30, 300], ['Item 4', 20, 500], ['Item 6', 40, 10], ['Item 7', 200, 10], ['Item 55', 50, 100]];
     $order = new Order([0, 1, 2, 3, 4, 5, 6], $itemList);
     $dist = new PackageDistributor(PackageDistributor::ALGO_BEST_PRICE);
     $dist->distributeItems($order, 250);
     // Here it distributes items into 2 packages with lower price than before but not eveny distributed
     $this->assertEquals(250, $order->getPackages()[0]->getTotalPrice());
     $this->assertEquals(110, $order->getPackages()[0]->getTotalWeight());
     $this->assertEquals(200, $order->getPackages()[1]->getTotalPrice());
     $this->assertEquals(1030, $order->getPackages()[1]->getTotalWeight());
 }