Amanda Duffin

Amanda Duffin

  • NA
  • 2
  • 2.5k

C# Default and Maximum number in a textbox. Please help

Sep 29 2013 6:23 PM
I have my program working and compiling correctly, but I am having an issue getting the maximum and default installations set. 

I would really appreciate some pointers. I'm new to C#, so any help is greatly appreciated!

A few guidelines: 'H' can have a maximum of 5 installations. The default is 1.
'E' has a default of 10 installations included. For each installation above 10, the additional price is added. 
Both 'H' and 'E' have a default of additional programs of 0. Number must not be negative.

Here is my code thus far:

 private void btnCalculate_Click(object sender, EventArgs e)
    {
        const decimal U_CUST = 999;
        const decimal E_CUST = 249;
        const decimal ADD_E_CUST = 99;
        const decimal H_CUST = 99;
        const decimal ADD_H_CUST = 49;
 
        char CustType;
        int AdditionalPrograms = 0;
        int Installations = 10;
        decimal Bill = 0;
 
        if (txtCustType.Text.Length != 1)
        {
            MessageBox.Show("Customer type is required.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtCustType.Focus();
            return;
        }
 
        CustType = char.Parse(txtCustType.Text);
        CustType = char.ToUpper(CustType);
        txtCustType.Text = CustType.ToString();
 
        if (CustType != 'U' && CustType != 'H' && CustType != 'E')
        {
            MessageBox.Show("Invalid customer type - must be H, E or U.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtCustType.Focus();
            return;
        }
 
        //calculate bill according to customer type
        if (CustType == 'U')
        {
            Bill = U_CUST;
        }
        else
        {
            if (txtAddPrograms.Text != "") // AdditionalPrograms will remain at 0 if textbox is empty
            {
                bool isValid = int.TryParse(txtAddPrograms.Text, out AdditionalPrograms);
                if (!isValid || AdditionalPrograms < 0)
                {
                    MessageBox.Show("Invalid number of additional programs - can't be negative.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtAddPrograms.Focus();
                    return;
                }
            }
 
            if (CustType == 'H')
            {
                Bill = H_CUST + ADD_H_CUST * AdditionalPrograms;
            }
            else  // Customer Type must be 'E'
            {
                if (txtInstallations.Text != "") // Installations will remain at 10 if textbox is empty
                {
                    bool isValid = int.TryParse(txtInstallations.Text, out Installations);
                    if (!isValid || Installations < 1)
                    {
                        MessageBox.Show("Invalid number of installations - must be positive.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtInstallations.Focus();
                        return;
                    }
                }
 
                Bill = E_CUST + ADD_E_CUST * AdditionalPrograms;
                if (Installations > 10) Bill *= 1 + (Installations - 10) * 0.1m;
            }
        }
 
        //Display the result.
        lblBill.Text = Bill.ToString("C");
        if (CustType == 'E' && Installations > 40)
        {
            MessageBox.Show("Note that the unlimited plan would be more economic.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

Answers (1)