Created
November 1, 2012 18:20
-
-
Save Kedrigern/3995525 to your computer and use it in GitHub Desktop.
Náčrt vypořádání se s různými měnami a s operacemi vzniklými z toho
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 | |
| /** | |
| * Author: Ondřej Profant, 2012 | |
| */ | |
| namespace finance; | |
| /** | |
| * Náhrada za výčtový typ. | |
| * Definuje konstanty pro jednotlivé měny. | |
| * Třídu nelze dědit ani konstruovat, čili je zcela statická. | |
| */ | |
| final class Currency { | |
| const Kc = 0; | |
| const Eur = 1; | |
| const USD = 2; | |
| const DefaultCurrency = 0; // $this::Kc | |
| /** | |
| * Privátní konstruktor brání vytvoření instance třídy | |
| */ | |
| private function __construct() {} | |
| public static function convert() | |
| { | |
| //TODO | |
| } | |
| } | |
| /** | |
| * | |
| * | |
| */ | |
| class Price { | |
| protected $Kc; | |
| protected $Eur; | |
| protected $USD; | |
| public function __construct( $amount = 0 ) | |
| { | |
| $this->Discart(); | |
| $this->Add( $amount ); | |
| } | |
| /** | |
| ** Discart each value in this instance | |
| **/ | |
| public function Discart() | |
| { | |
| $this->Kc = 0; | |
| $this->Eur = 0; | |
| $this->USD = 0; | |
| } | |
| /** | |
| * Add amount in any currency | |
| * @param amount | |
| * @param currency | |
| */ | |
| public function Add( $amount , $currency = Currency::DefaultCurrency ) | |
| { | |
| switch( $currency ) { | |
| case Currency::Kc : | |
| $this->Kc += $amount; | |
| break; | |
| case Currency::Eur : | |
| $this->Eur += $amount; | |
| break; | |
| case Currency::USD : | |
| $this->USD += $amount; | |
| break; | |
| default: | |
| throw new Exception(); | |
| break; | |
| } | |
| } | |
| /** | |
| * @return total amount in given currency (with the smallest error in the convert) | |
| */ | |
| public function Total( $currency = Currency::DefaultCurrency ) | |
| { | |
| // convert and sum | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment