public void MakeBuyOffer(Commodity commodity, int ammount, AssetsOwner buyer) { int currNeeded = ammount; var matchingOffers = sellOffers.Where(x => x.Commodity == commodity).ToList(); if (matchingOffers.Sum(x => x.Ammount) < ammount) { throw new ApplicationException(); } decimal pricePaid = 0.0M; while (currNeeded > 0) { var minPriceOffer = matchingOffers.MinElement(x => x.PricePerPiece); if (minPriceOffer.Ammount <= currNeeded) { pricePaid += minPriceOffer.Ammount * minPriceOffer.PricePerPiece; FinalizeOfferAndLog(minPriceOffer, buyer); matchingOffers.Remove(minPriceOffer); sellOffers.Remove(minPriceOffer); currNeeded -= minPriceOffer.Ammount; } else { pricePaid += currNeeded * minPriceOffer.PricePerPiece; FinizeOfferPartiallyAndLog(minPriceOffer, currNeeded, buyer); currNeeded -= currNeeded; } } Console.WriteLine(String.Format("{0} bought {1} of {2} for {3} on market {4}", buyer.Name, ammount, commodity.Name, pricePaid, this.Name)); }
public void TransferMoney(AssetsOwner other, decimal ammount) { if (this.money < ammount) { throw new ApplicationException(); } Console.WriteLine(String.Format("Money transfer from {0} to {1} - {2}", this.Name, other.Name, ammount)); this.money -= ammount; other.money += ammount; }
public void SellAsset(AssetsOwner seller, Commodity commodity, int ammount) { var lastTurnPrice = Market.SalesHistory.GetActualPrice(commodity); var priceToOffer = lastTurnPrice * (1 + SELL_PREMIUM); Market.AddSellOffer(new SellOfferWithDiscountPerTurn( commodity: commodity, price: priceToOffer, ammount: ammount, seller: seller, discount: SELL_DISCOUNT_PER_TURN)); }
public SellOfferWithDiscountPerTurn(Commodity commodity, int ammount, decimal price, AssetsOwner seller, double discount) : base(commodity, ammount, seller) { if (discount <= 0.0d || discount >= 1.0d) { throw new ArgumentException(); } this.discount = discount; this.initialPrice = price; offerBegginingTurn = TurnCounter.Now; }
public void FinalizeOfferPartially(AssetsOwner buyer, int partialAmmount) // TODO: test it! { if (partialAmmount >= Ammount) { throw new ApplicationException(); } decimal moneyToTransfer = PricePerPiece * partialAmmount; buyer.TransferMoney(Seller, moneyToTransfer); Ammount -= partialAmmount; buyer.commodityStorage.Deposit(Commodity, partialAmmount); }
public void FinalizeOffer(AssetsOwner buyer) // TODO: test it! { if (finalized) { throw new ApplicationException(); } finalized = true; decimal moneyToTransfer = PricePerPiece * Ammount; buyer.TransferMoney(Seller, moneyToTransfer); buyer.commodityStorage.Deposit(Commodity, Ammount); }
private void FinizeOfferPartiallyAndLog(SellOffer offer, int ammount, AssetsOwner buyer) { offer.FinalizeOfferPartially(buyer, ammount); SalesHistory.AddTodaySaleData(offer.Commodity, ammount, offer.PricePerPiece); }
private void FinalizeOfferAndLog(SellOffer offer, AssetsOwner buyer) { offer.FinalizeOffer(buyer); SalesHistory.AddTodaySaleData(offer.Commodity, offer.Ammount, offer.PricePerPiece); }
public SellOffer(Commodity commodity, int ammount, AssetsOwner seller) { Commodity = commodity; Ammount = ammount; Seller = seller; }