Home

Dictionary-Based Collections Application: Bethesda Car Rental

     

Introduction

A dictionary is a list of items with the following two rules:

  • Each item is a combination of a key and a value. An item can be made of three parts: a key, a value, and a correspondence between them. The correspondence can be represented by the assignment operator: Key=Value. In some environments, the = operator is used. In some others, the : operator is used. Yet in some others, there is no actual operator that joins both sides but you must know that the key and the value go in pair
  • As each item is a combination of a key and a value, each key must be unique. The list of items in the dictionary can be very huge, sometimes in the thousands or even millions. Among the items, each key must be distinct from any other key in the dictionary

In some cases, in addition to these two rules, the items should - in some cases must - be ordered. To order the items, the keys are used. Because in most cases a dictionary is made of words, the keys are ordered in alphabetical order. A dictionary can also be made of items whose keys are date values. In this case, the items would be ordered in chronological order.

There are various types of dictionary types of list used in daily life. The word "dictionary" here does not imply the traditional dictionary that holds the words and their meanings in the English language. The concept is applied in various scenarios.

To illustrate dictionary-based collections, we will create an application used to run a car-rental bompany.

Practical LearningPractical Learning: Introducing Dictionary Collections

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click FILE -> New -> Project...
  3. In the middle list, click Windows Forms Application and set the Name to BethesdaCarRental1
  4. Click OK
  5. Save the following picture to your computer
     
    Bethesda Car Rental - Default Picture

The Rental Rates

Because customers have many needs, a car rental company has various types of vehicles, small, medium, big, trucks, passenger-oriented, etc. as a result, not all cars cost the same to rent. To make our application interesting, we will also use different ways to set the rental cost depending on whether the car will be rented during the week, during a weekend, or for a whole month.

Practical LearningPractical Learning: Setting the Rental Rates

  1. To add a new form to the project, in the Solution Explorer, right-click BethesdaCarRental1 -> 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 SubItems SubItems SubItems SubItems
      Text Text Text Text
    Economy 35.95 32.75 28.95 24.95
    Compact 39.95 35.75 32.95 28.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:
     
    Rental Rates
  6. Save all

The Employees

The employees, also called clerks, of a car rental company register new customers, allocate cars to them, perform the rental process, and complete the renting when a customer brings the car back.

To deal with the employees of our company, we will create a class named Employee. We will keep information to a minimum.

Practical LearningPractical Learning: Creating the Employees

  1. To add a new class to the project, in the Class View, right-click BethesdaCarRental1 -> Add -> Class...
  2. Set the Class Name to Employee and click Add
  3. Change the Employee.cs file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BethesdaCarRental1
    {
        [Serializable]
        public class Employee
        {
            public string FirstName { get; set; }
            public string LastName  { get; set; }
            public string Title     { get; set; }
    
            public string EmployeeName
            {
                get { return LastName + ", " + FirstName; }
            }
    
            public Employee()
            {
                FirstName = "Unknown";
                LastName  = "Unknown";
                Title     = "N/A";
            }
    
            public Employee(string fname, string lname, string title)
            {
                FirstName = fname;
                LastName  = lname;
                Title     = title;
            }
        }
    }
  4. To add a new form to the application, in the Solution Explorer, right-click BethesdaCarRental1 -> Add -> Windows Form...
  5. Set the Name to Employees and click Add
  6. From the Toolbox, add a ListView to the form
  7. While the new list view is still selected, in the Properties window, click the ellipsis button of the Columns field and create the columns as follows:
     
    (Name) Text TextAlign Width
    colEmployeeNumber Empl #   45
    colFirstName First Name   65
    colLastName Last Name   65
    colEmployeeName Employee Name   140
    colTitle Title   120
  8. Design the form as follows: 
     
    Bethesda Car Rental - Employees
     
    Control (Name) Text Other Properties
    ListView List View lvwEmployees   Anchor: Top, Bottom, Left, Right
    FullRowSelect: True
    GridLines: True
    View: Details
    Button Button btnNewEmployee New &Employee...  
    Button Button btnClose &Close  
  9. To add another form to the application, in the Solution Explorer, right- click BethesdaCarRental1 -> Add -> Windows Form...
  10. Set the Name to EmployeeEditor and click Add
  11. Design the form as follows: 
     
    Bethesda Car Rental - New Employee
     
    Control (Name) Text Properties
    Label Label   &Employee #:  
    TextBox Text Box txtEmployeeNumber   Mask: 00-000
    Modifiers: public
    Label Label   First Name:  
    TextBox Text Box txtFirstName   Modifiers: public
    Label Label   Last Name:  
    TextBox Text Box txtLastName   Modifiers: public
    Label Label   Employee Name:  
    TextBox Text Box txtEmployeeName   Enabled: False
    Modifiers: public
    Label Label   Title:  
    TextBox Text Box txtTitle   Modifiers: public
    Button Button btnOK OK DialogResult: OK
    Button Button btnCancel Cancel DialogResult: Cancel
    Form     AcceptButton: btnOK
    CancelButton: btnCancel
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  12. Click the Last Name text box
  13. In the Properties window, click the Events button and double-click Leave
  14. Implement the event as follows:
    // This code is used to create and display the customer's full name
    private void txtLastName_Leave(object sender, EventArgs e)
    {
        string strFirstName = txtFirstName.Text;
        string strLastName = txtLastName.Text;
        string strEmployeeName;
    
        if (strFirstName.Length == 0)
            strEmployeeName = strLastName;
        else
            strEmployeeName = strLastName + ", " + strFirstName;
    
        txtEmployeeName.Text = strEmployeeName;
    }
  15. Save all

The Vehicles to Rent

The purpose of a car rental company is to rent vehicles. A vehicle record contains such information as the tag number, the make, the model, the category, etc. The tag number be unique.

Practical LearningPractical Learning: Creating the Vehicles Records

  1. To add a new class to the project, in the Class View, right-click BethedaCarRental1 -> Add -> Class...
  2. Set the Class Name to Vehicle and click Add
  3. Change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BethesdaCarRental1
    {
        [Serializable]
        public class Vehicle
        {
            public string Make         { get; set; }
            public string Model        { get; set; }
            public int    Doors        { get; set; }
            public int    Passengers   { get; set; }
            public string Condition    { get; set; }
            public string Category     { get; set; }
            public string Availability { get; set; }
    
            public Vehicle()
            {
                Make         = "";
                Model        = "";
                Doors        = 4;
                Passengers   = 4;
                Condition    = "Excellent";
                Category     = "Compact";
                Availability = "Available";
            }
    
            public Vehicle(string make, string model,
                           int doors, int passengers, string condition,
                           string category, string available)
            {
                Make         = make;
                Model        = model;
                Doors        = doors;
                Passengers   = passengers;
                Condition    = condition;
                Category     = category;
                Availability = available;
            }
        }
    }
  4. To add a new form to the application, in the Solution Explorer, right-click BethesdaCarRental1 -> Add -> Windows Forms
  5. Set the Name to VehicleEditor and click Add
  6. Design the form as follows: 
     
    Bethesda Car Rental - New Vehicle
    Control (Name) Text Other Properties
    Label Label   Tag Number:  
    TextBox Text Box txtTagNumber    
    Label Label   Make:  
    TextBox Text Box txtMake    
    Label Label   Model:  
    TextBox Text Box txtModel    
    Label Label   Doors  
    TextBox Text Box txtDoors    
    Label Label   Passengers:  
    TextBox Text Box txtPassengers    
    Label Label   Condition:  
    ComboBox ComboBox cbxCondition   Items:
    Good
    Other
    Excellent
    Driveable
    Needs Repair
    Label Label   Category:  
    ComboBox ComboBox cbxCategories   Items:
    SUV
    Truck
    Full Size
    Mini Van
    Compact
    Standard
    Economy
    Passenger Van
    Label Label   Availability  
    ComboBox ComboBox   cbxAvailabilities Items:
    Rented
    Available
    Being Serviced
    PictureBox Picture Box pbxVehicle   SizeMode: Zoom
    Button Button btnSelectPicture &Select Vehicle Picture...  
    Label Label Picture Name lblPictureName  
    Button Button Submit btnSubmit  
    Button Button Close btnClose DialogResult: Cancel
    OpenFileDialog OpenFileDialog (Name): dlgOpen
    Title: Select Item Picture
    DefaultExt: jpg
    Filter: JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp|PNG Files (*.png)|*.png
    Form     FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  7. Double-click the Select Vehicle Picture button and implement its event as follows:
    private void btnSelectPicture_Click(object sender, EventArgs e)
    {
        if (dlgPicture.ShowDialog() == DialogResult.OK)
        {
            lblPictureName.Text = dlgPicture.FileName;
            pbxVehicle.Image = Image.FromFile(lblPictureName.Text);
        }
    }
  8. Save all

Rental Orders

A rental order is a contract that describes the transaction between the car-rental company and the customer. The contract contains the customer information, the car information, and other related pieces of information. A rental order must be uniquely identified. This is usually done by a receive number (or an invoice number).

Practical LearningPractical Learning: Creating the Vehicles Records

  1. To add a new class to the project, in the Class View, right-click BethesdaCarRental -> Add -> Class...
  2. Set the Class Name to RentalOrder and press Enter
  3. Access the RentalOrder.cs file and change it as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BethesdaCarRental1
    {
        [Serializable]
        public class RentalOrder
        {
            public DateTime DateProcessed     { get; set; }
            public string   ClerkNumber       { get; set; }
            public string   CustomerFirstName { get; set; }
            public string   CustomerLastName  { get; set; }
            public string   CustomerAddress   { get; set; }
            public string   CustomerCity      { get; set; }
            public string   CustomerState     { get; set; }
            public string   CustomerZIPCode   { get; set; }
            public string   VehicleTagNumber  { get; set; }
            public string   VehicleCondition  { get; set; }
            public string   TankLevel         { get; set; }
            public int      MileageStart      { get; set; }
            public int      MileageEnd        { get; set; }
            public int      MileageTotal      { get; set; }
            public DateTime RentStartDate     { get; set; }
            public DateTime RentEndDate       { get; set; }
            public int      TotalDays         { get; set; }
            public double   RateApplied       { get; set; }
            public double   SubTotal          { get; set; }
            public double   TaxRate           { get; set; }
            public double   TaxAmount         { get; set; }
            public double   OrderTotal        { get; set; }
            public double   OrderStatus       { get; set; }
            public string   Notes             { get; set; }
        }
    }
  4. To add a new form to the project, in the Solution Explorer, right-click BethesdaCarRenat1 -> Add -> Windows Form...
  5. Set the Name to UpdateRentalOrder and press Enter
  6. From the Printing section of the Toolbox, click PrintDocument and click the form
  7. In the Properties window, set its (Name) to docPrint and press Enter
  8. From the Printing section of the Toolbox, click PrintDialog and click the form
  9. In the Properties window, change its Name to dlgPrint
  10. Still in the Properties windows, set its Document property to docPrint
  11. From the Printing section of the Toolbox, click PrintPreviewDialog and click the form
  12. In the Properties window, change its (Name) to dlgPrintPreview
  13. Still in the Properties windows, set its Document property to docPrint
  14. Design the form as follows:
     
    Bethesda Car Rental - Rental Orders

    Control (Name) Text Other Properties
    Label Label   Processed By: AutoSize: False
    BackColor: Gray
    BorderStyle: FixedSingle
    ForeColor: White
    TextAlign: MiddleLeft
    Label Label   Order Timing AutoSize: False
    BackColor: Gray
    BorderStyle: FixedSingle
    ForeColor: White
    TextAlign: MiddleLeft
    Label Label   Employee #:  
    TextBox Text Box txtEmployeeNumber    
    TextBox Text Box txtEmployeeName    
    Label Label   Date Processed:  
    TextBox Text Box dtpDateProcessed    
    Label Label   Processed For AutoSize: False
    BackColor: Gray
    BorderStyle: FixedSingle
    ForeColor: White
    TextAlign: MiddleLeft
    Label Label   Rent Start Date:  
    DateTimePicker Date Time Picker dtpRentStartDate    
    Label Label   First Name:  
    TextBox Text Box txtCustomerFirstName    
    Label Label   Last Name:  
    TextBox Text Box txtCustomerLastName    
    Label Label   Rent End Date:  
    DateTimePicker Date Time Picker dtpRentEndDate    
    Label Label   Address:  
    TextBox Text Box txtCustomerAddress    
    Label Label   Total Days:  
    TextBox Text Box txtTotalDays 1 TextAlign: Right
    Label Label   City:  
    TextBox Text Box txtCustomerCity    
    Label Label   State:  
    Label Label Order Evaluation   AutoSize: False
    BackColor: Gray
    BorderStyle: FixedSingle
    ForeColor: White
    TextAlign: MiddleLeft
    ComboBox ComboBox cbxCustomerStates   DropDownStyle: DropDownList
    Sorted: True
    Items: AB, AL, AK, AZ, AR, BC, CA, CO, CT, DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NB, NE, NV, NH, NJ, NL, NM, NS, NY, NC, ND, OH, OK, ON, OR, PA, PE, QC, RI, SC, SD, SK, TN, TX, UT, VT, VA, WA, WV, WI, WY
    Label Label   ZIP/Postal Code:  
    TextBox Text Box txtCustomerZIPCode    
    Label Label   Rate Applied:  
    TextBox Text Box txtRateApplied 0.00 TextAlign: Right
    Button Button btnRentalRates Rental Rates  
    Label Label Vehicle Selected   AutoSize: False
    BackColor: Gray
    BorderStyle: FixedSingle
    ForeColor: White
    TextAlign: MiddleLeft
    Label Label   Sub-Total:  
    TextBox Text Box txtSubTotal 0.00 TextAlign: Right
    Button Button btnCalculate Calculate  
    Label Label   Tag Number:  
    TextBox Text Box txtTagNumber    
    Label Label   Condition:  
    ComboBox ComboBox cbxVehicleConditions   Sorted: True
    Items:
    Bad
    Good
    Other
    Excellent
    Driveable
    Needs Repair
    Label Label   Tax Rate:  
    TextBox Text Box txtTaxRate 7.75 TextAlign: Right
    Label Label   %  
    Label Label   Make:  
    TextBox Text Box txtMake    
    Label Label   Model:  
    TextBox Text Box txtModel    
    Label Label   Tax Amount:  
    TextBox Text Box txtTaxAmount 0.00 TextAlign: Right
    Button Button Print... btnPrint  
    Label Label   Mileage Start:  
    TextBox Text Box txtMileageStart   TextAlign: Right
    Label Label   Tank Level:  
    ComboBox ComboBox cbxTanksLevels   Empty
    1/4 Empty
    Half Tank
    3/4 Full
    Full
    Label Label   Order Total:  
    TextBox Text Box txtOrderTotal 0.00 TextAlign: Right
    Button   btnPrintPreview Print Preview...  
    Label Label   Mileage End:  
    TextBox Text Box txtMileageEnd   TextAlign: Right
    Label Label   Mileage Total:  
    TextBox Text Box txtMileageTotal   TextAlign: Right
    Label Label Order Status    
    ComboBox ComboBox   cbxOrderStatus Items:
    Order Reserved
    Vehicle With Customer
    Rental Order Completed
    Label Label   Notes:  
    TextBox Text Box txtNotes   Multiline: True
    ScrollBars: Vertical
    Label Label Receipt #:    
    TextBox Text Box   txtReceiptNumber  
    Button Button btnOpen Open  
    Button Button btnUpdateRentalOrder Update Rental Order  
    Button Button Close btnClose  
  15. On the Update Rental Order form, click the Mileage End text box
  16. On the Properties window, click the Events button and double-click Leave
  17. Implement the event as follows:
    private void txtMileageEnd_Leave(object sender, EventArgs e)
    {
        int mileageStart = 0, mileageEnd = 0;
    
        try
        {
            mileageStart = int.Parse(txtMileageStart.Text);
        }
        catch (FormatException fe)
        {
            MessageBox.Show("There was a problem with the mileage start value.\n" +
                            "Please report the error as\n" +
                            fe.Message,
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        try
        {
            mileageEnd = int.Parse(txtMileageEnd.Text);
        }
        catch (FormatException fe)
        {
            MessageBox.Show("There was a problem with the mileage end value.\n" +
                            "Please report the error as\n" +
                            fe.Message,
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        txtMileageTotal.Text = (mileageEnd - mileageStart).ToString();
    }
  18. Return to the Update Rental Order form and double-click the Rent Start Date date time picker control
  19. Implement the event as follows:
    private void dtpRentStartDate_ValueChanged(object sender, EventArgs e)
    {
        dtpRentEndDate.Value = dtpRentStartDate.Value;
    }
  20. Return to the Update Rental Order form and double-click the Rent End Date control
  21. Implement its Click event as follows:
    // This event approximately evaluates the number of days as a
    // difference between the end date and the starting date
    private void dtpRentEndDate_ValueChanged(object sender, EventArgs e)
    {
        DateTime dteStart = dtpRentStartDate.Value;
        DateTime dteEnd   = dtpRentEndDate.Value;
    
        // Let's calculate the difference in days
        TimeSpan tme = dteEnd - dteStart;
        int days = tme.Days;
    
        // If the customer returns the car the same day, 
        // we consider that the car was rented for 1 day
        if (days == 0)
            days = 1;
    
        txtTotalDays.Text = days.ToString();
        // At any case, we will let the clerk specify the actual number of days
    }
  22. Return to the Order Processing form and double-click the Rental Rates button
  23. Implement its event as follows:
    private void btnRentalRates_Click(object sender, EventArgs e)
    {
        RentalRates wndRates = new RentalRates();
        wndRates.Show();
    }
  24. Return to the Order Processing form and double-click the Calculate button
  25. Implement its event as follows:
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        int    Days = 0;
        double RateApplied = 0.00;
        double SubTotal = 0.00;
        double TaxRate = 0.00;
        double TaxAmount = 0.00;
        double OrderTotal = 0.00;
    
        try
        {
            Days = int.Parse(txtTotalDays.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid Number of Days",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        try
        {
            RateApplied = double.Parse(txtRateApplied.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid Amount for Rate Applied",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        SubTotal = Days * RateApplied;
        txtSubTotal.Text = SubTotal.ToString("F");
    
        try
        {
            TaxRate = double.Parse(txtTaxRate.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid Tax Rate",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    
        TaxAmount = SubTotal * TaxRate / 100;
        txtTaxAmount.Text = TaxAmount.ToString("F");
    
        OrderTotal = SubTotal + TaxAmount;
        txtOrderTotal.Text = OrderTotal.ToString("F");
    }
  26. Return to the Update Rental Order form and, under the form, double-click docPrint
  27. Implement its event as follows:
    private void docPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 90, 750, 90);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 93, 750, 93);
    
        string strDisplay = "Bethesda Car Rental";
        System.Drawing.Font fntBoldString = new Font("Times New Roman", 28, FontStyle.Bold);
        e.Graphics.DrawString(strDisplay, fntBoldString, Brushes.Black, 240, 100);
    
        strDisplay = "Vehicle Rental Order";
        System.Drawing.Font fntRegularString = new System.Drawing.Font("Times New Roman", 22, FontStyle.Regular);
        e.Graphics.DrawString(strDisplay, fntRegularString, Brushes.Black, 280, 150);
    
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 187, 750, 187);
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 190, 750, 190);
    
        fntBoldString = new Font("Times New Roman", 12, FontStyle.Bold);
        fntRegularString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular);
    
        e.Graphics.DrawString("Receipt #:  ", fntBoldString, Brushes.Black, 100, 220);
        e.Graphics.DrawString(txtReceiptNumber.Text, fntRegularString, Brushes.Black, 260, 220);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 240, 380, 240);
    
        e.Graphics.DrawString("Processed By:  ", fntBoldString, Brushes.Black, 420, 220);
        e.Graphics.DrawString(txtEmployeeName.Text, fntRegularString, Brushes.Black, 550, 220);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 240, 720, 240);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 260, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 260, 620, 20));
    
        e.Graphics.DrawString("Customer", fntBoldString, Brushes.White, 100, 260);
    
        e.Graphics.DrawString("First Name: ", fntBoldString, Brushes.Black, 100, 300);
        e.Graphics.DrawString(txtCustomerFirstName.Text, fntBoldString, Brushes.Black, 260, 300);
        e.Graphics.DrawString("Last Name: ", fntBoldString, Brushes.Black, 420, 300);
        e.Graphics.DrawString(txtCustomerLastName.Text, fntBoldString, Brushes.Black, 540, 300);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 320, 720, 320);
    
        e.Graphics.DrawString("Address: ", fntBoldString, Brushes.Black, 100, 330);
        e.Graphics.DrawString(txtCustomerAddress.Text, fntRegularString, Brushes.Black, 260, 330);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 350, 720, 350);
    
        strDisplay = txtCustomerCity.Text + " " + cbxCustomerStates.Text + " " + txtCustomerZIPCode.Text;
        e.Graphics.DrawString(strDisplay, fntRegularString, Brushes.Black, 260, 360);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 260, 380, 720, 380);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 410, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 410, 620, 20));
    
        e.Graphics.DrawString("Car Information", fntBoldString, Brushes.White, 100, 410);
    
        e.Graphics.DrawString("Tag #: ", fntBoldString, Brushes.Black, 100, 450);
        e.Graphics.DrawString(txtTagNumber.Text, fntRegularString, Brushes.Black, 260, 450);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 470, 380, 470);
    
        e.Graphics.DrawString("Condition: ", fntBoldString, Brushes.Black, 420, 450);
        e.Graphics.DrawString(cbxVehiclesConditions.Text, fntRegularString, Brushes.Black, 530, 450); // ?
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 470, 720, 470);
    
        e.Graphics.DrawString("Make: ", fntBoldString, Brushes.Black, 100, 480);
        e.Graphics.DrawString(txtMake.Text, fntRegularString, Brushes.Black, 260, 480);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 500, 380, 500);
    
        e.Graphics.DrawString("Model: ", fntBoldString, Brushes.Black, 420, 480);
        e.Graphics.DrawString(txtModel.Text, fntRegularString, Brushes.Black, 530, 480);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 500, 720, 500);
    
        e.Graphics.DrawString("Vehicle Condition: ", fntBoldString, Brushes.Black, 100, 510);
        e.Graphics.DrawString(cbxVehiclesConditions.Text, fntRegularString, Brushes.Black, 260, 510);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 530, 380, 530);
    
        e.Graphics.DrawString("Tank Level: ", fntBoldString, Brushes.Black, 420, 510);
        e.Graphics.DrawString(cbxTanksLevels.Text, fntRegularString, Brushes.Black, 530, 510);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 530, 720, 530);
    
        e.Graphics.DrawString("Mileage Start:", fntBoldString, Brushes.Black, 100, 540);
        e.Graphics.DrawString(txtMileageStart.Text, fntRegularString, Brushes.Black, 260, 540);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 560, 380, 560);
    
        e.Graphics.DrawString("Mileage End:", fntBoldString, Brushes.Black, 420, 540);
        e.Graphics.DrawString(txtMileageEnd.Text, fntRegularString, Brushes.Black, 530, 540);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 560, 720, 560);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 590, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 590, 620, 20));
    
        e.Graphics.DrawString("Order Timing Information", fntBoldString, Brushes.White, 100, 590);
    
        e.Graphics.DrawString("Start Date:", fntBoldString, Brushes.Black, 100, 620);
        e.Graphics.DrawString(dtpRentStartDate.Value.ToString("D"), fntRegularString, Brushes.Black, 260, 620);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 640, 720, 640);
    
        e.Graphics.DrawString("End Date:", fntBoldString, Brushes.Black, 100, 650);
        e.Graphics.DrawString(dtpRentEndDate.Value.ToString("D"), fntRegularString, Brushes.Black, 260, 650);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 670, 520, 670);
    
        e.Graphics.DrawString("Total Days:", fntBoldString, Brushes.Black, 550, 650);
        e.Graphics.DrawString(txtTotalDays.Text, fntRegularString, Brushes.Black, 640, 650);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 550, 670, 720, 670);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 700, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 700, 620, 20));
    
        e.Graphics.DrawString("Order Evaluation", fntBoldString, Brushes.White, 100, 700);
    
        StringFormat fmtString = new StringFormat();
        fmtString.Alignment = StringAlignment.Far;
    
        e.Graphics.DrawString("Rate Applied:", fntBoldString, Brushes.Black, 100, 740);
        e.Graphics.DrawString(txtRateApplied.Text, fntRegularString, Brushes.Black, 300, 740, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 760, 380, 760);
    
        e.Graphics.DrawString("Tax Rate:", fntBoldString, Brushes.Black, 420, 740);
        e.Graphics.DrawString(txtTaxRate.Text, fntRegularString, Brushes.Black, 640, 740, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 760, 720, 760);
    
        e.Graphics.DrawString("Sub-Total:", fntBoldString, Brushes.Black, 100, 770);
        e.Graphics.DrawString(txtSubTotal.Text, fntRegularString, Brushes.Black, 300, 770, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 790, 380, 790);
    
        e.Graphics.DrawString("Tax Amount:", fntBoldString, Brushes.Black, 420, 770);
        e.Graphics.DrawString(txtTaxAmount.Text, fntRegularString, Brushes.Black, 640, 770, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 790, 720, 790);
    
        e.Graphics.DrawString("Order Total:", fntBoldString, Brushes.Black, 420, 800);
        e.Graphics.DrawString(txtOrderTotal.Text, fntRegularString, Brushes.Black, 640, 800, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 820, 720, 820);
    }
  28. Return to the Update Rental Order form and double-click the Print button
  29. Implement the event as follows:
    private void btnPrint_Click(object sender, EventArgs e)
    {
        if (dlgPrint.ShowDialog() == DialogResult.OK)
            docPrint.Print();
    }
  30. Return to the Update Rental Order form and double-click the Print Preview button
  31. Implement the event as follows:
    private void btnPrintPreview_Click(object sender, EventArgs e)
    {
        dlgPrintPreview.ShowDialog();
    }
  32. To add a new form to the project, in the Solution Explorer, right-click BethesdaCarRenat1 -> Add -> Windows Form...
  33. Set the Name to NewRentalOrder and press Enter
  34. From the Printing section of the Toolbox, click PrintDocument and click the form
  35. In the Properties window, set its (Name) to docPrint and press Enter
  36. From the Printing section of the Toolbox, click PrintDialog and click the form
  37. In the Properties window, change its Name to dlgPrint
  38. Still in the Properties windows, set its Document property to docPrint
  39. From the Printing section of the Toolbox, click PrintPreviewDialog and click the form
  40. In the Properties window, change its (Name) to dlgPrintPreview
  41. Still in the Properties windows, set its Document property to docPrint
  42. Set the dimensions to those of the Update Rental Order form, then copy all controls from the Update Rental Order form and paste them on the New Rental Order form.
    Design the form as follows:
     
    Bethesda Car Rental - New Rental Order

    Control (Name) Text Other Properties
    Button Button btnSaveRentalOrder Save Rental Order  
    Button Button Close btnClose  
  43. On the New Rental Order form, double-click the Rent Start Date date time picker control
  44. Implement the event as follows:
    private void dtpRentStartDate_ValueChanged(object sender, EventArgs e)
    {
        dtpRentEndDate.Value = dtpRentStartDate.Value;
    }
  45. Return to the New Rental Order form and double-click the Rental Rates button
  46. Implement its event as follows:
    private void btnRentalRates_Click(object sender, EventArgs e)
    {
        RentalRates wndRates = new RentalRates();
        wndRates.Show();
    }
  47. Return to the Order Processing form and, under the form, double-click docPrint
  48. Implement its event as follows:
    private void docPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 90, 750, 90);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 93, 750, 93);
    
        string strDisplay = "Bethesda Car Rental";
        System.Drawing.Font fntBoldString = new Font("Times New Roman", 28, FontStyle.Bold);
        e.Graphics.DrawString(strDisplay, fntBoldString, Brushes.Black, 240, 100);
    
        strDisplay = "Vehicle Rental Order";
        System.Drawing.Font fntRegularString = new System.Drawing.Font("Times New Roman", 22, FontStyle.Regular);
        e.Graphics.DrawString(strDisplay, fntRegularString, Brushes.Black, 280, 150);
    
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 80, 187, 750, 187);
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 190, 750, 190);
    
        fntBoldString = new Font("Times New Roman", 12, FontStyle.Bold);
        fntRegularString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular);
    
        e.Graphics.DrawString("Receipt #:  ", fntBoldString, Brushes.Black, 100, 220);
        e.Graphics.DrawString(txtReceiptNumber.Text, fntRegularString, Brushes.Black, 260, 220);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 240, 380, 240);
    
        e.Graphics.DrawString("Processed By:  ", fntBoldString, Brushes.Black, 420, 220);
        e.Graphics.DrawString(txtEmployeeName.Text, fntRegularString, Brushes.Black, 550, 220);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 240, 720, 240);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 260, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 260, 620, 20));
    
        e.Graphics.DrawString("Customer", fntBoldString, Brushes.White, 100, 260);
    
        e.Graphics.DrawString("First Name: ", fntBoldString, Brushes.Black, 100, 300);
        e.Graphics.DrawString(txtCustomerFirstName.Text, fntBoldString, Brushes.Black, 260, 300);
        e.Graphics.DrawString("Last Name: ", fntBoldString, Brushes.Black, 420, 300);
        e.Graphics.DrawString(txtCustomerLastName.Text, fntBoldString, Brushes.Black, 540, 300);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 320, 720, 320);
    
        e.Graphics.DrawString("Address: ", fntBoldString, Brushes.Black, 100, 330);
        e.Graphics.DrawString(txtCustomerAddress.Text, fntRegularString, Brushes.Black, 260, 330);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 350, 720, 350);
    
        strDisplay = txtCustomerCity.Text + " " + cbxCustomerStates.Text + " " + txtCustomerZIPCode.Text;
        e.Graphics.DrawString(strDisplay, fntRegularString, Brushes.Black, 260, 360);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 260, 380, 720, 380);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 410, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 410, 620, 20));
    
        e.Graphics.DrawString("Car Information", fntBoldString, Brushes.White, 100, 410);
    
        e.Graphics.DrawString("Tag #: ", fntBoldString, Brushes.Black, 100, 450);
        e.Graphics.DrawString(txtTagNumber.Text, fntRegularString, Brushes.Black, 260, 450);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 470, 380, 470);
    
        e.Graphics.DrawString("Condition: ", fntBoldString, Brushes.Black, 420, 450);
        e.Graphics.DrawString(cbxVehiclesConditions.Text, fntRegularString, Brushes.Black, 530, 450); // ?
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 470, 720, 470);
    
        e.Graphics.DrawString("Make: ", fntBoldString, Brushes.Black, 100, 480);
        e.Graphics.DrawString(txtMake.Text, fntRegularString, Brushes.Black, 260, 480);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 500, 380, 500);
    
        e.Graphics.DrawString("Model: ", fntBoldString, Brushes.Black, 420, 480);
        e.Graphics.DrawString(txtModel.Text, fntRegularString, Brushes.Black, 530, 480);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 500, 720, 500);
    
        e.Graphics.DrawString("Vehicle Condition: ", fntBoldString, Brushes.Black, 100, 510);
        e.Graphics.DrawString(cbxVehiclesConditions.Text, fntRegularString, Brushes.Black, 260, 510);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 530, 380, 530);
    
        e.Graphics.DrawString("Tank Level: ", fntBoldString, Brushes.Black, 420, 510);
        e.Graphics.DrawString(cbxTanksLevels.Text, fntRegularString, Brushes.Black, 530, 510);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 530, 720, 530);
    
        e.Graphics.DrawString("Mileage Start:", fntBoldString, Brushes.Black, 100, 540);
        e.Graphics.DrawString(txtMileageStart.Text, fntRegularString, Brushes.Black, 260, 540);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 560, 380, 560);
    
        e.Graphics.DrawString("Mileage End:", fntBoldString, Brushes.Black, 420, 540);
        e.Graphics.DrawString(txtMileageEnd.Text, fntRegularString, Brushes.Black, 530, 540);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 560, 720, 560);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 590, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 590, 620, 20));
    
        e.Graphics.DrawString("Order Timing Information", fntBoldString, Brushes.White, 100, 590);
    
        e.Graphics.DrawString("Start Date:", fntBoldString, Brushes.Black, 100, 620);
        e.Graphics.DrawString(dtpRentStartDate.Value.ToString("D"), fntRegularString, Brushes.Black, 260, 620);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 640, 720, 640);
    
        e.Graphics.DrawString("End Date:", fntBoldString, Brushes.Black, 100, 650);
        e.Graphics.DrawString(dtpRentEndDate.Value.ToString("D"), fntRegularString, Brushes.Black, 260, 650);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 670, 520, 670);
    
        e.Graphics.DrawString("Total Days:", fntBoldString, Brushes.Black, 550, 650);
        e.Graphics.DrawString(txtTotalDays.Text, fntRegularString, Brushes.Black, 640, 650);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 550, 670, 720, 670);
    
        e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(100, 700, 620, 20));
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(100, 700, 620, 20));
    
        e.Graphics.DrawString("Order Evaluation", fntBoldString, Brushes.White, 100, 700);
    
        StringFormat fmtString = new StringFormat();
        fmtString.Alignment = StringAlignment.Far;
    
        e.Graphics.DrawString("Rate Applied:", fntBoldString, Brushes.Black, 100, 740);
        e.Graphics.DrawString(txtRateApplied.Text, fntRegularString, Brushes.Black, 300, 740, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 760, 380, 760);
    
        e.Graphics.DrawString("Tax Rate:", fntBoldString, Brushes.Black, 420, 740);
        e.Graphics.DrawString(txtTaxRate.Text, fntRegularString, Brushes.Black, 640, 740, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 760, 720, 760);
    
        e.Graphics.DrawString("Sub-Total:", fntBoldString, Brushes.Black, 100, 770);
        e.Graphics.DrawString(txtSubTotal.Text, fntRegularString, Brushes.Black, 300, 770, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 790, 380, 790);
    
        e.Graphics.DrawString("Tax Amount:", fntBoldString, Brushes.Black, 420, 770);
        e.Graphics.DrawString(txtTaxAmount.Text, fntRegularString, Brushes.Black, 640, 770, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 790, 720, 790);
    
        e.Graphics.DrawString("Order Total:", fntBoldString, Brushes.Black, 420, 800);
        e.Graphics.DrawString(txtOrderTotal.Text, fntRegularString, Brushes.Black, 640, 800, fmtString);
        e.Graphics.DrawLine(new Pen(Color.Black, 1), 420, 820, 720, 820);
    }
  49. Return to the Order Processing form and double-click the Print button
  50. Implement the event as follows:
    private void btnPrint_Click(object sender, EventArgs e)
    {
        if (dlgPrint.ShowDialog() == DialogResult.OK)
            docPrint.Print();
    }
  51. Return to the Order Processing form and double-click the Print Preview button
  52. Implement the event as follows:
    private void btnPrintPreview_Click(object sender, EventArgs e)
    {
        dlgPrintPreview.ShowDialog();
    }
  53. To add a new form to the application, in the Solution Explorer, right-click BethesdaCarRental1 -> Add -> Windows Form...
  54. Set the Name to RentalOrders and click Add
  55. Design the form as follows:
     

    Bethesda Car Rental: Rental Orders

    Control (Name) Text Other Properties
    ListView lvwRentalOrders   Anchor: Top, Bottom, Left, Right
     
    (Name) Text TextAlign Width
    colReceiptOrderNumber Receipt #   65
    colProcessedDate Date Processed Center 90
    colEmployee Employee   140
    colCustomer Customer   120
    colVehicle Vehicle   140
    colCarCondition Condition    
    colTank Tank Level   70
    colStartingMileage Mileage Start Right 75
    colEndingMileage Mileage End Right 75
    colTotalMileage Total Right 45
    colRentStartingDate Start Date Center  
    colRentEndingDate End Date Center  
    colDaysTotal Total Right 45
    colDailyRate Cost Right 50
    colDailySubTotal Sub-Total Right  
    colTaxPercentage Tax Rate Right  
    colTaxPaid Tax Amt Right 55
    colTotalOrder Total Order Right 65
    colStatusOfOrder Order Status   150
    Button btnNewRentalOrder New Rental Order ... Anchor: Bottom, Right
    Button btnUpdateRentalOrder Update Rental Order ... Anchor: Bottom, Right
    Button btnClose Close Anchor: Bottom, Right
  56. Save all
 
 
 

A Dictionary-Based Collection Class

A dictionary is a list of objects where each item is in the form Key=Value. In real life, the key on the left doesn't have to be unique, but each key most have a set of values. In a database application, each key most be unique so that it is distinguishable from the other keys. For example in a a computer database where you have a list of products sold in a store, each product must have a unique number that distinguishes it from the other products, even if the description is the same. That idea makes it possible to use the concept of dictionary in a computer database. The Key can be used to uniquely identify each record. The Value, or the section that represents the Value, is used to describe the record.

The .NET Framework provides various interfaces and classes to support dictionary-based lists. Two of the dictionary-based collections classes are Hashtable and SortedList. These classes implement the IDictionary interface, the ICollection interface, the IEnumerable interface, and the ICloneable interface.

The Hashtable class implements the ISerializable interface, which makes it possible for the list to be serialized. Still, the SortedList class is marked with the Serializable attribute, which makes it possible to file-process its list. The System.Collections.Generic namespace provides dictionary support through the Dictionary, the SortedDictionary, and the SortedList classes.

In this application, we will use the Dictionary class to see how a dictionary-based collection can be used in a database.

Practical LearningPractical Learning: Adding Items to the Collection

  1. Display the Vehicle Editor dialog box and double-click the Submit button
  2. Implement the Click event as follows:
    private void btnSubmit_Click(object sender, EventArgs e)
    {
        Vehicle vehicle = new Vehicle();
        BinaryFormatter bfmVehicles = new BinaryFormatter();
        Dictionary<string, Vehicle> lstVehicles = new Dictionary<string, Vehicle>();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Vehicles.crs";
    
        if (File.Exists(strFileName))
        {
            using (FileStream stmVehicles = new FileStream(strFileName,
                                                           FileMode.Open,
                                                           FileAccess.Read,
                                                           FileShare.Read))
            {
                lstVehicles = (Dictionary<string, Vehicle>)bfmVehicles.Deserialize(stmVehicles);
            }
        }
    
        if (txtTagNumber.Text.Length == 0)
        {
            MessageBox.Show("You must enter the car's tag number.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        if (txtMake.Text.Length == 0)
        {
            MessageBox.Show("You must specify the car's manufacturer.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        if (txtModel.Text.Length == 0)
        {
            MessageBox.Show("You must enter the model of the vehicle.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        // Create a Vehicle object
        vehicle.Make = txtMake.Text;
        vehicle.Model = txtModel.Text;
        vehicle.Doors = int.Parse(txtDoors.Text);
        vehicle.Passengers = int.Parse(txtPassengers.Text);
        vehicle.Condition = cbxConditions.Text;
        vehicle.Category = cbxCategories.Text;
        vehicle.Availability = cbxAvailabilities.Text;
    
        // Call the Add method of our collection class to add the vehicle
        lstVehicles.Add(txtTagNumber.Text, vehicle);
    
        // Save the list
        using (FileStream stmVehicles = new FileStream(strFileName,
                                                        FileMode.Create,
                                                        FileAccess.Write,
                                                        FileShare.Write))
        {
            bfmVehicles.Serialize(stmVehicles, lstVehicles);
    
            if (lblPictureName.Text.Length != 0)
            {
                FileInfo flePicture = new FileInfo(lblPictureName.Text);
                flePicture.CopyTo(@"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\" +
                                    txtTagNumber.Text +
                                    flePicture.Extension);
            }
    
            txtTagNumber.Text = "";
            txtMake.Text = "";
            txtModel.Text = "";
            txtDoors.Text = "";
            txtPassengers.Text = "0";
            cbxConditions.SelectedIndex = 0;
            cbxCategories.Text = "Economy";
            cbxAvailabilities.SelectedIndex = 0;
            lblPictureName.Text = ".";
            pbxVehicle.Image = Image.FromFile(@"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Vehicle1.jpg");
        }
    }
  3. Return to the Vehicle Editor form and double-click the Close button
  4. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  5. Save all

Practical LearningPractical Learning: Enumerating the Members of a Collection

  1. Display the Employees form and double-click an unoccupied area of its body
  2. Change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace BethesdaCarRental1
    {
        public partial class Employees : Form
        {
            public Employees()
            {
                InitializeComponent();
            }
    
            internal void ShowEmployees()
            {
                BinaryFormatter bfmEmployees = new BinaryFormatter();
                Dictionary<string, Employee> lstEmployees = new Dictionary<string, Employee>();
                // This is the file that holds the list of employees
                string strFilename = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Employees.cre";
    
                if (File.Exists(strFilename))
                {
                    using (FileStream stmEmployees = new FileStream(strFilename,
    	                                	                FileMode.Open,
            	                	                	FileAccess.Read,
                    		                	        FileShare.Read))
                    {
                        // Retrieve the list of employees from file
                        lstEmployees = (Dictionary<string, Employee>)bfmEmployees.Deserialize(stmEmployees);
    
                        // Before displaying the employees, empty the list view
                        lvwEmployees.Items.Clear();
                        // This variable will allow us to identify the odd and even indexes
                        int i = 1;
                        // Use the KeyValuePair class to visit each key/value item
                        foreach (KeyValuePair<string, Employee> kvp in lstEmployees)
                        {
                            ListViewItem lviEmployee = new ListViewItem(kvp.Key);
    
                            Employee empl = kvp.Value;
    
                            lviEmployee.SubItems.Add(empl.FirstName);
                            lviEmployee.SubItems.Add(empl.LastName);
                            lviEmployee.SubItems.Add(empl.EmployeeName);
                            lviEmployee.SubItems.Add(empl.Title);
    
                            if (i % 2 == 0)
                            {
                                lviEmployee.BackColor = Color.FromArgb(255, 128, 0);
                                lviEmployee.ForeColor = Color.White;
                            }
                            else
                            {
                                lviEmployee.BackColor = Color.FromArgb(128, 64, 64);
                                lviEmployee.ForeColor = Color.White;
                            }
    
                            lvwEmployees.Items.Add(lviEmployee);
    
                            i++;
                        }
                    }
                }
            }
    
            private void Employees_Load(object sender, EventArgs e)
            {
                ShowEmployees();
            }
        }
    }
  3. Return to the Employees form and double-click the New Employee button
  4. Implement its event as follows:
    private void btnNewEmployee_Click(object sender, EventArgs e)
    {
        EmployeeEditor editor = new EmployeeEditor();
        BinaryFormatter bfBethesdaCarRental = new BinaryFormatter();
        Dictionary<string, Employee> lstEmployees = new Dictionary<string, Employee>();
        string strFilename = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Employees.cre";
    
        if (File.Exists(strFilename))
        {
            using (FileStream stmEmployees = new FileStream(strFilename,
                                                            FileMode.Open,
                                                            FileAccess.Read,
                                                            FileShare.Read))
            {
                // Retrieve the list of employees from file
                lstEmployees = (Dictionary<string, Employee>)bfBethesdaCarRental.Deserialize(stmEmployees);
            }
        }
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            if (editor.txtEmployeeNumber.Text == "")
            {
                MessageBox.Show("You must provide an employee number.",
                                "Bethesda Car Rental",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
    
            if (editor.txtLastName.Text == "")
            {
                MessageBox.Show("You must provide the employee's last name.",
                                "Bethesda Car Rental",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
    
            Employee empl = new Employee();
    
            empl.FirstName = editor.txtFirstName.Text;
            empl.LastName = editor.txtLastName.Text;
            empl.Title = editor.txtTitle.Text;
            lstEmployees.Add(editor.txtEmployeeNumber.Text, empl);
    
            using (FileStream fsBethesdaCarRental = new FileStream(strFilename,
                                                    FileMode.Create,
                                                    FileAccess.Write,
                                                    FileShare.Write))
            {
                bfBethesdaCarRental.Serialize(fsBethesdaCarRental, lstEmployees);
            }
        }
                
        ShowEmployees();
    }
  5. Return to the Employees form and double-click the Close button
  6. Implement its event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  7. Display the New Rental Order form and double-click an unoccupied area of its body
  8. Implement the event as follows:
    private void NewRentalOrder_Load(object sender, EventArgs e)
    {
        int iReceiptNumber = 100000;
        BinaryFormatter bfRentalOrders = new BinaryFormatter();
        Dictionary<int, RentalOrder> lstRentalOrders = new Dictionary<int, RentalOrder>();
        string strFilename = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\RentalOrders.ros";
    
        if (File.Exists(strFilename))
        {
            using (FileStream stmRentalOrders = new FileStream(strFilename,
    	                                                   FileMode.Open,
            	                                           FileAccess.Read,
                    	                                   FileShare.Read))
            {
                lstRentalOrders = (Dictionary<int, RentalOrder>)bfRentalOrders.Deserialize(stmRentalOrders);
    
                foreach (KeyValuePair<int, RentalOrder> kvp in lstRentalOrders)
                {
                    iReceiptNumber = kvp.Key;
                }
            }
        }
    
        txtReceiptNumber.Text = (iReceiptNumber + 1).ToString();
    }
  9. Return to the New Rental Order button and double-click the Save Rental Order button
  10. Implement the event as follows:
    private void btnSaveRentalOrder_Click(object sender, EventArgs e)
    {
        BinaryFormatter bfmRentalOrders = new BinaryFormatter();
        Dictionary<int, RentalOrder> lstRentalOrders = new Dictionary<int, RentalOrder>();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\RentalOrders.ros";
    
        if (File.Exists(strFileName))
        {
            using (FileStream stmRentalOrders = new FileStream(strFileName,
                                                        FileMode.Open,
                                                        FileAccess.Read,
                                                        FileShare.Read))
            {
                lstRentalOrders = (Dictionary<int, RentalOrder>)bfmRentalOrders.Deserialize(stmRentalOrders);
            }
        }
    
        if (string.IsNullOrEmpty(txtReceiptNumber.Text))
        {
            MessageBox.Show("The receipt number is missing.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        // Don't save this rental order if we don't
        // know who processed it
        if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
        {
            MessageBox.Show("You must enter the employee number of the " +
                            "clerk who processed this order.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        // Don't save the rental order if we don't
        // know what car is being rented
        if (string.IsNullOrEmpty(txtTagNumber.Text))
        {
            MessageBox.Show("You must enter the tag number " +
                            "of the vehicle that is being rented.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        // Create a rental order based on the information on the form
        RentalOrder order = new RentalOrder();
    
        order.DateProcessed = dtpDateProcessed.Value.ToShortDateString();
        order.EmployeeNumber = txtEmployeeNumber.Text;
        order.CustomerFirstName = txtCustomerFirstName.Text;
        order.CustomerLastName = txtCustomerLastName.Text;
        order.CustomerAddress = txtCustomerAddress.Text;
        order.CustomerCity = txtCustomerCity.Text;
        order.CustomerState = cbxCustomerStates.Text;
        order.CustomerZIPCode = txtCustomerZIPCode.Text;
        order.VehicleTagNumber = txtTagNumber.Text;
        order.VehicleCondition = cbxVehiclesConditions.Text;
        order.TankLevel = cbxTanksLevels.Text;
    
        try
        {
            order.MileageStart = int.Parse(txtMileageStart.Text);
            order.MileageEnd = int.Parse(txtMileageStart.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid mileage start value",
    	                "Bethesda Car Rental",
            	        MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        order.MileageTotal = 0;
        try
        {
            order.RentStartDate = dtpRentStartDate.Value.ToShortDateString();
            order.RentEndDate = dtpRentStartDate.Value.ToShortDateString();
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid start date",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        order.TotalDays = 0;
        try {
            order.RateApplied = double.Parse(txtRateApplied.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid rate applied value",
    	                "Bethesda Car Rental",
            	        MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        order.SubTotal = double.Parse(txtSubTotal.Text);
                
        try {
            order.TaxRate = double.Parse(txtTaxRate.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid start date",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        order.TaxAmount = double.Parse(txtTaxAmount.Text);
        order.OrderTotal = double.Parse(txtOrderTotal.Text);
        order.OrderStatus = cbxOrderStatus.Text;
        order.Notes = txtNotes.Text;
    
        lstRentalOrders.Add(int.Parse(txtReceiptNumber.Text), order);
    
        using (FileStream bcrStream = new FileStream(strFileName,
    	                                         FileMode.Create,
            	                                 FileAccess.Write,
                    	                         FileShare.Write))
        {
            bfmRentalOrders.Serialize(bcrStream, lstRentalOrders);
        }
    
        Close();
    }
  11. Return to the New Rental Order form and double-click the Close button
  12. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  13. Display the Update Rental Order form and click the Employee # text box
  14. On the Properties window, click the Events button and double-click Leave
  15. Implement the event as follows:
    private void txtEmployeeNumber_Leave(object sender, EventArgs e)
    {
        BinaryFormatter bfmEmployees = new BinaryFormatter();
        Dictionary<string, Employee> lstEmployees = new Dictionary<string, Employee>();
        string strFilename = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Employees.cre";
    
        if (File.Exists(strFilename))
        {
            using (FileStream stmEmployees = new FileStream(strFilename,
                                                        FileMode.Open,
                                                        FileAccess.Read,
                                                        FileShare.Read))
            {
                // Retrieve the list of employees from file
                lstEmployees = (Dictionary<string, Employee>)bfmEmployees.Deserialize(stmEmployees);
    
                // Use the KeyValuePair class to visit each key/value item
                foreach (KeyValuePair<string, Employee> kvp in lstEmployees)
                {
                    Employee empl = kvp.Value;
    
                    if( kvp.Key == txtEmployeeNumber.Text )
                        txtEmployeeName.Text = empl.EmployeeName;
                }
            }
        }
    }
  16. Return to the Update Rental Order form and click the Tag Number text box
  17. In the Events section of the Properties window, double-click Leave and implement the event as follows:
    private void txtTagNumber_Leave(object sender, EventArgs e)
    {
        BinaryFormatter bfVehicles = new BinaryFormatter();
        Dictionary<string, Vehicle> lstVehicles = new Dictionary<string, Vehicle>();
        string strFilename = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Vehicles.crs";
    
    
        if (File.Exists(strFilename))
        {
            using (FileStream stmVehicles = new FileStream(strFilename,
                                                           FileMode.Open,
                                                           FileAccess.Read,
                                                           FileShare.Read))
            {
                lstVehicles = (Dictionary<string, Vehicle>)bfVehicles.Deserialize(stmVehicles);
                
                foreach (KeyValuePair<string, Vehicle> kvp in lstVehicles)
                {
                    Vehicle car = kvp.Value;
    
                    if (kvp.Key == txtTagNumber.Text)
                    {
                        txtMake.Text  = car.Make;
                        txtModel.Text = car.Model;
                        cbxVehiclesConditions.Text = car.Condition;
                    }
                }
            }
        }
    }
  18. Display the Update Rental Order form and double-click the Open button
  19. Implement the event as follows:
    private void btnOpen_Click(object sender, EventArgs e)
    {
        BinaryFormatter bfmRentalOrders = new BinaryFormatter();
        Dictionary<int, RentalOrder> lstRentalOrders = new Dictionary<int, RentalOrder>();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\RentalOrders.ros";
    
        if (File.Exists(strFileName))
        {
            using (FileStream stmRentalOrders = new FileStream(strFileName,
                                                        FileMode.Open,
                                                        FileAccess.Read,
                                                        FileShare.Read))
            {
                lstRentalOrders = (Dictionary<int, RentalOrder>)bfmRentalOrders.Deserialize(stmRentalOrders);
            }
        }
    
        if (string.IsNullOrEmpty(txtReceiptNumber.Text))
        {
            MessageBox.Show("You must enter a receipt number.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        if (File.Exists(strFileName))
        {
            using (FileStream stmRentalOrders = new FileStream(strFileName,
                                                     FileMode.Open,
                                                     FileAccess.Read,
                                                     FileShare.Read))
            {
                lstRentalOrders = (Dictionary<int, RentalOrder>)bfmRentalOrders.Deserialize(stmRentalOrders);
    
                foreach (KeyValuePair<int, RentalOrder> kvp in lstRentalOrders)
                {
                    if (kvp.Key == int.Parse(txtReceiptNumber.Text))
                    {
                        RentalOrder ro = kvp.Value;
    
                        dtpDateProcessed.Value = DateTime.Parse(ro.DateProcessed);
                        txtEmployeeNumber.Text = ro.EmployeeNumber;
                        txtEmployeeNumber_Leave(sender, e);
                        txtCustomerFirstName.Text = ro.CustomerFirstName;
                        txtCustomerLastName.Text = ro.CustomerLastName;
                        txtCustomerAddress.Text = ro.CustomerAddress;
                        txtCustomerCity.Text = ro.CustomerCity;
                        cbxCustomerStates.Text = ro.CustomerState;
                        txtCustomerZIPCode.Text = ro.CustomerZIPCode;
                        txtTagNumber.Text = ro.VehicleTagNumber;
                        txtTagNumber_Leave(sender, e);
                        cbxVehiclesConditions.Text = ro.VehicleCondition;
                        cbxTanksLevels.Text = ro.TankLevel;
                        txtMileageStart.Text = ro.MileageStart.ToString();
                        txtMileageEnd.Text = ro.MileageEnd.ToString();
                        txtMileageTotal.Text = ro.MileageTotal.ToString();
                        dtpRentStartDate.Value = DateTime.Parse(ro.RentStartDate);
                        dtpRentEndDate.Value = DateTime.Parse(ro.RentEndDate);
                        txtTotalDays.Text = ro.TotalDays.ToString();
                        txtRateApplied.Text = ro.RateApplied.ToString();
                        txtSubTotal.Text = ro.SubTotal.ToString();
                        txtTaxRate.Text = ro.TaxRate.ToString();
                        txtTaxAmount.Text = ro.TaxAmount.ToString();
                        txtOrderTotal.Text = ro.OrderTotal.ToString();
                        cbxOrderStatus.Text = ro.OrderStatus;
                        txtNotes.Text = ro.Notes;
                        return;
                    }
                }
            }
        }
    }
  20. Display the Update Rental Order form and double-click the Update Rental Order button
  21. Implement the event as follows:
    private void btnUpdateRentalOrder_Click(object sender, EventArgs e)
    {
        BinaryFormatter bfmRentalOrders = new BinaryFormatter();
        Dictionary<int, RentalOrder> lstRentalOrders = new Dictionary<int, RentalOrder>();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\RentalOrders.ros";
    
        if (string.IsNullOrEmpty(txtReceiptNumber.Text))
        {
            MessageBox.Show("The receipt number is missing.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        // Don't save this rental order if we don't
        // know who processed it
        if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
        {
            MessageBox.Show("You must enter the employee number of the " +
                            "clerk who processed this order.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        // Don't save the rental order if we don't
        // know what car is being rented
        if (string.IsNullOrEmpty(txtTagNumber.Text))
        {
            MessageBox.Show("You must enter the tag number " +
                            "of the vehicle that is being rented.",
                            "Bethesda Car Rental",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
    
        if (File.Exists(strFileName))
        {
            using (FileStream stmRentalOrders = new FileStream(strFileName,
                                                        FileMode.Open,
                                                        FileAccess.Read,
                                                        FileShare.Read))
            {
                lstRentalOrders = (Dictionary<int, RentalOrder>)bfmRentalOrders.Deserialize(stmRentalOrders);
    
                foreach (KeyValuePair<int, RentalOrder> kvpRentalOrder in lstRentalOrders)
                {
                    if (kvpRentalOrder.Key == int.Parse(txtReceiptNumber.Text))
                    {
                        RentalOrder order = kvpRentalOrder.Value;
    
                        order.DateProcessed = dtpDateProcessed.Value.ToShortDateString();
                        order.EmployeeNumber = txtEmployeeNumber.Text;
                        order.CustomerFirstName = txtCustomerFirstName.Text;
                        order.CustomerLastName = txtCustomerLastName.Text;
                        order.CustomerAddress = txtCustomerAddress.Text;
                        order.CustomerCity = txtCustomerCity.Text;
                        order.CustomerState = cbxCustomerStates.Text;
                        order.CustomerZIPCode = txtCustomerZIPCode.Text;
                        order.VehicleTagNumber = txtTagNumber.Text;
                        order.VehicleCondition = cbxVehiclesConditions.Text;
                        order.TankLevel = cbxTanksLevels.Text;
                        order.MileageStart = int.Parse(txtMileageStart.Text);
                        order.MileageEnd = int.Parse(txtMileageEnd.Text);
                        order.MileageTotal = int.Parse(txtMileageTotal.Text);
                        order.RentStartDate = dtpRentStartDate.Value.ToShortDateString();
                        order.RentEndDate = dtpRentEndDate.Value.ToShortDateString();
                        order.TotalDays = int.Parse(txtTotalDays.Text);
                        order.RateApplied = double.Parse(txtRateApplied.Text);
                        order.SubTotal = double.Parse(txtSubTotal.Text);
                        order.TaxRate = double.Parse(txtTaxRate.Text);
                        order.TaxAmount = double.Parse(txtTaxAmount.Text);
                        order.OrderTotal = double.Parse(txtOrderTotal.Text);
                        order.OrderStatus = cbxOrderStatus.Text;
                        order.Notes = txtNotes.Text;
    
                        break;
                    }
                }
            }
        }
    
        using (FileStream bcrStream = new FileStream(strFileName,
                                                     FileMode.Create,
                                                     FileAccess.Write,
                                                     FileShare.Write))
        {
            bfmRentalOrders.Serialize(bcrStream, lstRentalOrders);
        }
    
        Close();
    }
  22. Return to the Update Rental Order form and double-click the Close button
  23. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  24. Display the Rental Orders form and double-click an unoccupied area of its body
  25. Implement the event as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace BethesdaCarRental1
    {
        public partial class RentalOrders : Form
        {
            public RentalOrders()
            {
                InitializeComponent();
            }
    
            private void ShowRentalOrders()
            {
                string   orderStatus, notes;
                string   condition, tankLevel;
                DateTime dateProcessed, rentStartDate, rentEndDate;
                string   customer = "", employee = "", vehicle = "";
                int      mileageStart, mileageEnd, mileageTotal, totalDays;
                string   employeeNumber, employeeFirstName, employeeLastName;
                double   rateApplied, subTotal, taxRate, taxAmount, orderTotal;
    
                BinaryFormatter bfmVehicles = new BinaryFormatter();
                BinaryFormatter bfmEmployees = new BinaryFormatter();
                BinaryFormatter bfmRentalOrders = new BinaryFormatter();
                Dictionary<string, Vehicle> lstVehicles = new Dictionary<string, Vehicle>();
                Dictionary<string, Employee> lstEmployees = new Dictionary<string, Employee>();
                Dictionary<int, RentalOrder> lstRentalOrders = new Dictionary<int, RentalOrder>();
                string strVehiclesFile = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Vehicles.crs";
                string strEmployeesFile = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\Employees.cre";
                string strRentalOrdersFile = @"C:\Microsoft Visual C# Application Design\Bethesda Car Rental\RentalOrders.ros";
    
                if (File.Exists(strRentalOrdersFile))
                {
                    lvwRentalOrders.Items.Clear();
    
                    using (FileStream stmRentalOrders = new FileStream(strRentalOrdersFile,
                                                             FileMode.Open,
                                                             FileAccess.Read,
                                                             FileShare.Read))
                    {
                        lstRentalOrders = (Dictionary<int, RentalOrder>)bfmRentalOrders.Deserialize(stmRentalOrders);
    
                        foreach (KeyValuePair<int, RentalOrder> kvp in lstRentalOrders)
                        {
                            RentalOrder ro = kvp.Value;
                            ListViewItem lviRentalOrder = new ListViewItem(kvp.Key.ToString());
    
                            dateProcessed = DateTime.Parse(ro.DateProcessed);
    
                            using (FileStream stmEmployees = new FileStream(strEmployeesFile,
                                                                     FileMode.Open,
                                                                     FileAccess.Read,
                                                                     FileShare.Read))
                            {
                                // Retrieve the list of employees from file
                                lstEmployees = (Dictionary<string, Employee>)bfmEmployees.Deserialize(stmEmployees);
    
                                // Use the KeyValuePair class to visit each key/value item
                                foreach (KeyValuePair<string, Employee> kvpEmployee in lstEmployees)
                                {
                                    Employee empl = kvpEmployee.Value;
    
                                    if (kvpEmployee.Key == ro.EmployeeNumber)
                                    {
                                        employee = kvpEmployee.Key + ": " + empl.EmployeeName;
                                        break;
                                    }
                                }
                            }
    
                            customer = ro.CustomerFirstName + " " + ro.CustomerLastName;
    
                            using (FileStream stmVehicles = new FileStream(strVehiclesFile,
                                                                   FileMode.Open,
                                                                   FileAccess.Read,
                                                                   FileShare.Read))
                            {
                                lstVehicles = (Dictionary<string, Vehicle>)bfmVehicles.Deserialize(stmVehicles);
    
                                foreach (KeyValuePair<string, Vehicle> kvpVehicle in lstVehicles)
                                {
                                    Vehicle car = kvpVehicle.Value;
    
                                    if (kvpVehicle.Key == ro.VehicleTagNumber)
                                    {
                                        vehicle = kvpVehicle.Key + ": " + car.Make + " " + car.Model;
                                    }
                                }
                            }
    
                            condition = ro.VehicleCondition;
                            tankLevel = ro.TankLevel;
                            mileageStart = ro.MileageStart;
                            mileageEnd = ro.MileageEnd;
                            mileageTotal = ro.MileageTotal;
                            rentStartDate = DateTime.Parse(ro.RentStartDate);
                            rentEndDate = DateTime.Parse(ro.RentEndDate);
                            totalDays = ro.TotalDays;
                            rateApplied = ro.RateApplied;
                            subTotal  = ro.SubTotal;
                            taxRate = ro.TaxRate;
                            taxAmount = ro.TaxAmount;
                            orderTotal = ro.OrderTotal;
                            orderStatus = ro.OrderStatus;
                            notes = ro.Notes;
    
                            lviRentalOrder.SubItems.Add(dateProcessed.ToShortDateString());
                            lviRentalOrder.SubItems.Add(employee);
                            lviRentalOrder.SubItems.Add(customer);
                            lviRentalOrder.SubItems.Add(vehicle);
                            lviRentalOrder.SubItems.Add(condition);
                            lviRentalOrder.SubItems.Add(tankLevel);
                            lviRentalOrder.SubItems.Add(mileageStart.ToString());
                            lviRentalOrder.SubItems.Add(mileageEnd.ToString());
                            lviRentalOrder.SubItems.Add(mileageTotal.ToString());
                            lviRentalOrder.SubItems.Add(rentStartDate.ToShortDateString());
                            lviRentalOrder.SubItems.Add(rentEndDate.ToShortDateString());
                            lviRentalOrder.SubItems.Add(totalDays.ToString());
                            lviRentalOrder.SubItems.Add(rateApplied.ToString());
                            lviRentalOrder.SubItems.Add(subTotal.ToString());
                            lviRentalOrder.SubItems.Add(taxRate.ToString());
                            lviRentalOrder.SubItems.Add(taxAmount.ToString());
                            lviRentalOrder.SubItems.Add(orderTotal.ToString());
                            lviRentalOrder.SubItems.Add(orderStatus);
                            lviRentalOrder.SubItems.Add(notes);
    
                            lvwRentalOrders.Items.Add(lviRentalOrder);
                        }
                    }
                }
            }
    
            private void RentalOrders_Load(object sender, EventArgs e)
            {
                ShowRentalOrders();
            }
        }
    }
  26. Rreturn to the Rental Orders form and double-click the New Rental Order button
  27. Implement the event as follows:
    private void btnNewRentalOrder_Click(object sender, EventArgs e)
    {
        NewRentalOrder nro = new NewRentalOrder();
    
        nro.ShowDialog();
        ShowRentalOrders();
    }
  28. Rreturn to the Rental Orders form and double-click the Update Rental Order button
  29. Implement the event as follows:
    private void btnUpdateRentalOrder_Click(object sender, EventArgs e)
    {
        UpdateRentalOrder uro = new UpdateRentalOrder();
     
        uro.ShowDialog();
        ShowRentalOrders();
    }
  30. Return to the Rental Orders form and double-click the Close button
  31. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  32. To add a new form, on the main menu, click PROJECT -> Add Windows Form ...
  33. Set the Name to Vehicles and click Add
  34. Design the form as follows:
     
    Bethesda Car Rental - Vehicles Inventory
    Control (Name) Text Other Properties
    Panel Panel     Dock: Top
    Label Label   Bethesda Car Rental - Vehicles Inventory ForeColor: Blue
    Label Label   ________________ Anchor: Left, Right
    AutoSize: False
    BackColor: Maroon
    BorderStyle: FixedSingle
    ListView List View lvwVehicles   Dock: Fill
       
    (Name) Text TextAlign Width
    colTagNumber Tag #   55
    colMake Make   70
    colModel Model   75
    colDoors Doors Right 40
    colPassengers Passengers Right 68
    colCondition Condition    
    colCategory Category   70
    colAvailability Availability   85
    PictureBox Picture Box pbxVehicle   Dock: Fill
    SizeMode: Zoom
    Button Button btnNewVehicle New Vehicle...  
    Button Button Close btnClose  
  35. Double-click an unoccupied area of the form and change the document as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace BethesdaCarRental1
    {
        public partial class Vehicles : Form
        {
            public Vehicles()
            {
                InitializeComponent();
            }
    
            internal void ShowVehicles()
            {
                BinaryFormatter bfmVehicles = new BinaryFormatter();
                Dictionary<string, Vehicle> lstVehicles = new Dictionary<string, Vehicle>();
                string strFilename = @"C:\Microsoft Visual C# Application Design\Vehicles.crs";
    
                lvwVehicles.Items.Clear();
    
                if (File.Exists(strFilename))
                {
                    using (FileStream stmVehicles = new FileStream(strFilename,
                                                                   FileMode.Open,
                                                                   FileAccess.Read,
                                                                   FileShare.Read))
                    {
                        lstVehicles = (Dictionary<string, Vehicle>)bfmVehicles.Deserialize(stmVehicles);
                    }
                }
    
                foreach (KeyValuePair<string, Vehicle> kvp in lstVehicles)
                {
                    ListViewItem lviVehicle = new ListViewItem(kvp.Key);
    
                    Vehicle car = kvp.Value;
    
                    lviVehicle.SubItems.Add(car.Make);
                    lviVehicle.SubItems.Add(car.Model);
                    lviVehicle.SubItems.Add(car.Doors.ToString());
                    lviVehicle.SubItems.Add(car.Passengers.ToString());
                    lviVehicle.SubItems.Add(car.Condition);
                    lviVehicle.SubItems.Add(car.Category);
                    lviVehicle.SubItems.Add(car.Availability);
    
                    if (car.Availability == "Available")
                    {
                        // For any vehicle that is available for rent,
                        // show its background in green color
                        lviVehicle.BackColor = Color.FromArgb(0, 128, 0);
                        lviVehicle.ForeColor = Color.White;
                    }
                    else if (car.Availability == "Rented")
                    {
                        // If the vehicle is rented, show its background in orange
                        lviVehicle.BackColor = Color.FromArgb(235, 45, 200);
                        lviVehicle.ForeColor = Color.White;
                    }
                    else
                    {
                        lviVehicle.BackColor = Color.FromArgb(255, 0, 0);
                        lviVehicle.ForeColor = Color.White;
                    }
    
                    lvwVehicles.Items.Add(lviVehicle);
                }
            }
    
            private void Vehicles_Load(object sender, EventArgs e)
            {
                ShowVehicles();
            }
        }
    }
  36. Return to the Vehicles form and click the list view
  37. On the Properties window, click the Events button Events
  38. In the Events section, double-click ItemSelectionChanged
  39. Implement the event as follows:
    private void lvwVehicles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        string strFileName = @"C:\Microsoft Visual C# Application Design\" + e.Item.Text + ".jpg";
    
        if (File.Exists(strFileName))
            pbxVehicle.Image = Image.FromFile(strFileName);
        else
            pbxVehicle.Image = Image.FromFile(@"C:\Microsoft Visual C# Application Design\Vehicle1.jpg");
    }
  40. Return to the Vehicles form and double-click the New Vehicle button
  41. Implement the event as follows:
    private void btnNewVehicle_Click(object sender, EventArgs e)
    {
        VehicleEditor editor = new VehicleEditor();
        editor.ShowDialog();
        ShowVehicles();
    }
  42. Return to the Vehicles form and
  43. Double-click the Close button and implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  44. In the Solution Explorer, right-click Form1.cs and click Rename
  45. Type BethesdaCarRental.cs and press Enter twice
  46. Design the form as follows:
     
    Bethesda Car Rental
    Control Text Name
    Button Button Customers &Rental Orders btnRentalOrders
    Button Button C&ars btnCars
    Button Button &Employees btnEmployees
    Button Button &Customers btnCustomers
    Button Button C&lose btnClose
  47. Double-click an unoccupied area of the form and change the document as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace BethesdaCarRental1
    {
        public partial class BethesdaCarRental : Form
        {
            public BethesdaCarRental()
            {
                InitializeComponent();
            }
    
            private void BethesdaCarRental_Load(object sender, EventArgs e)
            {
    	    // If the directory and the sub-directory don't exist, create them
                Directory.CreateDirectory(@"C:\Microsoft Visual C# Application Design\Bethesda Car Rental");
            }
        }
    }
  48. Double-click the Rental Orders button and implement its event as follows:
    private void btnRentalOrders_Click(object sender, EventArgs e)
    {
        RentalOrders ros = new RentalOrders();
        ros.Show();
    }
  49. Return to the Bethesda Car Rental form and double-click the Vehicles button
  50. Implement the event as follows:
    private void btnVehicles_Click(object sender, EventArgs e)
    {
        Vehicles cars = new Vehicles();
        cars.Show();
    }
  51. Return to the Bethesda Car Rental form and double-click the Employees button
  52. Implement the event as follows:
    private void btnEmployees_Click(object sender, EventArgs e)
    {
        Employees staff = new Employees();
        staff.Show();
    }
  53. Return to the Bethesda Car Rental form and double-click the Close button
  54. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
 
 
   
  1. Execute the application
  2. Click the Vehicles button and create a few vehicles records as follows:
     
    Tag # Make Model Doors Passengers Condition Category Availability
    2AM9952 Ford Fiesta SE 4 5 Driveable Economy Available
    6AD8274 Mazda CX-9 4 5 Excellent Mini Van Available
    8AG3584 Toyota Sienna LE FWD 4 8 Excellent Passenger Van Available
    KER204 Ford Focus SF 4 5 Excellent Compact Being Serviced
    3AD9283 Kia Rio EX 4 5 Excellent Economy Rented
    8AE9294 Lincoln MKT 3.5L 4 5 Excellent Full Size Available
    KLT840 Ford E-350 XL 3 15 Driveable Passenger Van Available
    8AL8033 Toyota Corolla LE 4 5 Excellent Compact Available
    4AF9284 Toyota Tacoma 2 2 Needs Repair Pickup Truck Available
    ADG279 GMC Acadia SLE 4 5 Excellent SUV Rented
    1AD8049 Dodge Charger SXT 4 5 Excellent Standard Being Serviced
    9MD3974 Toyota Sienna LE FWD 4 8 Driveable Passenger Van Rented
    5AJ9274 BMW 528i 4 5 Excellent Full Size Available
    GTH295 Kia Rio5 4 5 Excellent Economy Available
    8AT2408 Mazda Miata MX-5 2 2 Excellent Compact Available
    6AP2486 Fiat 500 2 4 Excellent Economy Available
    2AL9485 Chrysler 200 2 2 Excellent Compact Available
    DFP924 Toyota Sienna LE FWD 4 8 Driveable Passenger Van Available
    2MD8382 Toyota RAV4 I4 4X4 4 5 Excellent SUV Available
    8AR9374 Honda Accord LX 4 5 Excellent Standard Rented
    5MD2084 Chevrolet Equinox LS 4 5 Driveable Mini Van Available
    BND927 Ford Fiesta SE 4 5 Driveable Economy Available
    6AP2749 Toyota Corolla LE 4 5 Excellent Compact Rented
    8AL7394 Ford F-250 SD Reg Cab 4X4 2 2 Excellent Pickup Truck Available
    4MD2840 Chevrolet 2500 LS 3 15 Excellent Passenger Van Rented
    G249580 Nissan Sentra SR 4 5 Excellent Compact Available
    3AK7397 Chrysler 200 2 2 Excellent Compact Available
    VGT927 Toyota Tundra Dbl Cab 4X4 2 5 Excellent Pickup Truck Available
    2AT9274 Ford Focus SF 4 5 Excellent Compact Available
    6AH8429 Lincoln MKT 3.5L 4 5 Needs Repair Full Size Available
    8MD9284 Ford Escape SE I4 4 5 Excellent Mini Van Available
    PLD937 Chevrolet Imapala LT 4 5 Excellent Compact Being Serviced
    5AK2974 Fiat 500 2 4 Excellent Economy Available
    1MD9284 Ford Escape SE I4 4 5 Excellent Mini Van Being Serviced
    SDG624 Chevrolet Volt 4 5 Excellent Standard Available
    2AR9274 Kia Rio SX 4 5 Excellent Economy Available
    JWJ814 Cadillac CTS-V 4 5 Excellent Full Size Available
    7MD9794 Ford Focus SF 4 5 Excellent Compact Rented
    UQW118 Chevrolet 2500 LS 3 15 Needs Repair Passenger Van Available
    2MD9247 Toyota RAV4 I4 4X4 4 5 Excellent SUV Available

    Bethesda Car Rental - Vehicles

    Bethesda Car Rental - Vehicles

  3. Click the Employees button
  4. Create a few employees as follows:
     
    Employee # First Name Last Name Title
    92735 Jeffrey Leucart General Manager
    29268 Catherine Rawley Administrative Assistant
    73948 Allison Garlow Rental Associate
    40508 David Stillson Technician
    24793 Michelle Taylor Accounts Manager
    20480 Peter Futterman Rental Associate
    72084 Georgia Rosen Customer Service Representative
    38240 Karen Blackney Rental Associate
  5. Close the Employees form
  6. Create new rental orders as follows:
     
    Receipt # RPPB First Name Last Name Address City State ZIP Code Tag # Car Condition Tank Level Mileage Start Start Date Rate Applied Order Status
    100001 20480 Marcel Buhler 6800 Haxell Crt Alexandria VA 22314 8AG3584 Excellent Empty 12728 7/14/2014 69.95 Vehicle With Customer
    100002 24793 Joan Altman 3725 South Dakota Ave NW Washington DC 20012 KER204 Good 3/4 Full 24715 7/18/2014 62.95 Vehicle With Customer
    100003 38240 Thomas Filder 4905 Herrenden St Arlington VA 22204 8AL8033 Excellent Full 6064 7/18/2014 34.95 Vehicle With Customer

    Bethesda Car Rental: Rental Order

  7. Update the following rental orders (make sure you click Calculate:
     
    Receipt # to Open Tank Level Mileage End End Date Total Days Rate Applied Sub-Total Tax Rate Tax Amount Order Total Order Status
    100001 Half Tank 13022 7/19/2014 5 69.95 349.75 7.75% 27.11 376.86 Rental Order Complete
    100003 Full 6229 7/21/2014 3 34.95 104.85 7.75% 8.13 112.98 Rental Order Complete

    Bethesda Car Rental: Rental Order

  8. Create a new rental order as follows:
     
    Receipt # RPPB First Name Last Name Address City State ZIP Code Tag # Car Condition Tank Level Mileage Start Start Date Rate Applied Order Status
    100004 73948 Gregory Strangeman 5530 Irving St College Park MD 20740 2AT9274 Excellent 1/2 Tank 8206 7/21/2014 28.95 Vehicle With Customer
  9. Update the following rental order:
     
    Receipt # to Open Tank Level Mileage End End Date Total Days Rate Applied Sub-Total Tax Rate Tax Amount Order Total Order Status
    100002 Full 25694 7/22/2014 4 62.95 251.8 7.75% 19.39 271.19 Rental Order Complete
  10. Create a new rental order as follows:
     
    Receipt # RPPB First Name Last Name Address City State ZIP Code Tag # Car Condition Tank Level Mileage Start Start Date Rate Applied Order Status
    100005 38240 Michelle Russell 10070 Weatherwood Drv Rockville MD 20853 8AE9294 Excellent Full 3659 7/22/2014 38.95 Vehicle With Customer
  11. Update the following rental orders:
     
    Receipt # to Open Tank Level Mileage End End Date Total Days Rate Applied Sub-Total Tax Rate Tax Amount Order Total Order Status
    100005 Full 3806 7/23/2014 1 38.95 38.95 7.75% 3.00 41.95 Rental Order Complete
    100004 3/4 Full 8412 7/25/2014 2 28.95 57.9 7.75% 4.46 62.36 Rental Order Complete
  12. Close the forms and return to your programming environment

Application

 
 

Home Copyright © 2014-2016, FunctionX