001 package videoautomat;
002 import java.awt.Rectangle;
003 import java.io.FileOutputStream;
004 import java.io.IOException;
005 import java.util.Iterator;
006
007 import log.Log;
008 import sale.*;
009 import users.UserManager;
010 import videoautomat.gui.Global;
011 import data.*;
012 import data.events.VetoException;
013 import data.ooimpl.CatalogImpl;
014 import data.ooimpl.CountingStockImpl;
015 import data.ooimpl.CurrencyItemImpl;
016 import data.ooimpl.EUROCurrencyImpl;
017 import data.ooimpl.MoneyBagImpl;
018
019 /**
020 * This is the central class for this application - the <code>Shop</code>. It contains the
021 * video-catalog and stock of this video-shop, the shop`s money and therefor a currency-catalog
022 * containing which coins this automat accepts and a <code>Timer</code> for simulation purposes.
023 *
024 */
025 public class VideoShop extends Shop {
026 /**
027 * Key of the video -<code>Catalog</code>
028 */
029 public static final String C_VIDEOS = new String("VideoCatalog");
030 /**
031 * Key of the currency -<code>Catalog</code>
032 */
033 public static final String C_CURRENCY = new String("CurrencyCatalog");
034 /**
035 * Key of the video -<code>CountingStock</code>
036 */
037 public static final String CC_VIDEOS = new String("VideoStock");
038 /**
039 * Key of the <code>MoneyBag</code>
040 */
041 public static final String MB_MONEY = new String("Money");
042 /**
043 * The name of the global log file
044 */
045 public static final String FILENAME = "automat.log";
046 /**
047 * Constructs a new VideoShop and set it as <code>Shop#setTheShop()</code>. Also initialize
048 * the global <code>Catalogs</code> and <code>Stocks</code> and sets a <code>Timer</code>.
049 *
050 */
051 public VideoShop() {
052 super();
053 setShopFrameBounds(new Rectangle(0, 0, 640, 480));
054
055 addCatalog(new CatalogImpl(C_VIDEOS));
056 addCatalog(new EUROCurrencyImpl(C_CURRENCY));
057 addStock(
058 new CountingStockImpl(
059 CC_VIDEOS,
060 (CatalogImpl) getCatalog(C_VIDEOS)));
061
062 UserManager.setGlobalUM(new UserManager());
063
064 addStock(
065 new MoneyBagImpl(
066 MB_MONEY,
067 (EUROCurrencyImpl) getCatalog(C_CURRENCY)));
068
069 setTimer(new StepTimer(new CalendarTime(System.currentTimeMillis())));
070 setShopFrameTitle(getTimer().getTime().toString());
071 try {
072 Log.setGlobalOutputStream(new FileOutputStream(FILENAME, true));
073 } catch (IOException ioex) {
074 System.err.println("Unable to create log file.");
075 }
076 }
077
078 /**
079 * @return the <code>Shop`s MenuSheet</code>, containing the default one, a button to start
080 * an automat and a button to switch the time further.
081 * @see sale.Shop#createShopMenuSheet()
082 */
083 protected MenuSheet createShopMenuSheet() {
084 MenuSheet ms_default = super.createShopMenuSheet();
085 MenuSheet ms_new = new MenuSheet(Global.MS_NEW);
086 MenuSheetItem msi_automat =
087 new MenuSheetItem(Global.MSI_AUTOMAT, new Action() {
088 public void doAction(SaleProcess p, SalesPoint sp)
089 throws Throwable {
090 addSalesPoint(new VideoAutomat());
091 }
092 });
093 ms_new.add(msi_automat);
094 MenuSheetItem msi_time =
095 new MenuSheetItem(Global.MSI_DAY, new Action() {
096 public void doAction(SaleProcess p, SalesPoint sp)
097 throws Throwable {
098 getTimer().goAhead();
099 setShopFrameTitle(getTimer().getTime().toString());
100 }
101 });
102 ms_new.add(msi_time);
103 ms_default.add(ms_new);
104 return ms_default;
105 }
106 /**
107 * Overidden to avoid the annoying save-query, when quiting the application.
108 *
109 * @see sale.Shop#quit()
110 */
111 public void quit() {
112 if (shutdown(false)) {
113 System.exit(0);
114 }
115 }
116 /**
117 * Helper method to avoid to long code-lines.
118 *
119 * @return the global <code>Catalog</code> of videos.
120 */
121 public static CatalogImpl getVideoCatalog() {
122 return (CatalogImpl) Shop.getTheShop().getCatalog(C_VIDEOS);
123 }
124 /**
125 * Helper method to avoid to long code-lines.
126 *
127 * @return the global <code>Stock</code> of videos
128 */
129 public static CountingStockImpl getVideoStock() {
130 return (CountingStockImpl) Shop.getTheShop().getStock(CC_VIDEOS);
131 }
132
133 /**
134 * Helper method to avoid to long code-lines.
135 *
136 * @return the global <code>MoneyBag</code> containing the money of the shop.
137 */
138 public static MoneyBagImpl getMoneyBag() {
139 return (MoneyBagImpl) Shop.getTheShop().getStock(MB_MONEY);
140 }
141 /**
142 * Helper method to avoid to long code-lines.
143 *
144 * @return the global <code>EUROCurrencyImpl</code> -instance
145 */
146 public static EUROCurrencyImpl getCurrency() {
147 return (EUROCurrencyImpl) Shop.getTheShop().getCatalog(C_CURRENCY);
148 }
149
150 /**
151 * Method to iterate over all rented videos and taking out those, which renting costs exceed
152 * the purchase price
153 *
154 */
155 public static void checkRentedVideos() {
156 Iterator it_users = UserManager.getGlobalUM().getUsers().iterator();
157 while (it_users.hasNext()) {
158 AutomatUser c = (AutomatUser) it_users.next();
159 Iterator it_stock = c.getVideoStock().iterator(null, false);
160 while (it_stock.hasNext()) {
161 VideoCassette vc = (VideoCassette) it_stock.next();
162 Value v =
163 ((QuoteValue) vc.getAssociatedItem(null).getValue())
164 .getOffer();
165 if (vc.getCost().compareTo(v) >= 0) {
166 try {
167 c.getVideoStock().remove(vc, null);
168 } catch (VetoException e) {
169 e.printStackTrace();
170 }
171 }
172 }
173 }
174 }
175 }