Home

MS Visual C# Applications:
Georgetown Cleaning Services

 

Introduction to Serialization

 

Cleaning Order Processing

To process an order, we will use the same approach as for the Georgetown Cleaning Services application to calculation the cost of a cleaning order

When an order has been processed, we will let the user save it. To make this useful and convenient, we will save cleaning orders for each day in a common file but we will give the user the opportunity to save the order for another day. To implement this behavior, we will use the serialization of an ArrayList object which itself will use a whole cleaning order stored in a variable.

 

Practical Learning Practical Learning: Creating a Serializable Class

  1. In the Solution Explorer, right-click the References node and click Add Reference...
  2. In the .NET property page of the Add Reference dialog box, select System.Runtime.Serialization.Formatters.Soap and click Select
     
    Add Reference
  3. Click OK
  4. To add a new class, on the main menu, click Project -> Add Class...
  5. Set the Name to CleaningOrder and click Open
  6. Change the file as follows:
     
    using System;
    
    namespace GCS5
    {
    	/// <summary>
    	/// Summary description for CleaningOrder.
    	/// </summary>
    	[Serializable]
    	public class CleaningOrder
    	{
    		public int      OrderID;
    		public string   OrderNumber;
    		public string   CustomerName;
    		public string   CustomerPhone;
    		public DateTime DateLeft;
    		public DateTime TimeLeft;
    		public DateTime DatePickedUp;
    		public DateTime TimePickedUp;
    		public decimal  UnitPriceShirts;
    		public int      QuantityShirts;
    		public decimal  SubTotalShirts;
    		public decimal  UnitPricePants;
    		public int      QuantityPants;
    		public decimal  SubTotalPants;
    		public string   Item1Name;
    		public decimal  UnitPriceItem1;
    		public int      QuantityItem1;
    		public decimal  SubTotalItem1;
    		public string   Item2Name;
    		public decimal  UnitPriceItem2;
    		public int      QuantityItem2;
    		public decimal  SubTotalItem2;
    		public string   Item3Name;
    		public decimal  UnitPriceItem3;
    		public int      QuantityItem3;
    		public decimal  SubTotalItem3;
    		public string   Item4Name;
    		public decimal  UnitPriceItem4;
    		public int      QuantityItem4;
    		public decimal  SubTotalItem4;
    		public decimal  CleaningTotal;
    		public decimal  TaxRate;
    		public decimal  TaxAmount;
    		public decimal  OrderTotal;
    
    		public CleaningOrder()
    		{
    			OrderID         = 0;
    			OrderNumber     = "010119000";
    			CustomerName    = "Unknown";
    			CustomerPhone   = "N/A";
    			DateLeft        = DateTime.Today;
    			TimeLeft        = DateTime.Now;
    			DatePickedUp    = DateTime.Today;;
    			TimePickedUp    = DateTime.Now;
    			UnitPriceShirts = 0.95M;
    			QuantityShirts  = 0;
    			SubTotalShirts  = 0.00M;
    			UnitPricePants  = 1.75M;
    			QuantityPants   = 0;
    			SubTotalPants   = 0.00M;
    			Item1Name	    = "None";
    			UnitPriceItem1  = 0.00M;
    			QuantityItem1   = 0;
    			SubTotalItem1   = 0.00M;
    			Item2Name	    = "None";
    			UnitPriceItem2  = 0.00M;
    			QuantityItem2   = 0;
    			SubTotalItem2   = 0.00M;
    			Item3Name	    = "None";
    			UnitPriceItem3  = 0.00M;
    			QuantityItem3   = 0;
    			SubTotalItem3   = 0.00M;
    			Item4Name	    = "None";
    			UnitPriceItem4  = 0.00M;
    			QuantityItem4   = 0;
    			SubTotalItem4   = 0.00M;
    			CleaningTotal   = 0.00M;
    			TaxRate         = 5.75M;
    			TaxAmount       = 0.00M;
    			OrderTotal      = 0.00M;
    		}
    	}
    }
  7. Display the Cleaning Orders form and double-click an unoccupied area of its body
  8. In the top section of the file, type:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
    
    namespace GCS5
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class OrderProcessing : System.Windows.Forms.Form
    	{
    		ArrayList CleaningOrders;
  9. Implement the Load event as follows:
     
    private void OrderProcessing_Load(object sender, System.EventArgs e)
    {
    	CleaningOrders = new ArrayList();
    }
  10. Return to the Cleaning Orders form and double-click the Save button
  11. Implement its event as follows:
     
    private void btnSave_Click(object sender, System.EventArgs e)
    {
    	int orderID = 0;
    	ArrayList CleaningOrders = new ArrayList();
    	string strFilename = "CleaningOrders.gcs";
    	SoapFormatter gcsSoap = new SoapFormatter();
    
    	if( File.Exists(strFilename) )
    	{
    FileStream gcsStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		CleaningOrders = (ArrayList)gcsSoap.Deserialize(gcsStream);
    
    		CleaningOrder order = (CleaningOrder)CleaningOrders[CleaningOrders.Count-1];
    		orderID = order.OrderID;
    		gcsStream.Close();
    	}
    	else
    	{
    FileStream gcsStream = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    		gcsSoap.Serialize(gcsStream, CleaningOrders);
    		gcsStream.Close();
    	}
    
    	if( this.txtCustomerName.Text == "" )
    	{
    	MessageBox.Show("A cleaning order must have a name for the customer");
    		this.txtCustomerName.Focus();
    		return;
    	}
    
    	CalculateTotal();
    
    	CleaningOrder cleanOrder   = new CleaningOrder();
    	cleanOrder.OrderID         = orderID + 1;
    	cleanOrder.CustomerName    = this.txtCustomerName.Text;
    	cleanOrder.CustomerPhone   = this.txtCustomerPhone.Text;
    	cleanOrder.DateLeft        = this.dtpDateLeft.Value;
    	cleanOrder.TimeLeft        = this.dtpTimeLeft.Value;
    	cleanOrder.DatePickedUp    = this.dtpDateExpected.Value;
    	cleanOrder.TimePickedUp    = this.dtpTimeExpected.Value;
    	cleanOrder.UnitPriceShirts = decimal.Parse(this.txtShirtsUnitPrice.Text);
    	cleanOrder.QuantityShirts  = int.Parse(this.txtShirtsQuantity.Text);
    	cleanOrder.SubTotalShirts  = decimal.Parse(this.txtShirtsSubTotal.Text);
    	cleanOrder.UnitPricePants  = decimal.Parse(this.txtPantsUnitPrice.Text);
    	cleanOrder.QuantityPants   = int.Parse(this.txtPantsQuantity.Text);
    	cleanOrder.SubTotalPants   = decimal.Parse(this.txtPantsSubTotal.Text);
    	cleanOrder.Item1Name       = this.cboItem1.Text;
    	cleanOrder.UnitPriceItem1  = decimal.Parse(this.txtItem1UnitPrice.Text);
    	cleanOrder.QuantityItem1   = int.Parse(this.txtItem1Quantity.Text);
    	cleanOrder.SubTotalItem1   = decimal.Parse(this.txtItem1SubTotal.Text);
    	cleanOrder.Item2Name       = this.cboItem2.Text;
    	cleanOrder.UnitPriceItem2  = decimal.Parse(this.txtItem2UnitPrice.Text);
    	cleanOrder.QuantityItem2   = int.Parse(this.txtItem2Quantity.Text);
    	cleanOrder.SubTotalItem2   = decimal.Parse(this.txtItem2SubTotal.Text);
    	cleanOrder.Item3Name       = this.cboItem3.Text;
    	cleanOrder.UnitPriceItem3  = decimal.Parse(this.txtItem3UnitPrice.Text);
    	cleanOrder.QuantityItem3   = int.Parse(this.txtItem3Quantity.Text);
    	cleanOrder.SubTotalItem3   = decimal.Parse(this.txtItem3SubTotal.Text);
    	cleanOrder.Item4Name       = this.cboItem4.Text;
    	cleanOrder.UnitPriceItem4  = decimal.Parse(this.txtItem4UnitPrice.Text);
    	cleanOrder.QuantityItem4   = int.Parse(this.txtItem4Quantity.Text);
    	cleanOrder.SubTotalItem4   = decimal.Parse(this.txtItem4SubTotal.Text);
    	cleanOrder.CleaningTotal   = decimal.Parse(this.txtCleaningTotal.Text);
    	cleanOrder.TaxRate         = decimal.Parse(this.txtTaxRate.Text);
    	cleanOrder.TaxAmount       = decimal.Parse(this.txtTaxAmount.Text);
    	cleanOrder.OrderTotal      = decimal.Parse(this.txtNetPrice.Text);
    	this.CleaningOrders.Add(cleanOrder);
    
    FileStream stmOrders = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    	gcsSoap.Serialize(stmOrders, this.CleaningOrders);
    	stmOrders.Close();
    
    	this.btnReset_Click(sender, e);
    }
  12. Execute the application
  13. Process a few orders and save each
     
  14. Close the form and return to your programming environment
  15. On the Cleaning Orders form, double-click the Open button and implement its Click event as follows:
     
    private void btnOpen_Click(object sender, System.EventArgs e)
    {
    	ArrayList lstCleaningOrders = new ArrayList();
    	string strFilename = "CleaningOrders.gcs";
    	SoapFormatter gcsSoap = new SoapFormatter();
    	int receiptNumber = int.Parse(this.txtReceiptNumber.Text);
    
    	if( File.Exists(strFilename) )
    	{
    FileStream gcsStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		lstCleaningOrders = (ArrayList)gcsSoap.Deserialize(gcsStream);
    		gcsStream.Close();
    
    		foreach(CleaningOrder order in lstCleaningOrders)
    		{
    			if( order.OrderID == receiptNumber )
    			{
    				this.txtCustomerName.Text = order.CustomerName;
    				this.txtCustomerPhone.Text = order.CustomerPhone;
    				this.dtpDateLeft.Value = order.DateLeft;
    				this.dtpTimeLeft.Value = order.TimeLeft;
    				this.dtpDateExpected.Value = order.DatePickedUp;
    				this.dtpTimeExpected.Value = order.TimePickedUp;
    				this.txtShirtsUnitPrice.Text = order.UnitPriceShirts.ToString();
    				this.txtShirtsQuantity.Text = order.QuantityShirts.ToString();
    				this.txtShirtsSubTotal.Text = order.SubTotalShirts.ToString();
    				this.txtPantsUnitPrice.Text = order.UnitPricePants.ToString();
    				this.txtPantsQuantity.Text = order.QuantityPants.ToString();
    				this.txtPantsSubTotal.Text = order.SubTotalPants.ToString();
    				this.cboItem1.Text = order.Item1Name;
    				this.txtItem1UnitPrice.Text = order.UnitPriceItem1.ToString();
    				this.txtItem1Quantity.Text = order.QuantityItem1.ToString();
    				this.txtItem1SubTotal.Text = order.SubTotalItem1.ToString();
    				this.cboItem2.Text = order.Item2Name;
    				this.txtItem2UnitPrice.Text = order.UnitPriceItem2.ToString();
    				this.txtItem2Quantity.Text  = order.QuantityItem2.ToString();
    				this.txtItem2SubTotal.Text  = order.SubTotalItem2.ToString();
    				this.cboItem3.Text          = order.Item3Name;
    				this.txtItem3UnitPrice.Text = order.UnitPriceItem3.ToString();
    				this.txtItem3Quantity.Text  = order.QuantityItem3.ToString();
    				this.txtItem3SubTotal.Text  = order.SubTotalItem3.ToString();
    				this.cboItem4.Text          = order.Item4Name;
    				this.txtItem4UnitPrice.Text = order.UnitPriceItem4.ToString();
    				this.txtItem4Quantity.Text  = order.QuantityItem4.ToString();
    				this.txtItem4SubTotal.Text  = order.SubTotalItem4.ToString();
    				this.txtCleaningTotal.Text  = order.CleaningTotal.ToString();
    				this.txtTaxRate.Text        = order.TaxRate.ToString();
    				this.txtTaxAmount.Text      = order.TaxAmount.ToString();
    				this.txtNetPrice.Text       = order.OrderTotal.ToString();
    			}
    		}
    	}
    	else
    	{
    		MessageBox.Show("There is no cleaning order with that receipt number!");
    		return;
    	}
    }
  16. Execute the application and try opening a previously saved order
  17. Close the form and return to your programming environment
 

Previous Copyright © 2004-2011 FunctionX