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);
}
}
VulpesPosted Sep 30, 2013, 5:01 AM
http://www.c-sharpcorner.com/Forums/Thread/232764/
As the question in that thread stated that the user can only enter the number of installations if the customer type is 'E', it's anybody's guess what you're supposed to do when the customer type is 'H' or 'U'.
One thing you could do is to simply print the following in the txtInstallations TextBox:
If customer type is 'H':
txtInstallations.Text = "1 to 5";
If customer type is 'U':
txtInstallations.Text = "No limit";
If customer type is 'E' but the user hasn't entered anything:
txtInstallations.Text = "1 to 10";
Similarly, for additional programs, if the user hasn't (or can't) enter a figure, you could print in the txtAddPrograms TextBox:
If customer type is 'H' or 'E':
txtAddPrograms.Text = "0";
If customer type is 'U':
txtAddPrograms = "No limit";