MartinGeorgiev

03. Gaming Store

Jan 31st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2. $startMoney = floatval(readline());
  3. $currentMoney = $startMoney;
  4.  
  5. $game = readline();
  6. $outOfMoney = false;
  7.  
  8. while ($game !== ("Game Time")) {
  9.     $gamePrice = 0;
  10.     $isValid = true;
  11.     switch ($game) {
  12.         case "OutFall 4":
  13.             $gamePrice = 39.99;
  14.             break;
  15.         case "CS: OG":
  16.             $gamePrice = 15.99;
  17.             break;
  18.         case "Zplinter Zell":
  19.             $gamePrice = 19.99;
  20.             break;
  21.         case "Honored 2":
  22.             $gamePrice = 59.99;
  23.             break;
  24.         case "RoverWatch":
  25.             $gamePrice = 29.99;
  26.             break;
  27.         case "RoverWatch Origins Edition":
  28.             $gamePrice = 39.99;
  29.             break;
  30.         default:
  31.             $isValid = false;
  32.             echo "Not Found" . PHP_EOL;
  33.             break;
  34.     }
  35.     if ($isValid) {
  36.         if ($currentMoney - $gamePrice >= 0) {
  37.             $currentMoney -= $gamePrice;
  38.             echo "Bought $game" . PHP_EOL;
  39.  
  40.             if ($currentMoney == 0) {
  41.                 echo "Out of money!" . PHP_EOL;
  42.                 $outOfMoney = true;
  43.                 break;
  44.             }
  45.         } else {
  46.             echo "Too Expensive" . PHP_EOL;
  47.         }
  48.     }
  49.  
  50.     $game = readline();
  51. }
  52.  
  53. if (!$outOfMoney) {
  54.     $totalSpend = $startMoney - $currentMoney;
  55.     printf("Total spent: $%.2f. Remaining: $%.2f" . PHP_EOL, $totalSpend, $currentMoney);
  56. }
Add Comment
Please, Sign In to add comment