// This method receives an order from the buffer, decodes using a Decoder object, and then passes it to an OrderProcessor object public void receiveOrder() { // Grabs an order from the buffer in encoded string format string encodedOrder = Program.buffer.getOneCell(); // Initialize our Decoder and OrderProcessing objects Decoder decode = new Decoder(encodedOrder); OrderProcessing processor = new OrderProcessing(); // Decode the string back into an OrderClass object and pass it to our OrderProcessing thread OrderClass order = decode.getOrder(); Thread t = new Thread(() => processor.process(order, getPrice())); // Start the OrderProcessing thread t.Start(); }
/* This method generates an order with a random card number and random quantity. * Then generates an OrderClass object which then is encoded to a string, which gets placed into our buffer. * Finally, it then signals to the ChickenFarm that an order has been generated using the delegate event. */ private void generateOrder(string senderID) { // Generate random valid numbers for the card number, and quantity int cardNo = rng.Next(5000, 7000); int amount = rng.Next(25, 100); // Generate OrderClass object, then encode to a string using our Encoder object OrderClass order = new OrderClass(senderID, cardNo, amount); Encoder encode = new Encoder(order); string encoded = encode.getEncodedString(); // Print the generation confirmation Console.WriteLine("Store {0}'s order generated, Time-stamp: {1}", senderID, DateTime.Now.ToString("hh:mm:ss")); // Place the encoded string into our buffer Program.buffer.setOneCell(encoded); // Signal to our ChickenFarm that the order has been generated using our delegate event orderGenerated(); }
/* This method will process the order and payment by checking the card number validity first, and then calculates total price. * Once total price calculated, the delegate event orderProcessed signals to ChickenFarm that the thread has completed * order processing. */ public void process(OrderClass order, int unitPrice) { // Check first if the card number is valid if (!checkCreditCardNumber(order.getCardNo())) { // Write an error to console if there's an error and then return Console.WriteLine("Card number {0} is not valid.", order.getCardNo()); return; } else { // Calculate the subtotal first double subtotal = (unitPrice * order.getAmount()); // Then calculate the tax amount double taxes = (subtotal * TAX); // Subtotal + Taxes + shipping and handling int total = Convert.ToInt32(subtotal + taxes + SHIPPING_HANDLING); // Signal to the retailer that the order has been processed orderProcessed(order.getSenderID(), total, unitPrice, order.getAmount()); } }