MartinGeorgiev

Vending Machine

May 26th, 2022 (edited)
266
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $input = readline();
  3. $money = 0;
  4. while ($input !== "Start") {
  5.     $coin = $input == 1 || $input == 2 || $input == 0.1 || $input == 0.2 || $input == 0.5 ? floatval($input) : 0;
  6.     $output = $coin === 0 ? "Cannot accept " . $input . PHP_EOL : '';
  7.     echo $output;
  8.     $money += $coin;
  9.     $input = readline();
  10. }
  11. $input = readline();
  12. while ($input !== "End") {
  13.     $spending = round($input === "Nuts" ? 2 : ($input === "Water" ? 0.7 : ($input == "Crisps" ? 1.5 : ($input === "Soda" ? 0.8 : ($input === "Coke" ? 1 : 0)))), 2);
  14.     $output = $spending == 0 ? "Invalid product\n" : ($spending > round($money, 2) ? "Sorry, not enough money\n" : "Purchased " . strtolower($input) . PHP_EOL);
  15.     echo $output;
  16.     $money -= $spending > round($money, 2) ? 0 : round($spending, 2);
  17.     $input = readline();
  18. }
  19. echo "Change: " . number_format($money, 2, '.', '');
  20.  
RAW Paste Data Copied