Created
August 25, 2014 13:52
-
-
Save yitsushi/7ab0f8ed792d6840fd18 to your computer and use it in GitHub Desktop.
Inheritance Example
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 | |
define('DEBUG', true); | |
function debug($message) { | |
if (DEBUG) { | |
echo " -D- ", $message, "\n"; | |
} | |
} | |
/** | |
* Money Interface | |
*/ | |
interface IMoney { | |
const CURRENCY = null; | |
public function __construct($value); | |
public function getValue(); | |
public function setValue($value); | |
public function toString(); | |
} | |
/** | |
* Pocket Interface | |
*/ | |
interface IPocket { | |
public function __construct($initialValue, $baseCurrency); | |
public function getCurrentState(); | |
public function addTransaction(Money $value); | |
public function listTransactions(); | |
public function toString(); | |
} | |
/** | |
* Base Money class | |
*/ | |
abstract class Money implements IMoney { | |
protected $value = 0; | |
public function __construct($value = 0) { | |
$this->value = round((float)$value, 2); | |
} | |
public function getValue() { | |
return $this->value; | |
} | |
public function setValue($value) { | |
$this->value = $value; | |
} | |
} | |
/** | |
* Pocket class | |
*/ | |
class Pocket { | |
protected $value = 0; | |
protected $transactions = array(); | |
public function __construct($initialValue, $baseCurrency = "$") { | |
$this->value = round((float)$initialValue, 2); | |
$this->baseCurrency = $baseCurrency; | |
} | |
public function getCurrentState() { | |
return $this->value; | |
} | |
public function addTransaction(Money $value) { | |
$this->transactions[] = $value; | |
$this->value += $value->getValue() * $this->exchange($value::CURRENCY, $this->baseCurrency); | |
debug("Transaction added with value of (" . $value->toString() . ")."); | |
} | |
public function listTransactions() { | |
return $this->transactions; | |
} | |
public function toString($in = "") { | |
if ($in == "") { | |
$in = $this->baseCurrency; | |
} | |
$converted = $this->value * $this->exchange($this->baseCurrency, $in); | |
if ($in == "HUF") { | |
$value = new HungarianForint($converted); | |
} else { | |
$value = new USDollar($converted); | |
} | |
return $value->toString() . " with " . count($this->transactions) . " transaction" . (count($this->transactions) > 1 ? "s" : ""); | |
} | |
protected function exchange($from, $to) { | |
switch($from . "_" . $to) { | |
case "HUF_\$": return 0.0042; break; | |
case "\$_HUF": return 237.11; break; | |
default: return 1; | |
} | |
} | |
} | |
/** | |
* Currencies / Money | |
*/ | |
class HungarianForint extends Money { | |
const CURRENCY = "HUF"; | |
public function __construct($value = 0) { | |
$this->value = (int)$value; | |
} | |
public function toString() { | |
return number_format($this->value, 0, ",", " ") . " " . self::CURRENCY; | |
} | |
} | |
class USDollar extends Money { | |
const CURRENCY = "$"; | |
public function toString() { | |
$sign = ($this->value < 0 ? "-" : ""); | |
return $sign . self::CURRENCY . number_format(abs($this->value), 2); | |
} | |
} | |
$pocket = new Pocket(0, "HUF"); | |
echo $pocket->toString(), "\n"; | |
echo $pocket->toString(USDollar::CURRENCY), "\n"; | |
$pocket->addTransaction(new HungarianForint(1200)); | |
echo $pocket->toString(), "\n"; | |
echo $pocket->toString(USDollar::CURRENCY), "\n"; | |
$pocket->addTransaction(new USDollar(-15.50)); | |
echo $pocket->toString(), "\n"; | |
echo $pocket->toString(USDollar::CURRENCY), "\n"; | |
$pocket->addTransaction(new USDollar(17.20)); | |
echo $pocket->toString(), "\n"; | |
echo $pocket->toString(USDollar::CURRENCY), "\n"; | |
echo "Transations:\n"; | |
foreach($pocket->listTransactions() as $money) { | |
echo " ", $money->toString(), "\n"; | |
} |
Author
yitsushi
commented
Aug 25, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment