Home

File-Based Applications:
Bethesda Car Rental

 

Cars

Cars are at the center of the rental transactions of our company. A car is the main reason a customer comes to the business. In our application, we will provide all the necessary information related to a car such as its make, model, year, picture, and whether it is available. Because we know that sometimes when renting or choosing a car, a customer may want to know the options available on a particular car, we will also list these basic pieces of information. Finally, we will mark a car as available or not. This will allow the clerk processing an order to know whether the customer can rent the car or not.

We will create two forms related to cars. One form will be used to enter a new car when the company acquires one. On the other hand, when interviewing a customer, if the clerk wants to see a list of the company cars, we will create a form that can help with this, allowing the clerk to navigate among cars for a review.

 

Practical Learning Practical Learning: Processing Cars

  1. Copy the following pictures to the debug sub-folder of the bin sub-folder inside the main folder of the current project (Save them with their default names):
     
  2. Return to your programming environment
  3. To add a new form to the application, on the main menu, click Project -> Add Windows Forms
  4. Set the Name to NewCar and press Enter
  5. Design the form as follows: 
     
    Bethesda Car Rental - New Car
    Control Text Name Other Properties
    Label Text #    
    TextBox   txtTagNumber  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear  
    Label Category:    
    ComboBox   cboCategory DropDownStyle: DropDownList
    Items: Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Van
    CheckBox Cassete Player chkK7Player CheckAlign: MiddleRight
    CheckBox DVD Player chkDVDPlayer CheckAlign: MiddleRight
    CheckBox CD Player chkCDPlayer CheckAlign: MiddleRight
    CheckBox Available chkAvailable CheckAlign: MiddleRight
    PictureBox   pctCar SizeMode: CenterImage
    Label Select Car Picture:    
    ComboBox none.gif cboPictures  
    Button Add Car btnAddCar  
    Button Close btnClose DialogResult: OK
    Form     FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  6. To add a new class to the project, on the main menu, click Project -> Add Class...
  7. Set the Class Name to Car and click Open
  8. Access the Car.cs file and change it as follows:
     
    using System;
    
    namespace BCR2
    {
    	/// <summary>
    	/// Summary description for Car.
    	/// </summary>
    	[Serializable]
    	sealed public class Car
    	{
    		public string TagNumber;
    		public string Make;
    		public string Model;
    		public int    Year;
    		public string Category;
    		public int    HasK7Player;
    		public int    HasCDPlayer;
    		public int    HasDVDPlayer;
    		public string PictureName;
    		public int    IsAvailable;
    		
    		public Car()
    		{
    			TagNumber    = "000-000";
    			Make         = "Make";
    			Model        = "Model";
    			Year         = 1960;
    			Category     = "Small";
    			HasK7Player  = 0;
    			HasCDPlayer  = 0;
    			HasDVDPlayer = 0;
    			PictureName  = "";
    			IsAvailable  = 0;
    		}
    						 
    		public Car(string tag, string mk, string mdl, 
    			int yr, string cat, int k7, int cd,
    			int dvd, string pct, int avl)
    		{	
    			TagNumber    = tag;
    			Make         = mk;
    			Model        = mdl;
    			Year         = yr;
    			Category     = cat;
    			HasK7Player  = k7;
    			HasCDPlayer  = cd;
    			HasDVDPlayer = dvd;
    			PictureName  = pct;
    			IsAvailable  = avl;
    		}
    	}
    }
  9. Display the NewCar form. Right-click it and click View Code
  10. In the top section of the file, type the following:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
    
    namespace BCR2
    {
  11. Declare an ArrayList variable and name it lstCars:
     
    private System.ComponentModel.Container components = null;
    ArrayList lstCars;
  12. Return to the NewCar form. Double-click an unoccupied area of its body to generate its Load event and implement it as follows:
     
    private void NewCar_Load(object sender, System.EventArgs e)
    {
    	lstCars = new ArrayList();
    	string strFilename = "Cars.bcr";
    	SoapFormatter bcrSoap = new SoapFormatter();
    			 
    	if( File.Exists(strFilename) )
    	{
    		FileStream bcrStream = new FileStream(strFilename, 
    			FileMode.Open, FileAccess.Read, FileShare.Read);
    		lstCars = (ArrayList)bcrSoap.Deserialize(bcrStream);
    		bcrStream.Close();
    	}
    	else
    	{
    		FileStream bcrStream = new FileStream(strFilename, 
    				FileMode.OpenOrCreate, FileAccess.Write, 
    				FileShare.Write);
    		bcrSoap.Serialize(bcrStream, lstCars);
    		bcrStream.Close();
    	}
    
    	// Locate the director that contains the current application
    	DirectoryInfo dirInfo = new DirectoryInfo(".\\");
    
    	// Get a reference to each file in that directory
    	FileInfo[] lstFiles = dirInfo.GetFiles();
    
    		// Display the names of the graphics files
    		foreach(FileInfo fi in lstFiles)
    		{
    			if( fi.Extension.Equals(".gif") ||
    				fi.Extension.Equals(".jpeg") ||
    				fi.Extension.Equals(".jpg") ||
    				fi.Extension.Equals(".bmp") ||
    				fi.Extension.Equals(".png") )
    				cboPictures.Items.Add(fi.Name);
    		}
    
    	cboPictures.Text = "none.gif";
    }
  13. Return to the NewCar form and double-click the combo box on then right side of Select Car Picture
  14. Implement its SelectedIndexChanged event as follows:
     
    private void cboPictures_SelectedIndexChanged(object sender, 
    		System.EventArgs e)
    {
    	this.pctCar.Image = Image.FromFile(cboPictures.Text);
    }
  15. Return to the NewCar form and double-click the Add Car button to generate its Click event
  16. Implement it as follows:
     
    private void btnAddCar_Click(object sender, System.EventArgs e)
    {
    	Car vehicle = new Car();
    
    	vehicle.TagNumber = this.txtTagNumber.Text;
    	vehicle.Make      = this.txtMake.Text;
    	vehicle.Model     = this.txtModel.Text;
    	vehicle.Year      = int.Parse(this.txtYear.Text);
    	vehicle.Category  = this.cboCategory.Text;
    	if( this.chkK7Player.Checked == true )
    		vehicle.HasK7Player  = 1;
    	else
    		vehicle.HasK7Player  = 0;
    	if( this.chkCDPlayer.Checked == true )
    		vehicle.HasCDPlayer  = 1;
    	else
    		vehicle.HasCDPlayer  = 0;
    	if( this.chkDVDPlayer.Checked == true )
    		vehicle.HasDVDPlayer = 1;
    	else
    		vehicle.HasDVDPlayer = 0;
    	vehicle.PictureName  = cboPictures.Text;
    	if( this.chkAvailable.Checked == true )
    		vehicle.IsAvailable  = 1;
    	else
    		vehicle.IsAvailable  = 0;
    
    	lstCars.Add(vehicle);
    
    	string strFilename = "Cars.bcr";
    	SoapFormatter bcrSoap = new SoapFormatter();
    	FileStream bcrStream = new FileStream(strFilename, 
    		FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    	bcrSoap.Serialize(bcrStream, lstCars);
    	bcrStream.Close();
    
    	this.txtTagNumber.Text = "";
    	this.txtMake.Text      = "";
    	this.txtModel.Text     = "";
    	this.txtYear.Text      = "1960";
    	this.cboCategory.SelectedIndex = 0;
    	this.chkK7Player.Checked = false;
    	this.chkCDPlayer.Checked  = false;
    	this.chkDVDPlayer.Checked = false;
    	cboPictures.Text = "none.gif";			 
    	this.chkAvailable.Checked = false;
    	this.pctCar.Image = Image.FromFile("none.gif");
    	this.txtTagNumber.Focus();
    }
  17. Display the first form, Switchboard.cs [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnNewCar
    Text: New Car
  18. Double-click the New Car button to generate its Click event
  19. Implement the event as follows:
     
    private void btnNewCar_Click(object sender, System.EventArgs e)
    {
    	NewCar car = new NewCar();
    	car.ShowDialog();
    }
  20. Execute the application
  21. Create a few cars as follows:
     
  22. Close the forms and return to your programming environment
  23. To add a new form to the application, on the main menu, click Project -> Add Windows Forms
  24. Set the Name to Cars and press Enter
  25. Design the form as follows: 
     
    Control Text Name Other Properties
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear  
    Label Category:    
    TextBox   txtCategory  
    CheckBox Cassete Player chkK7Player CheckAlign: MiddleRight
    CheckBox DVD Player chkDVDPlayer CheckAlign: MiddleRight
    CheckBox CD Player chkCDPlayer CheckAlign: MiddleRight
    CheckBox Available chkAvailable CheckAlign: MiddleRight
    Label Tag #:    
    TextBox   txtTagNumber  
    PictureBox   pctCar  
    Button First btnFirst  
    Button Previous btnPrevious  
    Button Next btnNext  
    Button Last btnLast  
    Button Close btnClose  
    Form     MaximizeBox: False
    StartPosition: CenterScreen
  26. Right-click it and click View Code
  27. In the top section of the file, type the following:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
    
    namespace BCR2
    {
  28. In the class, declare an ArrayList and name it lstCars:
     
    private System.ComponentModel.Container components = null;
    ArrayList lstCars;
    int CurrentPosition;
  29. Return to the Cars form and double-click an empty area of its body
  30. Implement the event as follows:
     
    private void Cars_Load(object sender, System.EventArgs e)
    {
    	lstCars = new ArrayList();
    	CurrentPosition = 0;
    	 
    	string strFilename = "Cars.bcr";
    	SoapFormatter bcrSoap = new SoapFormatter();
    
    	if( File.Exists(strFilename) )
    	{
    		FileStream bcrStream = new FileStream(strFilename, 
    FileMode.Open, FileAccess.Read, FileShare.Read);
    		lstCars = (ArrayList)bcrSoap.Deserialize(bcrStream);
    
    		bcrStream.Close();
    		this.btnFirst_Click(sender, e);
    	}
    }
    
    void ShowCarInformation(Car vehicle)
    {
    	this.txtTagNumber.Text = vehicle.TagNumber;
    	this.txtMake.Text = vehicle.Make;
    	this.txtModel.Text = vehicle.Model;
    	this.txtYear.Text  = vehicle.Year.ToString();
    	this.txtCategory.Text  = vehicle.Category;
    
    	if( vehicle.HasK7Player == 1 )
    		this.chkK7Player.Checked = true;
    	else
    		this.chkK7Player.Checked = false;
    
    	if( vehicle.HasCDPlayer == 1 )
    		this.chkCDPlayer.Checked = true;
    	else
    		this.chkCDPlayer.Checked = false;
    
    	if( vehicle.HasDVDPlayer == 1 )
    		this.chkDVDPlayer.Checked = true;
    	else 
    		this.chkDVDPlayer.Checked = false;
    
    	string strPictureName = vehicle.PictureName;
    
    	try 
    	{
    		this.pctCar.Image = 
    			Image.FromFile(vehicle.PictureName);
    	}
    	catch(OutOfMemoryException)
    	{
    		this.pctCar.Image = Image.FromFile("none.gif");
    	}
    
    	if( vehicle.IsAvailable == 1 )
    		this.chkAvailable.Checked = true;
    	else
    		this.chkAvailable.Checked = false;
    }
  31. Return to the Cars form and double-click the First button
  32. Implement its Click event as follows:
     
    private void btnFirst_Click(object sender, System.EventArgs e)
    {
    	if( lstCars.Count == 0 )
    		return;
    			 
    	CurrentPosition = 0;
    	Car car = new Car();
    
    	car = (Car)this.lstCars[CurrentPosition];
    
    	ShowCarInformation(car);
    }
  33. Return to the Cars form
  34. Double-click the Previous button and implement its Click event as follows:
     
    private void btnPrevious_Click(object sender, System.EventArgs e)
    {
    	if( lstCars.Count == 0 )
    		return;
    			 
    	if( CurrentPosition == 0 )
    		return;
    	 
    	CurrentPosition--;
    	Car vehicle = new Car();
    
    	vehicle = (Car)lstCars[CurrentPosition];
    
    	ShowCarInformation(vehicle);
    }
  35. Return to the Cars form. Double-click the Next button and implement its Click event as follows:
     
    private void btnNext_Click(object sender, System.EventArgs e)
    {
    	if( lstCars.Count == 0 )
    		 return;
    
    	 if( CurrentPosition == lstCars.Count - 1 )
    		 return;
    	 else
    	 {
    		 CurrentPosition++;
    	 
    		 Car vehicle = new Car();
    
    		 vehicle = (Car)lstCars[CurrentPosition];
    
    		ShowCarInformation(vehicle);
    	 }
    }
  36. Return to the Cars form. Double-click the Last button and implement its Click event as follows:
     
    private void btnLast_Click(object sender, System.EventArgs e)
    {
    	if( lstCars.Count == 0 )
    		return;
    			 
    	CurrentPosition = lstCars.Count - 1;
    	Car vehicle = new Car();
    
    	vehicle = (Car)this.lstCars[CurrentPosition];
    
    	ShowCarInformation(vehicle);
    }
  37. Return to the Cars form. Double-click the Close button and implement its Click event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  38. Display the first form, Switchboard.cs [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnCarsReview
    Text: Car Review
  39. Double-click the New Car button to generate its Click event
  40. Implement the event as follows:
     
    private void btnCarsReview_Click(object sender, System.EventArgs e)
    {
    	Cars frmCars = new Cars();
    	frmCars.Show();
    }
  41. Execute the application and review the list of cars using the Cars form
     
  42. Close the forms and return to your programming environment
 

Rental Orders

To main purpose of a car rental company is to rent car. This is done by receiving orders from a customer and processing such an order. To proceed, a clerk would use a form to enter the customer and the car information. In our application, we will also make sure that name of the clerk who processed an order is specified. Other than than, we will enter as much information as possible to assist the user.

Practical Learning Practical Learning: Creating a Serializable Class

  1. To add a new form to the project, on the main menu, click Project -> Add Windows Form...
  2. Set the Name to RentalRates and press Enter
  3. Add a ListView to the form and create its Columns as follows:
     
    (Name) Text TextAlign Width
    colCategory Category   90
    colDaily Daily Right  
    colWeekly Weekly Right  
    colMonthly Monthly Right  
    colWeekend Weekend Right  
  4. Create its Items as follows:
     
    ListViewItem ListViewSubItem ListViewSubItem ListViewSubItem ListViewSubItem
      Text Text Text Text
    Economy 32.95 29.75 22.95 19.95
    Compact 39.95 34.75 29.95 24.95
    Standard 45.95 39.75 35.95 32.95
    Full Size 49.95 42.75 38.95 35.95
    Mini Van 55.95 50.75 45.95 42.95
    SUV 55.95 50.75 45.95 42.95
    Truck 42.75 38.75 35.95 32.95
    Van 69.95 62.75 55.95 52.95
  5. Complete the design of the form as follows:
     
  6. To add a new class to the project, on the main menu, click Project -> Add Class...
  7. Set the Class Name to RentalOrder and press Enter
  8. Access the RentalOrder.h file and change it as follows:
     
    using System;
    
    namespace BCR2
    {
    	/// <summary>
    	/// Summary description for RentalOrder.
    	/// </summary>
    	[Serializable]
    	sealed public class RentalOrder
    	{
    		public int      ReceiptNumber;
    		public string  ProcessedBy;
    		public string  CarSelected;
    		public string  Make;
    		public string  Model;
    		public int	 Year;
    		public string  CarCondition;
    		public string  CustDrvLicNbr;
    		public string  CustName;
    		public string  CustAddress;
    		public string  CustCity;
    		public string  CustState;
    		public string  CustZIPCode;
    		public string  CustCountry;
    		public string  TankLevel;
    		public long     Mileage;
    		public DateTime StartDate;
    		public DateTime EndDate;
    		public int      Days;
    		public decimal  RateApplied;
    		public decimal  SubTotal;
    		public decimal  TaxRate;
    		public decimal  TaxAmount;
    		public decimal  OrderTotal;
    
    		public RentalOrder()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    	}
    }
  9. To add a new form to the project, on the main menu, click Project -> Add Windows Form
  10. Set the Name to RentalOrders and press Enter
  11. Design the form as follows:
     
    Control Text Name Other Properties
    GroupBox Order Identification    
    Label Processed By:    
    ComboBox   cboEmployees  
    GroupBox Car Selected    
    ComboBox   cboCars  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtCarYear  
    label Car Condition:    
    ComboBox   cboCarConditions  
    GroupBox Customer    
    ComboBox   cboCustomers  
    Label Name:    
    TextBox   txtCustName  
    Label Address:    
    TextBox   txtCustAddress  
    TextBox   txtCustCity  
    TextBox MD txtCustState  
    TextBox   txtCustZIPCode  
    TextBox USA txtCustCountry  
    GroupBox Order Evaluation    
  12. Right-click anywhere in the form and click View Code
  13. In the top section of the file, type the following:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
    
    namespace BCR2
    {
  14. In the class, declare a variable of type ArrayList named lstRentalOrders
     
    private System.ComponentModel.Container components = null;
    ArrayList lstRentalOrders;
  15. Return to the CleaningOrders form
  16. In the combo box above the Properties window, select RentalOrders and click the Events button
  17. In the Events section, double-click Load and implement the event as follows:
     
    private void RentalOrders_Load(object sender, System.EventArgs e)
    {
    	lstRentalOrders = new ArrayList();
    	 ArrayList lstEmployees = new ArrayList();
    	 SoapFormatter bcrSoap = new SoapFormatter();
    	 string strFilename = "Employees.bcr";
    			 
    	 if( File.Exists(strFilename) )
    	 {
    FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		 lstEmployees = (ArrayList)bcrSoap.Deserialize(bcrStream);
    		 bcrStream.Close();
    
    		 string firstName, lastName, title;
    
    		 cboEmployees.Items.Clear();
    
    		 foreach(Employee empl in lstEmployees)
    		 {
    			 firstName = empl.FirstName;
    			 lastName  = empl.LastName;
    			 title     = empl.Title;
    			 
    			 cboEmployees.Items.Add(String.Concat(lastName, ", ", firstName, " - ", title));
    		 }
    	 }
    
    	 strFilename = "Cars.bcr";
    	 ArrayList lstCars = new ArrayList();
    
    	 if( File.Exists(strFilename) )
    	 {
    FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		 lstCars = (ArrayList)bcrSoap.Deserialize(bcrStream);
    		 bcrStream.Close();
    
    		 cboCars.Items.Clear();
    
    		 foreach(Car vehicle in lstCars)
    			 cboCars.Items.Add(vehicle.TagNumber);
    	 }
    
    	 strFilename = "Customers.bcr";
    	 ArrayList lstCustomers = new ArrayList();
    			 
    	 if( File.Exists(strFilename) )
    	 {
    FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		 lstCustomers = (ArrayList)bcrSoap.Deserialize(bcrStream);
    		 bcrStream.Close();
    
    		 cboCustomers.Items.Clear();
    
    		 foreach(Customer cust in lstCustomers)
    			 cboCustomers.Items.Add(cust.DrvLicNbr);
    	 }
    }
  18. Return to the RentalOrders form. Double-click the Car Selected combo box and implement its SelectedIndexChanged event as follows:
     
    private void cboCars_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	string strFilename = "Cars.bcr";
    	ArrayList lstCars = new ArrayList();
    	SoapFormatter bcrSoap = new SoapFormatter();
    
    FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    	lstCars = (ArrayList)bcrSoap.Deserialize(bcrStream);
    	bcrStream.Close();
    
    	foreach(Car vehicle in lstCars)
    	{
    		if( vehicle.TagNumber == cboCars.Text )
    		{
    			this.txtMake.Text    = vehicle.Make;
    			this.txtModel.Text   = vehicle.Model;
    			this.txtCarYear.Text = vehicle.Year.ToString();
    			return;
    		}
    	}
    }
  19. Return to the RentalOrders form. Double-click the Customer combo box and implement its SelectedIndexChanged event as follows:
     
    private void cboCustomers_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	string strFilename = "Customers.bcr";
    	ArrayList lstCustomers = new ArrayList();
    	SoapFormatter bcrSoap = new SoapFormatter();
    
    FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    	lstCustomers = (ArrayList)(bcrSoap.Deserialize(bcrStream));
    	bcrStream.Close();
    
    	foreach(Customer cust in lstCustomers)
    	{
    		if( cust.DrvLicNbr == cboCustomers.Text )
    		{
    			this.txtCustName.Text    = cust.FullName;
    			this.txtCustAddress.Text = cust.Address;
    			this.txtCustCity.Text    = cust.City;
    			this.txtCustState.Text   = cust.State;
    			this.txtCustZIPCode.Text = cust.ZIPCode;
    			this.txtCustCountry.Text = cust.Country;
    			return;
    		}
    	}
    }
  20. Return to the RentalOrders form and double-click the Rate Applied button to implement its Click event as follows:
     
    private void btnRateApplied_Click(object sender, System.EventArgs e)
    {
    	RentalRates frmRates = new RentalRates();
    	frmRates.Show();
    }
  21. Return to the RentalOrders form and double-click the End Date control
  22. Implement its Click event as follows:
     
    private void dtpEndDate_ValueChanged(object sender, System.EventArgs e)
    {
    	DateTime dteStart = this.dtpStartDate.Value;
    	DateTime dteEnd  = this.dtpEndDate.Value;
    	TimeSpan tme = dteEnd - dteStart;
    	int days = tme.Days;
    
    	this.txtDays.Text = days.ToString();
    }
  23. Return to the RentalOrders form and click the text box on the right side of the Rate Applied button
  24. In the Properties window, click Events and double-click the Leave field
  25. Implement its event as follows:
     
    private void txtRateApplied_Leave(object sender, System.EventArgs e)
    {
    	int days = int.Parse(this.txtDays.Text);
    	decimal rateApplied = decimal.Parse(this.txtRateApplied.Text);
    	decimal subTotal = days * rateApplied;
    	this.txtSubTotal.Text = subTotal.ToString("F");
    	decimal taxRate = decimal.Parse(this.txtTaxRate.Text);
    	decimal taxAmount = subTotal * taxRate / 100;
    	this.txtTaxAmount.Text = taxAmount.ToString("F");
    	decimal totalOrder = subTotal + taxAmount;
    	this.txtOrderTotal.Text = totalOrder.ToString("F");
    }
  26. Return to the RentalOrders form and double-click the Save button to implement its Click event as follows:
     
    private void btnSave_Click(object sender, System.EventArgs e)
    		{
    			int receiptNumber = 0;
    		ArrayList lstRentalOrders = new ArrayList();
    		string strFilename = "RentalOrders.bcr";
    		SoapFormatter bcrSoap = new SoapFormatter();
    
    		if( File.Exists(strFilename) )
    	{
    		FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		lstRentalOrders = (ArrayList)bcrSoap.Deserialize(bcrStream);
    
    		RentalOrder order = (RentalOrder)lstRentalOrders[lstRentalOrders.Count-1];
    		receiptNumber = order.ReceiptNumber;
    		bcrStream.Close();
    	}
    	else
    {
    	FileStream bcrStream = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    	bcrSoap.Serialize(bcrStream, lstRentalOrders);
    	bcrStream.Close();
    }
    
    	RentalOrder rntOrder = new RentalOrder();
    
    	rntOrder.ReceiptNumber = receiptNumber + 1;
    	rntOrder.ProcessedBy   = this.cboEmployees.Text;
    	rntOrder.CarSelected   = this.cboCars.Text;
    	rntOrder.Make			 = this.txtMake.Text;
    	rntOrder.Model		 = this.txtModel.Text;
    	rntOrder.Year			 = int.Parse(this.txtCarYear.Text);
    	rntOrder.CarCondition  = this.cboCarConditions.Text;
    	rntOrder.CustDrvLicNbr = this.cboCustomers.Text;
    	rntOrder.CustName		 = this.txtCustName.Text;
    	rntOrder.CustAddress   = this.txtCustAddress.Text;
    	rntOrder.CustCity		 = this.txtCustCity.Text;
    	rntOrder.CustState	 = this.txtCustState.Text;
    	rntOrder.CustZIPCode	 = this.txtCustZIPCode.Text;
    	rntOrder.CustCountry	 = this.txtCustCountry.Text;
    	rntOrder.TankLevel	 = this.cboTankLevels.Text;
    	rntOrder.Mileage		 = long.Parse(this.txtMileage.Text);
    	rntOrder.StartDate	 = this.dtpStartDate.Value;
    	rntOrder.EndDate		 = this.dtpEndDate.Value;
    	rntOrder.Days			 = int.Parse(this.txtDays.Text);
    	rntOrder.RateApplied	 = decimal.Parse(this.txtRateApplied.Text);
    	rntOrder.SubTotal		 = decimal.Parse(this.txtSubTotal.Text);
    	rntOrder.TaxRate		 = decimal.Parse(this.txtTaxRate.Text);
    	rntOrder.TaxAmount	 = decimal.Parse(this.txtTaxAmount.Text);
    	rntOrder.OrderTotal	 = decimal.Parse(this.txtOrderTotal.Text);
    			 
    	lstRentalOrders.Add(rntOrder);
    
    FileStream stmOrders = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    	bcrSoap.Serialize(stmOrders, lstRentalOrders);
    	stmOrders.Close();
    
    	this.cboEmployees.Text = "";
    	this.cboCars.Text = "";
    	this.txtMake.Text = "";
    	this.txtModel.Text = "";
    	this.txtCarYear.Text = "2000";
    	this.cboCarConditions.Text = "";
    	this.cboCustomers.Text = "";
    	this.txtCustAddress.Text = "";
    	this.txtCustAddress.Text = "";
    	this.txtCustCity.Text = "";
    	this.txtCustState.Text = "MD";
    	this.txtCustZIPCode.Text = "";
    	this.txtCustCountry.Text = "USA";
    	this.cboTankLevels.Text = "";
    	this.txtMileage.Text = "";
    	this.dtpStartDate.Value = DateTime.Now;
    	this.dtpEndDate.Value = DateTime.Now;
    	this.txtDays.Text = "0";
    	this.txtRateApplied.Text = "24.95";
    	this.txtSubTotal.Text = "0.00";
    	this.txtTaxRate.Text = "7.75";
    	this.txtTaxAmount.Text = "0.00";
    	this.txtOrderTotal.Text = "0.00";
    	this.cboEmployees.Focus();
    }
  27. Display the first form, Switchboard, and complete its design as follows:
     
    Control Name Text
    Button btnRentalOrders Rental Orders
    Button btnEmployees Employees
    Button btnCustomers Customers
  28. Double-click the Rental Orders button and implement its Click event as follows:
     
    private void btnRentalOrders_Click(object sender, System.EventArgs e)
    {
    	RentalOrders frmOrders = new RentalOrders();
    	frmOrders.Show();
    }
  29. Return to the Switchboard form and double-click the Close button
  30. Implement it as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	 Close();
    }
  31. Execute the application and create a few rental orders
     
  32. Close the forms and return to your programming environment
  33. Return to the RentalOrders form and double-click the Open button to implement its Click event as follows:
     
    private void btnOpen_Click(object sender, System.EventArgs e)
    {
    	ArrayList lstRentalOrders = new ArrayList();
    	string strFilename = "RentalOrders.bcr";
    	SoapFormatter bcrSoap = new SoapFormatter();
    	int receiptNumber = int.Parse(this.txtReceiptNumber.Text);
    
    	if( File.Exists(strFilename) )
    	{
    FileStream bcrStream = new FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
    		lstRentalOrders = (ArrayList)bcrSoap.Deserialize(bcrStream);
    
    		bcrStream.Close();
    
    		foreach(RentalOrder order in lstRentalOrders)
    		{
    			if( order.ReceiptNumber == receiptNumber )
    			{
    				this.cboEmployees.Text   = order.ProcessedBy;
    				this.cboCars.Text        = order.CarSelected;
    				this.txtMake.Text        = order.Make;
    				this.txtModel.Text       = order.Model;
    				this.txtCarYear.Text     = order.Year.ToString();
    				this.cboCarConditions.Text = order.CarCondition;
    				this.cboCustomers.Text   = order.CustDrvLicNbr;
    				this.txtCustName.Text    = order.CustName;
    				this.txtCustAddress.Text = order.CustAddress;
    				this.txtCustCity.Text    = order.CustCity;
    				this.txtCustState.Text   = order.CustState;
    				this.txtCustZIPCode.Text = order.CustZIPCode;
    				this.txtCustCountry.Text = order.CustCountry;
    				this.cboTankLevels.Text  = order.TankLevel;
    				this.txtMileage.Text     = order.Mileage.ToString();
    				this.dtpStartDate.Value  = order.StartDate;
    				this.dtpEndDate.Value    = order.EndDate;
    				this.txtDays.Text        = order.Days.ToString();
    				this.txtRateApplied.Text = order.RateApplied.ToString();
    				this.txtSubTotal.Text    = order.SubTotal.ToString();
    				this.txtTaxRate.Text     = order.TaxRate.ToString();
    				this.txtTaxAmount.Text   = order.TaxAmount.ToString();
    				this.txtOrderTotal.Text  = order.OrderTotal.ToString();
    			}
    		}
    	}
    	else
    	{
    		MessageBox.Show("There is no rental order with that receipt number!");
    		return;
    	}
    }
  34. Return to the Rental Orders form and double-click the Close button
  35. Implement its Click event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  36. Execute the application and try opening previous created orders
 

Previous Copyright © 2005-2016, FunctionX Next