예제 #1
0
        const string path = "clients.txt"; // located in bin/Debug folder

        public static List <Client> ReadClients()
        {
            List <Client> clientList = new List <Client>(); //create an empty list
            Client        c;                                // for reading clients
            string        line;                             // next line from the file

            string[] fields;                                // line broken into fields
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)// while there is still unread data
                    {
                        line   = sr.ReadLine();
                        fields = line.Split(',');//split where the comma's are
                        string  Name          = fields[0];
                        int     AccountNumber = Convert.ToInt32(fields[1]);
                        string  ClientType    = fields[2];
                        decimal ChargeAmount  = Convert.ToDecimal(fields[3]);
                        //create class based on client type
                        if (fields[2] == "r")
                        {
                            c = new Residential(Name, AccountNumber, ClientType, ChargeAmount);
                        }
                        else if (fields[2] == "c")
                        {
                            c = new Commercial(Name, AccountNumber, ClientType, ChargeAmount);
                        }
                        else
                        {
                            c = new Industrial(Name, AccountNumber, ClientType, ChargeAmount);
                        }
                        clientList.Add(c);
                    }
                } // closes stream reader and recycles
            }     // closes file stream and recycles
            return(clientList);
        }
예제 #2
0
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            //variables
            decimal kwh;           //user input for kwh
            decimal offkwh;        //user input for off hours
            string  name;          //class constructor value for name
            int     accountNumber; //class constructor value for account number
            string  clientType;    //class constructor value for client type (residential exc)

            //validate input Dec 11th
            if (Validator.IsNonNegativeInt32(txtUseage, "KWH") &&
                Validator.IsPresent(txtUseage, "KWH") &&
                Validator.IsNonNegativeInt32(txtAccountNo, "Account Number") &&
                Validator.IsPresent(txtAccountNo, "Account Number") &&
                Validator.IsPresent(txtClientName, "Client Name")
                )
            {
                //allocate values from text boxes
                kwh           = Convert.ToDecimal(txtUseage.Text); //taking user input for kwh
                accountNumber = Convert.ToInt32(txtAccountNo.Text);
                name          = txtClientName.Text;

                //calculate residential rate
                if (btnResidential.Checked == true)
                {
                    clientType = "r";
                    Residential r = new Residential(name, accountNumber, clientType);
                    lblBillTotal.Text = (r.CalculateBill(kwh)).ToString("c");

                    //add it to the list
                    r.ChargeAmount = (r.CalculateBill(kwh));
                    clients.Add(r);
                    DisplayClients();
                }
                //calculate commercial rate
                else if (btnCommercial.Checked == true)
                {
                    clientType = "c";
                    Commercial c = new Commercial(name, accountNumber, clientType);
                    lblBillTotal.Text = (c.CalculateBill(kwh)).ToString("c");

                    //add it to the list
                    c.ChargeAmount = (c.CalculateBill(kwh));
                    clients.Add(c);
                    DisplayClients();
                }
                else
                {
                    //calculate industrial peak rate
                    //validate off hours Dec 11th
                    if (Validator.IsNonNegativeInt32(txtOffHours, "Off hours") &&
                        Validator.IsPresent(txtOffHours, "Off hours"))
                    {
                        //calculate off hours, add peak hours and add client to list
                        clientType = "i";
                        offkwh     = Convert.ToDecimal(txtOffHours.Text); //taking user input for off hours
                        Industrial i = new Industrial(name, accountNumber, clientType);
                        lblBillTotal.Text = (i.CalculateBill(kwh, offkwh)).ToString("c");

                        //add it to the list
                        i.ChargeAmount = (i.CalculateBill(kwh, offkwh));
                        clients.Add(i);
                        DisplayClients();
                    }
                }
            }
        }