Beispiel #1
0
        private void CalculateButton_Click(object sender, EventArgs e)
        {
            //Declaring local variables.
            int     NoOfFullFareRider = 0, NoOfStudentRider = 0, NoOfDiscountRider = 0, NoOfTotalPassenger = 0;
            decimal Receipt = 0m;

            //Checks if Driver name is given.
            if (DriverNameTextBox.Text != "")
            {
                //Parsing is done in all try blocks.
                try
                {
                    NoOfFullFareRider = int.Parse(FullFareRiderTextBox.Text);

                    try
                    {
                        NoOfStudentRider = int.Parse(StudentRiderTextBox.Text);

                        try
                        {
                            NoOfDiscountRider = int.Parse(DiscountRiderTextBox.Text);

                            //Passengers are calculated for the particular driver.
                            NoOfTotalPassenger = NoOfFullFareRider + NoOfStudentRider + NoOfDiscountRider;

                            //Global variables for count is incremented.
                            TotalDrivers   += 1;
                            TotalPassengers = TotalPassengers + NoOfTotalPassenger;

                            //Calculation and visibile change are done.
                            Receipt                   = (NoOfFullFareRider * FullFare) + (NoOfStudentRider * StudentFare) + (NoOfDiscountRider * DiscountFare);
                            TotalReceipts             = TotalReceipts + Receipt;
                            CalculateButton.Enabled   = false;
                            SummaryButton.Enabled     = true;
                            DetailsGroupBox.Enabled   = false;
                            CalculateGroupBox.Visible = true;

                            //Calculated summary for particular driver is displayed.
                            CalculateDriverNameDisplayLabel.Text     = DriverNameTextBox.Text;
                            CalculateTotalPassengerDisplayLabel.Text = NoOfTotalPassenger.ToString();
                            CalculateTotalReceiptsDisplayLabel.Text  = Receipt.ToString("c", new CultureInfo("en-FR"));
                            CalculateAvgReceiptsDisplayLabel.Text    = (Receipt / NoOfTotalPassenger).ToString("c", new CultureInfo("en-FR"));
                        }
                        //Try exceptions thrown are handled by catch blocks.
                        catch
                        {
                            MessageBox.Show("Please enter numerical discount rider", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            DiscountRiderTextBox.Focus();
                            DiscountRiderTextBox.SelectAll();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Please enter numerical Student rider", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        StudentRiderTextBox.Focus();
                        StudentRiderTextBox.SelectAll();
                    }
                }
                catch
                {
                    MessageBox.Show("Please enter numerical Full Fare rider", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    FullFareRiderTextBox.Focus();
                    FullFareRiderTextBox.SelectAll();
                }
            }
            else
            {
                //Exception if driver name text box is left empty.
                MessageBox.Show("Please enter driver name", "Enter data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DriverNameTextBox.Focus();
            }
        }
        private void CalculateButton_Click(object sender, EventArgs e)
        {
            int NoOfFullFareRider = 0, NoOfStudentRider = 0, NoOfDiscountRider = 0, NoOfTotalPassenger = 0;

            //This block is executed until an exception is thrown.
            try
            {
                if (DriversNameTextBox.Text != "")
                {
                    //Counts the total drivers in the company.
                    CalculateNameDisplay.Text = DriversNameTextBox.Text;
                    if (CalculateNameDisplay.Text != "")
                    {
                        TotalDrivers = TotalDrivers + 1;
                    }

                    /*
                     * Parsing the input values into an integer and displaying the total passengers.
                     * Parsed values are stored inside the assigned int var.
                     */
                    TotalPassengerDisplayLabel.Text = ((NoOfFullFareRider = int.Parse(FullFareTextBox.Text)) + (NoOfStudentRider = int.Parse(StudentRiderTextBox.Text)) + (NoOfDiscountRider = int.Parse(DiscountRiderTextBox.Text))).ToString();
                    NoOfTotalPassenger = NoOfFullFareRider + NoOfStudentRider + NoOfDiscountRider;
                    TotalFare          = NoOfFullFareRider * FullFare + NoOfStudentRider * StudentFare + NoOfDiscountRider * DiscountFare;

                    //Currency Formatting is done do display amount in euros.
                    TotalReceiptsDisplay.Text = (TotalFare).ToString("C", new CultureInfo("en-FR"));
                    CalculateButton.Enabled   = false;
                    SummaryButton.Enabled     = true;
                    DetailsGroupBox.Enabled   = false;
                    CalculateGroupBox.Visible = true;
                    AvgReceiptsDisplay.Text   = (TotalFare / NoOfTotalPassenger).ToString("c", new CultureInfo("en-FR"));

                    //Counts the total passengers & receipts if condition satisfies.
                    if (NoOfFullFareRider != 0 && NoOfStudentRider != 0 && NoOfDiscountRider != 0)
                    {
                        TotalPassengers = TotalPassengers + NoOfTotalPassenger;
                        TotalReceipts   = TotalReceipts + TotalFare;
                    }
                }
                else
                {
                    MessageBox.Show("Please enter driver name", "No Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DriversNameTextBox.Focus();
                }
            }
            //This block handles an exception if found.
            catch
            {
                if (NoOfFullFareRider != 0)
                {
                    if (NoOfStudentRider != 0)
                    {
                        if (NoOfDiscountRider != 0)
                        {
                        }

                        /*
                         * Every else block displays an error message respective to its if condition,
                         * Empties and focusses on the box of error.
                         * Decrements the default increment of total drivers.
                         */
                        else
                        {
                            MessageBox.Show("Please enter numerical data for discount rider", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            DiscountRiderTextBox.Text = string.Empty;
                            DiscountRiderTextBox.Focus();
                            TotalDrivers -= 1;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please enter numerical data for student rider", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        StudentRiderTextBox.Text = string.Empty;
                        StudentRiderTextBox.Focus();
                        TotalDrivers -= 1;
                    }
                }

                else
                {
                    MessageBox.Show("Please enter numerical data for full fare rider", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    FullFareTextBox.Text = string.Empty;
                    FullFareTextBox.Focus();
                    TotalDrivers -= 1;
                }
            }
        }