locked
My Calculator code not working , Please help me out with the changes to do ? RRS feed

  • Question

  • User-2089670273 posted

    protected void TaxBtn_Click(object sender, EventArgs e)
    {

    int num1 = Convert.ToInt32(Price.Text.ToString());
    int num2 = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
    int num3 = Convert.ToInt32(DropDownList2.SelectedValue.ToString());
    int petrol = Convert.ToInt32(DropDownList1.SelectedValue = 1.ToString());
    int cng = Convert.ToInt32(DropDownList1.SelectedValue = 2.ToString());
    int diesel = Convert.ToInt32(DropDownList1.SelectedValue = 3.ToString());
    int personal = Convert.ToInt32(DropDownList2.SelectedValue = 4.ToString());
    int commercial = Convert.ToInt32(DropDownList2.SelectedValue = 5.ToString());
    int tax ;

    petrol = 1000;
    cng = 2000;
    diesel = 3000;
    personal = 500;
    commercial = 700;

    tax = num1 + num2 + num3;

    CalTax.Text = tax.ToString();

    -------HTML---------

    <div>
    <div class="car_card">
    <label class="label1">Road Tax Calculator</label>
    <div class="toggle">
    <asp:Button ID="CarBtn" Cssclass="CarBtn" runat="server" Text="Car" OnClick="CarBtn_Click" />
    <asp:Button ID="BikeBtn" CssClass="BikeBtn" runat="server" Text="Bike" OnClick="BikeBtn_Click" />
    </div>
    <div class="Car">
    <label class="label2">Registration Date</label>
    <br />
    <asp:TextBox ID="RegDate" runat="server" TextMode="Date" CssClass="Date"></asp:TextBox>
    <br />
    <label class="label3">Price</label>
    <br />
    <asp:TextBox ID="Price" runat="server" CssClass="Price" TextMode="Number"></asp:TextBox>
    <br />
    <label class="label4">Fuel Type</label>
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="Drop1">
    <asp:ListItem Value="1">Petrol</asp:ListItem>
    <asp:ListItem Value="2">CNG</asp:ListItem>
    <asp:ListItem Value="3">Diesel</asp:ListItem>
    </asp:DropDownList>
    <br />
    <label class="label5">User Type</label>
    <br />
    <asp:DropDownList ID="DropDownList2" runat="server" CssClass="Drop2">
    <asp:ListItem Value="4">Personal</asp:ListItem>
    <asp:ListItem Value="5">Commercial</asp:ListItem>
    </asp:DropDownList>
    <br />
    <label class="label7">Calculated Road Tax &#8659;</label>
    <br />
    <asp:TextBox ID="CalTax" runat="server" CssClass="CalTax"></asp:TextBox>
    <br />
    <asp:Button ID="TaxBtn" runat="server" Text="Get Road Tax" CssClass="TaxBtn" OnClick="TaxBtn_Click" />
    </div>
    </div>
    </div>
    }

    Monday, July 27, 2020 9:15 AM

Answers

  • User-1330468790 posted

    Hi Flame103,

     

    I am not sure that what kind of price range you want to add.

    From my best guess, I think you want to assign the variable "num1" with different value based on a given price range, e.g. tax-exempt rate.

     

    You should provide us with more description so that we don't need to guess.

    However, I construct a demo and you could refer to below codes to see if it is what you want. 

    .ASPX Page:

    <div class="car_card">
                    <label class="label1">Road Tax Calculator</label>
                    <div class="toggle">
                        <asp:Button ID="CarBtn" CssClass="CarBtn" runat="server" Text="Car"  />
                        <asp:Button ID="BikeBtn" CssClass="BikeBtn" runat="server" Text="Bike"  />
                    </div>
                    <div class="Car">
                        <label class="label2">Registration Date</label>
                        <br />
                        <asp:TextBox ID="RegDate" runat="server" TextMode="Date" CssClass="Date"></asp:TextBox>
                        <br />
                        <label class="label3">Price</label>
                        <br />
                        <asp:TextBox ID="Price" runat="server" CssClass="Price" TextMode="Number"></asp:TextBox>
                        <br />
                        <label class="label4">Fuel Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="Drop1">
                            <asp:ListItem Value="1">Petrol</asp:ListItem>
                            <asp:ListItem Value="2">CNG</asp:ListItem>
                            <asp:ListItem Value="3">Diesel</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label5">User Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="Drop2">
                            <asp:ListItem Value="1">Personal</asp:ListItem>
                            <asp:ListItem Value="2">Commercial</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label7">Calculated Road Tax &#8659;</label>
                        <br />
                        <asp:TextBox ID="CalTax" runat="server" CssClass="CalTax"></asp:TextBox>
                        <br />
                        <asp:Button ID="TaxBtn" runat="server" Text="Get Road Tax" CssClass="TaxBtn" OnClick="TaxBtn_Click" />
                    </div>
                </div>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void TaxBtn_Click(object sender, EventArgs e)
            {
    
                double num1 = Convert.ToInt32(Price.Text);
                double num2, num3;
                double tax;
    
                int petrol = 1000,
                    cng = 2000,
                    diesel = 3000,
                    personal = 500,
                    commercial = 700;
                /* less than 1,000: 100% tax exemption
                 * 1,001 - 20,000: 60% tax exemption
                 * 20,001- 25,000: -500
                 * more than 25,001:  -400
                 */
                double taxRate = 0.05;
    
                if (num1 <= 1000)
                {
                    // 100% 
                    num1 = 0;
                } else if (num1 <= 20000)
                {
                    num1 = num1 * taxRate* (1-0.6);
                }
                else if (num1 <= 25000)
                {
                    num1 = num1 * taxRate - 500;
                }
                else
                {
                    num1 = num1 * taxRate - 400;
                }
                
    
                switch (DropDownList1.SelectedValue)
                {
                    case "1":
                        num2 = petrol; 
                        break;
                    case "2":
                        num2 = cng;
                        break;
                    case "3":
                        num2 = diesel;
                        break;
                    default:
                        num2 = 0;
                        break;
                }
    
                switch (DropDownList2.SelectedValue)
                {
                    case "1":
                        num3 = personal;
                        break;
                    case "2":
                        num3 = commercial;
                        break;
                    default:
                        num3 = 0;
                        break;
                }
    
                tax = num1 + num2 + num3;
                CalTax.Text = tax.ToString();
            }

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2020 10:51 AM
  • User-1330468790 posted

    Hi Flame103,

     

    Since your problem is about implementation, reading codes would be easier to understand how to solve it.

    Please refer to below demo:

    aspx:

    <div class="car_card">
                    <label class="label1">Road Tax Calculator</label>
                    <div class="toggle">
                        <asp:Button ID="CarBtn" CssClass="CarBtn" runat="server" Text="Car"  />
                        <asp:Button ID="BikeBtn" CssClass="BikeBtn" runat="server" Text="Bike"  />
                    </div>
                    <div class="Car">
                        <label class="label2">Registration Date</label>
                        <br />
                        <asp:TextBox ID="RegDate" runat="server" TextMode="Date" CssClass="Date"></asp:TextBox>
                        <br />
                        <label class="label3">Price</label>
                        <br />
                        <asp:TextBox ID="Price" runat="server" CssClass="Price" TextMode="Number"></asp:TextBox>
                        <br />
                        <label class="label4">Fuel Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="Drop1">
                            <asp:ListItem Value="1">Petrol</asp:ListItem>
                            <asp:ListItem Value="2">CNG</asp:ListItem>
                            <asp:ListItem Value="3">Diesel</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label5">User Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="Drop2">
                            <asp:ListItem Value="1">Personal</asp:ListItem>
                            <asp:ListItem Value="2">Commercial</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label7">Calculated Road Tax &#8659;</label>
                        <br />
                        <asp:TextBox ID="CalTax" runat="server" CssClass="CalTax"></asp:TextBox>
                        <br />
                        <asp:Button ID="TaxBtn" runat="server" Text="Get Road Tax" CssClass="TaxBtn" OnClick="TaxBtn_Click" />
    
                        <asp:Label ID="DisplayDetail" runat="server"></asp:Label>
                    </div>
                </div>

    Code behind:

    protected void TaxBtn_Click(object sender, EventArgs e)
            {
    
                double num1 = Convert.ToInt32(Price.Text);
                double num2, num3, num4;
                double tax;
    
                // You might need to do validation for the date in the aspx page by yourself.
                DateTime date = Convert.ToDateTime(RegDate.Text);
    
                int petrol = 1000,
                    cng = 2000,
                    diesel = 3000,
                    personal = 500,
                    commercial = 700;
    
                /*
                 * Handle num1
                 * Ceiling method returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
                 */
                num1 = Math.Ceiling(num1 / 100000) * 1000;
                
    
                /*
                 * Handle num2
                 */
                switch (DropDownList1.SelectedValue)
                {
                    case "1":
                        num2 = petrol; 
                        break;
                    case "2":
                        num2 = cng;
                        break;
                    case "3":
                        num2 = diesel;
                        break;
                    default:
                        num2 = 0;
                        break;
                }
    
                /*
                 * Handle num3
                 */
                switch (DropDownList2.SelectedValue)
                {
                    case "1":
                        num3 = personal;
                        break;
                    case "2":
                        num3 = commercial;
                        break;
                    default:
                        num3 = 0;
                        break;
                }
    
                /*
                 * Handle num4
                 * Check Year only
                 */
                if (CheckRange(date.Year, 2000, 2005))
                {
                    num4 = 100;
                } else if(CheckRange(date.Year, 2005, 2010))
                {
                    // same?
                    num4 = 100;
                }
                else
                {
                    num4 = 0;
                }
    
    
    
                tax = num1 + num2 + num3 + num4;
                CalTax.Text = tax.ToString();
    
                DisplayDetail.Text = string.Format("<br/> num1 = {0} <br/> num2 = {1} <br/> num3 = {2} <br/> num4 = {3} ", num1, num2, num3, num4);
            }
    
            private bool CheckRange(int toCheck, int left, int right)
            {
    
                if (toCheck > left && toCheck <= right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 3:58 AM
  • User-2089670273 posted

    This Exactly what I needed , My Calculator tool is all done thanks to you ..  smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 4:52 AM

All replies

  • User-1330468790 posted

    Hi Flame103,

     

    Could you please specify what "not working" means?

    I tried your codes and found that the variable "CalTax.Text" will be assigned with the sum of num1, num2 and num3, which is expected as the codes show. 

      

    If this is not what you want and you are making the calculator with your calculation logic, you could post it here so that I would be able to help you to modify codes.

     

    Best regards,

    Sean

    Tuesday, July 28, 2020 6:27 AM
  • User-2089670273 posted

    This is my new revised code . Can you help me out about how can I add a price range and make num1 depend on price range !! 

    int num1 = Convert.ToInt32(Price.Text);
    int num2 = Convert.ToInt32(DropDownList1.SelectedValue);
    int num3 = Convert.ToInt32(DropDownList2.SelectedValue);
    int tax;
    //int price;

    int petrol;
    int cng;
    int diesel;
    int personal;
    int commercial;

    petrol = 1000;
    cng = 2000;
    diesel = 3000;
    personal = 500;
    commercial = 700;
    tax = 0;
    //price = 0;


    if (DropDownList1.SelectedValue == "1" | DropDownList2.SelectedValue == "1" )
    {
    tax = num1 + petrol + personal;
    }
    else if (DropDownList1.SelectedValue == "2" | DropDownList2.SelectedValue == "1" )
    {
    tax = num1 + cng + personal;
    }
    else if (DropDownList1.SelectedValue == "3" | DropDownList2.SelectedValue == "1" )
    {
    tax = num1 + diesel + personal;
    }
    else
    {

    }

    if (DropDownList1.SelectedValue == "1" | DropDownList2.SelectedValue == "2" )
    {
    tax = num1 + petrol + commercial;
    }
    else if (DropDownList1.SelectedValue == "2" | DropDownList2.SelectedValue == "2" )
    {
    tax = num1 + cng + commercial;
    }
    else if (DropDownList1.SelectedValue == "3" | DropDownList2.SelectedValue == "2" )
    {
    tax = num1 + diesel + commercial;
    }
    else
    {

    }

    CalTax.Text = tax.ToString();

    Tuesday, July 28, 2020 6:31 AM
  • User-1330468790 posted

    Hi Flame103,

     

    I am not sure that what kind of price range you want to add.

    From my best guess, I think you want to assign the variable "num1" with different value based on a given price range, e.g. tax-exempt rate.

     

    You should provide us with more description so that we don't need to guess.

    However, I construct a demo and you could refer to below codes to see if it is what you want. 

    .ASPX Page:

    <div class="car_card">
                    <label class="label1">Road Tax Calculator</label>
                    <div class="toggle">
                        <asp:Button ID="CarBtn" CssClass="CarBtn" runat="server" Text="Car"  />
                        <asp:Button ID="BikeBtn" CssClass="BikeBtn" runat="server" Text="Bike"  />
                    </div>
                    <div class="Car">
                        <label class="label2">Registration Date</label>
                        <br />
                        <asp:TextBox ID="RegDate" runat="server" TextMode="Date" CssClass="Date"></asp:TextBox>
                        <br />
                        <label class="label3">Price</label>
                        <br />
                        <asp:TextBox ID="Price" runat="server" CssClass="Price" TextMode="Number"></asp:TextBox>
                        <br />
                        <label class="label4">Fuel Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="Drop1">
                            <asp:ListItem Value="1">Petrol</asp:ListItem>
                            <asp:ListItem Value="2">CNG</asp:ListItem>
                            <asp:ListItem Value="3">Diesel</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label5">User Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="Drop2">
                            <asp:ListItem Value="1">Personal</asp:ListItem>
                            <asp:ListItem Value="2">Commercial</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label7">Calculated Road Tax &#8659;</label>
                        <br />
                        <asp:TextBox ID="CalTax" runat="server" CssClass="CalTax"></asp:TextBox>
                        <br />
                        <asp:Button ID="TaxBtn" runat="server" Text="Get Road Tax" CssClass="TaxBtn" OnClick="TaxBtn_Click" />
                    </div>
                </div>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void TaxBtn_Click(object sender, EventArgs e)
            {
    
                double num1 = Convert.ToInt32(Price.Text);
                double num2, num3;
                double tax;
    
                int petrol = 1000,
                    cng = 2000,
                    diesel = 3000,
                    personal = 500,
                    commercial = 700;
                /* less than 1,000: 100% tax exemption
                 * 1,001 - 20,000: 60% tax exemption
                 * 20,001- 25,000: -500
                 * more than 25,001:  -400
                 */
                double taxRate = 0.05;
    
                if (num1 <= 1000)
                {
                    // 100% 
                    num1 = 0;
                } else if (num1 <= 20000)
                {
                    num1 = num1 * taxRate* (1-0.6);
                }
                else if (num1 <= 25000)
                {
                    num1 = num1 * taxRate - 500;
                }
                else
                {
                    num1 = num1 * taxRate - 400;
                }
                
    
                switch (DropDownList1.SelectedValue)
                {
                    case "1":
                        num2 = petrol; 
                        break;
                    case "2":
                        num2 = cng;
                        break;
                    case "3":
                        num2 = diesel;
                        break;
                    default:
                        num2 = 0;
                        break;
                }
    
                switch (DropDownList2.SelectedValue)
                {
                    case "1":
                        num3 = personal;
                        break;
                    case "2":
                        num3 = commercial;
                        break;
                    default:
                        num3 = 0;
                        break;
                }
    
                tax = num1 + num2 + num3;
                CalTax.Text = tax.ToString();
            }

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2020 10:51 AM
  • User-2089670273 posted

    Wow !!! Thank you so much with this revised code , this really simplified my code to great extent ! 

    Sir , I actually want to add the Price Range this way 

    1. If price entered by user is between ( 0 to 100000 ) num1 = 1000;
    2. If price entered by user is between ( 100001 to 200000 ) num1 = 2000;

          and so on !! 

    And Sir , I actually also want to implement a date parameter ;

    Like when the user enters a date in Registration date textbox , now depending on the year I want to assign some values to Year Ranges

    Example : Lets introduce a new variable : num4 !

    1. If entered year is between ( 2000 - 2005 ) num4 = 100;
    2. If entered year is between ( 2005 - 2010 ) num4 = 100;

    and then the total : tax = num1 + num2 + num3 +num4

    And again thank you so much for your assistance !

    Tuesday, July 28, 2020 11:32 AM
  • User-1330468790 posted

    Hi Flame103,

     

    Since your problem is about implementation, reading codes would be easier to understand how to solve it.

    Please refer to below demo:

    aspx:

    <div class="car_card">
                    <label class="label1">Road Tax Calculator</label>
                    <div class="toggle">
                        <asp:Button ID="CarBtn" CssClass="CarBtn" runat="server" Text="Car"  />
                        <asp:Button ID="BikeBtn" CssClass="BikeBtn" runat="server" Text="Bike"  />
                    </div>
                    <div class="Car">
                        <label class="label2">Registration Date</label>
                        <br />
                        <asp:TextBox ID="RegDate" runat="server" TextMode="Date" CssClass="Date"></asp:TextBox>
                        <br />
                        <label class="label3">Price</label>
                        <br />
                        <asp:TextBox ID="Price" runat="server" CssClass="Price" TextMode="Number"></asp:TextBox>
                        <br />
                        <label class="label4">Fuel Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="Drop1">
                            <asp:ListItem Value="1">Petrol</asp:ListItem>
                            <asp:ListItem Value="2">CNG</asp:ListItem>
                            <asp:ListItem Value="3">Diesel</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label5">User Type</label>
                        <br />
                        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="Drop2">
                            <asp:ListItem Value="1">Personal</asp:ListItem>
                            <asp:ListItem Value="2">Commercial</asp:ListItem>
                        </asp:DropDownList>
                        <br />
                        <label class="label7">Calculated Road Tax &#8659;</label>
                        <br />
                        <asp:TextBox ID="CalTax" runat="server" CssClass="CalTax"></asp:TextBox>
                        <br />
                        <asp:Button ID="TaxBtn" runat="server" Text="Get Road Tax" CssClass="TaxBtn" OnClick="TaxBtn_Click" />
    
                        <asp:Label ID="DisplayDetail" runat="server"></asp:Label>
                    </div>
                </div>

    Code behind:

    protected void TaxBtn_Click(object sender, EventArgs e)
            {
    
                double num1 = Convert.ToInt32(Price.Text);
                double num2, num3, num4;
                double tax;
    
                // You might need to do validation for the date in the aspx page by yourself.
                DateTime date = Convert.ToDateTime(RegDate.Text);
    
                int petrol = 1000,
                    cng = 2000,
                    diesel = 3000,
                    personal = 500,
                    commercial = 700;
    
                /*
                 * Handle num1
                 * Ceiling method returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
                 */
                num1 = Math.Ceiling(num1 / 100000) * 1000;
                
    
                /*
                 * Handle num2
                 */
                switch (DropDownList1.SelectedValue)
                {
                    case "1":
                        num2 = petrol; 
                        break;
                    case "2":
                        num2 = cng;
                        break;
                    case "3":
                        num2 = diesel;
                        break;
                    default:
                        num2 = 0;
                        break;
                }
    
                /*
                 * Handle num3
                 */
                switch (DropDownList2.SelectedValue)
                {
                    case "1":
                        num3 = personal;
                        break;
                    case "2":
                        num3 = commercial;
                        break;
                    default:
                        num3 = 0;
                        break;
                }
    
                /*
                 * Handle num4
                 * Check Year only
                 */
                if (CheckRange(date.Year, 2000, 2005))
                {
                    num4 = 100;
                } else if(CheckRange(date.Year, 2005, 2010))
                {
                    // same?
                    num4 = 100;
                }
                else
                {
                    num4 = 0;
                }
    
    
    
                tax = num1 + num2 + num3 + num4;
                CalTax.Text = tax.ToString();
    
                DisplayDetail.Text = string.Format("<br/> num1 = {0} <br/> num2 = {1} <br/> num3 = {2} <br/> num4 = {3} ", num1, num2, num3, num4);
            }
    
            private bool CheckRange(int toCheck, int left, int right)
            {
    
                if (toCheck > left && toCheck <= right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 3:58 AM
  • User-2089670273 posted

    This Exactly what I needed , My Calculator tool is all done thanks to you ..  smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 4:52 AM