/*calulates the no of rooms based on the price difference and the need * calls the encoder to encode the order and places the order in the buffer*/ public void calcRooms(int i, float newPrice) { try { int need = rng.Next(0, 5); int maxRoomPriceDiff = 5000; float diff = currentPrice[i] - newPrice; currentPrice[i] = newPrice; float fraction = (diff + maxRoomPriceDiff) / (2 * maxRoomPriceDiff); int rooms = (Int32)Math.Round(fraction * need); if (rooms == 0) { return; } o1 = new Order(); int tempOrder = ++orderId; o1.setOrder(agencyId, cardNo, h[i].getHotelId(), rooms, tempOrder, newPrice); String buffer_str = EncoderDecoder.encode(o1); pendingReciept++;//increment the pending reciept with every order that is placed in buffer Buffer.setBuffer(buffer_str, agencyId, h[i].getHotelId(), tempOrder, newPrice); Thread.Sleep(rng.Next(400, 600)); return; } catch (Exception e) { Console.WriteLine(e.Message); } }
/*gets the order confirmation from the RecieptBuffer class, wheather * the order is allocated romms or is cancelled*/ public void getReciept() { try { while (pendingReciept > 0 || atLeastOneIsAlive() || agencyAlive) { if (pendingReciept >= 0) { if (atLeastOneIsDead()) { for (int i = 0; i < HotelBookingSystem.Buffer.getLength(); i++) { String x = HotelBookingSystem.Buffer.getBuffer(i); if (x.Equals("-1")) { continue; } else { //Console.WriteLine("Hi"); Order o = EncoderDecoder.decode(x); for (int j = 0; j < h.Length; j++) { if (o.getAgencyId() == agencyId && h[j].getHotelId() == o.getHotelId() && !h[j].isHotelAlive()) { Buffer.freeBuffer(i); pendingReciept--; Console.WriteLine("Agency {1}-> Cancelling Order {0} to Hotel {2} because Hotel has terminated.", o.getOrderId(), agencyId, o.getHotelId(), System.DateTime.Now); } } } } } String receipt = ReceiptBuffer.getBuffer(agencyId); if (!receipt.Equals("-1")) { Console.WriteLine("Agency {0}-> Confirmation Received: " + receipt, agencyId); pendingReciept--; } Thread.Sleep(500); } } } catch (Exception e) { Console.WriteLine(e.Message); } }
//This method is started as a thread public void runHotel() { try { ArrayList orderthreadarr = new ArrayList(); hotelAlive = true; String x = ""; new Thread(new ThreadStart(runPricingControl)).Start(); while (hotelAlive) { for (int j = 0; j < HotelBookingSystem.Buffer.getLength(); j++) { x = HotelBookingSystem.Buffer.getBuffer(j); if (x.Equals("-1")) { continue; } else { Order o = EncoderDecoder.decode(x); if (o.getHotelId() == hotelId) { HotelBookingSystem.Buffer.freeBuffer(j); totalRoomsOrdered += o.getRooms(); Console.WriteLine("Hotel {0}-> Received OrderId-{1} from Agency{2} ..PriceQuote:{3} , Rooms Ordered:{4} ", hotelId, o.getOrderId(), o.getAgencyId(), o.getQuotePrice(), o.getRooms()); Thread t = new Thread(new ParameterizedThreadStart(processOrder)); t.Start(o); orderthreadarr.Add(t); } } } Thread.Sleep(500); } foreach (Thread item in orderthreadarr) { item.Join(); } Console.WriteLine("Hotel {0}-> Terminated at Timestamp: {1}", hotelId, System.DateTime.Now); } catch (Exception e) { Console.WriteLine(e.Message); } }