Method constructor() removed, several small fixes
[shipsimu.git] / application / ship-simu / main / class_Merchant.php
1 <?php
2 /**
3  * A class for merchants which can trade items
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class Merchant extends BaseFrameworkSystem {
25         // Name des Haendlers
26         private $merchantName   = "Namenloser H&auml;ndler";
27
28         // Preislite (Objekte wiedermal!)
29         private $priceList      = null;
30
31         // Zugewiesener Hafen
32         private $harborInstance = null;
33
34         // Konstruktor
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38
39                 // Debug message
40                 if (((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
41                         $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
42                                 __CLASS__,
43                                 __LINE__
44                         ));
45                 }
46
47                 // Set description
48                 $this->setObjectDescription("H&auml;ndler");
49
50                 // Generate unique ID number
51                 $this->createUniqueID();
52
53                 // Clean up a little
54                 $this->removeSystemArray();
55         }
56
57         // Haendler mit Namen erzeugen
58         public final static function createMerchant ($merchantName, Harbor $harborInstance) {
59                 // String absichern
60                 $merchantName = (string) $merchantName;
61
62                 // Get new instance
63                 $merchantInstance = new Merchant();
64
65                 // Debug message
66                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
67                         $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",
68                                 __CLASS__,
69                                 __LINE__,
70                                 $merchantName,
71                                 $harborInstance->getHarborName()
72                         ));
73                 }
74
75                 // Haendlernamen setzen
76                 $merchantInstance->setMerchantName($merchantName);
77
78                 // In dem angegebenen Hafen den Haendler ansiedeln
79                 $merchantInstance->setHarborInstance($harborInstance);
80
81                 // Preisliste initialisieren
82                 $merchantInstance->createPriceList();
83
84                 // Instanz zurueckliefern
85                 return $merchantInstance;
86         }
87
88         // Initialize pricing list
89         private function createPriceList () {
90                 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",
91                         __CLASS__,
92                         __LINE__,
93                         $this->getMerchantName()
94                 ));
95                 $this->priceList = new FrameworkArrayObject();
96         }
97
98         // Setter for merchant name
99         public final function setMerchantName ($merchantName) {
100                 // Secure string
101                 $merchantName = (string) $merchantName;
102
103                 // Debug message
104                 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",
105                         __CLASS__,
106                         __LINE__,
107                         $merchantName
108                 ));
109                 $this->merchantName = $merchantName;
110         }
111
112         // Getter for merchant name
113         public final function getMerchantName () {
114                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der H&auml;ndlername <strong>%s</strong> wird verlangt.<br />\n",
115                         __CLASS__,
116                         __LINE__,
117                         $this->merchantName
118                 ));
119                 return $this->merchantName;
120         }
121
122         // Setter for harbor instance
123         public final function setHarborInstance (Harbor $harborInstance) {
124                 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",
125                         __CLASS__,
126                         __LINE__,
127                         $this->getMerchantName(),
128                         $harborInstance->getHarborName()
129                 ));
130                 $this->harborInstance = $harborInstance;
131         }
132
133         // Getter for harbor instance
134         public final function getHarborInstance () {
135                 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",
136                         __CLASS__,
137                         __LINE__,
138                         $this->getMerchantName(),
139                         $harborInstance->getHarborName()
140                 ));
141                 return $this->harborInstance;
142         }
143
144         // Add new item to merchant's price list
145         public function addItemToPriceList (TradeableItem $itemInstance, $price) {
146                 // Secure pricing
147                 $price = (float) $price;
148
149                 // Debug message
150                 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",
151                         __CLASS__,
152                         __LINE__,
153                         $this->getMerchantName(),
154                         $itemInstance->__toString(),
155                         $itemInstance->getObjectDescription(),
156                         $this->formatCurrency($price)
157                 ));
158
159                 // Construct pricing item and add it to the list
160                 $this->priceList->append(array(
161                         'item'  => $itemInstance,
162                         'price' => $price
163                 ));
164
165                 // Remove price attribute
166                 $itemInstance->removePrice();
167         }
168
169         // Get a price from the merchant's list
170         public final function getPriceFromList (TradeableItem $itemInstance) {
171                 $price = 0;
172
173                 // Iterate throw whole list
174                 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
175                         // Get current item
176                         $item = $iter->current();
177
178                         // Does this item match? The unique ID may not work...
179                         if ($item['item']->itemMatches($itemInstance)) {
180                                 // Extract price and stop searching
181                                 $price = $item['price'];
182                                 break;
183                         }
184                 }
185
186                 // Was the item found?
187                 if ($price === 0) {
188                         // Throw exception
189                         throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);
190                 }
191
192                 // Return price
193                 return $price;
194         }
195
196         /**
197          * Stub!
198          */
199         public function saveObjectToDatabase () {
200                 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",
201                         $this->__toString(),
202                         __FUNCTION__
203                 ));
204         }
205 }
206
207 // [EOF]
208 ?>