Last active
November 15, 2021 10:54
-
-
Save DaveDevYT/26c9275a6efab733e5b70c5847baebcb to your computer and use it in GitHub Desktop.
Conditions and if else statements Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$currentKeys = 2; | |
$maxKeyCapacity = 2; | |
$currentPotions = 4; | |
$maxPotionCapacity = 5; | |
$currentInventoryCapacity = $currentKeys + $currentPotions; | |
$maxInventoryCapacity = 10; | |
// New item incoming | |
$newItemType = 'potion'; | |
// Write your inventory code here | |
if($currentInventoryCapacity < $maxInventoryCapacity) | |
{ | |
switch($newItemType) | |
{ | |
case 'potion': | |
{ | |
if($currentPotions < $maxPotionCapacity) | |
{ | |
$currentPotions++; | |
$currentInventoryCapacity++; | |
echo 'potion added to inventory<br/>'; | |
} | |
else | |
{ | |
echo 'You can not take more potions<br/>'; | |
} | |
break; | |
} | |
case 'key': | |
{ | |
if($currentPotions < $maxPotionCapacity) | |
{ | |
$currentPotions++; | |
$currentInventoryCapacity++; | |
echo 'key added to inventory<br/>'; | |
} | |
else | |
{ | |
echo 'You can not take more keys<br/>'; | |
} | |
break; | |
} | |
default: | |
{ | |
echo 'item added to inventory<br/>'; | |
$currentInventoryCapacity++; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
echo 'The inventory is full<br/>'; | |
} | |
// New item incoming | |
$newItemType = 'key'; | |
// Copy the previous item inventory code here aswell | |
if($currentInventoryCapacity < $maxInventoryCapacity) | |
{ | |
switch($newItemType) | |
{ | |
case 'potion': | |
{ | |
if($currentPotions < $maxPotionCapacity) | |
{ | |
$currentPotions++; | |
$currentInventoryCapacity++; | |
echo 'potion added to inventory<br/>'; | |
} | |
else | |
{ | |
echo 'You can not take more potions<br/>'; | |
} | |
break; | |
} | |
case 'key': | |
{ | |
if($currentKeys < $maxKeyCapacity) | |
{ | |
$currentKeys++; | |
$currentInventoryCapacity++; | |
echo 'key added to inventory<br/>'; | |
} | |
else | |
{ | |
echo 'You can not take more keys<br/>'; | |
} | |
break; | |
} | |
default: | |
{ | |
echo 'item added to inventory<br/>'; | |
$currentInventoryCapacity++; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
echo 'The inventory is full<br/>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment