コード例 #1
0
 // calculate customer charge
 public virtual decimal CalculateCharge()
 {
     // customer charge passed to customer type objects
     if (this.customerType == 'R')
     {
         Customer c = new ResidentialCustomer(accountNo, customerName, customerType, kwh1, kwh2);
         return(c.CalculateCharge());
     }
     else if (this.customerType == 'C')
     {
         Customer c = new CommercialCustomer(accountNo, customerName, customerType, kwh1, kwh2);
         return(c.CalculateCharge());
     }
     else if (this.customerType == 'I')
     {
         Customer c = new IndustrialCustomer(accountNo, customerName, customerType, kwh1, kwh2);
         return(c.CalculateCharge());
     }
     return(0);
 }
コード例 #2
0
        private void CalculateBillbutton_Click(object sender, EventArgs e)
        {
            //this event perform all bill calculation using seperate methods for different customer type
            //string customerInput = (CustomerTypecomboBox1.Text);
            decimal kwhUsedPerCustomer = decimal.Parse(KwhTextBox1.Text.Trim()); //converting customer input text to double to perform calculations
            string  customerName       = customerNameTextBox1.Text.Trim();
            int     customerAccountNum;

            //this try catch block handles the exception created
            //if the user tries to calculate the bill without inserting valid account number
            try
            {
                customerAccountNum = int.Parse(accountNumberTextBox.Text.Trim());
            }
            catch (FormatException)
            {
                MessageBox.Show($"Invalid Account number, please insert your account number.");
                return;
            }
            //in this try catch block the calculate bill is performed
            //based on the selected item/index by the user
            //objects are created as instances of sub classes depending on the selection by the customer
            try
            {
                switch (CustomerTypecomboBox1.SelectedItem)
                {
                case "Residential":
                    ResidentialCustomer rcust = new ResidentialCustomer(customerAccountNum, customerName, "R");
                    rcust.ChargeAmount      = rcust.CalculateCharge(kwhUsedPerCustomer);
                    EstimatedBillLabel.Text = "Estimated Bill: " + rcust.ChargeAmount.ToString("C");
                    break;

                case "Commercial":
                    CommercialCustomer cCustomer = new CommercialCustomer(customerAccountNum, customerName, "C");
                    cCustomer.ChargeAmount  = cCustomer.CalculateCharge(kwhUsedPerCustomer);
                    EstimatedBillLabel.Text = "Estimated Bill: " + cCustomer.ChargeAmount.ToString("C");
                    break;

                case "Industrial":
                    decimal            offpeakKwhUsedPerCustomer = decimal.Parse(OffpeakKwhTextBox1.Text.Trim());
                    IndustrialCustomer indCustomer = new IndustrialCustomer(customerAccountNum, customerName, "I");
                    indCustomer.ChargeAmount = indCustomer.CalculateCharge(kwhUsedPerCustomer, offpeakKwhUsedPerCustomer);
                    EstimatedBillLabel.Text  = "Estimated Bill: " + indCustomer.ChargeAmount.ToString("C");
                    break;

                default:
                    MessageBox.Show("Something went wrong bill could not be estimated");
                    break;
                }
                //eraseTextField();
            }
            catch (ArgumentException excp)
            {
                MessageBox.Show($"Unable to create new customer invalid information. {excp.Message}");
                return;
            }
            catch (FormatException excp)
            {
                MessageBox.Show($"Non numeric data in a numeric field. {excp.Message}");
                return;
            }
        }
コード例 #3
0
        //this event adds the customer details into the list box
        private void AddCustomerButton_Click(object sender, EventArgs e)
        {
            decimal kwhUsedPerCustomer = decimal.Parse(KwhTextBox1.Text.Trim()); //converting customer input text to double to perform calculations
            string  customerName       = customerNameTextBox1.Text.Trim();
            int     customerAccountNum;

            //this try catch block handles the exception created
            //if the user tries to calculate the bill without inserting valid account number
            try
            {
                customerAccountNum = int.Parse(accountNumberTextBox.Text.Trim());
            }
            catch (FormatException)
            {
                MessageBox.Show($"Invalid Account number, please insert your account number.");
                return;
            }


            Customer c = null;

            //Based the on user's selection in the combo box
            // a new customer object is created
            try
            {
                if (CustomerTypecomboBox1.SelectedIndex == 0)
                {
                    c = new ResidentialCustomer(customerAccountNum, customerName, "R");
                    c.ChargeAmount = c.CalculateCharge(kwhUsedPerCustomer);
                }
                else if (CustomerTypecomboBox1.SelectedIndex == 1)
                {
                    c = new CommercialCustomer(customerAccountNum, customerName, "C");
                    c.ChargeAmount = c.CalculateCharge(kwhUsedPerCustomer);
                }
                else
                {
                    decimal offpeakKwhUsedPerCustomer = decimal.Parse(OffpeakKwhTextBox1.Text.Trim());
                    c = new IndustrialCustomer(customerAccountNum, customerName, "I");
                    c.ChargeAmount = c.CalculateCharge(kwhUsedPerCustomer, offpeakKwhUsedPerCustomer);
                }
            }
            catch (ArgumentException excp)
            {
                MessageBox.Show($"Unable to create new customer, invalid information. {excp.Message}");
                return;
            }
            catch (FormatException excp)
            {
                MessageBox.Show($"Non numeric data in a numeric field. {excp.Message}");
                return;
            }
            //if a customer object is created successfully
            // add the object to a customer list above and display in the list box

            if (c != null)
            {
                CustomerList.Add(c);
                customerListListBox.Items.Add(c.ToFormattedString());
            }

            //writing the customer data unto customer.txt!
            try
            {
                string pathLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string fileLocation = Path.Combine(pathLocation, "Customer.txt");

                StreamWriter output = new StreamWriter(fileLocation, true);
                foreach (Customer customer in CustomerList)
                {
                    output.WriteLine(customer.ToFileString());
                }
                output.Close();
            }
            catch (Exception excp)
            {
                MessageBox.Show($"File did not write. {excp.Message}");
                return;
            }
            MessageBox.Show("Customer data has been save succesfully in customer.txt");
        }