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; } }
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; }
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); }
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)); }
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); }
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); } }
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; }
//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); } }
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); }
// 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()); } } }
// 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; } } }
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); }
// Constructor public TravelAgency() { currentPrice = 500; placeOrder = false; orderToBePlaced = null; }
public OrderProcessing(OrderClass order) { this.order = order; }
//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); }