public String GetOneCell(String receiverId)
        {
            lock (buffer)
            {
                OrderClass order = new OrderClass();
                String orderStr = null;
                int orderIndex = -1;

                for (int i = 0; i < buffer.Length; i++)
                {
                    if (buffer[i] != null)
                    {
                        order = Decoder.DecodeOrder(buffer[i]);

                        if (order.GetReceiverId() == receiverId)
                        {
                            orderStr = buffer[i];
                            orderIndex = i;
                        }
                    }
                }

                if (orderStr != null)
                {
                    buffer[orderIndex] = null;
                }
                return orderStr;
            }
        }
Exemplo n.º 2
0
        public static String EncodeOrder(OrderClass order)
        {
            String ordStr = order.GetSenderId() + "," + Convert.ToInt32(order.GetCardNo()) + "," + order.GetReceiverId()
                + "," + Convert.ToInt32(order.GetAmount()) + "," + Convert.ToInt32(order.GetUnitPrice()) + ","
                + Convert.ToString(order.GetTimestamp());

            return ordStr;
        }
Exemplo n.º 3
0
        public static String EncodeOrder(OrderClass order)
        {
            String ordStr = order.GetSenderId() + "," + Convert.ToInt32(order.GetCardNo()) + "," + order.GetReceiverId()
                            + "," + Convert.ToInt32(order.GetAmount()) + "," + Convert.ToInt32(order.GetUnitPrice()) + ","
                            + Convert.ToString(order.GetTimestamp());

            return(ordStr);
        }
Exemplo n.º 4
0
        public void saleOnCars(string plantName, int prevAmt, double prevPrice, double curPrice)
        {
            //Figure out how many cars to buy
            int        curAmt    = (int)((prevAmt * prevPrice) / curPrice);
            int        dealerNum = rng.Next(1, 6);
            OrderClass newOrder  = new OrderClass("Dealer " + (dealerNum).ToString(), cardNo, plantName, curAmt, curPrice);

            //Place order into the buffer
            Driver.orderBuffer.setOneCell(Encoder.encode(newOrder));
        }
Exemplo n.º 5
0
        public string encode(OrderClass order)
        {
            string result     = "";
            string senderId   = order.getSenderId();
            int    cardNo     = order.getCardNo();
            string receiverId = order.getReceiverId();
            int    amount     = order.getAmount();
            int    unitPrice  = order.getUnitPrice();

            result = senderId + "-" + cardNo.ToString() + "-" + receiverId + "-" + amount.ToString() + "-" + unitPrice.ToString();
            return(result);
        }
Exemplo n.º 6
0
        public void procOrder(OrderClass order)
        {
            int cardNo = order.getCardNo();

            if (cardNo > 5000 && cardNo < 7000)
            {
                //(car price * order amount) + tax
                double orderTotal = (order.getUnitPrice() * order.getAmount()) * (1.080);
                //Place confirmation in the buffer
                Driver.confirmBuffer.setConfirm(order.getSenderId(), orderTotal);
            }
        }
Exemplo n.º 7
0
 public static OrderClass DecodeOrder(String orderStr)
 {
     OrderClass order = new OrderClass();
     Char[] delimiter = {','};
     String[] strArray = orderStr.Split(delimiter);
     order.SetSenderId(strArray[0]);
     order.SetCardNo(Convert.ToInt32(strArray[1]));
     order.SetReceiverId(strArray[2]);
     order.SetAmount(Convert.ToInt32(strArray[3]));
     order.SetUnitPrice(Convert.ToInt32(strArray[4]));
     order.SetTimestamp(Convert.ToDateTime(strArray[5]));
     return order;
 }
Exemplo n.º 8
0
        //Pricing Model takes into account the total amount of orders to the plants and whether the day the order was made during a weekday or a weekend
        public void pricingModel(string name)
        {
            //Once 20 price cuts have been achieved the thread will die
            while (p < 20)
            {
                //Console.WriteLine("Thread for" + name);
                Console.WriteLine("p = " + p);
                Console.WriteLine(name + " total amounts of orders: " + orderTotal);
                Thread.Sleep(500);
                string orderDescrip = "";
                orderDescrip = Driver.orderBuffer.getOneCell();

                if (orderDescrip != "")
                {
                    OrderClass order = Decoder.decode(orderDescrip);
                    //if the order receiver ID matches the plant then process the order and start a new thread
                    if (name == order.getReceiverId())
                    {
                        amountOfUnits = order.getAmount();
                        Thread orderProc = new Thread(new ThreadStart(() => op.procOrder(order)));
                        orderProc.Start();
                        Driver.orderBuffer.eraseOneCell(Encoder.encode(order));
                        orderTotal++;
                    }
                }
                double    value = 0;
                DayOfWeek day   = DateTime.Now.DayOfWeek;//check current day of the week
                //If the total order amount for the plant is greater than 3 and it is a weekday, the new car price will be between 251 and 500
                if (orderTotal > 3 && (day >= DayOfWeek.Monday) && (day <= DayOfWeek.Friday))
                {
                    value = rng.Next(251, 500);
                }
                //If the total order amount for the plant is less than or equal to 3 and it is a weekday, the new car price will be between 50 and 250
                else if (orderTotal <= 3 && (day >= DayOfWeek.Monday) && (day <= DayOfWeek.Friday))
                {
                    value = rng.Next(50, 250);
                }
                //If the total order amount for the plant is greater than 3 and it is a weekend, the new car price will be between 251 and 500
                else if (orderTotal > 3 && (day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
                {
                    value = rng.Next(251, 500);
                }
                //If the total order amount for the plant is less than or equal to 3 and it is a weekend, the new car price will be between 50 and 250
                else if (orderTotal <= 3 && (day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
                {
                    value = rng.Next(50, 250);
                }

                Plant.changePrice(name, amountOfUnits, price, value);
            }
        }
Exemplo n.º 9
0
        public static OrderClass DecodeOrder(String orderStr)
        {
            OrderClass order = new OrderClass();

            Char[]   delimiter = { ',' };
            String[] strArray  = orderStr.Split(delimiter);
            order.SetSenderId(strArray[0]);
            order.SetCardNo(Convert.ToInt32(strArray[1]));
            order.SetReceiverId(strArray[2]);
            order.SetAmount(Convert.ToInt32(strArray[3]));
            order.SetUnitPrice(Convert.ToInt32(strArray[4]));
            order.SetTimestamp(Convert.ToDateTime(strArray[5]));
            return(order);
        }
Exemplo n.º 10
0
        // Thread method
        public void TravelAgencyFunc()
        {
            while (MainProgram.airlineThreadCount > 0)
            {
                // Check if an order should be placed
                if (placeOrder)
                {
                    orderToBePlaced.SetSenderId(Thread.CurrentThread.Name);
                    orderToBePlaced.SetTimestamp(DateTime.Now);

                    // print order to the console
                    Console.WriteLine(orderToBePlaced.GetSenderId() + " has placed an order for " + orderToBePlaced.GetAmount()
                                      + " tickets for " + orderToBePlaced.GetUnitPrice() + " each at " + orderToBePlaced.GetTimestamp());

                    // Encode the order for transmission
                    String orderStr = Encoder.EncodeOrder(orderToBePlaced);

                    // Write to buffer
                    MainProgram.orderBuffer.sem.WaitOne();
                    MainProgram.orderBuffer.SetOneCell(orderStr);

                    // Update current price
                    currentPrice = orderToBePlaced.GetUnitPrice();
                    placeOrder   = false;
                }

                // Read a confirmation from the buffer
                String confString = MainProgram.confirmationBuffer.GetOneCell(Thread.CurrentThread.Name);

                // If a confirmation was received
                if (confString != null)
                {
                    MainProgram.confirmationBuffer.sem.Release(1);
                    OrderClass order = Decoder.DecodeOrder(confString);
                    Console.WriteLine(Thread.CurrentThread.Name + " received order confirmation from " + order.GetSenderId() + " for "
                                      + order.GetAmount() + " at " + order.GetUnitPrice() + " each ordered at " + order.GetTimestamp());
                }
            }
        }
Exemplo n.º 11
0
        // Event handler for the Airline to call when a price-cut event occurs
        public void TicketPriceCut(String airlineId, int newPrice)
        {
            if (!placeOrder)
            {
                int orderAmount = 0;
                int priceDifference = currentPrice - newPrice;

                // Only proceed if new price is less than current price
                if (priceDifference > 0)
                {
                    if (priceDifference < 100)
                    {
                        orderAmount = 5;
                    }
                    else if (priceDifference < 300)
                    {
                        orderAmount = 15;
                    }
                    else if (priceDifference < 500)
                    {
                        orderAmount = 25;
                    }
                    else if (priceDifference <= 800)
                    {
                        orderAmount = 35;
                    }

                    // Create a new order
                    orderToBePlaced = new OrderClass();
                    orderToBePlaced.SetCardNo(6000);
                    orderToBePlaced.SetReceiverId(airlineId);
                    orderToBePlaced.SetAmount(orderAmount);
                    orderToBePlaced.SetUnitPrice(newPrice);

                    placeOrder = true;
                }
            }
        }
Exemplo n.º 12
0
        // Event handler for the Airline to call when a price-cut event occurs
        public void TicketPriceCut(String airlineId, int newPrice)
        {
            if (!placeOrder)
            {
                int orderAmount     = 0;
                int priceDifference = currentPrice - newPrice;

                // Only proceed if new price is less than current price
                if (priceDifference > 0)
                {
                    if (priceDifference < 100)
                    {
                        orderAmount = 5;
                    }
                    else if (priceDifference < 300)
                    {
                        orderAmount = 15;
                    }
                    else if (priceDifference < 500)
                    {
                        orderAmount = 25;
                    }
                    else if (priceDifference <= 800)
                    {
                        orderAmount = 35;
                    }

                    // Create a new order
                    orderToBePlaced = new OrderClass();
                    orderToBePlaced.SetCardNo(6000);
                    orderToBePlaced.SetReceiverId(airlineId);
                    orderToBePlaced.SetAmount(orderAmount);
                    orderToBePlaced.SetUnitPrice(newPrice);

                    placeOrder = true;
                }
            }
        }
Exemplo n.º 13
0
        public void AirlineFunc()
        {
            while (priceCutCounter <= 20)
            {
                Thread.Sleep(100);
                GenerateTicketPrice();

                // Read an order from the buffer
                String orderStr = MainProgram.orderBuffer.GetOneCell(Thread.CurrentThread.Name);

                // If an order for the airline was received, process order
                if (orderStr != null)
                {
                    OrderClass order = Decoder.DecodeOrder(orderStr);
                    MainProgram.orderBuffer.sem.Release(1);
                    orderCounter++;
                    ticketsAvailable -= order.GetAmount();
                    OrderProcessing orderProc       = new OrderProcessing(order);
                    Thread          orderProcThread = new Thread(new ThreadStart(orderProc.ProcessNewOrder));
                    orderProcThread.Start();
                }
            }
            MainProgram.airlineThreadCount--;
        }
        /*
         * decodeStringToObject
         * This function reads splits the human readable string and populates
         * the OrderClass object accordingly.
         */
        public OrderClass decodeStringToObject(String inputEncrypted)
        {
            string inputDecrpyted = decryptData(inputEncrypted);
            string[] stringSeparators = new string[] { ":::" };
            string[] str = inputDecrpyted.Split(stringSeparators, StringSplitOptions.None);
            int amount = Convert.ToInt32(str[0]);
            long cardNo = Convert.ToInt64(str[1]);
            int noOfChickens = Convert.ToInt32(str[2]);
            int senderID = Convert.ToInt32(str[3]);
            DateTime dateTimeStamp = Convert.ToDateTime(str[4]);

            OrderClass orderObj = new OrderClass();
            orderObj.setAmount(amount);
            orderObj.setCardNo(cardNo);
            orderObj.setNoOfChickens(noOfChickens);
            orderObj.setSenderID(senderID);
            orderObj.setDateTimeStamp(dateTimeStamp);

            return orderObj;
        }
 /*
  * chickensOnSale
  * This is an Event Handler function.
  */
 public void chickensOnSale(int price, int prev)
 {
     OrderClass orderObj = new OrderClass(this.retailerID, this.cardNumber, getNoOfChickens(), price);
     object ord = (object)orderObj;
     Thread chickenOnSale_thread = new Thread(new ParameterizedThreadStart(placeOrderFunc));
     chickenOnSale_thread.Start(ord);
 }
Exemplo n.º 16
0
 // Constructor
 public TravelAgency()
 {
     currentPrice = 500;
     placeOrder = false;
     orderToBePlaced = null;
 }
Exemplo n.º 17
0
 // Constructor
 public TravelAgency()
 {
     currentPrice    = 500;
     placeOrder      = false;
     orderToBePlaced = null;
 }
Exemplo n.º 18
0
 public OrderProcessing(OrderClass order)
 {
     this.order = order;
 }
Exemplo n.º 19
0
 //encode order data
 private string encoder(OrderClass order)
 {
     return(Convert.ToBase64String(Encoding.UTF8.GetBytes(order.ToString())));
 }
 /*
  * encodeObjectToString
  * This function reads the members of the object and coverts it into a human readable
  * string. Then this is passed to ecryption algorithm, which is no longer
  * comprehedible by human.
  */
 public String encodeObjectToString(OrderClass orderClass)
 {
     int amount = orderClass.getAmount();
     long cardNo = orderClass.getCardNo();
     int noOfChickens = orderClass.getNoOfChickens();
     int senderID = orderClass.getSenderID();
     DateTime timeStamp = orderClass.getDateTimeStamp();
     String strToEncode = amount.ToString() + ":::" + cardNo.ToString() + ":::" + noOfChickens.ToString() + ":::" + senderID.ToString() + ":::" + timeStamp.ToString();
     return encryptData(strToEncode);
 }
Exemplo n.º 21
0
 public OrderProcessing(OrderClass order)
 {
     this.order = order;
 }