Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created August 25, 2014 13:52
Show Gist options
  • Save yitsushi/7ab0f8ed792d6840fd18 to your computer and use it in GitHub Desktop.
Save yitsushi/7ab0f8ed792d6840fd18 to your computer and use it in GitHub Desktop.
Inheritance Example
<?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";
}
@yitsushi
Copy link
Author

0 HUF with 0 transaction
$0.00 with 0 transaction
 -D- Transaction added with value of (1 200 HUF).
1 200 HUF with 1 transaction
$5.04 with 1 transaction
 -D- Transaction added with value of (-$15.50).
-2 475 HUF with 2 transactions
-$10.40 with 2 transactions
 -D- Transaction added with value of ($17.20).
1 603 HUF with 3 transactions
$6.73 with 3 transactions
Transations:
  1 200 HUF
  -$15.50
  $17.20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment