private void frmMain_Load(object sender, EventArgs e) // this block helps to load customers.txt when the form loads { FileStream fs; StreamReader sr = null; string line; // for reading string[] fields; // line broken into fields CustomerData cust; string name; //customer name int acctNo; //customer account num string customerType; // type if residential, commercial or industrial decimal charge; if (radResidential.Checked) { customerType = "R"; } else if (radCommercial.Checked) { customerType = "C"; } else { customerType = "I"; } try { fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read); sr = new StreamReader(fs); // read data while (!sr.EndOfStream) // while there are still products in the file { line = sr.ReadLine(); // read next line fields = line.Split(','); // CSV file name = fields[0]; customerType = fields[1]; acctNo = Convert.ToInt32(fields[2]); charge = Convert.ToDecimal(fields[3]); cust = new CustomerData(name, customerType, acctNo, charge); customers.Add(cust); } DisplayCustomers(); } catch (Exception ex) { MessageBox.Show("Error while reading products from file: " + ex.Message, ex.GetType().ToString()); } finally { if (sr != null) { sr.Close(); } } }
// calculate customer charge private void btnCalculate_Click(object sender, EventArgs e) { int kWh = 0, peakKWh = 0, offPeaKWh = 0; decimal chargeAmt = 0; if (radResidential.Checked) { if (Validator.IsPresent(txtkWh1, "kWh") && Validator.IsNonNegativeInteger(txtkWh1, "kWh")) { kWh = Convert.ToInt32(txtkWh1.Text); chargeAmt = CustomerData.ResidentialCharge(kWh); } } else if (radCommercial.Checked) { if (Validator.IsPresent(txtkWh1, "Used kWh") && Validator.IsNonNegativeInteger(txtkWh1, "kWh")) { kWh = Convert.ToInt32(txtkWh1.Text); chargeAmt = CustomerData.CommercialCharge(kWh); } } else // must be industrial { if (Validator.IsPresent(txtkWh1, "Peak kWh") && Validator.IsNonNegativeInteger(txtkWh1, "Peak kWh") && Validator.IsPresent(txtkWh2, "Off-peak kWh") && Validator.IsNonNegativeInteger(txtkWh2, "Off-peak kWh")) { peakKWh = Convert.ToInt32(txtkWh1.Text); offPeaKWh = Convert.ToInt32(txtkWh2.Text); chargeAmt = CustomerData.IndustrialCharge(peakKWh, offPeaKWh); } } // display charge lblCharge.Text = chargeAmt.ToString(); }
// add information to the list and save the list to customers.txt private void btnAdd_Click(object sender, EventArgs e) { string name; int acctNo; string customerType; decimal charge; //validations if (Validator.IsPresent(txtCustName, "Name") && Validator.IsPresent(txtAcctNum, "Accout Number") && Validator.IsNonNegativeInteger(txtAcctNum, "Accout Number") && Validator.IsPresentlbl(lblCharge, "Calculate Charge")) { // assign values to objects name = txtCustName.Text; acctNo = Convert.ToInt32(txtAcctNum.Text); charge = Convert.ToDecimal(lblCharge.Text); if (radResidential.Checked) { customerType = "R"; } else if (radCommercial.Checked) { customerType = "C"; } else { customerType = "I"; } CustomerData newCust = new CustomerData(name, customerType, acctNo, charge); // add new product to the list customers.Add(newCust); // and refresh display DisplayCustomers(); FileStream fs; StreamWriter sw = null; string line; try { // open file for writing fs = new FileStream(path, FileMode.Create, FileAccess.Write); sw = new StreamWriter(fs); // write all products to the file in form like: foreach (CustomerData p in customers) { line = p.Name + "," + p.CustomerType + "," + p.AcctNo + "," + p.Charge; // CSV file sw.WriteLine(line); } } catch (Exception ex) { MessageBox.Show("Error while writing products to file: " + ex.Message, ex.GetType().ToString()); } finally { if (sw != null) { sw.Close(); } } txtCustName.Focus(); } }