63 169
Assignments Done
99,3%
Successfully Done
In July 2018

Answer to Question #4497 in C# for Evan

Question #4497
I have to write a program that calculates restaurant bill. It has four comboBoxes, when an item is selected or added from the comboBoxed, subtotal, tax and total should be displayed. I have the comboboxes ready with the items added to them but how do I calculate amount for items selected and dislply them; thanks in advance for your help.
Expert's answer
RestaurantBill.cs

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 Bill
{
public partial class RestaurantBill : Form
{
public RestaurantBill()
{
InitializeComponent();
}

Dictionary<string, float> prices;

/* Is callen when the form is displayed for the first time */
private void Form_Load(object sender, EventArgs e)
{
/* Filling up comboboxes */
Beverage.Items.AddRange(new string[] { "Coffee", "Tea", "Juice" });
Appetizers.Items.AddRange(new string[] { "Pepperoni Bread", "Chicken Quesadilla", "Potato Skins", "Battered Mushrooms" });
Maincourses.Items.AddRange(new string[] { "Vegertarian Linguini", "Sirloin Steak", "Fillet of Venison", "Chicken Salad on Croissant", "Spaghetti with Fresh Spinach" });
Desserts.Items.AddRange(new string[] { "Berry Crumble", "Coconut-cream pie", "Banana Split" });

/* Setting up foods' prices */
prices = new Dictionary<string, float>();
prices["Coffee"] = 4.55f;
prices["Tea"] = 3.15f;
prices["Juice"] = 6.40f;

prices["Pepperoni Bread"] = 19.0f;
prices["Chicken Quesadilla"] = 2.92f;
prices["Potato Skins"] = 6.10f;
prices["Battered Mushrooms"] = 4.85f;

prices["Vegertarian Linguini"] = 15.55f;
prices["Sirloin Steak"] = 23.20f;
prices["Fillet of Venison"] = 40.90f;
prices["Chicken Salad on Croissant"] = 9.31f;
prices["Spaghetti with Fresh Spinach"] = 12.70f;

prices["Berry Crumble"] = 6.40f;
prices["Coconut-cream pie"] = 7.88f;
prices["Banana Split"] = 5.06f;
}

private void Calculate_Click(object sender, EventArgs e)
{
float subtotal = 0;
float tax;
float total;

/* Summing ordered foods prices */
foreach (string food in OrderedFoods.Items)
subtotal += prices[food];

tax = subtotal * 0.05f; //tax percent (0.05) could be different
total = subtotal + tax;

LSubtotal.Text = "Subtotal: " + subtotal.ToString();
LTax.Text = "Tax: " + tax.ToString();
LTotal.Text = "Total: " + total.ToString();
}

private void Clear_Click(object sender, EventArgs e)
{
OrderedFoods.Items.Clear();
LSubtotal.Text = "Subtotal: ";
LTax.Text = "Tax: ";
LTotal.Text = "Total: ";
}

private void BAdd_Click(object sender, EventArgs e)
{
OrderedFoods.Items.Add(Beverage.SelectedItem);
}

private void AAdd_Click(object sender, EventArgs e)
{
OrderedFoods.Items.Add(Appetizers.SelectedItem);
}

private void MAdd_Click(object sender, EventArgs e)
{
OrderedFoods.Items.Add(Maincourses.SelectedItem);
}

private void DAdd_Click(object sender, EventArgs e)
{
OrderedFoods.Items.Add(Desserts.SelectedItem);
}
}
}


Restaurant Bill.Designer.cs
namespace Bill
{
partial class RestaurantBill
{
private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.Beverage = new System.Windows.Forms.ComboBox();
this.Appetizers = new System.Windows.Forms.ComboBox();
this.Desserts = new System.Windows.Forms.ComboBox();
this.Maincourses = new System.Windows.Forms.ComboBox();
this.Calculate = new System.Windows.Forms.Button();
this.LSubtotal = new System.Windows.Forms.Label();
this.LTax = new System.Windows.Forms.Label();
this.LTotal = new System.Windows.Forms.Label();
this.OrderedFoods = new System.Windows.Forms.ListBox();
this.BAdd = new System.Windows.Forms.Button();
this.AAdd = new System.Windows.Forms.Button();
this.MAdd = new System.Windows.Forms.Button();
this.DAdd = new System.Windows.Forms.Button();
this.Clear = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Beverage
//
this.Beverage.FormattingEnabled = true;
this.Beverage.Location = new System.Drawing.Point(12, 12);
this.Beverage.Name = "Beverage";
this.Beverage.Size = new System.Drawing.Size(121, 21);
this.Beverage.TabIndex = 0;
//
// Appetizers
//
this.Appetizers.FormattingEnabled = true;
this.Appetizers.Location = new System.Drawing.Point(12, 39);
this.Appetizers.Name = "Appetizers";
this.Appetizers.Size = new System.Drawing.Size(121, 21);
this.Appetizers.TabIndex = 1;
//
// Desserts
//
this.Desserts.FormattingEnabled = true;
this.Desserts.Location = new System.Drawing.Point(12, 93);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions