Wednesday, May 11, 2016

Program 8: Windows Form Application:Generate Electric Bill


1. Open Visual studio 2013.
2. On the menu bar, choose File, New, and Project. Choose Programming language as Visual C#. In the templates list, choose the Windows Forms Application icon. 
3. Name the project as ElectricityBill and specify the address of your folder.
4. In the main window design the form as shown below and set the properties as required.


The form name is given as ElectricBill. There are 3 Textboxes , 2 ComboBoxes, one button , one GroupBox and 8 labels used in this form. all these labels are named accordingly. The Text property of these controls can also be changed in properties.
e.g. The text property of Button is set to Generate Bill. similary Text property of lables is equal to the name appears on the form for that particular label.

5. In the ElectricityBill_Load method write the following code

      private void ElectricityBill_Load(object sender, EventArgs e)
        {
            MyBindCategories();
            MyBindMonths();
            lblAmount.Text = String.Empty;
            lblServiceCharge.Text = String.Empty;
            lblUnits.Text = String.Empty;
        }   

   6.   In MyBindCategories() method write the following code

       private void MyBindCategories()
        {
           // categories = new List(2);
            //categories.Add("House");
            //categories.Add("Industries");

       String[] categories = new String[2] { "House","Industries"};
        
            foreach (String item in categories)
            cboMeterCat.Items.Add(item);      //comboBox
            cboMeterCat.SelectedIndex = 0;
        }

7.   In MyBindMonths() method write the following code
        private void MyBindMonths()
        {
            String[] months = new String[12] { "Jan", "Feb", "Mar",     "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            cboMonth.Items.Clear();
            foreach (String month in months)
            cboMonth.Items.Add(month);
            cboMonth.SelectedIndex = 0;
        }

8.   In MyValidation() method write the following code

        private int MyValidation()
        {
            if (txtCName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter consumer name");
                txtCName.Focus();
                return 0;
            }
            else if (txtCMR.Text.Trim().Length == 0)
            {
                MessageBox.Show("Enter current month reading");
                txtCMR.Focus();
                return 0;
            }
            else if (txtPMR.Text.Trim().Length == 0)
            {
                MessageBox.Show("Enter previous month reading");
                txtPMR.Focus();
                return 0;
            }
            else if (int.Parse(txtCMR.Text)< int.Parse(txtPMR.Text))
            {
                MessageBox.Show("Current month reading must be                       greater than or equal to previous month reading");
                txtCMR.Focus();
                return 0;
            }
            return 1;
        }         
9.   In MyHouseCalculation() method write the following code

        private float MyHouseCalculation(float UnitsConsumed)
        {
            float amt = 0;
            if (UnitsConsumed <= 50)
                amt = UnitsConsumed * 1.45f;
            else if (UnitsConsumed <= 100)
                amt = (50 * 1.45f) + (UnitsConsumed - 50) * 2.8f;
            else if (UnitsConsumed <= 200)
   
            amt = (50 * 1.45f) + (50 * 2.8f) + (UnitsConsumed - 100)                  * 3.05f;
           
            else if (UnitsConsumed <= 300)
  
           amt = (50 * 1.45f) + (50 * 2.8f) + (100 * 3.05f) +                        (UnitsConsumed - 200) * 4.5f;
           
           else
                
           amt = (50 * 1.45f) + (50 * 2.8f) + (100 * 3.05f) + (100 *                  4.5f) + (UnitsConsumed - 300) * 5.5f;
            
           return amt;
        }

10.   In MyIndustryCalculation() method write the following code

        private float MyIndustryCalculation(float UnitsConsumed)
        {
            float amt = 0;
            if (UnitsConsumed <= 100)
                amt = UnitsConsumed * 2.5f;
            else if (UnitsConsumed <= 300)
                amt = (100 * 2.5f) + (UnitsConsumed - 100) * 3.8f;
            else if (UnitsConsumed <= 500)
            amt = (100 * 2.5f) + (200 * 3.8f) + (UnitsConsumed -       
                  300) * 5.5f;
            else
            amt = (100 * 2.5f) + (200 * 3.8f) + (200 * 5.5f) +                        (UnitsConsumed- 500) * 6.7f;           
            return amt;
        }
        public ElectricityBill()
        {
            InitializeComponent();
        }

       private void ElectricityBill_Load(object sender, EventArgs e)
        {
            MyBindCategories();
            MyBindMonths();
            lblAmount.Text = String.Empty;
            lblServiceCharge.Text = String.Empty;
            lblUnits.Text = String.Empty;
        }

       
11.   In btnGenerateBill_Clich() method write the following code

      private void btnGenerateBill_Click(object sender, EventArgs e)
        {
            if(MyValidation() == 1)
            {
            int units=int.Parse(txtCMR.Text)-int.Parse(txtPMR.Text);
            float amt=0;
            float serviceCharge=0;
            String cat =                                  
            cboMeterCat.SelectedItem.ToString().ToUpper();
            switch(cat)
                {
                    case "HOUSE":
                        amt = MyHouseCalculation(units);
                        serviceCharge = 25f;
                        amt += serviceCharge; //ADD SERVICE CHARGE
                        if (amt < 70) //IF NOT EXCEED MINIMUM CHARGE
                            amt = 70;
                        break;
                    case "INDUSTRIES":
                        amt = MyIndustryCalculation(units);
                        serviceCharge = 50f;
                        amt += serviceCharge;  // ADD SERVICE CHARGE
                        if (amt < 150) // IF NOT EXCEED MIN CHARGE
                            amt = 150;
                        break;
                    default:
                        break;
                }
                lblUnits.Text = "Units Consumed  : " + units;
                lblServiceCharge.Text = "Service Charge    : " +                                             serviceCharge;
                lblAmount.Text = "Amount is            : " + amt;
            }
            else
            {
                lblAmount.Text = String.Empty;
                lblServiceCharge.Text = String.Empty;
                lblUnits.Text = String.Empty;
            }
        }
12.    Run your program  and check the result

No comments:

Post a Comment