public void CalculateChargeTest()
        { // Test of normal input, kWh amount greater than the base charge
            ResidentialCustomer cust = new ResidentialCustomer("Test", 12345);
            int     kWh            = 2000;
            decimal expectedCharge = 110m;
            decimal actualCharge;

            actualCharge = cust.CalculateCharge(kWh);

            Assert.AreEqual(expectedCharge, actualCharge);
        }
Ejemplo n.º 2
0
        public void CalculateChargeWithZeroUsage()
        {
            // Arrange
            int    usage          = 0;
            double expectedCharge = 6;
            double actualCharge;

            // Act
            actualCharge = ResidentialCustomer.CalculateCharge(usage);
            // Assert
            Assert.AreEqual(expectedCharge, actualCharge);
        }
        public void CalculateChargeTest1()
        {   // Test of abnormal input
            // validation should save the method from seeing a negative number, but if it does get there somehow,
            // it's expected to treat it as a zero and only charge the base amount
            ResidentialCustomer cust = new ResidentialCustomer("Test", 12345);
            int     kWh            = -2000;
            decimal expectedCharge = 6m;
            decimal actualCharge;

            actualCharge = cust.CalculateCharge(kWh);

            Assert.AreEqual(expectedCharge, actualCharge);
        }
Ejemplo n.º 4
0
        public void ResidentialCustomerTestCheckForInvalidArguments2()
        {
            //Arrange
            int    number               = 1234;
            string name                 = "Kingsley";
            string type                 = "R";
            ResidentialCustomer c       = new ResidentialCustomer(number, name, type);
            decimal             khwused = -30M;
            decimal             expectedChargeAmount = 6M + (0.052M * khwused);
            //act
            decimal returnedChargeAmount = c.CalculateCharge(khwused);

            //Assert
            Assert.Fail();
        }
        public void CalculateChargeMoreThanZero()
        {
            //arrange
            decimal             resBase             = 6m;
            decimal             unitRate            = 0.052m;
            decimal             energyUsed          = 100;
            decimal             expectedCharge      = 11.2m;
            decimal             actualCharge        = 0;
            ResidentialCustomer residentialCustomer = new ResidentialCustomer(1, "john", "R", actualCharge);

            //act
            actualCharge = residentialCustomer.CalculateCharge(resBase, unitRate, energyUsed);

            //assert
            Assert.AreEqual(expectedCharge, actualCharge);
        }
Ejemplo n.º 6
0
        // 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();
                }
            }
        }
        // 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);
        }