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