4 class Merchant extends BaseFrameworkSystem {
6 private $merchantName = "Namenloser Händler";
8 // Preislite (Objekte wiedermal!)
9 private $priceList = null;
12 private $harborInstance = null;
15 private function __construct () {
16 // Call parent constructor
17 parent::constructor(__CLASS__);
20 if (((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
21 $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
28 $this->setPartDescr("Händler");
30 // Generate unique ID number
31 $this->createUniqueID();
34 $this->removeSystemArray();
37 // Haendler mit Namen erzeugen
38 public static function createMerchant ($merchantName, Harbor $harborInstance) {
40 $merchantName = (string) $merchantName;
43 $merchantInstance = new Merchant();
46 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
47 $merchantInstance->getDebugInstance()->output(sprintf("[%s:%d] Ein Händler <strong>%s</strong> wird angelegt und soll sich am <strong>%s</strong> niederlassen.<br />\n",
51 $harborInstance->getHarborName()
55 // Haendlernamen setzen
56 $merchantInstance->setMerchantName($merchantName);
58 // In dem angegebenen Hafen den Haendler ansiedeln
59 $merchantInstance->setHarborInstance($harborInstance);
61 // Preisliste initialisieren
62 $merchantInstance->createPriceList();
64 // Instanz zurueckliefern
65 return $merchantInstance;
68 // Initialize pricing list
69 private function createPriceList () {
70 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> erhält eine leere Preisliste.<br />\n",
73 $this->getMerchantName()
75 $this->priceList = new FrameworkArrayObject();
78 // Setter for merchant name
79 public function setMerchantName ($merchantName) {
81 $merchantName = (string) $merchantName;
84 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händlername <strong>%s</strong> ist nun bekannt.<br />\n",
89 $this->merchantName = $merchantName;
92 // Getter for merchant name
93 public function getMerchantName () {
94 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händlername <strong>%s</strong> wird verlangt.<br />\n",
99 return $this->merchantName;
102 // Setter for harbor instance
103 public function setHarborInstance (Harbor $harborInstance) {
104 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> lässt sich am <strong>%s</strong> nieder.<br />\n",
107 $this->getMerchantName(),
108 $harborInstance->getHarborName()
110 $this->harborInstance = $harborInstance;
113 // Getter for harbor instance
114 public function getHarborInstance () {
115 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> hat sich am <strong>%s</strong> niedergelassen.<br />\n",
118 $this->getMerchantName(),
119 $harborInstance->getHarborName()
121 return $this->harborInstance;
124 // Add new item to merchant's price list
125 public function addItemToPriceList (ItemIsTradeable $itemInstance, $price) {
127 $price = (float) $price;
130 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Händler <strong>%s</strong> kann nun das Schiffsteil <strong>%s</strong> "<strong>%s</strong>" zu <strong>%s</strong> verkaufen.<br />\n",
133 $this->getMerchantName(),
134 $itemInstance->__toString(),
135 $itemInstance->getPartDescr(),
136 $this->formatCurrency($price)
139 // Construct pricing item and add it to the list
140 $this->priceList->append(array(
141 'item' => $itemInstance,
145 // Remove price attribute
146 $itemInstance->removePrice();
149 // Get a price from the merchant's list
150 public function getPriceFromList (ItemIsTradeable $itemInstance) {
153 // Iterate throw whole list
154 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
156 $item = $iter->current();
158 // Does this item match? The unique ID may not work...
159 if ($item['item']->itemMatches($itemInstance)) {
160 // Extract price and stop searching
161 $price = $item['price'];
166 // Was the item found?
169 throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);
179 public function saveObjectToDatabase () {
180 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",