Advertisement
Mihail_Atnsv

vendingMachine

Sep 30th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2. $input = readline();
  3. $totalMoney = 0;
  4.  
  5. //0.1, 0.2, 0.5, 1, and 2 coins
  6. //"Nuts", "Water", "Crisps", "Soda", "Coke".
  7. //The prices are: 2.0, 0.7, 1.5, 0.8, 1.0
  8.  
  9. while ($input != "Start") {
  10.     $inputCoins = floatval($input);
  11.     if ($inputCoins == 0.1 || $inputCoins == 0.2 || $inputCoins == 0.5 || $inputCoins == 1 || $inputCoins == 2) {
  12.         $totalMoney += $inputCoins;
  13.     } else {
  14.         echo "Cannot accept $inputCoins" . PHP_EOL;
  15.     }
  16.     $input = readline();
  17. }
  18.  
  19. $input = readline();
  20.  
  21. while ($input != "End") {
  22.     if ($input == "Nuts") {
  23.         if ($totalMoney >= 2) {
  24.             echo "Purchased nuts" . PHP_EOL;
  25.             $totalMoney -= 2;
  26.         } else {
  27.             echo "Sorry, not enough money" . PHP_EOL;
  28.         }
  29.     } else if ($input == "Water") {
  30.         if ($totalMoney >= 0.7) {
  31.             echo "Purchased water" . PHP_EOL;
  32.             $totalMoney -= 0.7;
  33.         } else {
  34.             echo "Sorry, not enough money" . PHP_EOL;
  35.         }
  36.     } else if ($input == "Crisps") {
  37.         if ($totalMoney >= 1.5) {
  38.             echo "Purchased crisps" . PHP_EOL;
  39.             $totalMoney -= 1.5;
  40.         } else {
  41.             echo "Sorry, not enough money" . PHP_EOL;
  42.         }
  43.     } else if ($input == "Soda") {
  44.         if ($totalMoney >= 0.8) {
  45.             echo "Purchased soda" . PHP_EOL;
  46.             $totalMoney -= 0.8;
  47.         } else {
  48.             echo "Sorry, not enough money" . PHP_EOL;
  49.         }
  50.     } else if ($input == "Coke") {
  51.         if ($totalMoney >= 1) {
  52.             echo "Purchased coke" . PHP_EOL;
  53.             $totalMoney -= 1;
  54.         } else {
  55.             echo "Sorry, not enough money" . PHP_EOL;
  56.         }
  57.     } else {
  58.         echo "Invalid product" . PHP_EOL;
  59.     }
  60.     $input = readline();
  61. }
  62. echo "Change: " . number_format($totalMoney, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement