24d21b6a093b9f24dd27f3c78d64b186ecacc535
[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                 // Set description
40                 $this->setObjectDescription("H&auml;ndler");
41
42                 // Generate unique ID number
43                 $this->createUniqueID();
44
45                 // Clean up a little
46                 $this->removeSystemArray();
47         }
48
49         // Haendler mit Namen erzeugen
50         public final static function createMerchant ($merchantName, Harbor $harborInstance) {
51                 // String absichern
52                 $merchantName = (string) $merchantName;
53
54                 // Get new instance
55                 $merchantInstance = new Merchant();
56
57                 // Debug message
58                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) {
59                         $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",
60                                 __CLASS__,
61                                 __LINE__,
62                                 $merchantName,
63                                 $harborInstance->getHarborName()
64                         ));
65                 }
66
67                 // Haendlernamen setzen
68                 $merchantInstance->setMerchantName($merchantName);
69
70                 // In dem angegebenen Hafen den Haendler ansiedeln
71                 $merchantInstance->setHarborInstance($harborInstance);
72
73                 // Preisliste initialisieren
74                 $merchantInstance->createPriceList();
75
76                 // Instanz zurueckliefern
77                 return $merchantInstance;
78         }
79
80         // Initialize pricing list
81         private function createPriceList () {
82                 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",
83                         __CLASS__,
84                         __LINE__,
85                         $this->getMerchantName()
86                 ));
87                 $this->priceList = new FrameworkArrayObject();
88         }
89
90         // Setter for merchant name
91         public final function setMerchantName ($merchantName) {
92                 // Secure string
93                 $merchantName = (string) $merchantName;
94
95                 // Debug message
96                 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",
97                         __CLASS__,
98                         __LINE__,
99                         $merchantName
100                 ));
101                 $this->merchantName = $merchantName;
102         }
103
104         // Getter for merchant name
105         public final function getMerchantName () {
106                 if ((defined('DEBUG_MERCHANT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der H&auml;ndlername <strong>%s</strong> wird verlangt.<br />\n",
107                         __CLASS__,
108                         __LINE__,
109                         $this->merchantName
110                 ));
111                 return $this->merchantName;
112         }
113
114         // Setter for harbor instance
115         public final function setHarborInstance (Harbor $harborInstance) {
116                 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",
117                         __CLASS__,
118                         __LINE__,
119                         $this->getMerchantName(),
120                         $harborInstance->getHarborName()
121                 ));
122                 $this->harborInstance = $harborInstance;
123         }
124
125         // Getter for harbor instance
126         public final function getHarborInstance () {
127                 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",
128                         __CLASS__,
129                         __LINE__,
130                         $this->getMerchantName(),
131                         $harborInstance->getHarborName()
132                 ));
133                 return $this->harborInstance;
134         }
135
136         // Add new item to merchant's price list
137         public function addItemToPriceList (TradeableItem $itemInstance, $price) {
138                 // Secure pricing
139                 $price = (float) $price;
140
141                 // Debug message
142                 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",
143                         __CLASS__,
144                         __LINE__,
145                         $this->getMerchantName(),
146                         $itemInstance->__toString(),
147                         $itemInstance->getObjectDescription(),
148                         $this->formatCurrency($price)
149                 ));
150
151                 // Construct pricing item and add it to the list
152                 $this->priceList->append(array(
153                         'item'  => $itemInstance,
154                         'price' => $price
155                 ));
156
157                 // Remove price attribute
158                 $itemInstance->removePrice();
159         }
160
161         // Get a price from the merchant's list
162         public final function getPriceFromList (TradeableItem $itemInstance) {
163                 $price = 0;
164
165                 // Iterate throw whole list
166                 for ($iter = $this->priceList->getIterator(); $iter->valid(); $iter->next()) {
167                         // Get current item
168                         $item = $iter->current();
169
170                         // Does this item match? The unique ID may not work...
171                         if ($item['item']->itemMatches($itemInstance)) {
172                                 // Extract price and stop searching
173                                 $price = $item['price'];
174                                 break;
175                         }
176                 }
177
178                 // Was the item found?
179                 if ($price === 0) {
180                         // Throw exception
181                         throw new ItemNotInPriceListException($itemInstance, self::EXCEPTION_ITEM_NOT_IN_PRICE_LIST);
182                 }
183
184                 // Return price
185                 return $price;
186         }
187 }
188
189 // [EOF]
190 ?>