// When the form loads, initialize the fields on the the form and display the saved customer data. private void frmElectricityCharges_Load(object sender, EventArgs e) { // Set the customer type to residential and put the focus on the usage input text box. The text // fields will be null when the form first loads so this does not need to be done manually. cboType.SelectedItem = "Residential"; lblUsage.Text = "Electricity Usage in kWh:"; txtUsage.Tag = "electricity usage"; lblUsagePeak.Visible = false; txtUsagePeak.Visible = false; txtUsage.Focus(); // Also read and display the data from the Customers.txt file, and display // the associated calculations custList = CustomerFile.ReadCustomers(); DisplayCustomerInfo(); }
// When the "Calculate and Add" button is clicked, obtain the user input (with validation) and // execute the calculations to determine the customers's total charges. The new data is also // added to the Customers.txt file, and displayed in the list box. private void btnCalculate_Click(object sender, EventArgs e) { // Declare variables bool success = false; // Indicates whether the calculation was successful // Obtain the customer type from the combo box. cboType.DropDownStyle has been // set to DropDownList, so the user cannot type in anything, and the selection will // always be either residential, commercial or industrial, and there is no need for // a default case. // In all cases, verify that input has been provided for all fields (customer name, account, // and usage(s)). Verify that the account number is a positive integer, and that the usage // field(s) are non-negative integers. switch (cboType.SelectedItem.ToString()) { case "Residential": // Validate and obtain the user input if (ValidateInput.IsProvided(txtName) && ValidateInput.IsProvided(txtAcctNum) && ValidateInput.IsPositiveInt(txtAcctNum) && ValidateInput.IsProvided(txtUsage) && ValidateInput.IsNonNegativeInt(txtUsage)) { ResidentialCustomer rc = new ResidentialCustomer(Convert.ToInt32(txtAcctNum.Text), txtName.Text); rc.UsageKWH = Convert.ToInt32(txtUsage.Text); rc.CalculateCharge(); lblCharges.Text = rc.ChargeAmount.ToString("c"); custList.Add(rc); success = true; } break; case "Commercial": // Validate and obtain the user input if (ValidateInput.IsProvided(txtName) && ValidateInput.IsProvided(txtAcctNum) && ValidateInput.IsPositiveInt(txtAcctNum) && ValidateInput.IsProvided(txtUsage) && ValidateInput.IsNonNegativeInt(txtUsage)) { CommercialCustomer cc = new CommercialCustomer(Convert.ToInt32(txtAcctNum.Text), txtName.Text); cc.UsageKWH = Convert.ToInt32(txtUsage.Text); cc.CalculateCharge(); lblCharges.Text = cc.ChargeAmount.ToString("c"); custList.Add(cc); success = true; } break; case "Industrial": // Validate and obtain the user inputs (both off-peak and peak usages) if (ValidateInput.IsProvided(txtName) && ValidateInput.IsProvided(txtAcctNum) && ValidateInput.IsPositiveInt(txtAcctNum) && ValidateInput.IsProvided(txtUsage) && ValidateInput.IsNonNegativeInt(txtUsage) && ValidateInput.IsProvided(txtUsagePeak) && ValidateInput.IsNonNegativeInt(txtUsagePeak)) { IndustrialCustomer ic = new IndustrialCustomer(Convert.ToInt32(txtAcctNum.Text), txtName.Text); ic.UsageKWH = Convert.ToInt32(txtUsage.Text); ic.UsagePeakKWH = Convert.ToInt32(txtUsagePeak.Text); ic.CalculateCharge(); lblCharges.Text = ic.ChargeAmount.ToString("c"); custList.Add(ic); success = true; } break; } // Re-display all of the customer data in the list box (the newly added customer // information will now appear). // No checking is done to determine whether the customer and/or account number already // exist in the file; assume that multiple charges can appear in the file for the // same customer/account. if (success) { CustomerFile.WriteCustomers(custList); DisplayCustomerInfo(); } }