示例#1
0
        /**************************************************
        *  create a new customer in customer list
        **************************************************/
        private void addCustomerToList(int id, string name, int kwh)
        {
            // calculate cost based on radio box
            if (radResidential.Checked == true)   // residential customer
            {
                ResidentialCustomer cust = new ResidentialCustomer(id, txtCustomer.Text, 'R', kwh);
                txtCost.Text = '$' + cust.Bill.ToString("f2"); // output to text box
                custList.Add(cust);
            }
            else if (radCommercial.Checked == true)     // commercial customer
            {
                CommercialCustomer cust = new CommercialCustomer(id, txtCustomer.Text, 'C', kwh);
                txtCost.Text = '$' + cust.Bill.ToString("f2"); // output to text box
                custList.Add(cust);
            }
            else if (radIndustrial.Checked == true)     // industrial customer ... include off-peak hours
            {
                int offPeakKwh = 0;

                // validate industrial off peak kWh
                if (IsValidIndustrial())
                {
                    offPeakKwh = Convert.ToInt32(txtOffPeak.Text); // get industrial off-peak hours from user
                }
                IndustrialCustomer cust = new IndustrialCustomer(id, txtCustomer.Text, 'I', kwh, offPeakKwh);
                txtCost.Text = '$' + cust.Bill.ToString("f2"); // output to text box
                custList.Add(cust);
            }
            // update listview
            DisplayList(custList);
        }
示例#2
0
        static string fpath = "customers.txt"; // the path to the file's location

        /**************************************************
         *  reads from file to creates CustomerList
         *************************************************/
        public static List <Customer> LoadListFromFile()
        {
            FileStream   fs;
            StreamReader sr = null;
            string       line;

            string[]        fields;
            int             id;
            string          name;
            char            category;
            decimal         bill;
            List <Customer> cl = new List <Customer>();

            // try to read from file
            try {
                fs = new FileStream(fpath, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs);
                while (!sr.EndOfStream)   // while more customers
                {
                    line     = sr.ReadLine();
                    fields   = line.Split(',');
                    id       = Convert.ToInt32(fields[0]);
                    name     = fields[1];
                    category = Convert.ToChar(fields[2]);
                    bill     = Convert.ToDecimal(fields[3]);

                    // create customer type based on category
                    if (category == 'R')
                    {
                        ResidentialCustomer c = new ResidentialCustomer(id, name, category, bill);
                        cl.Add(c); // add to list
                    }
                    else if (category == 'C')
                    {
                        CommercialCustomer c = new CommercialCustomer(id, name, category, bill);
                        cl.Add(c);
                    }
                    else if (category == 'I')
                    {
                        IndustrialCustomer c = new IndustrialCustomer(id, name, category, bill);
                        cl.Add(c);
                    }
                }
                // handle exceptions
            } catch (Exception ex) {
                MessageBox.Show("Error while reading customer data \n" + ex.GetType().ToString() + ": " + ex.Message);
            } finally {
                if (sr != null)
                {
                    sr.Close();
                }
            }
            return(cl);
        }