Пример #1
0
        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));
        }
Пример #2
0
        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;
        }
Пример #3
0
        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;
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
 private void FinizeOfferPartiallyAndLog(SellOffer offer, int ammount, AssetsOwner buyer)
 {
     offer.FinalizeOfferPartially(buyer, ammount);
     SalesHistory.AddTodaySaleData(offer.Commodity, ammount, offer.PricePerPiece);
 }
Пример #8
0
 private void FinalizeOfferAndLog(SellOffer offer, AssetsOwner buyer)
 {
     offer.FinalizeOffer(buyer);
     SalesHistory.AddTodaySaleData(offer.Commodity, offer.Ammount, offer.PricePerPiece);
 }
Пример #9
0
 public SellOffer(Commodity commodity, int ammount, AssetsOwner seller)
 {
     Commodity = commodity;
     Ammount   = ammount;
     Seller    = seller;
 }