Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I'm new to the forums, but I am having an extremely difficult time figuring out how to create a project.
I am following the guidelines to create a calculator for pricing. Here are the guidelines:

H: $99 with word processor and spreadsheet. $49 per additional program, such as presentation, publisher, web page editor, and the like. A maximum of five installations are allowed.
E: $249 with word processor, spreadsheet, and presentation. $99 per additional program. This basic pricing allows a maximum of ten installations. Every additional installation adds a 10% additional charge.
U: $999 with all programs included and unlimited number of installations.
Customer type must be entered and must be one of H, E, and U.
If the customer type is H or E, the user may enter the number of additional programs (default is zero). This number must not be negative.
If the customer type is E, the user may also enter the number of installations (default is ten). This number must be positive. If the number of installations is over 40, remind the user that the unlimited plan is more economic.
Please help me.. I am so stuck.

Here is my code:

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;
    double AdditionalPrograms = 0;
    double Installations = 0;
    decimal Bill = 0;

    if (txtCustType.Text == "")
    {
        MessageBox.Show("Customer type is required.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        txtCustType.Focus();
        return;
    }
    //get customer type
    CustType = char.Parse(txtCustType.Text);
    CustType = char.ToUpper(CustType);
    txtCustType.Text = CustType.ToString();

    //check and get customer type
    if (CustType == 'U')
    {
        // Min: the other two inputs are irrelevant for 'U' customer.
   
    }
   
    else if (CustType == 'H' || CustType == 'E')
    {
        // Min: get the second input, if it's entered.
        if (txtAddPrograms.Text == ""||txtInstallations .Text=="")
        {
            AdditionalPrograms = double.Parse(txtAddPrograms.Text);
        }

        if (AdditionalPrograms < 0)
        {
            MessageBox.Show("Number of Additional Programs must not be negative.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtAddPrograms.Focus();
            return;
        }
        switch (CustType)
        {
            case 'U':
                Bill = U_CUST;
                break;
            case 'E':
                if (Installations <=10) 
                {
                    Bill = 249 + (99 * AdditionalPrograms);
                    //MessageBox.Show("The Unlimited plan is more economic. Would you like to switch?", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                if (Installations>10)
                {
                    Bill=E_CUST +(99*AdditionalPrograms) +(AdditionalPrograms -10)*.1(E_CUST+99*AdditionalPrograms )
                {
                    Bill = (decimal)(AdditionalPrograms) + (E_CUST) + (ADD_E_CUST) + (decimal)(Installations * .10);
                }
                break;
            case 'H':
                Bill = (decimal)(H_CUST) + (ADD_H_CUST * (decimal)AdditionalPrograms);
               
                break;
        }

        //Display the result.
        lblBill.Text = Bill.ToString("C");
    }
}
Posted
Updated 28-Sep-13 21:32pm
v2
Comments
Richard MacCutchan 29-Sep-13 3:31am    
Stuck with what? Please explain your problem.
BillWoodruff 29-Sep-13 4:20am    
I think this is a good question ! And, you are doing well for someone new to C# and .NET.

Please say more about what you are having difficulty with.
Mandyy14 29-Sep-13 4:28am    
Thank you! Though I certainly should be doing a lot better.. haha
My difficulty is getting the Bill amount to update correctly. When I add an additional program or installment, my Bill total is supposed to calculate and update based on pre-declared amounts. But I am thoroughly confused on the syntax I should use to make the Bill calculate and update.
I am currently editing my project according to the solution below, but I think I will still have some questions.

You need to look at that a bit more closely, and pay attention to what is going on. If I rip out loads of stuff to leave just the "bare bones" of your code, there is a problem which leaps to mind:
C#
if (CustType == 'U')
{
}
else if (CustType == 'H' || CustType == 'E')
{
    switch (CustType)
    {
        case 'U':
            break;
        case 'E':
            break;
        case 'H':
            break;
    }
}
How is a CustType of 'U' going to get into the switch statement?

What I would suggest you do is put a breakpoint at the top of the method, and use the single step facility to work your way through the code look at what is going on. It's not difficult, but the chances are you are "too close" to the code to see the problems, and a step-by-step walk through with you predicting what should happen before each line is executed and comparing that to what does happen would probably help you a lot!

Then go back to your instructions and look at them closely.
Try to process each type individually on paper, and see what you need to do - it might help you work out how to implement this.

BTW: One of the instructions was "...must be one of..." - what happens if I enter a CustType of 'X'? or 'e', or 'h'?
 
Share this answer
 
Comments
BillWoodruff 29-Sep-13 4:04am    
+++++ Excellent advice !
Mandyy14 29-Sep-13 4:21am    
Thank you very much! I think you're right, I'm just too close to the code to see the problems. I will follow your suggestions and I appreciate your help!
OriginalGriff 29-Sep-13 4:25am    
You're welcome!
To keep it simple leave out the if else block and have everything in switch case.

C#
if (CustType == 'U')
    {
        // Min: the other two inputs are irrelevant for 'U' customer.

    }

    else if (CustType == 'H' || CustType == 'E')
    {
}



C#
switch (CustType)
    {
        case 'U':
            break;
        case 'E':
            break;
        case 'H':
            break;
case else:
//do something
} 


and keep this into a seperate method and call it in Case 'E' and Case 'H' to avoid repeating.
C#
if (txtAddPrograms.Text == ""||txtInstallations .Text=="")
       {
           AdditionalPrograms = double.Parse(txtAddPrograms.Text);
       }

       if (AdditionalPrograms < 0)
       {
           MessageBox.Show("Number of Additional Programs must not be negative.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
           txtAddPrograms.Focus();
           return;
       }
   }
 
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