001    package market.statistics;
002    
003    import java.util.Calendar;
004    import java.util.Iterator;
005    import java.util.LinkedList;
006    import java.util.List;
007    
008    import market.SMarket;
009    import data.ooimpl.CatalogItemImpl;
010    
011    
012    /**
013     * Represents a statistics for one article of the market's offer.
014     */
015    public class CISalesStats extends CatalogItemImpl {
016    
017        private String articleID;
018        private int revenue;
019        private int amount;
020    
021        /**
022         * Saves every change of the article's price. Both price and date of price change are recorded.
023         */
024        private List priceHistory = new LinkedList();
025    
026        /**
027         * Saves the orders of the belonging item. Whenever an item is ordered, both the amount and the
028         * date are recorded.
029         */
030        private List orderHistory = new LinkedList();
031    
032        /**
033         * @param articleID the ID of the article statistics. It must match the real article's ID.
034         * @param revenue the revenue made from this article.
035         * @param amount the amount of sold items.
036         */
037        public CISalesStats(String articleID, int revenue, int amount) {
038            super(articleID);
039            this.articleID = articleID;
040            this.revenue = revenue;
041            this.amount = amount;
042        }
043    
044        public String getArticleID() {
045            return articleID;
046        }
047    
048        public int getRevenue() {
049            return revenue;
050        }
051    
052        public int getAmount() {
053            return amount;
054        }
055    
056        /**
057         * Returns the price history. If the last history entry is provisional, it is removed from
058         * the returned list. It is, however, not removed from the internal {@link #priceHistory}.
059         * @return the price history.
060         */
061        public List getPriceHistory() {
062            List tmpPriceHistory = (List)((LinkedList)priceHistory).clone();
063            if (isLastPriceChangeProvisional()) {
064                tmpPriceHistory.remove(tmpPriceHistory.size() - 1);
065            }
066            return tmpPriceHistory;
067        }
068    
069        /**
070         * Checks if the last entry of the price history is provisional.
071         * @return <code>true</code> if the last entry is provisional, otherwise <code>false</code>.
072         */
073        private boolean isLastPriceChangeProvisional() {
074            int size = priceHistory.size();
075            if (size == 0) {
076                return false;
077            }
078            PriceHistoryEntry lastEntry = (PriceHistoryEntry)priceHistory.get(size-1);
079            return lastEntry.isProvisional();
080        }
081    
082        public List getOrderHistory() {
083            return orderHistory;
084        }
085    
086        /**
087         * @param l the history ({@link #orderHistory} or {@link #priceHistory})list of which the
088         * last item is of interest.
089         * @return the last entry of a history list.
090         */
091        public HistoryEntry getLastEntry(List l) {
092            int size = l.size();
093            return size == 0 ? null : (HistoryEntry)l.get(size-1);
094        }
095    
096        /**
097         * Increases the saved amount of sold items.
098         * @param amount the amount of itmes to add.
099         */
100        public void addAmount(int amount) {
101            this.amount += amount;
102        }
103    
104        /**
105         * Increases the revenue made from this article.
106         * @param revenue the revenue to be added.
107         */
108        public void addRevenue(int revenue) {
109            this.revenue += revenue;
110        }
111    
112        /**
113         * Appends a new entry to the {@link #priceHistory}.
114         * @param date the date of the price change.
115         * @param newPrice the new price.
116         */
117        public void newPriceSet(Calendar date, int newPrice) {
118            if (priceHistory.size() > 0) {
119                PriceHistoryEntry lastEntry = (PriceHistoryEntry)priceHistory.get(priceHistory.size() - 1);
120                if (lastEntry.isProvisional()) {
121                    priceHistory.remove(lastEntry);
122                }
123                if (!SMarket.hasTimeAdvanced()) {
124                    date.add(Calendar.DATE, 1);
125                }
126            }
127            priceHistory.add(new PriceHistoryEntry(date, newPrice));
128        }
129    
130        /**
131         * Appends a new entry to the {@link #orderHistory}.
132         * @param date the date of the price change.
133         * @param amount the amount ordered
134         */
135        public void ordered(Calendar date, int amount) {
136            orderHistory.add(new HistoryEntry(date, amount));
137        }
138    
139        /**
140         * Concatenates an external price history with this one.
141         * @param ph the price history to be added.
142         */
143        public void appendPriceHistory(List ph) {
144            priceHistory.addAll(ph);
145        }
146    
147        /**
148         * Concatenates an external order history with this one.
149         * @param oh the order history to be added.
150         */
151        public void appendOrderHistory(List oh) {
152            orderHistory.addAll(oh);
153        }
154    
155        /**
156         * Iterates over the order history and sums up the amount of bought items.
157         * @return the amount of items ordered by the manager.
158         */
159        public int getOrderAmount() {
160            int amount = 0;
161            Iterator it = orderHistory.iterator();
162            while (it.hasNext()) {
163                amount += ((HistoryEntry)it.next()).getValue();
164            }
165            return amount;
166        }
167    
168        public CatalogItemImpl getShallowClone() {
169            CISalesStats ciss = new CISalesStats(articleID, revenue, amount);
170            ciss.appendOrderHistory(orderHistory);
171            ciss.appendPriceHistory(priceHistory);
172            return ciss;
173        }
174    
175        public String toString() {
176            return "amount: " + amount + "; revenue: " + revenue + "; priceHistory: " +
177                    priceHistory + "; orderHistory: "  + orderHistory;
178        }
179    }