Esempio n. 1
0
 public Bid(Sale sale, double value, Buyer bidder)
 {
     Sale = sale;
     Value = value;
     Bidder = bidder;
     Time = DateTime.Now;
 }
Esempio n. 2
0
        public void AddSale(Sale sale)
        {
            var lastOrDefault = _sales.LastOrDefault();
            if (lastOrDefault != null) sale.Number = lastOrDefault.Number + 1;

            if (sale.Duration < Settings.MinSaleDuration)
            {
                sale.Duration = Settings.MinSaleDuration;
            }
            _sales.Add(sale);
        }
Esempio n. 3
0
 private bool IsValidSale(Sale sale)
 {
     return !_sales.Contains(sale) && (_sellers.Contains(sale.Seller) && _series.Contains(sale.Series)) &&
            sale.Lot != null;
 }
Esempio n. 4
0
        static void Main()
        {
            // Создаем аукцион и регистрируем продавцов и покупателей
            var auction = new Auction("test auction");
            auction.AddBuyer(new Buyer("blogin1", "Fred", "Durst"));
            auction.AddBuyer(new Buyer("blogin2", "Samuel", "Rivers"));
            auction.AddBuyer(new Buyer("blogin3", "John", "Otto"));
            auction.AddBuyer(new Buyer("blogin4", "Wesley", "Borland"));
            auction.AddSeller(new Seller("slogin1", "Sid", "Wilson"));
            auction.AddSeller(new Seller("slogin2", "Jonas", "Jordison"));
            auction.AddSeller(new Seller("slogin3", "Chris", "Fehn"));
            auction.AddSeller(new Seller("slogin4", "James", "Root"));

            // Создаем две серии. В примере используется только первая
            var series1 = new Series("first series");
            var series2 = new Series("second series");

            // Добавляем серии в аукцион
            auction.AddSeries(series1);
            Console.WriteLine("series created:\t" + series1.Name);
            auction.AddSeries(series2);
            Console.WriteLine("series created:\t" + series2.Name);
            Console.WriteLine();

            // Создаем лоты
            var lot1 = new Lot("first lot", "amazing lot!", null);
            var lot2 = new Lot("second lot", "complex lot", null);
            var lot3 = new Lot("third lot", "Green Book", null);
            var lot4 = new Lot("fourth lot", "Red Book", null);
            var lot5 = new Lot("fifth lot", "Black Book", null, lot1, lot2, lot3, lot4);

            // Создаем продажу длительностью 5 секунд
            var sale1 = new Sale("first sale", lot5, auction.GetSellerByLogin("slogin1"),
                100, 1, TimeSpan.FromSeconds(5), new Category("elite"));

            // Добавляем продажу
            series1.AddSale(sale1);
            // Ожидаем, пока продажа закончится и сообщаем об этом
            var waitForFinish = new Thread(FinishMessage);
            waitForFinish.Start(sale1);
            Console.WriteLine("sale created:\t" + sale1.Name +"\tduration (seconds):" + sale1.Duration.TotalSeconds + "\t{series = " + series1.Name+"}");

            // Создаем ставки
            var bid1 = new Bid(120, auction.GetBuyerByLogin("blogin1"));
                var bid12 = new Bid(121, auction.GetBuyerByLogin("blogin1")); //вторая ставка одним покупателем. не должна пройти
            var bid2 = new Bid( 125, auction.GetBuyerByLogin("blogin2"));
            var bid3 = new Bid( 127, auction.GetBuyerByLogin("blogin1"));
            var bid4 = new Bid( 130, auction.GetBuyerByLogin("blogin3"));
            var bid5 = new Bid( 131, auction.GetBuyerByLogin("blogin1"));

            // Делаем ставки через 0.5 секунды и выводим последнюю принятую ставку
            auction.MakeBid(sale1, bid1);
            auction.MakeBid(sale1, bid12); // не принимается (последняя ставка остается 120)
            Console.WriteLine("sale #" + sale1.Number + "\t\"" + sale1.Name + "\"\tlastbidder:\t" + (sale1.LastBidder==null?"none":sale1.LastBidder.FullName) + "\t val:" + sale1.CurrentPrice);
            Thread.Sleep(500);
            auction.MakeBid(sale1, bid2);
            Console.WriteLine("sale #" + sale1.Number + "\t\"" + sale1.Name + "\"\tlastbidder:\t" + (sale1.LastBidder == null ? "none" : sale1.LastBidder.FullName) + "\t val:" + sale1.CurrentPrice);
            Thread.Sleep(500);
            auction.MakeBid(sale1, bid3);
            Console.WriteLine("sale #" + sale1.Number + "\t\"" + sale1.Name + "\"\tlastbidder:\t" + (sale1.LastBidder == null ? "none" : sale1.LastBidder.FullName) + "\t val:" + sale1.CurrentPrice);
            Thread.Sleep(500);
            auction.MakeBid(sale1, bid4);
            Console.WriteLine("sale #" + sale1.Number + "\t\"" + sale1.Name + "\"\tlastbidder:\t" + (sale1.LastBidder == null ? "none" : sale1.LastBidder.FullName) + "\t val:" + sale1.CurrentPrice);
            Thread.Sleep(500);
            auction.MakeBid(sale1, bid5);
            Console.WriteLine("sale #" + sale1.Number + "\t\"" + sale1.Name + "\"\tlastbidder:\t" + (sale1.LastBidder == null ? "none" : sale1.LastBidder.FullName) + "\t val:" + sale1.CurrentPrice);
            Thread.Sleep(500);

            Console.ReadKey();
        }
Esempio n. 5
0
 protected virtual void OnSaleFinished(Sale sale)
 {
     EventHandler<ActionEventArgs<Sale>> handler = SaleFinished;
     if (handler != null) handler(this, new ActionEventArgs<Sale>(sale));
 }
Esempio n. 6
0
 protected virtual void OnSaleCreated(Sale newSale)
 {
     EventHandler<ActionEventArgs<Sale>> handler = SaleStarted;
     if (handler != null) handler(this, new ActionEventArgs<Sale>(newSale));
 }
Esempio n. 7
0
        public void AddSale(Sale sale)
        {
            if (IsValidSale(sale))
            {
                var lastOrDefault = _sales.LastOrDefault();
                if (lastOrDefault != null)
                {
                    sale.Number = lastOrDefault.Number + 1;
                }

                if (sale.Duration < Settings.MinSaleDuration)
                {
                    sale.Duration = Settings.MinSaleDuration;
                }

                AddLot(sale.Lot);
                _sales.Add(sale);
                OnSaleCreated(sale);
                sale.SaleFinished += SaleFinish;
                sale.Start();
            }
        }
Esempio n. 8
0
        static void Main()
        {
            var testAuction = new Auction();

            testAuction.AddBuyer(new Buyer("blogin1", "Fred", "Durst"));
            testAuction.AddBuyer(new Buyer("blogin2", "Samuel", "Rivers"));
            testAuction.AddBuyer(new Buyer("blogin3", "John", "Otto"));
            testAuction.AddBuyer(new Buyer("blogin4", "Wesley", "Borland"));
            testAuction.AddSeller(new Seller("slogin1", "Sid", "Wilson"));
            testAuction.AddSeller(new Seller("slogin2", "Jonas", "Jordison"));
            testAuction.AddSeller(new Seller("slogin3", "Chris", "Fehn"));
            testAuction.AddSeller(new Seller("slogin4", "James", "Root"));

            testAuction.AddCategory(new Category("Elite"));
            testAuction.AddCategory(new Category("Nom-nom"));
            testAuction.AddSeries(new Series("Musical Instruments"));
            testAuction.AddSeries(new Series("Fruits"));

            var sale1 = new Sale("Hamer Standard guitar USA 1976",
                                  new Lot("Hamer Standard guitar USA 1976", "Super Guitar!", null),
                                  testAuction.Series.FirstOrDefault(s => s.Name == "Musical Instruments"),
                                  testAuction.Sellers.First(s => s.Login == "slogin2"), 101, 3, TimeSpan.FromSeconds(1.1),
                                  testAuction.Categories.FirstOrDefault(c => c.Name == "Elite"));
            var sale2 = new Sale("Apple",
                                  new Lot("Green apple", "=^__^=", null),
                                  testAuction.Series.FirstOrDefault(s => s.Name == "Fruits"),
                                  testAuction.Sellers.First(s => s.Login == "slogin1"), 10, 1, TimeSpan.FromSeconds(4),
                                  testAuction.Categories.FirstOrDefault(c => c.Name == "Nom-nom"));
            testAuction.AddSale(sale1);
            testAuction.AddSale(sale2);

            var b1 = new Bid(sale1, 105, testAuction.Buyers.First(b => b.Login == "blogin1"));
            var b2 = new Bid(sale1, 110, testAuction.Buyers.First(b => b.Login == "blogin1"));
            var b3 = new Bid(sale1, 109, testAuction.Buyers.First(b => b.Login == "blogin2"));
            var b4 = new Bid(sale1, 111, testAuction.Buyers.First(b => b.Login == "blogin3"));
            var b5 = new Bid(sale1, 113, testAuction.Buyers.First(b => b.Login == "blogin4"));
            var b6 = new Bid(sale1, 120, testAuction.Buyers.First(b => b.Login == "blogin2"));
            var b7 = new Bid(sale1, 125, testAuction.Buyers.First(b => b.Login == "blogin3"));
            var b8 = new Bid(sale1, 150, testAuction.Buyers.First(b => b.Login == "blogin1"));

            var b9 = new Bid(sale2, 15, testAuction.Buyers.First(b => b.Login == "blogin1"));
            var b10 = new Bid(sale2, 23, testAuction.Buyers.First(b => b.Login == "blogin3"));
            var b11 = new Bid(sale2, 29, testAuction.Buyers.First(b => b.Login == "blogin1"));

            sale1.BidMaked += ReportBid;
            sale2.BidMaked += ReportBid;

            testAuction.SaleFinished += ReportSaleFinish;

            testAuction.MakeBid(b1);
            Thread.Sleep(500);
            testAuction.MakeBid(b2);    testAuction.MakeBid(b9);
            Thread.Sleep(500);
            testAuction.MakeBid(b3);
            Thread.Sleep(500);
            testAuction.MakeBid(b4);    testAuction.MakeBid(b10);
            Thread.Sleep(500);
            testAuction.MakeBid(b5);    testAuction.MakeBid(b11);
            Thread.Sleep(500);
            testAuction.MakeBid(b6);
            Thread.Sleep(500);
            testAuction.MakeBid(b7);
            Thread.Sleep(500);
            testAuction.MakeBid(b8);
            Thread.Sleep(500);

            //active Buyers (50%)
            Console.WriteLine("\nActive Buyers:");
            var activeBuyers = testAuction.GetActiveBuyers(50);
            foreach (var activeBuyer in activeBuyers)
            {
                Console.WriteLine(activeBuyer.Login);
            }
        }
Esempio n. 9
0
 public void MakeBid(Sale sale, Bid bid)
 {
     if (!Sales.Contains(sale)) return;
     if (GetUserBids(bid.Bidder).Sum(b=>b.Value) >= sale.Category.Restriction)
     {
         sale.RegisterBid(bid);
     }
 }