Por xdtibia16, 12 de jan. de 2012 em Programação Web
info
Grupo: Visconde
Registrado: 29/06/08
Posts: 354
Reputação: +10
Char no Tibia: Sir Aluxes
Começei a estudar PHP (Orientação a Objetos), fiz este código só para treinar e estou compartilhando para a comunidade XTibia.
/class/Computer.class.php
<?php define("HARDWARE_QUANTITY", 2); class Computer { public $computerMemory; public $hardwareMemory; public $memoryName; public $computerProcessor; public $hardwareProcessor; public $processorName; function __construct($computerMemory, $hardwareMemory, $computerProcessor, $hardwareProcessor, $hardwareName) { $this->computerMemory = $computerMemory; $this->hardwareMemory = $hardwareMemory; $this->computerProcessor = $computerProcessor; $this->hardwareProcessor = $hardwareProcessor; $this->hardwareName = $hardwareName; } function showHardwareInformation($hardwareType, $computerArray, $arrayPosition) { if ($hardwareType <= HARDWARE_QUANTITY - 1 and $hardwareType >= 0) { echo " <b>". $computerArray->hardwareName[0]->hardwareName[$hardwareType] ." ". ($arrayPosition + 1) ."</b>: "; if ($hardwareType == 0) { echo $computerArray->computerMemory ."GB.<br />\n"; } else { echo $computerArray->computerProcessor ."Ghz.<br />\n"; } } else { return true; } } function upgradeHardware($hardwareType, $hardwareUpgrade) { if ($hardwareUpgrade > 0) { switch ($hardwareType) { case $this->hardwareMemory: $this->computerMemory += $hardwareUpgrade; return $hardwareType; case $this->hardwareProcessor: $this->computerProcessor += $hardwareUpgrade; return $hardwareType; default: return false; } return false; } else if ($hardwareType == $this->hardwareMemory) { return 0x11; } else if ($hardwareType == $this->hardwareProcessor) { return 0x12; } } } ?>
/class/Hardware.class.php
<?php final class Hardware { public $hardwareName; function __construct($hardwareName) { $this->hardwareName = $hardwareName; } } ?>
index.php
<?php include_once "/class/Computer.class.php"; include_once "/class/Hardware.class.php"; $hardwareName = new Hardware(array("Memória", "Processador")); $computerArray = array(); $memoryUpgrade = 4; $processorUpgrade = 1; $hardwareType = 0; array_push($computerArray, new Computer(4, 0, 2, 1, array($hardwareName, "a", "o"))); array_push($computerArray, new Computer(2, 0, 1, 1, array($hardwareName, "a", "o"))); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Computador</title> <style type="text/css"> body { background: #d4dded; } </style> </head> <body> <font face="Arial"> <? foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareMemory, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareProcessor, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } echo " <br />\n"; foreach ($computerArray as $arrayKey => $arrayValue) { if (($hardwareType = $arrayValue->upgradeHardware($arrayValue->hardwareMemory, $memoryUpgrade)) !== false and $hardwareType != 0x11 and $hardwareType != 0x12) { echo " ". strtoupper($arrayValue->hardwareName[$hardwareType + 1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[$hardwareType]. " ". ($arrayKey + 1) ."</b> foi melhorada de ". ($arrayValue->computerMemory - $memoryUpgrade) ."GB para ". $arrayValue->computerMemory ."GB."; } else if ($hardwareType == 0x11) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[0] . " ". ($arrayKey + 1) ."</b>."; } else if ($hardwareType == 0x12) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[2]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[1] . " ". ($arrayKey + 1) ."</b>."; } else { echo " Não foi possível melhorar <b>indefinido</b>."; } echo "<br />\n"; } echo " <br />\n"; foreach ($computerArray as $arrayKey => $arrayValue) { if (($hardwareType = $arrayValue->upgradeHardware($arrayValue->hardwareProcessor, $processorUpgrade)) !== false and $hardwareType != 0x11 and $hardwareType != 0x12) { echo " ". strtoupper($arrayValue->hardwareName[$hardwareType + 1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[$hardwareType]. " ". ($arrayKey + 1) ."</b> foi melhorado de ". ($arrayValue->computerProcessor - $processorUpgrade) ."Ghz para ". $arrayValue->computerProcessor ."Ghz."; } else if ($hardwareType == 0x11) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[0] . " ". ($arrayKey + 1) ."</b>."; } else if ($hardwareType == 0x12) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[2]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[1] . " ". ($arrayKey + 1) ."</b>."; } else { echo " Não foi possível melhorar <b>indefinido</b>."; } if ($arrayKey == count($arrayValue)) { echo "<br />"; } else { echo "<br />\n"; } } echo "\n <br />\n"; foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareMemory, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareProcessor, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } ?> </font> </body> </html>
Editado Janeiro 13 por Aluxes
info
Grupo: Visconde
Registrado: 29/06/08
Posts: 354
Reputação: +10
Char no Tibia: Sir Aluxes
Começei a estudar PHP (Orientação a Objetos), fiz este código só para treinar e estou compartilhando para a comunidade XTibia.
/class/Computer.class.php
<?php define("HARDWARE_QUANTITY", 2); class Computer { public $computerMemory; public $hardwareMemory; public $memoryName; public $computerProcessor; public $hardwareProcessor; public $processorName; function __construct($computerMemory, $hardwareMemory, $computerProcessor, $hardwareProcessor, $hardwareName) { $this->computerMemory = $computerMemory; $this->hardwareMemory = $hardwareMemory; $this->computerProcessor = $computerProcessor; $this->hardwareProcessor = $hardwareProcessor; $this->hardwareName = $hardwareName; } function showHardwareInformation($hardwareType, $computerArray, $arrayPosition) { if ($hardwareType <= HARDWARE_QUANTITY - 1 and $hardwareType >= 0) { echo " <b>". $computerArray->hardwareName[0]->hardwareName[$hardwareType] ." ". ($arrayPosition + 1) ."</b>: "; if ($hardwareType == 0) { echo $computerArray->computerMemory ."GB.<br />\n"; } else { echo $computerArray->computerProcessor ."Ghz.<br />\n"; } } else { return true; } } function upgradeHardware($hardwareType, $hardwareUpgrade) { if ($hardwareUpgrade > 0) { switch ($hardwareType) { case $this->hardwareMemory: $this->computerMemory += $hardwareUpgrade; return $hardwareType; case $this->hardwareProcessor: $this->computerProcessor += $hardwareUpgrade; return $hardwareType; default: return false; } return false; } else if ($hardwareType == $this->hardwareMemory) { return 0x11; } else if ($hardwareType == $this->hardwareProcessor) { return 0x12; } } } ?>/class/Hardware.class.php
<?php final class Hardware { public $hardwareName; function __construct($hardwareName) { $this->hardwareName = $hardwareName; } } ?>index.php
<?php include_once "/class/Computer.class.php"; include_once "/class/Hardware.class.php"; $hardwareName = new Hardware(array("Memória", "Processador")); $computerArray = array(); $memoryUpgrade = 4; $processorUpgrade = 1; $hardwareType = 0; array_push($computerArray, new Computer(4, 0, 2, 1, array($hardwareName, "a", "o"))); array_push($computerArray, new Computer(2, 0, 1, 1, array($hardwareName, "a", "o"))); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Computador</title> <style type="text/css"> body { background: #d4dded; } </style> </head> <body> <font face="Arial"> <? foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareMemory, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareProcessor, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } echo " <br />\n"; foreach ($computerArray as $arrayKey => $arrayValue) { if (($hardwareType = $arrayValue->upgradeHardware($arrayValue->hardwareMemory, $memoryUpgrade)) !== false and $hardwareType != 0x11 and $hardwareType != 0x12) { echo " ". strtoupper($arrayValue->hardwareName[$hardwareType + 1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[$hardwareType]. " ". ($arrayKey + 1) ."</b> foi melhorada de ". ($arrayValue->computerMemory - $memoryUpgrade) ."GB para ". $arrayValue->computerMemory ."GB."; } else if ($hardwareType == 0x11) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[0] . " ". ($arrayKey + 1) ."</b>."; } else if ($hardwareType == 0x12) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[2]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[1] . " ". ($arrayKey + 1) ."</b>."; } else { echo " Não foi possível melhorar <b>indefinido</b>."; } echo "<br />\n"; } echo " <br />\n"; foreach ($computerArray as $arrayKey => $arrayValue) { if (($hardwareType = $arrayValue->upgradeHardware($arrayValue->hardwareProcessor, $processorUpgrade)) !== false and $hardwareType != 0x11 and $hardwareType != 0x12) { echo " ". strtoupper($arrayValue->hardwareName[$hardwareType + 1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[$hardwareType]. " ". ($arrayKey + 1) ."</b> foi melhorado de ". ($arrayValue->computerProcessor - $processorUpgrade) ."Ghz para ". $arrayValue->computerProcessor ."Ghz."; } else if ($hardwareType == 0x11) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[1]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[0] . " ". ($arrayKey + 1) ."</b>."; } else if ($hardwareType == 0x12) { echo " Não foi possível melhorar ". strtolower($arrayValue->hardwareName[2]) ." <b>". $arrayValue->hardwareName[0]->hardwareName[1] . " ". ($arrayKey + 1) ."</b>."; } else { echo " Não foi possível melhorar <b>indefinido</b>."; } if ($arrayKey == count($arrayValue)) { echo "<br />"; } else { echo "<br />\n"; } } echo "\n <br />\n"; foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareMemory, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } foreach ($computerArray as $arrayKey => $arrayValue) { $commandError = $arrayValue->showHardwareInformation($arrayValue->hardwareProcessor, $arrayValue, $arrayKey); } if ($commandError) { echo " Não foi possível pegar a informação do hardware.<br />\n"; } ?> </font> </body> </html>Editado Janeiro 13 por Aluxes