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