public Trade(ItemType type, float unitPrice, int quantity, int minQuantity) { this.type = type; this.unitPrice = unitPrice; this.quantity = quantity; this.minQuantity = minQuantity; }
protected override void InterpretRow(Dictionary<string, string> fields) { int id = ParseId(fields["typeID"]); string name = fields["typeName"]; float volume = ParseNumber(fields["volume"]); ItemType t = new ItemType(id, name, volume); if (!itemTypesByName.ContainsKey(t.Name)) { itemTypesByName.Add(t.Name, t); itemTypesById.Add(t.Id, t); } else Console.WriteLine("Warning: duplicate item " + t.Name); }
public Trade(ItemType type, float unitPrice, int quantity) : this(type, unitPrice, quantity, 0) { }
public float GetSaleValueOf(ItemType type, int quantity) { float runningTotal = 0.0f; int quantityToSell = quantity; foreach (Trade trade in ItemsWanted[type]) // this had better iterate in sorted order { int quantitySold = Math.Min(quantityToSell, trade.Quantity); runningTotal += trade.UnitPrice * quantitySold; quantityToSell -= quantitySold; if (quantityToSell == 0) break; } return runningTotal; }
internal TransactionList GetItemTransactionList(Station source, Station destination, ItemType type) { TransactionList list = new TransactionList(); Trade[] forSale = source.ItemsForSale[type].ToArray(); Trade[] wanted = destination.ItemsWanted[type].ToArray(); int buyIndex = 0; int sellIndex = 0; int buyAmount = 0; int sellAmount = 0; TransactionItem purchase = null; TransactionItem sale = null; Transaction currentTransaction = null; bool finished = false; int minQtyNeeded = 0; while (!finished) { // Source station has more than destination wants if ((forSale[buyIndex].Quantity - buyAmount) > (wanted[sellIndex].Quantity - sellAmount)) { // Set the amount (qty) of the transaction int amount = (wanted[sellIndex].Quantity - sellAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Set the buy amount up by the amount that can be sold buyAmount += amount; // reset the sell amount sellAmount = 0; sellIndex++; } // Source station has less than destination wants else if ((forSale[buyIndex].Quantity - buyAmount) < (wanted[sellIndex].Quantity - sellAmount)) { // Set the amount (qty) of the transaction int amount = (forSale[buyIndex].Quantity - buyAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Set the buy amount up by the amount that can be sold sellAmount += amount; // reset the buy amount buyAmount = 0; buyIndex++; } else { // Set the amount (qty) of the transaction int amount = (wanted[sellIndex].Quantity - sellAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Reset both buy and sell amount buyAmount = 0; sellAmount = 0; buyIndex++; sellIndex++; } if (currentTransaction == null) { currentTransaction = new Transaction(purchase, sale); } else { currentTransaction.AddPurchase(purchase); if (sale != null) { currentTransaction.AddSale(sale); } } purchase = null; sale = null; if ((wanted.Length <= sellIndex) || (forSale.Length <= buyIndex) || (wanted[sellIndex].UnitPrice <= forSale[buyIndex].UnitPrice)) { finished = true; } // If minimum quantity is achieved, add the transaction if ((finished) || ((minQtyNeeded <= 0) && (!((wanted[sellIndex].UnitPrice == wanted[Math.Max(sellIndex-1, 0)].UnitPrice) && (forSale[buyIndex].UnitPrice == forSale[Math.Max(buyIndex-1, 0)].UnitPrice))) )) { if (currentTransaction.Profit >= 0.0f) { list.Add(currentTransaction); } currentTransaction = null; minQtyNeeded = 0; } } return list; }
internal void Init(ItemType itemType) { type = itemType; sales = new TransactionItemList(); purchases = new TransactionItemList(); }
private Transaction(ItemType itemType) { Init(itemType); }