/* * Encoder is a class or a method in a class: The Encoder will convert an OrderObject into a string. You * can choose any way to encode the values into a string, as long as you can decode the string back to the * original order object */ public static string encode(OrderClass obj) { string encode; encode = obj.getSenderId() + "," + obj.getCardNo().ToString() + ",PUBID:" + obj.getRecieverID() + "," + obj.getAmount().ToString() + "," + obj.getUnitPrice().ToString() + "," + obj.getTimeStamp() + "," + obj.getOrderNumber(); return(encode); }
/* * Decoder is a class or a method in a class: The Decoder will convert the encoded string back into the * OrderObject */ public static OrderClass decode(string encode) { //use delimiter of comma char delimit = ','; string[] arrayofencode = encode.Split(delimit); //back to order object OrderClass newobj = new OrderClass(arrayofencode[0], Convert.ToInt32(arrayofencode[1]), arrayofencode[2].Substring(arrayofencode[2].IndexOf(":") + 1), Convert.ToInt32(arrayofencode[3]), Convert.ToDouble(arrayofencode[4]), Convert.ToInt32(arrayofencode[6]), arrayofencode[5]); return(newobj); }
public static OrderClass decode(string encode) { //use delimiter of comma char delimit = { ' , ' }; string[] arrayofencode = encode.Split(delimit); //back to order object OrderClass newobj = new OrderClass(arrayofencode[0], Convert.ToInt32(arrayofencode[1]), arrayofencode[2], Convert.ToInt32(arrayofencode[3]), Convert.ToInt32(arrayofencode[4])); return(newobj); }
public void OrderProcessing() { string encodedString = mb.getObject(id); //check to see if the retrieved object pertains to the current publisher //if so, process order if (encodedString != "ERROR") { OrderClass order = Coders.decode(encodedString); Console.WriteLine("Publisher {0} is processing order {1} sent from Bookstore {2}", order.getRecieverID(), order.getOrderNumber(), order.getSenderId()); double totalCharge = (order.getUnitPrice() * order.getAmount()) * (1 + taxPercent) + getLocCharge(); string validity = Bank.validate(order.getCardNo(), totalCharge); if (validity == "valid") { numBooks -= order.getAmount(); } callBack(validity, order.getSenderId()); } }