예제 #1
0
 /**
  * @covers \CoinOptimizer\CoinOptimizer
  */
 public function testValuesSortedNone()
 {
     $coinOptimizer = new CoinOptimizer(USCoinCollectionFactory::create());
     $optimizedCoins = $coinOptimizer->optimize(0.0);
     $expectedQuantities = ['silver-dollar' => 0, 'half-dollar' => 0, 'quarter' => 0, 'dime' => 0, 'nickel' => 0, 'penny' => 0];
     $this->assertEquals(0.0, $optimizedCoins->getSum());
     $this->assertEquals($expectedQuantities, $optimizedCoins->getQuantities());
 }
예제 #2
0
 * Coin Optimization
 *
 * Write a class or function that will output the optimal coin combination (fewest number of coins) that fulfills a given dollar value.
 *
 * Usage: php optimize.php [input] [currency]
 * Example: php optimize.php 3.67
 *
 * @author Konr Ness <*****@*****.**>
 */
require_once __DIR__ . '/vendor/autoload.php';
use CoinOptimizer\CoinOptimizer;
use CoinOptimizer\EuroCoinCollectionFactory;
use CoinOptimizer\USCoinCollectionFactory;
$input = (double) isset($argv[1]) ? $argv[1] : 0;
$currency = isset($argv[2]) ? $argv[2] : "usd";
switch ($currency) {
    case "usd":
        $coinCollection = USCoinCollectionFactory::create();
        break;
    case "euro":
        $coinCollection = EuroCoinCollectionFactory::create();
        break;
    default:
        echo "Invalid currency. Available options are 'usd' or 'euro'" . PHP_EOL;
        exit(1);
}
$coinOptimizer = new CoinOptimizer($coinCollection);
$coins = $coinOptimizer->optimize($input);
foreach ($coins as $coin) {
    echo $coin->getName() . ": " . $coins[$coin] . PHP_EOL;
}