/// <summary> /// Method to add or insert customers to the list. /// </summary> /// <param name="pos">Index position for Customer List</param> private void InsertCustomer(int pos) { int k; // Basic KWH used int o; // Offpeak KWH used (in case of Industrial customer) string n; // To hold customer name; Customer c; // For new customer to be stored into // First, let us validate the data that is supposed to be entered if ((Validator.IsPresent(txtName) && Validator.WithinLength(txtName, 20) && Validator.IsNumberWithinRange(txtKWH1, 0, 999999)) == false) { // if the data entered cannot be validated, then exit here. return; } // Grab the Customer name and KWH from the form. n = txtName.Text; k = Int32.Parse(txtKWH1.Text); // Check which radio button is selected and create new Customer object accordingly if (rbResidential.Checked == true) { c = new Residential(n, k); } else if (rbIndustrial.Checked == true) { // Let's validate the second KWH text box here because we need to grab the second KWH value. if (Validator.IsNumberWithinRange(txtKWH2, 0, 999999) == false) { // if the data entered cannot be validated, then exit here. return; } o = Int32.Parse(txtKWH2.Text); c = new Industrial(n, k, o); } else // Obviously, this is a Commercial customer if neither of the first two radio buttons are clicked { c = new Commercial(n, k); } // Calculate the bill for the new customer, then insert customer to List c.CalculateBill(); cust.Insert(pos, c); // Display all of the customers into the listbox lstCustomers.Items.Clear(); foreach (Customer d in cust) { lstCustomers.Items.Add(d.ToString()); } // Clear the Name and the KWH entry boxes and select the Name text box (for convenience) txtName.Text = ""; txtKWH1.Text = ""; txtKWH2.Text = ""; txtName.Select(); }
private void rbIndustrial_CheckedChanged(object sender, EventArgs e) { // When Industrial radio button is checked // We ask that the user enter peak and off-peak hours // Change the labels lblKWH1.Text = "Peak kWh Used:"; lblKWH2.Text = "Off-Peak kWh Used:"; // Clear the two KWH entry boxes txtKWH1.Text = ""; txtKWH2.Text = ""; // Set the tags for the KWH entry boxes txtKWH1.Tag = "Peak kWh Used"; txtKWH2.Tag = "Off-Peak kWh Used"; // Enable _BOTH_ entry boxes here txtKWH1.ReadOnly = false; txtKWH2.ReadOnly = false; // Provide the Industrial description in the rich text box rtbCustDesc.Text = Industrial.CustomerDescription(); }
/// <summary> /// Load data from the file to the arrays; returns boolean flag that indicates success /// </summary> /// <param name="cust">The customer list is passed.</param> /// <returns>Whether the file was read successfully or not.</returns> public static bool LoadData(List <Customer> cust) { // The following code was borrowed from an example that was demonstrated in Lecture #1 of ICT-711 (with some modifications) FileStream fs; StreamReader sr; // for file reading string line; // one line from the file string[] parts; // line will be split into multiple parts Customer c; // Customer object try { // open the file fs = new FileStream(savePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs); } catch (FileNotFoundException ex) { MessageBox.Show(ex.Message); return(false); } // file opened successfully // read all lines, storing data in the cust List try { while (!sr.EndOfStream) { line = sr.ReadLine(); // read next line parts = line.Split(','); // split the line into parts using comma as delimiter switch (parts[0]) // Verify the first item in the line, which determines the type of customer { // This is a Residential customer, and we read the next 2 items in the line to // create the object, calculate it's bill and add it to the Customer List. case "R": c = new Residential(parts[1], Int32.Parse(parts[2])); c.CalculateBill(); cust.Add(c); break; // This is an Industrial customer, and we read the next 3 items in the line to // create the object, calculate it's bill and add it to the Customer List. case "I": c = new Industrial(parts[1], Int32.Parse(parts[2]), Int32.Parse(parts[3])); c.CalculateBill(); cust.Add(c); break; // This is a Commercial customer, and we read the next 2 items in the line to // create the object, calculate it's bill and add it to the Customer List. case "C": c = new Commercial(parts[1], Int32.Parse(parts[2])); c.CalculateBill(); cust.Add(c); break; // If we get here, then obviously the line that was read is invalid, so we do nothing. default: break; } } // If we get here, then the file was read successfully. sr.Close(); return(true); } // Catch any exceptions to the attempts at file reading above. catch (IOException ex) { MessageBox.Show("I/O error occurred while reading from the file: " + ex.Message); return(false); } catch (Exception ex) { MessageBox.Show("Unanticipated error occurred while reading from the file: " + ex.Message); return(false); } finally { sr.Close(); } }