001    package market;
002    
003    import java.util.Iterator;
004    
005    import data.CatalogItem;
006    import data.CountingStock;
007    import data.events.VetoException;
008    import data.ooimpl.CatalogImpl;
009    import data.ooimpl.CountingStockImpl;
010    
011    /**
012     * A Catalog containing {@link CIOpenPurchaseOrders}.
013     */
014    public class COpenPurchaseOrders extends CatalogImpl {
015    
016        /**
017         * @param name the catalogs's name.
018         */
019        public COpenPurchaseOrders(String name) {
020            super(name);
021        }
022    
023        /**
024         * Removes a CatalogItem from this Catalog.
025         * @param ci the CatalogItem to be removed.
026         * @return the removed CatalogItem.
027         */
028        public CIOpenPurchaseOrders remove(CatalogItem ci) {
029            try {
030                return (CIOpenPurchaseOrders)super.remove(ci, null);
031            }
032            catch (VetoException e) {
033                System.err.println("Fehler beim Löschen aus COpenPurchaseOrders");
034                e.printStackTrace();
035                return null;
036            }
037        }
038    
039        /**
040         * Gets a CatalogItem from this Catalog.
041         * @param key the searched CatalogItem's key.
042         * @return the searched CatalogItem, <code>null</code> if not found.
043         */
044        public CIOpenPurchaseOrders get(String key) {
045            try {
046                return (CIOpenPurchaseOrders)super.get(key, null, false);
047            }
048            catch (VetoException e) {
049                System.err.println("Fehler beim Holen eines Elements aus COpenPurchaseOrders");
050                e.printStackTrace();
051                return null;
052            }
053        }
054    
055        /**
056         * Decreases the number of days to wait for open purchase orders according to the days that passed.
057         * All purchases where the days to wait have dropped to or below zero are summed up and returned.
058         * @param i the number of passed days.
059         * @return the accumulated orders that arrived.
060         */
061        public CountingStock subtractPassedDays(int i) {
062            CountingStock arrived = new CountingStockImpl("arrived", SMarket.getArticleCatalog());
063            Iterator it = keySet(null).iterator();
064            while (it.hasNext()) {
065                CIOpenPurchaseOrders next = get((String)it.next());
066                fireEditingCatalogItem(next, null); //update remaining days in table view (if open)
067                next.decreaseDaysTillArrival(i);
068                if (next.getDaysTillArrival() <= 0) {
069                    remove(next);
070                    arrived.addStock(next.getOrders(), null, false);
071                }
072            }
073            return arrived;
074        }
075    }