public static Order decode(string orderStr) { CultureInfo provider = CultureInfo.InvariantCulture; Order order = new Order(); /* begin parsing orderStr */ order.setThreadId(Int32.Parse(orderStr.Substring(0, (orderStr.IndexOf(';'))))); order.setCardNum(Int32.Parse(orderStr.Substring(orderStr.IndexOf(';') + 1, 4))); order.setAmount(Int32.Parse(orderStr.Substring((orderStr.LastIndexOf(';')) + 1, orderStr.IndexOf(',') - orderStr.LastIndexOf(';') - 1))); order.setUnitPrice(Int32.Parse(orderStr.Substring((orderStr.IndexOf(',') + 1), orderStr.IndexOf('=') - orderStr.IndexOf(',') - 1))); order.setStartTime(DateTime.ParseExact(orderStr.Substring(orderStr.IndexOf('=') + 1), Order.myDateFormat, provider)); /* end parsing orderStr */ return(order); }
// method to be run as the 'retailer thread' public void retailerFunc() { while (Program.ChickenFarmIsAlive()) { //sleep on startup of the loop (whenever the retailer finishes ordering) Thread.Sleep(100); if (priceWasCutArray[getId()]) { Random randNum = new Random(); // set the number of chickens to order Int32 numOfChickensToOrder = 980 / (nPrice); Order newOrder = new Order(); newOrder.setUnitPrice(nPrice); newOrder.setAmount(numOfChickensToOrder); //Sets the order credit card number between 1000 and 9999 newOrder.setCardNum(randNum.Next(1000, 9999)); newOrder.setThreadId(this.myId); //encode the order as a string String encodedOrder = Encoder.encode(newOrder); // keep sleeping until the buffer has an open cell while (Program.orderBuffer.IsFull()) { Thread.Sleep(randNum.Next(10, 100)); } // lock the buffer so the thread can input its encoded order lock (Program.orderBuffer) { Program.orderBuffer.setCell(encodedOrder); } // indicate that the ordering has been submitted and that the thread // shouldn't order again until the price is cut again priceWasCutArray[getId()] = false; } } }
// method to be run as the 'retailer thread' public void retailerFunc() { while (Program.ChickenFarmIsAlive()) { //sleep on startup of the loop (whenever the retailer finishes ordering) Thread.Sleep(100); if (priceWasCutArray[getId()]) { Random randNum = new Random(); // set the number of chickens to order Int32 numOfChickensToOrder = 980 / (nPrice); Order newOrder = new Order(); newOrder.setUnitPrice(nPrice); newOrder.setAmount(numOfChickensToOrder); //Sets the order credit card number between 1000 and 9999 newOrder.setCardNum(randNum.Next(1000, 9999)); newOrder.setThreadId(this.myId); //encode the order as a string String encodedOrder = Encoder.encode(newOrder); // keep sleeping until the buffer has an open cell while (Program.orderBuffer.IsFull()) { Thread.Sleep(randNum.Next(10, 100)); } // lock the buffer so the thread can input its encoded order lock (Program.orderBuffer) { Program.orderBuffer.setCell(encodedOrder); } // indicate that the ordering has been submitted and that the thread // shouldn't order again until the price is cut again priceWasCutArray[getId()] = false; } } }