private void btnAdd_Click(object sender, EventArgs e) { // various validation // if OK, pass info back to the parent and close // if not ok wait for them to fix the issues if (Validator.IsPresent(txtCustName, "Customer Name") && Validator.CSVFriendly(txtCustName, "Customer Name") && Validator.IsNonNegativeInt32(txtCustAcct, "Account Number") && Validator.IsSelected(cmbCustType, "Customer Type")) { Object newCust; switch (cmbCustType.SelectedIndex) { case 0: newCust = new ResidentialCustomer(txtCustName.Text, Convert.ToInt32(txtCustAcct.Text)); break; case 1: newCust = new CommercialCustomer(txtCustName.Text, Convert.ToInt32(txtCustAcct.Text)); break; case 2: newCust = new IndustrialCustomer(txtCustName.Text, Convert.ToInt32(txtCustAcct.Text)); break; default: newCust = null; break; } this.Tag = newCust; this.Close(); } }
public void CalculateChargeTest2() { // Test of normal input, kWh amount less than the base charges IndustrialCustomer cust = new IndustrialCustomer("Test", 12345); int kWh = 500; int offkWh = 500; decimal expectedCharge = 116m; decimal actualCharge; actualCharge = cust.CalculateCharge(kWh, offkWh); Assert.AreEqual(expectedCharge, actualCharge); }
public void CalculateChargeBothBelowBaseTest() { // Arrange int phUsage = 100, opUsage = 100; double expectedCharge = 116; double actualCharge; // Act actualCharge = IndustrialCustomer.CalculateCharge(phUsage, opUsage); // Assert Assert.AreEqual(expectedCharge, actualCharge); }
// testing that zero value is being added for KWH2 for industrial calculations public void IndustrialKWH2ZeroValue() { int acctnum = 1111; string custName = "John Doe"; char custType = 'R'; decimal expectedkwh2value = 0; decimal kwhvalue = 1500; decimal kwhvalue2 = 0; Customer calc = new IndustrialCustomer(acctnum, custName, custType, kwhvalue, kwhvalue2); Assert.AreEqual(expectedkwh2value, kwhvalue2); }
public void CalculateOffPeakChargeOver1000Amount() { //arrange decimal offPeakBase = 40m; decimal unitRate = 0.028m; decimal energyUsed = 1010; decimal expectedCharge = 40.28m; decimal actualCharge = 0; IndustrialCustomer industrialCustomer = new IndustrialCustomer(1, "john", "I", actualCharge); //act actualCharge = industrialCustomer.CalculateCharge(offPeakBase, unitRate, energyUsed); //assert Assert.AreEqual(expectedCharge, actualCharge); }
public void CalculatePeakZeroAmount() { //arrange decimal peakBase = 76m; decimal unitRate = 0.065m; decimal energyUsed = 0; decimal expectedCharge = 76m; decimal actualCharge = 0; IndustrialCustomer industrialCustomer = new IndustrialCustomer(1, "john", "I", actualCharge); //act actualCharge = industrialCustomer.CalculateCharge(peakBase, unitRate, energyUsed); //assert Assert.AreEqual(expectedCharge, actualCharge); }
public void IndustrialCustomerTestCheckForInvalidArgument() { //Arrange int number = 1234; string name = "Kingsley"; string type = "R"; IndustrialCustomer c = new IndustrialCustomer(number, name, type); decimal peakKhwused = -30M; decimal offPeakKwhused = 40; //act decimal billAmount = c.CalculateCharge(peakKhwused, offPeakKwhused); //Assert Assert.Fail(); }
// Calculate Button Clicked: do validation, do calculation based on user type, show result(s) private void btnCalculate_Click(object sender, EventArgs e) { // if not industrial user (residential or commercial), after validation, extract input, do calculation, show result if (!radIndustrial.Checked) { if (Validator.TBHasNonNegativeInt(txtUsage, "Usage")) { // validation passed, extract input int usage = Convert.ToInt32(txtUsage.Text); // if residential user, use residential method if (radResidential.Checked) { totalAmt = ResidentialCustomer.CalculateCharge(usage); } // if commercial user, use commercial method else if (radCommercial.Checked) { totalAmt = CommercialCustomer.CalculateCharge(usage); } // do output txtTotal.Text = totalAmt.ToString("c"); txtUsage.SelectAll(); // select all for easy next entry } } // if industrial user, after validation, extract inputs, do calculation, show results else { if (Validator.TBHasNonNegativeInt(txtPeakUsage, "Peak Hour Usage") && Validator.TBHasNonNegativeInt(txtOPUsage, "Off Peak Usage")) { // validation passed, extract inputs int peakUsage = Convert.ToInt32(txtPeakUsage.Text); int opUsage = Convert.ToInt32(txtOPUsage.Text); totalAmt = IndustrialCustomer.CalculateCharge(peakUsage, opUsage); // calculate use industrial method // output peak & offpeak & total amount separately grpForIndusAmt.Visible = true; txtPeakCharge.Text = IndustrialCustomer.peakAmt.ToString("c"); txtOPCharge.Text = IndustrialCustomer.opAmt.ToString("c"); txtTotal.Text = totalAmt.ToString("c"); // select all text for easy next entry txtPeakUsage.Focus(); txtPeakUsage.SelectAll(); } } }
public void IndustrialCustomerTestCheckForValidArgument() { //Arrange int number = 1234; string name = "Kingsley"; string type = "R"; IndustrialCustomer c = new IndustrialCustomer(number, name, type); decimal peakKhwused = 30M; decimal offPeakKwhused = 40; //since input kwh hours for both peak and offpeak hours used is less than 1000. expected charge amount //should be equal to the Flat rate decimal flatRateforPeak = 76M; decimal flatRateforOffPeak = 40M; decimal totalFlatRate = flatRateforOffPeak + flatRateforPeak; //act decimal returnedChargeAmount = c.CalculateCharge(peakKhwused, offPeakKwhused); //Assert Assert.AreEqual(totalFlatRate, returnedChargeAmount); }
//Adds customer to the listBox private void btnAddCustomer_Click(object sender, EventArgs e) { //local variables int accountNo = 0; string customerName = ""; string customerType = ""; decimal chargeAmount; Customer customer; if (Validator.IsPresent(txtAccountNo, "Account No") && Validator.IsNonNegativeInt32(txtAccountNo, "Account No") && Validator.IsPresent(txtCustomerName, "Customer Name") && Validator.IsPresent(txtAmount, "Charge Amount")) { accountNo = Convert.ToInt32(txtAccountNo.Text); customerName = txtCustomerName.Text; chargeAmount = Convert.ToDecimal(txtAmount.Text.Remove(0, 1)); if (radResidential.Checked) { customerType = "R"; customer = new ResidentialCustomer(accountNo, customerName, customerType, chargeAmount); } else if (radCommercial.Checked) { customerType = "C"; customer = new CommercialCustomer(accountNo, customerName, customerType, chargeAmount); } else { customerType = "I"; customer = new IndustrialCustomer(accountNo, customerName, customerType, chargeAmount); } AddToList(customer); DisplayCustomers(); Statistics(); } }
// When calculate button is clicked private void btnCalculate_Click(object sender, EventArgs e) { // local variables decimal chargeAmount = 0; int accountNo = 0; string customerName = ""; //inistializes new customer; Customer customer; if (Validator.IsPresent(txtAccountNo, "Account No") && Validator.IsNonNegativeInt32(txtAccountNo, "Account No") && Validator.IsPresent(txtCustomerName, "Customer Name")) { accountNo = Convert.ToInt32(txtAccountNo.Text); customerName = txtCustomerName.Text; } /* if residential or commercial customer type radio buttons are checked this if statement runs. * Since residential and commercial both use the same energy used field, they are both wrapped in one if statement * */ if (radResidential.Checked || radCommercial.Checked) { //validates if the field (Energy used) contains a value and then validates that the value is a positive whole number. //takes the input from energy used textBox and stores it in a decimal variable if (Validator.IsPresent(txtEnergy, "Energy Used") && Validator.IsNonNegativeInt32(txtEnergy, "Energy Used")) { decimal energyUsed = Convert.ToDecimal(txtEnergy.Text); // when the residential customer type is selected // calculates the amount charged by calling a method and passing in base rate, the energy units to charge and the rate per unit if (radResidential.Checked) { customer = new ResidentialCustomer(accountNo, customerName, "R", chargeAmount); chargeAmount = customer.CalculateCharge(RESIDENTIAL_BASE, RESIDENTIAL_UNIT_RATE, energyUsed); } // when commercial cusotmer type is selected else if (radCommercial.Checked) { customer = new CommercialCustomer(accountNo, customerName, "C", chargeAmount); chargeAmount = customer.CalculateCharge(COMMERCIAL_FLAT_RATE, COMMERCIAL_UNIT_RATE, energyUsed); } // displays the amount charged by calling display amount and passing in the amount calculated in previous steps // which is then shown in the amount charge field //displayAmount(chargeAmount); } } /* * When the industrial customer type is checked, this statement runs * This is seperated from the top if condition because industrial used peak and off-peak hours energy used rather than total energy used. * */ else if (radIndustrial.Checked) { // validates if peak hours and off peak hours fields have a value and that the value is a positive whole number. if (Validator.IsPresent(txtPeakHours, "Peak Hours") && Validator.IsNonNegativeInt32(txtPeakHours, "Peak Hours") && Validator.IsPresent(txtOffPeakHours, "Off-Peak Hours") && Validator.IsNonNegativeInt32(txtOffPeakHours, "Off-Peak Hours")) { decimal peakHours = Convert.ToDecimal(txtPeakHours.Text); // stores peak hours input from txtpeakhours textbox in a decimal peakhours variable decimal offPeakHours = Convert.ToDecimal(txtOffPeakHours.Text); // stores off-peak hours input from txtoffpeakhours textbox in a decimal offpeakhours variable //initialize local peak and off-peak charge amount variables decimal peakChargeAmount = 0; // variable to keep track of the peak energy used charge amount. decimal offPeakChargeAmount = 0; // variable to keep track of the off- peak energy used charge amount customer = new IndustrialCustomer(accountNo, customerName, "I", chargeAmount); peakChargeAmount = customer.CalculateCharge(INDUSTRIAL_PEAK_FLAT_RATE, PEAK_UNIT_RATE, peakHours); offPeakChargeAmount = customer.CalculateCharge(INDUSTRIAL_OFF_PEAK_FLAT_RATE, OFF_PEAK_UNIT_RATE, offPeakHours); // the total charge amount for industrial customer is the sum of the peak hours and off peak hours charge amount. chargeAmount = peakChargeAmount + offPeakChargeAmount; } } // calls displayamount method and passess in the total charge amount to be displayed in the amount charge field. displayAmount(chargeAmount); }
private void BtnCalculate_Click(object sender, EventArgs e) { // create new customer type based on selection switch (cboxCustType.SelectedIndex) { case 0: // residential // set customer type customerType = 'R'; this.ActiveControl = txtCustName; if ( // validating customer name Validator.IsNotEmpty(txtCustName, "Customer name") && // validating account number Validator.IsNotEmpty(txtAcctNum, "Account number") && Validator.IsInteger(txtAcctNum, "Account number") && Validator.IsNonNegativeInt(txtAcctNum, "Account number") && // validating KWH Total Validator.IsNotEmpty(txtKwhUsed, "kWh") && Validator.IsInteger(txtKwhUsed, "kWh") && Validator.IsNonNegativeInt(txtKwhUsed, "kWh")) { // set text values to customer variables kwh1 = Convert.ToDecimal(txtKwhUsed.Text); accountNo = Convert.ToInt32(txtAcctNum.Text); customerName = Convert.ToString(txtCustName.Text); // create new customer object ResidentialCustomer newResCust = new ResidentialCustomer(accountNo, customerName, customerType, kwh1, kwh2 = 0); myCustomers.Add(newResCust); // add to list CustomerDB.SaveCustomers(myCustomers); DisplayCustomers(); ClearFields(); } break; case 1: //commercial // set customer type customerType = 'C'; this.ActiveControl = txtCustName; if ( // validating customer name Validator.IsNotEmpty(txtCustName, "Customer name") && // validating account number Validator.IsNotEmpty(txtAcctNum, "Account number") && Validator.IsInteger(txtAcctNum, "Account number") && Validator.IsNonNegativeInt(txtAcctNum, "Account number") && // validating KWH Total Validator.IsNotEmpty(txtKwhUsed, "kWh") && Validator.IsInteger(txtKwhUsed, "kWh") && Validator.IsNonNegativeInt(txtKwhUsed, "kWh")) { // set text values to customer variables kwh1 = Convert.ToDecimal(txtKwhUsed.Text); accountNo = Convert.ToInt32(txtAcctNum.Text); customerName = Convert.ToString(txtCustName.Text); // create new customer object CommercialCustomer newComCust = new CommercialCustomer(accountNo, customerName, customerType, kwh1, kwh2 = 0); myCustomers.Add(newComCust); // add to list CustomerDB.SaveCustomers(myCustomers); DisplayCustomers(); ClearFields(); } break; case 2: // industrial // set customer type customerType = 'I'; this.ActiveControl = txtCustName; if ( // validating customer name Validator.IsNotEmpty(txtCustName, "Customer name") && // validating account number Validator.IsNotEmpty(txtAcctNum, "Account number") && Validator.IsInteger(txtAcctNum, "Account number") && Validator.IsNonNegativeInt(txtAcctNum, "Account number") && // validation Peak Hours Validator.IsNotEmpty(txtPeakHours, "Peak Hours") && Validator.IsInteger(txtPeakHours, "Peak Hours") && Validator.IsNonNegativeInt(txtPeakHours, "Peak Hours") && // validation Off-Peak Hours Validator.IsNotEmpty(txtOffPeakHours, "Off-Peak Hours") && Validator.IsInteger(txtOffPeakHours, "Off-Peak Hours") && Validator.IsNonNegativeInt(txtOffPeakHours, "Off-Peak Hours")) { // set text values to customer variables kwh1 = Convert.ToDecimal(txtPeakHours.Text); kwh2 = Convert.ToDecimal(txtOffPeakHours.Text); accountNo = Convert.ToInt32(txtAcctNum.Text); customerName = Convert.ToString(txtCustName.Text); // create new customer object IndustrialCustomer newIndusCust = new IndustrialCustomer(accountNo, customerName, customerType, kwh1, kwh2); myCustomers.Add(newIndusCust); // add to list CustomerDB.SaveCustomers(myCustomers); DisplayCustomers(); ClearFields(); } break; } }