const string path = "customers.txt"; // located in bin/Debug folder /// <summary> /// ReadCustomers is called to read information stored in a file /// </summary> /// <returns> a list of customer entities</returns> public static List <Customer> ReadCustomers() { //List<Customer> custList = new Customer(); List <Customer> custList = new List <Customer>(); 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 commas are //Create corresponding Customer entity based on the 'Type' if (Convert.ToChar(fields[2]) == 'R') { Residential c = new Residential(); c.Name = fields[0]; c.AccountType = Convert.ToChar(fields[2]); c.ChargeAmount = Convert.ToDecimal(fields[3]); c.AccountNumber = Convert.ToInt32(fields[1]); custList.Add(c); // add it to the list } else if (Convert.ToChar(fields[2]) == 'C') { Commercial c = new Commercial(); c.Name = fields[0]; c.AccountNumber = Convert.ToInt32(fields[1]); c.AccountType = Convert.ToChar(fields[2]); c.ChargeAmount = Convert.ToDecimal(fields[3]); custList.Add(c); // add it to the list } else if (Convert.ToChar(fields[2]) == 'I') { Industrial c = new Industrial(); c.Name = fields[0]; c.AccountNumber = Convert.ToInt32(fields[1]); c.AccountType = Convert.ToChar(fields[2]); c.ChargeAmount = Convert.ToDecimal(fields[3]); custList.Add(c); // add it to the list } } } // closes sr and recycles } // closes fs and recycles return(custList); // returns the list of customers read from disk }
// Fires when the Calculate button is clicked, will test for valid data and // set various Customer object (or derivatives) private void btnCalculate_Click(object sender, EventArgs e) { // Create a Base Customer object that will hold the object info // set a few varibles for later use int hours = 0, opHours = 0; string name; // set default customer 'Type' to residential char type = 'R'; // check if we have a client name if (Validator.IsPresent(txtName, "Name")) { name = txtName.Text; // set the name variable // Check txtInput for a valid integer, if false, display error icon // and select data in field if (Int32.TryParse(txtInput.Text, out int val)) { // if the test passes, clear any previous errors and set the hours variable errorProvider1.SetError(txtInput, ""); hours = Convert.ToInt32(txtInput.Text); } else { // if the test fails. flash the error icon, select bad data // and set the focus on text field errorProvider1.SetError(txtInput, "Invalid data"); txtInput.SelectAll(); txtInput.Focus(); } if (radCommercial.Checked) { type = 'C'; // sets the 'Type' as Commercial; // creates a new Commercial customer object Commercial client = new Commercial(); client.Hours = hours; // set the usage hours client.Name = name; // set name attribute client.AccountType = type; // set the 'Type' client.CalculateRate(); // calculate the payout customers.Add(client); // add the customer to the List } else if (radIndustrial.Checked) { // sets the opHours variable to be passed into the method // Check txtOffPeak for a valid integer, if false, // display error icon and select data in field if (Int32.TryParse(txtOffPeak.Text, out val)) { // if the test passes, clear any previous errors and set the opHours variable errorProvider1.SetError(txtOffPeak, ""); opHours = Convert.ToInt32(txtOffPeak.Text); } else { // if the test fails. flash the error icon, select bad data // and set the focus on test field errorProvider1.SetError(txtOffPeak, "Invalid data"); txtOffPeak.SelectAll(); txtOffPeak.Focus(); } type = 'I'; // sets the type as Industrial Industrial client = new Industrial(); // creates a new Industrial customer object client.PeakHours = hours; // sets Peak usage client.OffPeakHours = opHours; // set Off Peak usage client.Name = name; // set name client.AccountType = type; // set 'Type' client.CalculateRate(); // Calculate the payout customers.Add(client); // add the customer to the List } else { // type is (R)esidential by default // create new Residential customer object Residential client = new Residential(); client.Hours = hours; // sets usage hours client.Name = name; // set Name client.AccountType = type; // set 'Type' client.CalculateRate(); // Calculate the Payout customers.Add(client); // add the customer to the List } DisplayCustomers(); // update the ListView } }