Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have my program working and compiling correctly now, but I am still 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:

C#
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);
       }
   }
Posted
Comments
Sergey Alexandrovich Kryukov 29-Sep-13 23:04pm    
Not a question. What issues?
—SA

1 solution

OK - let's have another look at this...

There is some rather odd code there - I take it you don't know that a string can be treated as an array of chars? So you could replace this:
C#
CustType = char.Parse(txtCustType.Text);
CustType = char.ToUpper(CustType);
txtCustType.Text = CustType.ToString();
With this:
C#
txtCustType.Text = txtCustType.Text.ToUpper();
CustType = txtCustType.Text[0];
Which is a little more obvious.

But...personally I wouldn't mess about with characters anyway - use the strings directly instead! :laugh:

Secondly, you seem to spend a fair amount of time converting numbers and checking they are positive - are you restricted to using TextBoxes? Because if you aren't, then there is a handy control called a NumericUpDown - which can only ever hold valid numbers in a certain range, and needs no checking or conversion as it provides a decimal value directly. This could clean your code up lots!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900