• 0

Restaurant Cash Register


Question

I have a code and a dll that i made.

It's a windows application of a Restaurant Cash Register

The selected items from each category (listBox1 and listBox3) are copied on listBox2. How can i sum the items on listBox2 and make it display it on the textBox1 below?

Thanks,

****************

This is my DLL:

using System;

namespace ClassLibrary1
{
/// <summary>
/// Food class that holds the name and the price of a single food item.
/// Regular Menu class and Kid Menu Class that inherits from the Food class.
/// Choices class which contain an array of Regular Menu and/or Kid Menu objects.
/// </summary>

// parent class
public class Food
{ // starts class food

public string name;
public double price;

public Food() // default constructor - initializes name and price to its lowest values.
{
name = " ";
price = 0.00;
}

public Food( string iName, double iPrice) // constructor takes two arguments of type string & double
{
name = iName;
price = iPrice;
}

public string iName // functions set and get for iName
{
get
{
return name;
}
set
{
name = value;
}
}

public double iPrice // functions set and get for iPrice
{
get
{
return price;
}
set
{
price = value;
}
}

} //ends class Food

public class RegularMenu : Food // starts class RegularMenu which inherits from class Food
{

public RegularMenu(string iName, double iPrice) // constructor takes two arguments of type string & double
{
name = iName;
price = iPrice;
}

} // ends class RegularMenu


public class KidMenu : Food // starts class KidMeni which inherits from class Food
{
public KidMenu(string iName, double iPrice) // constructor takes two arguments of type string & double
{
name= iName;
price = iPrice;

}

} // ends class KidMenu


public class Selection // starts class Selection
{
// arrays of objects of RegularMenu and KidMenu
private RegularMenu[] rmenu = new RegularMenu[50];
private KidMenu[] kmenu = new KidMenu[50];

private int sizeRegmenu =0;
private int sizeKidmenu =0;

public int sizereg // get/set functions
{
get
{
return sizeRegmenu;
}
}

public int sizekid
{
get
{
return sizeKidmenu;
}
}


public Selection()
{
rmenu[0]= new RegularMenu("pizza slice", 2.50);
rmenu[1]= new RegularMenu("hamburguer", 1.75);
rmenu[2]= new RegularMenu("salad", 1.90);
rmenu[3]= new RegularMenu("large soda", 1.20);
rmenu[4]= new RegularMenu("medium soda", 0.90);

sizeRegmenu = 5;

kmenu[0]=new KidMenu("hot dog", 1.50);
kmenu[1]= new KidMenu("small soda", 0.60);
kmenu[2]= new KidMenu("cookie", 0.86);

sizeKidmenu =3;

}

public void add_rmenu( RegularMenu m)
{
rmenu[++ sizeRegmenu] = m;
}

public void add_kid(KidMenu m)
{
kid[++sizeKidmenu] = m;
}


}

}

------------------------

This is the code right now but it gives me some errors. I think i should put the class Selections (That's on the DLL) on the code, but im not sure and i did it and it still gave me some errors.. Can someone help me?:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ClassLibrary3; // dll created

namespace WindowsApplication1
{
/// <summary>
/// Program that will allow to pick several items from two different menus; RegularMenu
/// and KidMenu and will prepare a Custom Order for the customer.
/// At the end, it will display the total to be paid.
/// It will also allow the user to clear the form and/or click the Quit button and decide
/// what to do with the order.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.ListBox listBox3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.PictureBox pictureBox1;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.listBox3 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label5 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label7);
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.button4);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.listBox3);
this.groupBox1.Controls.Add(this.listBox2);
this.groupBox1.Controls.Add(this.listBox1);
this.groupBox1.Location = new System.Drawing.Point(16, 104);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(632, 312);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// label7
//
this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label7.Location = new System.Drawing.Point(272, 288);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(88, 16);
this.label7.TabIndex = 13;
this.label7.Text = "Staff: Mar-Iam ";
//
// label6
//
this.label6.BackColor = System.Drawing.SystemColors.Window;
this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label6.ImageAlign = System.Drawing.ContentAlignment.TopLeft;
this.label6.Location = new System.Drawing.Point(256, 72);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(104, 16);
this.label6.TabIndex = 12;
this.label6.Text = "Description Price";
this.label6.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.Control;
this.textBox1.Location = new System.Drawing.Point(536, 264);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(80, 20);
this.textBox1.TabIndex = 11;
this.textBox1.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(472, 264);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 23);
this.label4.TabIndex = 10;
this.label4.Text = "Total:";
//
// button4
//
this.button4.Location = new System.Drawing.Point(112, 264);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 24);
this.button4.TabIndex = 9;
this.button4.Text = "Quit";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(24, 264);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 24);
this.button3.TabIndex = 8;
this.button3.Text = "Clear";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(400, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 7;
this.button2.Text = "<<";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(168, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 24);
this.button1.TabIndex = 6;
this.button1.Text = ">>";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label3
//
this.label3.Font = new System.Drawing.Font("Eras Light ITC", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.Location = new System.Drawing.Point(480, 24);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(136, 23);
this.label3.TabIndex = 5;
this.label3.Text = "Kids Menu";
this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label2
//
this.label2.Font = new System.Drawing.Font("Eras Light ITC", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(248, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(136, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Custom Order";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label1
//
this.label1.Font = new System.Drawing.Font("Eras Light ITC", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 23);
this.label1.TabIndex = 3;
this.label1.Text = "Regular Menu";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// listBox3
//
this.listBox3.Items.AddRange(new object[] {
"hot dog ($1.50)",
"small soda ($0.60)",
"cookie ($0.86)"});
this.listBox3.Location = new System.Drawing.Point(480, 64);
this.listBox3.Name = "listBox3";
this.listBox3.Size = new System.Drawing.Size(136, 160);
this.listBox3.TabIndex = 2;
//
// listBox2
//
this.listBox2.Items.AddRange(new object[] {
"",
"",
""});
this.listBox2.Location = new System.Drawing.Point(248, 64);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(136, 160);
this.listBox2.TabIndex = 1;
//
// listBox1
//
this.listBox1.Items.AddRange(new object[] {
"pizza slice ($2.50)",
"burger ($1.75)",
"salad ($1.90)",
"large soda ($1.20)",
"medium soda ($0.90)"});
this.listBox1.Location = new System.Drawing.Point(16, 64);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(136, 160);
this.listBox1.TabIndex = 0;
//
// label5
//
this.label5.Font = new System.Drawing.Font("Edwardian Script ITC", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label5.Location = new System.Drawing.Point(176, 8);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(344, 48);
this.label5.TabIndex = 1;
this.label5.Text = "Restaurant Cash Register";
this.label5.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(264, 56);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(200, 40);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(664, 430);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label5);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

// >> add button from Regular Menu
private void button1_Click(object sender, System.EventArgs e)
{
listBox2.Items.Add (listBox1.SelectedItem);
double total = 0.00;

for (int r=0; r<5; r++)
{
if (listBox1.SelectedItems.Equals(RegularMenu[r].getName()))
{
total += RegularMenu[r].getPrice();
textBox1.Text = String.Format("${0:0.00}", total);
}
} // ends for loop
}

// << add button from Kids Menu
private void button2_Click(object sender, System.EventArgs e)
{
listBox2.Items.Add (listBox3.SelectedItem);
double total = 0.00;

for (int r=0; r<3; r++)
{
if (listBox1.SelectedItems.Equals(KidMenu[r].getName()))
{
total += KidMenu[r].getPrice();
textBox1.Text = String.Format("${0:0.00}", total);
}
}
}


// clears all selected items from the Custom Menu
private void button3_Click(object sender, System.EventArgs e)
{
listBox2.Items.Clear();
}

// quit
private void button4_Click(object sender, System.EventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure you want to quit your order?", "Quit Order",
MessageBoxButtons.YesNo,MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
switch (res)
{
case DialogResult.Yes:
// application will exit
listBox2.Items.Clear();
textBox1.Clear();
break;

case DialogResult.No:
// will return to the main application
return;

}

}
/* WHAT I TRIED AT FIRST TO CALCULATE THE SUM
*
public int getPrice( ListBox aList )
{

int startIndex = 0;
int endIndex = 0;
string subString;

startIndex = aList.Text.LastIndexOf("$"); // give me the index at char "$"
endIndex = aList.Text.LastIndexOf("]"); // give me the index at char "]"

subString = aList.Text.Substring(startIndex + 1, (endIndex - 1) - startIndex);

return Convert.ToInt16(subString);
}


public void calculatePrice()
{ // begins calculatePrice

//listBox2.Items.Add (listBox3.SelectedItem);
double totalPrice = 0.00;

totalPrice += getPrice(listBox2);

textBox1.Text = totalPrice.ToString();

} // end calculatePrice

*/




}

}

Edited by marie
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

create a new class for the item so you can have it as an object, then override the to string to display the text as you want it... but to sum it go through each object in the one you want to sum call its price and add that up

this is VB.NET code

Public Class FoodItem

private name as string = string.empty

private price as decimal = 0d

public sub new(byval ItemName as string, byval Price as decimal)

price = Price

name = ItemName

end sub

Public Function Overrides ToString() as string

' this will be displayed when added to items collection of list box

return ItemName & " " & price

end function

public property Price as decimal

get

return price

end get

end property

end class

then make a new object for each item of that class type

me.listbox1.add(new FoodItem("Hamburger","1.99")

me.listbox1.add(new FoodItem("Cheeseburger","2.30")

to sum it

dim totalPrice as decimal = 0d

for each FoodItem in me.listbox1.items

totalPrice += FoodItem.price

next

that is VB.NET code, but C# is the same way just change the syntax

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.