Link to home
Start Free TrialLog in
Avatar of rtod2
rtod2Flag for United States of America

asked on

Calculate cost of meal

I have a Visual C# application pictured here >> http://screencast.com/t/Tt3D9mqbfsCD that is supposed to calculate the cost of a multi-person meal at a restaurant.  The totals are supposed to accumulate until the program is exited and the resulting value after the last person has been entered should be shown in the overallTotalsTextBox.  The clear should reset the values so that a new meal can be entered but not erase the data stored in the totals field.

What is happening is that the totalTextBox is displaying the overallTotal.

How do I correct this mistake in my code below?
// Purpose: To calculate an Overall Total for the cost of a multi-person 
// meal a restaurant.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _304_PennerT_MockExam1
{
    public partial class mealCalculatorForm : Form
    {
        // Declare Constants
        private decimal SALES_TAX = .07m;
        private decimal TIP_RATE = .15m;

        // Identify fields (same as a variable except at class level with lifetime of application running)
        private decimal overallTotal = 0.0m;

        public mealCalculatorForm()
        {
            InitializeComponent();
        }
        private void knifeforkPictureBox_Click(object sender, EventArgs e)
        {
            try
            {
                // Declare Varialbles
                decimal charge;
                decimal salesTax;
                decimal tip;
                decimal total;

                // Convert Text Box Values (string values) to appropriate data 
                // type using a Parse method and assign to related variable.
                charge = decimal.Parse(chargeTextBox.Text);

                // Process Calculations
                // This is often 4th section following constants, variables, and 
                // conversion of text box values to data type.
                salesTax = charge * SALES_TAX;
                tip = charge * TIP_RATE;
                total = charge + salesTax + tip;

                // Accumulate Total Charges - field declared at class level.
                overallTotal += total;

                // Display Numeric Output to appropriate control by converting 
                // the numeric value to a string value using the ToString method.
                salesTaxTextBox.Text = salesTax.ToString("c");
                tipTextBox.Text = tip.ToString("c");
                totalTextBox.Text = overallTotal.ToString("c");
                overallTotalTextBox.Text = overallTotal.ToString("c");
            }
            catch (Exception ex)
            {
                // Display the default error message and set focus.
                MessageBox.Show(ex.Message);
                
                // Set Focus
                chargeTextBox.Focus();
            }
                    }  
        private void clearButton_Click(object sender, EventArgs e)
        {
            // Clear values from all meals.
            chargeTextBox.Clear();
            salesTaxTextBox.Clear();
            tipTextBox.Clear();
            totalTextBox.Clear();
            overallTotalTextBox.Clear();

            // Set Focus
            chargeTextBox.Focus();
        }
        private void exitButton_Click(object sender, EventArgs e)
        {
            // Exit form application
            this.Close();

            // Set Focus
            chargeTextBox.Focus();
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You must be omitting something.

1. Is there a difference in 'totalTextBox' definition vs. the others?
2. Any other code that runs after ViewState?
3. Any client script?
Glad to help you
Raj
in this part

      private void clearButton_Click(object sender, EventArgs e)
        {
            // Clear values from all meals.
            chargeTextBox.Clear();
            salesTaxTextBox.Clear();
            tipTextBox.Clear();
            totalTextBox.Clear();
            overallTotalTextBox.Clear();

            // Set Focus
            chargeTextBox.Focus();
        }


you should remove the overallTextBox.Clear....  if you dont want it to clear out...

then on this part:

/ Display Numeric Output to appropriate control by converting
                // the numeric value to a string value using the ToString method.
                salesTaxTextBox.Text = salesTax.ToString("c");
                tipTextBox.Text = tip.ToString("c");
                totalTextBox.Text = overallTotal.ToString("c");
                overallTotalTextBox.Text = overallTotal.ToString("c");
            }

on this line

                totalTextBox.Text = overallTotal.ToString("c");

should be instead

                totalTextBox.Text = total.ToString("c");
You are incorrectly setting 'totalTextBox' text, as Raj pointed out.

However, I read your problem as saying that the 'totalTextBox' is displaying overall total after hitting the 'Clear' button.
Clearing the overallTotal textbox won't clear the overallTotal member var, unless you are using some kind of data binding.