//place order takes the price per room. public void placeOrder(int price, string name) { lock (this) { cardNo = rand.Next(5000, 7000); amount = rand.Next(1, 5); //it generates a random number of rooms between 1 & 5 to be booked for the thread. DateTime orderTime = DateTime.Now; OrderClass o = new OrderClass(SenderID, amount, name, cardNo, price, orderTime); //creates an order using the OrderClass. encodedOrder = Encoder.Encrypt(o, "ABCDEFGHIJKLMNOP"); // sends the order for encoding. Program.mb.setOneCell(encodedOrder); //encoded string received and pushed to the empty cell in buffer. } }
//order processing thread enters this method. public void orderProcessing() { for (int i = 0; i < 50; i++) //since there are 5 travel agencies to book the rooms for 1 hotel and the price cut event is called 10 times per hotel, there will be a total of 50 bookings per hotel. I have used 2 OrderProcessing threads for 2 different HotelSuppliers { string order = Program.mb.getOneCell(); // Gets a cell from the buffer. OrderClass final = Decoder.Decrypt(order, "ABCDEFGHIJKLMNOP"); //Decode the order and store it in the object. //card processing, price calculation, time span for each order and sending order confirmation to the TravelAgency by calling orderConfirm event. if (initialRooms > 0 && final.cardNo >= 5000 && final.cardNo <= 7000) { try { Monitor.Wait(this); } catch (Exception e) { } initialRooms = initialRooms - final.amount; double total = final.amount * final.price; double tax = (total * 12) / 100; double finalprice = Convert.ToDouble(total) + tax; DateTime orderTime = final.orderTime; DateTime timeStamp = DateTime.Now; TimeSpan totTime = timeStamp.Subtract(orderTime); Console.WriteLine("\n\n*******************************************************************************************************\n\t\t\t\t\t\tOrder Confirmation:\n1. Booking Id: " + (++bookingId) + "\n2. Sender Id: " + final.senderId + "\n3. Receiver Id: " + final.receiverId + "\n4. Number of Rooms: " + final.amount + "\n5. Card Number: " + final.cardNo + "\n6. Unit Price: " + final.price + "\n7. Total Amount to be paid: " + finalprice + "\n*******************************************************************************************************"); count++; //calling orderConfirm event and sending the confirmation to the TravelAgency. if (orderConfirm != null) { orderConfirm(final.senderId, bookingId, timeStamp, totTime); } } else if (initialRooms > 0) { Console.WriteLine("\n\n*******************************************************************************************************\n\t\t\t\t\t\tOrder Confirmation:\n1. Booking Id: " + (++bookingId) + "\nCredit Card Number is not correct\n*******************************************************************************************************"); } else { Console.WriteLine("\n\n*******************************************************************************************************\n\t\t\t\t\t\tOrder Confirmation:\n1.Booking Id: " + (++bookingId) + "\nNot enough rooms available in " + receiverID + "\n*******************************************************************************************************"); } //Thread.CurrentThread.Join(); } }
public static OrderClass Decrypt(String input, string key) { Byte[] inputArray = Convert.FromBase64String(input); TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider(); tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key); tripleDES.Mode = CipherMode.ECB; tripleDES.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tripleDES.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length); tripleDES.Clear(); string result = UTF8Encoding.UTF8.GetString(resultArray); string[] res = result.Split(','); DateTime od = Convert.ToDateTime(res[5]); OrderClass obj = new OrderClass(res[0], Convert.ToInt32(res[1]), res[2], Convert.ToInt32(res[3]), Convert.ToInt32(res[4]), od); return(obj); }