Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Create a Formfor a restaurant. Allow the user to choose one
item from at least three options in each of the following
categories—appetizer, entrée, and dessert. Assign a different
price to each selection and display the total when the user
clicks a Button. Use the Controls that you think are best
for each function. Label items appropriately and use fonts
and colors to achieve an attractive design. Save the project
as Restaurant.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication14

{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            {
                if (comboBox3.SelectedIndex == 0)
                {
                    double dessertPrice = 3.00;
                }
                else if (comboBox3.SelectedIndex == 1)
                {
                    double dessertPrice = 2.00;
                }
                else if (comboBox3.SelectedIndex == 2)
                {
                    double dessertPrice = 2.50;
                }
                else
                {
                    double dessertPrice = 2.00;
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex == 0)
{
double entreePrice = 25.00;
}
else if (comboBox2.SelectedIndex == 1)
{
double entreePrice = 15.00;
}
else if (comboBox2.SelectedIndex == 2)
{
double entreePrice = 12.00;
}
else
{
double entreePrice = 20.00;
}
}

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
        if (comboBox1.SelectedIndex == 0)
{
double appetizerPrice = 3.00;
}
else if (comboBox1.SelectedIndex == 1)
{
double appetizerPrice = 5.00;
}
else if (comboBox1.SelectedIndex == 2)
{
double appetizerPrice = 3.00;
}
else
{
double appetizerPrice = 3.00;
}
}

private void button1_Click_1(object sender, EventArgs e)
{
    MessageBox.Show("Your total is {0}.");
}
 }
  }


just help me making this program complete! i don't know what should be the total price ?
Posted
Updated 14-Jul-13 6:10am
v2
Comments
[no name] 14-Jul-13 12:16pm    
Wow... okay I believe that the easiest thing for you to do would be to make your prices class level variables or properties so that they are available to your , I assume it's a Total, button click handler. Re-read your textbook about variable scope as you have made all of your prices local to your SelectedIndexChanged event handlers and once that is finished, you do not have a price anymore.
Mudassar Naeem 14-Jul-13 12:24pm    
I Have used this but it didn't working
double total = Form1.comboBox3.SelectedIndexChanged.dessert + Form1.comboBox2.SelectedIndexChanged.entree + Form1.comboBox1.SelectedIndexChanged.appetizer
[no name] 14-Jul-13 12:31pm    
No and that would never work.

1 solution

Hi
I think your application has some problems:
The first one, you must read price from a table. because price will be change as soon as therefore you must design a form for change that and save that in a table or Xml data file.

The second one, you must implement a way for calculate total price. for this section I have two suggestion:

1- You can use from a Property as TotalPrice and store and read value.
C#
private double DessertPrice { get; set; }
private double EntreePrice { get; set; }
private double AppetizerPrice { get; set; }
private double TotalPrice { get; set; }

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    if (comboBox3.SelectedIndex == 0)
        DessertPrice = 3.00;
    else if (comboBox3.SelectedIndex == 1)
        DessertPrice = 2.00;
    else if (comboBox3.SelectedIndex == 2)
        DessertPrice = 2.50;
    else
        DessertPrice = 2.00;
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox2.SelectedIndex == 0)
        EntreePrice = 25.00;
    else if (comboBox2.SelectedIndex == 1)
        EntreePrice = 15.00;
    else if (comboBox2.SelectedIndex == 2)
        EntreePrice = 12.00;
    else
        EntreePrice = 20.00;
}

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == 0)
        AppetizerPrice = 3.00;
    else if (comboBox1.SelectedIndex == 1)
        AppetizerPrice = 5.00;
    else if (comboBox1.SelectedIndex == 2)
        AppetizerPrice = 3.00;
    else
        AppetizerPrice = 3.00;
}

private void button1_Click_1(object sender, EventArgs e)
{
    TotalPrice = DessertPrice + EntreePrice + AppetizerPrice;

    MessageBox.Show(string.Format("Your total is {0}.", TotalPrice));

    DessertPrice = 0;
    EntreePrice = 0;
    AppetizerPrice = 0;
    TotalPrice = 0;
}


2- You can use from a class for this problem. (I think it is best way)
C#
public class Price
{
    internal double DessertPrice { get; set; }
    internal double EntreePrice { get; set; }
    internal double AppetizerPrice { get; set; }
    internal double TotalPrice { get; set; }

    public Price()
    {
        DessertPrice = 0;
        EntreePrice = 0;
        AppetizerPrice = 0;
        TotalPrice = 0;
    }

    public double CalculateTotalPrice()
    {
        TotalPrice = DessertPrice + EntreePrice + AppetizerPrice;
    }
}

    Price price = new Price();

    private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        if (comboBox3.SelectedIndex == 0)
            price.DessertPrice = 3.00;
        else if (comboBox3.SelectedIndex == 1)
            price.DessertPrice = 2.00;
        else if (comboBox3.SelectedIndex == 2)
            price.DessertPrice = 2.50;
        else
            price.DessertPrice = 2.00;
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox2.SelectedIndex == 0)
            price.EntreePrice = 25.00;
        else if (comboBox2.SelectedIndex == 1)
            price.EntreePrice = 15.00;
        else if (comboBox2.SelectedIndex == 2)
            price.EntreePrice = 12.00;
        else
            price.EntreePrice = 20.00;
    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex == 0)
            price.AppetizerPrice = 3.00;
        else if (comboBox1.SelectedIndex == 1)
            price.AppetizerPrice = 5.00;
        else if (comboBox1.SelectedIndex == 2)
            price.AppetizerPrice = 3.00;
        else
            price.AppetizerPrice = 3.00;

    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        MessageBox.Show(string.Format("Your total is {0}.", price.CalculateTotalPrice()));
    }


I hope it's helpful for you.
 
Share this answer
 
Comments
H.Brydon 14-Jul-13 23:09pm    
I just gave you several +5 votes. Good answers. Welcome to CP.
Reza Alipour Fard 15-Jul-13 13:43pm    
Welcome, I wish you will be success in your life.

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