Initial import of current development status
[shipsimu.git] / ship-simu / application / ship-simu / main / class_Merchant.php
1 <?php
2
3 // Die Haendler-Klasse
4 class Merchant extends BaseFrameworkSystem {
5         // Name des Haendlers
6         private $merchantName   = "Namenloser H&auml;ndler";
7
8         // Preislite (Objekte wiedermal!)
9         private $priceList      = null;
10
11         // Zugewiesener Hafen
12         private $harborInstance = null;
13
14         // Konstruktor
15         private function __construct () {
16                 // Eltern-Konstruktor aufrufen
17                 parent::constructor(__CLASS__);
18
19                 // Debug message
20                 if (((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
21                         $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
22                                 __CLASS__,
23                                 __LINE__
24                         ));
25                 }
26
27                 // Beschreibung setzen
28                 $this->setPartDescr("H&auml;ndler");
29
30                 // Unique-ID erzeugen
31                 $this->createUniqueID();
32
33                 // Clean up a little
34                 $this->removeSystemArray();
35         }
36
37         // Haendler mit Namen erzeugen
38         public static function createMerchant ($merchantName, Harbor $harborInstance) {
39                 // String absichern
40                 $merchantName = (string) $merchantName;
41
42                 // Instanz holen
43                 $merchantInstance = new Merchant();
44
45                 // Debug message
46                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
47                         $merchantInstance->getDebugInstance()->output(sprintf("[%s:%d] Ein H&auml;ndler <strong>%s</strong> wird angelegt und soll sich am <strong>%s</strong> niederlassen.<br />\n",
48                                 __CLASS__,
49                                 __LINE__,
50                                 $merchantName,
51                                 $harborInstance->getHarborName()
52                         ));
53                 }
54
55                 // Haendlernamen setzen
56                 $merchantInstance->setMerchantName($merchantName);
57
58                 // In dem angegebenen Hafen den Haendler ansiedeln
59                 $merchantInstance->setHarborInstance($harborInstance);
60
61                 // Preisliste initialisieren
62                 $merchantInstance->createPriceList();
63
64                 // Instanz zurueckliefern
65                 return $merchantInstance;
66         }
67
68         // Initialize pricing list
69         private function createPriceList () {
70                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der H&auml;ndler <strong>%s</strong> erh&auml;lt eine leere Preisliste.<br />\n",
71                         __CLASS__,
72                         __LINE__,
73                         $this->getMerchantName()
74                 ));
75                 $this->priceList = new FrameworkArrayObject();
76         }
77
78         // Setter for merchant name
79         public function setMerchantName ($merchantName) {
80                 // Secure string
81                 $merchantName = (string) $merchantName;
82
83                 // Debug message
84                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der H&auml;ndlername <strong>%s</strong> ist nun bekannt.<br />\n",
85                         __CLASS__,
86                         __LINE__,
87                         $merchantName
88                 ));
89                 $this->merchantName = $merchantName;
90         }
91
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&auml;ndlername <strong>%s</strong> wird verlangt.<br />\n",
95                         __CLASS__,
96                         __LINE__,
97                         $this->merchantName
98                 ));
99                 return $this->merchantName;
100         }
101
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&auml;ndler <strong>%s</strong> l&auml;sst sich am <strong>%s</strong> nieder.<br />\n",
105                         __CLASS__,
106                         __LINE__,
107                         $this->getMerchantName(),
108                         $harborInstance->getHarborName()
109                 ));
110                 $this->harborInstance = $harborInstance;
111         }
112
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&auml;ndler <strong>%s</strong> hat sich am <strong>%s</strong> niedergelassen.<br />\n",
116                         __CLASS__,
117                         __LINE__,
118                         $this->getMerchantName(),
119                         $harborInstance->getHarborName()
120                 ));
121                 return $this->harborInstance;
122         }
123
124         // Add new item to merchant's price list
125         public function addItemToPriceList (ItemIsTradeable $itemInstance, $price) {
126                 // Secure pricing
127                 $price = (float) $price;
128
129                 // Debug message
130                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der H&auml;ndler <strong>%s</strong> kann nun das Schiffsteil <strong>%s</strong> &quot;<strong>%s</strong>&quot; zu <strong>%s</strong> verkaufen.<br />\n",
131                         __CLASS__,
132                         __LINE__,
133                         $this->getMerchantName(),
134                         $itemInstance->__toString(),
135                         $itemInstance->getPartDescr(),
136                         $this->formatCurrency($price)
137                 ));
138
139                 // Construct pricing item and add it to the list
140                 $this->priceList->append(array(
141                         'item'  => $itemInstance,
142                         'price' => $price
143                 ));
144
145                 // Remove price attribute
146                 $itemInstance->removePrice();
147         }
148
149         // Get a price from the merchant's list
150         public function getPriceFromList (ItemIsTradeable $itemInstance) {
151                 $price = 0;
152
153                 // Iterate throw whole list
154                 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
155                         // Get current item
156                         $item = $iter->current();
157
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'];
162                                 break;
163                         }
164                 }
165
166                 // Was the item found?
167                 if ($price === 0) {
168                         // Throw exception
169                         throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);
170                 }
171
172                 // Return price
173                 return $price;
174         }
175
176         /**
177          * Stub!
178          */
179         public function saveObjectToDatabase () {
180                 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",
181                         $this->__toString(),
182                         __FUNCTION__
183                 ));
184         }
185 }
186
187 // [EOF]
188 ?>