//method to read the list of customer data from a file to the listbox
        public static List <Customer> ReadCustomers()
        {
            List <Customer> customers = new List <Customer>(); //create empty list

            Customer customer;                                 // reading customers
            string   line;                                     // next line from the file

            string[] fields;                                   // line broken into fields

            int     accountNo    = 0;
            string  customerName = "";
            string  customerType = "";
            decimal chargeAmount;

            //uses filestream to read the file
            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (StreamReader streamReader = new StreamReader(fileStream))
                {
                    //continues reading until the end of file is reached.
                    while (!streamReader.EndOfStream)
                    {
                        line   = streamReader.ReadLine();
                        fields = line.Split(','); // split on commas

                        //asign the data to variables
                        accountNo    = Convert.ToInt32(fields[0]);
                        customerName = fields[1];
                        customerType = fields[2];
                        chargeAmount = Convert.ToDecimal(fields[3]);

                        //checking which customer is in the read line.
                        if (fields[2] == "R")
                        {
                            customer = new ResidentialCustomer(accountNo, customerName, customerType, chargeAmount);
                        }
                        else if (fields[2] == "C")
                        {
                            customer = new CommercialCustomer(accountNo, customerName, customerType, chargeAmount);
                        }
                        else
                        {
                            customer = new IndustrialCustomer(accountNo, customerName, customerType, chargeAmount);
                        }
                        customers.Add(customer);
                    }
                }
            }

            return(customers);
        }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
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;
            }
        }
Ejemplo n.º 4
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");
        }