public void BuyThing(Thing thing, int quantity, string thingID, ref ConcurrentDictionary<string,  Thing> things)
        {
            //     StockController thingStockController = new StockController(_things);
            ////    things.AddOrUpdate(thingID, thing, (key, val) => { thing.quantity = thing.quantity + quantity; return thing; });// oldThing.quantity
            things.AddOrUpdate(thingID, thing, (key, val) => { thing.quantity = thing.quantity + 100; return thing; });// oldThing.quantity
               //     Interlocked.Add(ref _totalQuantityBought, thing.quantity);                                                                                                     //  thingStockController.showThings();
            //foreach (var thingy in things)
            //{
            //    Console.WriteLine("thing: key " + thingy.Key + " value: " + thingy.Value.name + " price: " + thingy.Value.price
            //            + " Quantity: " + thingy.Value.quantity);
            //    Console.ReadLine();

            //}
        }
        public void BuyThing(Thing thing, int quantity)
        {
            //things.AddOrUpdate(3, newThing, (key, oldvalue) => newThing);

            _stock.AddOrUpdate("brightshinyThing", thing, (key, val) => { thing.quantity = thing.quantity + quantity;  return thing; });// oldThing.quantity
        }
 private static void GetOrAdd(ConcurrentDictionary<string, Thing> things)
 {
     Thing newerThing = new Thing();
     newerThing.ID = 10; newerThing.name = "newestThing"; newerThing.price = 99.99;
     Thing think = things.GetOrAdd("wingdingThing", newerThing);
     Console.WriteLine(think.ID.ToString() + " " + think.name + " ", think.price);
 }
        private static void AddorUpdate(ConcurrentDictionary<string, Thing> things)
        {
            Thing newThing = new Thing();
            newThing.ID = 3; newThing.name = "fourthThing"; newThing.price = 85.54;
            //to return the value just added, use the return value. Any later retrieval of this indexed item
            //could have been changed by another thread.
            Thing think = things.AddOrUpdate("worthlessThing", newThing, (key, oldvalue) => newThing);

            Console.WriteLine(think.ID.ToString() + " " + think.name + " ", think.price);
        }
Пример #5
0
        public void test_BuyThings()
        {
            // var stockController = new
            var stockController = new ConcurrentCollections.StockController();
              //      stockController.LoadCurrentStock();
            //  ConcurrentDictionary<string, ConcurrentCollections.Thing> stock = new ConcurrentDictionary<string, ConcurrentCollections.Thing>();
            //  stock = prog.GetCurrentStock();

            //Add five items to the Things collection
            var Thing1 = new ConcurrentCollections.Thing()
            {
                ID = 1111,
                name = "UnitTest1Thing",
                price = 11.11,
                quantity = 0
            };
            var Thing2 = new ConcurrentCollections.Thing()
            {
                ID = 2222,
                name = "UnitTest2Thing",
                price = 22.22,
                quantity = 0
            };

            var Thing3 = new ConcurrentCollections.Thing()
            {
                ID = 3333,
                name = "UnitTest3Thing",
                price = 33.33,
                quantity = 0
            };

            var Thing4 = new ConcurrentCollections.Thing()
            {
                ID = 4444,
                name = "UnitTest4Thing",
                price = 44.44,
                quantity = 0
            };

            var Thing5 = new ConcurrentCollections.Thing()
            {
                ID = 5555,
                name = "UnitTest5Thing",
                price = 55.55,
                quantity = 0
            };
            //first, add items to collection with zero inventory

            //Task t1 = Task.Run(() => stockController.BuyThing(Thing1, 0, "UnitTestThing"));
            //Task t2 = Task.Run(() => stockController.BuyThing(Thing2, 0, "UnitTestThing"));
            //Task t3 = Task.Run(() => stockController.BuyThing(Thing3, 0, "UnitTestThing"));
            //Task t4 = Task.Run(() => stockController.BuyThing(Thing4, 0, "UnitTestThing"));
            //Task t5 = Task.Run(() => stockController.BuyThing(Thing5, 0, "UnitTestThing"));

            //stockController.BuyThing(Thing1, 0, "UnitTestThing1", _testThings);
            //stockController.BuyThing(Thing2, 0, "UnitTestThing2", _testThings);
            stockController.BuyThing(Thing3, 0, "UnitTestThing3", ref _testThings);
            //stockController.BuyThing(Thing4, 0, "UnitTestThing4", _testThings);
            //stockController.BuyThing(Thing5, 0, "UnitTestThing5", _testThings);

            TimeSpan workDay = new TimeSpan(0,10,0);
            simulateWork(stockController, workDay);
            foreach (var thingy in _testThings)
            {
                Console.WriteLine("thing: key " + thingy.Key + " value: " + thingy.Value.name + " price: " + thingy.Value.price
                        + " Quantity: " + thingy.Value.quantity);
                Console.ReadLine();

            }
        }
Пример #6
0
 private void simulateWork(StockController stockController, TimeSpan workDay)
 {
     Random rand = new Random();
     DateTime start = DateTime.Now;
     while  (DateTime.Now - start < workDay)
     {
         Thread.Sleep(rand.Next(100));
         bool buy = (rand.Next(6) == 0);
         // string itemName =
         //   if (buy)
         if (true)
         {
             int quantity = rand.Next(9) + 1;
             var thing = new ConcurrentCollections.Thing()
             {
                 ID = 3333,
                 name = "UnitTestThing3"
                 //price = 33.33,
                 //quantity = quantity
             };
             stockController.BuyThing(thing, quantity, "UnitTestThing3", ref _testThings);
         }
     }
 }