Esempio n. 1
0
        private OrderClass Decoder(string s)
        {
            string[] stringSeparators = new string[] { "|" };
            string[] result;
            result = s.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
            OrderClass decodedobj = new OrderClass();

            decodedobj.setSenderID(result[0]);
            decodedobj.setCardNo(Int32.Parse(result[1]));
            decodedobj.setReceiverID(result[2]);
            decodedobj.setAmount(Int32.Parse(result[3]));
            decodedobj.setUnitPrice(Int32.Parse(result[4]));
            decodedobj.setDateTime(Convert.ToDateTime(result[5]));
            return(decodedobj);
        }
Esempio n. 2
0
        public void carDealerFunc()
        {
            Plant  carPlant  = new Plant();
            Plant2 carPlant2 = new Plant2();
            String receiverName;
            Int32  parkingLot = 10;
            Int32  creditCard = rng.Next(5000, 7000);
            Int32  carPrice;
            Int32  carPrice2; //Plant2 carprice
            Int32  dealPrice;
            Int32  plantIndex;

            for (Int32 i = 0; i < 10; i++)
            {
                Thread.Sleep(rng.Next(500, 1000));
                carPrice  = carPlant.getPrice();
                carPrice2 = carPlant2.getPrice();
                if (carPrice > carPrice2)
                {
                    dealPrice  = carPrice2;
                    plantIndex = 2;
                }
                else
                {
                    dealPrice  = carPrice;
                    plantIndex = 1;
                }
                //check if the car in the sell list
                if (!carStatus.ContainsKey(dealPrice))
                {
                    carStatus.TryAdd(dealPrice, false);
                }
                //the dealer only buy the sale car or the car price is lower than 200
                //the first dealer which have the empty places can buy the car, the others need to wait for next price cut.
                lock (order)
                {
                    if (parkingLot != 0 && carStatus[dealPrice] == false)
                    {
                        if (plantIndex == 1)
                        {
                            receiverName = carPlant.getPlantName();
                        }
                        else
                        {
                            receiverName = carPlant2.getPlantName();
                        }
                        try
                        {
                            order.setSenderID(Thread.CurrentThread.Name);
                            order.setCardNo(creditCard);
                            order.setReceiverID(receiverName);
                            order.setAmount(1);
                            order.setUnitPrice(dealPrice);
                            order.setDateTime(DateTime.Now);
                            CarProject.Cellbuffer.setOneCell(this.Encoder(order));
                        }
                        finally
                        {
                            parkingLot--;
                            carStatus[dealPrice] = true;
                            Console.WriteLine("dealer {0} buy the car, the empty lot is {1}, the price is {2}", Thread.CurrentThread.Name, parkingLot, dealPrice);
                        }
                    }
                }
                String confirmString = CarProject.confirmBuffer.getOneCell(Thread.CurrentThread.Name);
                if (!"0".Equals(confirmString))
                {
                    Console.WriteLine(confirmString);
                }
            }
        }