// Event handler for makingOrder event // This method is executed when an orderObj is inserted into the multicell buffer public static void retrieveOrder() { String encodedOrder; Monitor.Enter(Program.mb); try { encodedOrder = Program.mb.getOneCell(); } finally { Monitor.Exit(Program.mb); } OrderObject decodedOrder = Decode(encodedOrder); Console.WriteLine(String.Format("\nNew order received -> From travel agency {0} to hotel supplier {1} for {2} rooms with card no. {3}", decodedOrder.getSenderId(), decodedOrder.getSupplierId(), decodedOrder.getAmount(), decodedOrder.getCardNo())); int supplierId = decodedOrder.getSupplierId(); if (supplierId == 1) { Console.WriteLine("Hotel 1 is processing the order"); Console.WriteLine(Program.hs1.ProcessOrder(decodedOrder)); } if (supplierId == 2) { Console.WriteLine("Hotel 2 is processing the order"); Console.WriteLine(Program.hs2.ProcessOrder(decodedOrder)); } }
// Convert the string back to object public static OrderObject Decode(String encodedString) { OrderObject decodedOrder = new OrderObject(); String[] strArr = encodedString.Split('|'); decodedOrder.setSenderId(Convert.ToInt32(strArr[0])); decodedOrder.setSupplierId(Convert.ToInt32(strArr[1])); decodedOrder.setCardNo((long)Convert.ToDouble(strArr[2])); decodedOrder.setAmount(Convert.ToInt32(strArr[3])); decodedOrder.setOrderPlacedTime(Convert.ToDateTime(strArr[4])); return(decodedOrder); }
public void placeOrder() { numRooms = Program.rand.Next(1, 10); int supplierId = Program.rand.Next(1, 3); OrderObject orderObj = new OrderObject(agencyId, supplierId, cardNo, numRooms); String encodedOrder = EncoderDecoder.Encode(orderObj); // Pushing the order into multicellbuffer Monitor.Enter(Program.mb); try { //Console.WriteLine("Thread " + Thread.CurrentThread.Name + " Setting Order"); Program.mb.setOneCell(encodedOrder); } finally { Monitor.Exit(Program.mb); } }
public String ProcessOrder(OrderObject order) { bool isValidCardno = false; int numRooms = order.getAmount(); Console.WriteLine("Checking card validity"); // Check validity of cardNo long cardNo = order.getCardNo(); if (cardNo > 5000000000000000 && cardNo < 7000000000000000) { isValidCardno = true; } if (isValidCardno) { Console.WriteLine("\tCardNo is valid"); if (numRooms <= this.capacity) { this.capacity -= numRooms; Console.WriteLine(String.Format("\tCost per room = {0}", hotelPrice)); // Time between order placed and order processed TimeSpan timeToProcessOrder = DateTime.Now - order.getOrderPlacedTime(); return(String.Format("ORDER CONFIRMED - Total amount = {0}, order process time = {1} milliseconds", numRooms * hotelPrice, timeToProcessOrder.Milliseconds)); } else { return("ORDER DECLINED - There are no sufficient rooms in this hotel"); } } else { return("ORDER DECLINED - Card is invalid"); } }
// Convert the object into a string public static string Encode(OrderObject orderObj) { return(String.Format("{0}|{1}|{2}|{3}|{4}", orderObj.getSenderId(), orderObj.getSupplierId(), orderObj.getCardNo(), orderObj.getAmount(), orderObj.getOrderPlacedTime())); }