A poinit of sale is the workstation where customers
purchases are handled. Point of sales used to be a big issue. You had to
purchase special machines and have a special setup. Although you still need
setup, you can build the whole thing yourself (but we will not discuss all of
that this time).
In our application, when a customer brings one or more items
to check out, the cashier (or employee) will enter each item number in a text
box and press enter. This will display information about the item, including the
discount if any. The employee will do this for all items brought by the
customer. The employee will also have the ability to remove an item. When all
items have been entered, the employee can save the order and start another order
or purchase.
Practical
Learning: Creating a Point of Sale Tool
|
|
- In the empty document, type the following code:
using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
public class NewShoppingSession : Form
{
private Label lblItemNumberSelected;
private TextBox txtItemNumberSelected;
private Label lblReceiptNumber;
private TextBox txtReceiptNumber;
private ColumnHeader colItemNumber;
private ColumnHeader colItemName;
private ColumnHeader colItemSize;
private ColumnHeader colUnitPrice;
private ColumnHeader colDiscountRate;
private ColumnHeader colDiscountAmount;
private ColumnHeader colSalePrice;
private ListView lvwSoldItems;
private TextBox txtEmployeeNumber;
private Label lblEmployeeNumber;
private DateTimePicker dtpSaleDate;
private Label lblSaleTime;
private TextBox txtAmountTendered;
private Label lblTendered;
private TextBox txtChange;
private Label lblChange;
private TextBox txtOrderTotal;
private Label lblOrderTotal;
private TextBox txtEmployeeName;
private Button btnSubmit;
private Button btnReset;
private TextBox txtItemNumberRemove;
private Label lblItemNumberToRemove;
private Button btnRemoveItem;
private Button btnClose;
private Timer tmrDateTime;
public NewShoppingSession()
{
InitializeComponent();
}
private void InitializeComponent()
{
// Label: Item Number to Add
lblItemNumberSelected = new Label();
lblItemNumberSelected.AutoSize = true;
lblItemNumberSelected.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblItemNumberSelected.Location = new Point(16, 22);
lblItemNumberSelected.TabIndex = 2;
lblItemNumberSelected.Text = "Item # to Add:";
// Text Box: Item Number to Add
txtItemNumberSelected = new TextBox();
txtItemNumberSelected.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtItemNumberSelected.Location = new Point(235, 18);
txtItemNumberSelected.Size = new System.Drawing.Size(155, 41);
txtItemNumberSelected.TabIndex = 3;
txtItemNumberSelected.KeyUp += new KeyEventHandler(txtItemNumberKeyUp);
// Label: Receipt Number
lblReceiptNumber = new Label();
lblReceiptNumber.AutoSize = true;
lblReceiptNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblReceiptNumber.Location = new Point(1081, 22);
lblReceiptNumber.TabIndex = 5;
lblReceiptNumber.Text = "Receipt #:";
Controls.Add(lblReceiptNumber);
// Text Box: Receipt Number
txtReceiptNumber = new TextBox();
txtReceiptNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtReceiptNumber.Location = new Point(1260, 18);
txtReceiptNumber.Size = new System.Drawing.Size(105, 41);
txtReceiptNumber.TabIndex = 6;
txtReceiptNumber.Text = "000000";
Controls.Add(txtReceiptNumber);
// Column: umn: Item Number
colItemNumber = new ColumnHeader();
colItemNumber.Text = "Item #";
colItemNumber.Width = 100;
// Column: umn: Item Name
colItemName = new ColumnHeader();
colItemName.Text = "Item Name/Description";
colItemName.Width = 610;
// Column: umn: Item Size
colItemSize = new ColumnHeader();
colItemSize.Text = "Size";
colItemSize.Width = 150;
// Column: umn: Unit Price
colUnitPrice = new ColumnHeader();
colUnitPrice.Text = "Unit Price";
colUnitPrice.TextAlign = HorizontalAlignment.Right;
colUnitPrice.Width = 140;
// Column: umn: Discount Rate
colDiscountRate = new ColumnHeader();
colDiscountRate.Text = "Dscnt Rt";
colDiscountRate.TextAlign = HorizontalAlignment.Right;
colDiscountRate.Width = 120;
// Column: umn: Discount Amount
colDiscountAmount = new ColumnHeader();
colDiscountAmount.Text = "Dscnt Amt";
colDiscountAmount.TextAlign = HorizontalAlignment.Right;
colDiscountAmount.Width = 140;
// Column: umn: Sale Price
colSalePrice = new ColumnHeader();
colSalePrice.Text = "Sale Price";
colSalePrice.TextAlign = HorizontalAlignment.Right;
colSalePrice.Width = 130;
// List View: Selected Items
lvwSoldItems = new ListView();
lvwSoldItems.Columns.AddRange(new ColumnHeader[]
{
colItemNumber, colItemName, colItemSize, colUnitPrice,
colDiscountRate, colDiscountAmount, colSalePrice
});
lvwSoldItems.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lvwSoldItems.FullRowSelect = true;
lvwSoldItems.GridLines = true;
lvwSoldItems.Location = new Point(16, 71);
lvwSoldItems.Size = new System.Drawing.Size(1424, 321);
lvwSoldItems.TabIndex = 8;
lvwSoldItems.View = View.Details;
// Date/Time Picker: Sale Date
dtpSaleDate = new DateTimePicker();
dtpSaleDate.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
dtpSaleDate.Location = new Point(16, 469);
dtpSaleDate.Size = new System.Drawing.Size(402, 39);
dtpSaleDate.TabIndex = 9;
lblSaleTime = new Label();
lblSaleTime.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblSaleTime.Location = new Point(429, 469);
lblSaleTime.Text = "Sale Time";
lblSaleTime.Size = new System.Drawing.Size(177, 39);
lblSaleTime.TabIndex = 11;
// Label: Tendered
lblTendered = new Label();
lblTendered.AutoSize = true;
lblTendered.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblTendered.Location = new Point(1089, 449);
lblTendered.TabIndex = 15;
lblTendered.Text = "Tendered:";
// Text Box: Amount Tendered
txtAmountTendered = new TextBox();
txtAmountTendered.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtAmountTendered.Location = new Point(1278, 445);
txtAmountTendered.Size = new System.Drawing.Size(130, 41);
txtAmountTendered.TabIndex = 14;
txtAmountTendered.Text = "0.00";
txtAmountTendered.TextAlign = HorizontalAlignment.Right;
txtAmountTendered.KeyUp += new KeyEventHandler(txtAmountTenderedKeyUp);
txtAmountTendered.Leave += new EventHandler(txtAmountTenderedLeave);
// Label: Item Number to Remove
lblItemNumberToRemove = new Label();
lblItemNumberToRemove.AutoSize = true;
lblItemNumberToRemove.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblItemNumberToRemove.Location = new Point(16, 416);
lblItemNumberToRemove.TabIndex = 18;
lblItemNumberToRemove.Text = "Item # to Remove:";
// Text Box: Item Number to Remove
txtItemNumberRemove = new TextBox();
txtItemNumberRemove.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtItemNumberRemove.Location = new Point(290, 412);
txtItemNumberRemove.Size = new System.Drawing.Size(155, 41);
txtItemNumberRemove.TabIndex = 19;
// Button: Remove Item
btnRemoveItem = new Button();
btnRemoveItem.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnRemoveItem.Location = new Point(451, 408);
btnRemoveItem.Size = new System.Drawing.Size(155, 46);
btnRemoveItem.TabIndex = 20;
btnRemoveItem.Text = "Remove";
btnRemoveItem.Click += new System.EventHandler(btnRemoveItemClick);
// Label: Order Total
lblOrderTotal = new Label();
lblOrderTotal.AutoSize = true;
lblOrderTotal.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblOrderTotal.Location = new Point(1089, 401);
lblOrderTotal.TabIndex = 12;
lblOrderTotal.Text = "Order Total:";
txtEmployeeNumber = new TextBox();
txtEmployeeName = new TextBox();
btnSubmit = new Button();
btnReset = new Button();
btnClose = new Button();
// Text Box: Order Total
txtOrderTotal = new TextBox();
txtOrderTotal.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtOrderTotal.Location = new Point(1278, 396);
txtOrderTotal.Size = new System.Drawing.Size(130, 41);
txtOrderTotal.TabIndex = 13;
txtOrderTotal.Text = "0.00";
txtOrderTotal.TextAlign = HorizontalAlignment.Right;
// Label: Change
lblChange = new Label();
lblChange.AutoSize = true;
lblChange.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblChange.Location = new Point(1089, 496);
lblChange.TabIndex = 16;
lblChange.Text = "Change:";
// Text Box: Change
txtChange = new TextBox();
txtChange.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtChange.Location = new Point(1278, 492);
txtChange.Size = new System.Drawing.Size(129, 41);
txtChange.TabIndex = 17;
txtChange.Text = "0.00";
txtChange.TextAlign = HorizontalAlignment.Right;
// Label: Employee Number
lblEmployeeNumber = new Label();
lblEmployeeNumber.AutoSize = true;
lblEmployeeNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblEmployeeNumber.Location = new Point(10, 558);
lblEmployeeNumber.TabIndex = 0;
lblEmployeeNumber.Text = "Employee #:";
// Text Box: Employee Number
txtEmployeeNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtEmployeeNumber.Location = new Point(206, 554);
txtEmployeeNumber.Size = new System.Drawing.Size(155, 41);
txtEmployeeNumber.TabIndex = 1;
txtEmployeeNumber.Leave += new System.EventHandler(txtEmployeeNumberLeave);
// Text Box: Employee Name
txtEmployeeName.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtEmployeeName.Location = new Point(370, 554);
txtEmployeeName.Size = new System.Drawing.Size(499, 41);
txtEmployeeName.TabIndex = 7;
// Button: Reset
btnReset.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnReset.Location = new Point(923, 551);
btnReset.Size = new System.Drawing.Size(168, 46);
btnReset.TabIndex = 10;
btnReset.Text = "Reset";
btnReset.Click += new System.EventHandler(btnResetClick);
// Button: Submit
btnSubmit.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnSubmit.Location = new Point(1097, 551);
btnSubmit.Size = new System.Drawing.Size(177, 46);
btnSubmit.TabIndex = 4;
btnSubmit.Text = "Submit";
btnSubmit.Click += new System.EventHandler(btnSubmitClick);
// Button: Close
btnClose.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnClose.Location = new Point(1281, 551);
btnClose.Size = new System.Drawing.Size(129, 46);
btnClose.TabIndex = 21;
btnClose.Text = "Close";
btnClose.Click += new System.EventHandler(btnCloseClick);
tmrDateTime = new Timer();
tmrDateTime.Enabled = true;
tmrDateTime.Tick += new EventHandler(tmrDateTimeTick);
ClientSize = new System.Drawing.Size(1454, 611);
Controls.Add(btnClose);
Controls.Add(btnRemoveItem);
Controls.Add(txtItemNumberRemove);
Controls.Add(lblItemNumberToRemove);
Controls.Add(btnReset);
Controls.Add(btnSubmit);
Controls.Add(txtEmployeeName);
Controls.Add(txtOrderTotal);
Controls.Add(lblOrderTotal);
Controls.Add(txtChange);
Controls.Add(lblChange);
Controls.Add(txtAmountTendered);
Controls.Add(lblTendered);
Controls.Add(lblSaleTime);
Controls.Add(dtpSaleDate);
Controls.Add(txtEmployeeNumber);
Controls.Add(lblEmployeeNumber);
Controls.Add(lvwSoldItems);
Controls.Add(txtItemNumberSelected);
Controls.Add(lblItemNumberSelected);
MaximizeBox = false;
MinimizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "Fun Department Store - New Shopping Session";
Load += new System.EventHandler(ShoppingSessionLoad);
}
private void txtItemNumberKeyUp(object sender, KeyEventArgs e)
{
bool itemFound = false;
double dblTotal = 0.00;
XmlDocument xdStoreItems = new XmlDocument();
string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
if (e.KeyCode == Keys.Enter)
{
if (string.IsNullOrEmpty(txtItemNumberSelected.Text))
{
MessageBox.Show("You must enter an item number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
else
{
if (File.Exists(strStoreItemsFile))
{
using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
{
xdStoreItems.Load(fsStoreItems);
string itemNumber, itemName, itemSize, strDiscountRate;
double unitPrice, discountRate, discountAmount, salePrice;
XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
foreach (XmlNode xnStoreItem in xnlStoreItems)
{
if (xnStoreItem.InnerText == txtItemNumberSelected.Text)
{
itemFound = true;
itemNumber = xnStoreItem.InnerText;
itemName = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
itemSize = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
unitPrice = double.Parse(xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
strDiscountRate = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
if ((string.IsNullOrEmpty(strDiscountRate)) || (double.Parse(strDiscountRate) <= 0.00))
{
discountRate = 0.00;
discountAmount = 0.00;
salePrice = unitPrice;
}
else
{
discountRate = double.Parse(strDiscountRate) / 100.00;
discountAmount = unitPrice * discountRate;
salePrice = unitPrice - discountAmount;
}
ListViewItem lviStoreItem = new ListViewItem(itemNumber); // Item Number
lviStoreItem.SubItems.Add(itemName); // Item Name
lviStoreItem.SubItems.Add(itemSize); // Item Size
lviStoreItem.SubItems.Add(unitPrice.ToString("F"));
if (discountRate == 0.00)
{
lviStoreItem.SubItems.Add(""); // Discount Rate
lviStoreItem.SubItems.Add(""); // Discount Amount
lviStoreItem.SubItems.Add(unitPrice.ToString("F")); // Unit Price
}
else
{
lviStoreItem.SubItems.Add((discountRate * 100).ToString() + "%");
lviStoreItem.SubItems.Add(discountAmount.ToString("F"));
lviStoreItem.SubItems.Add(salePrice.ToString("F"));
}
lvwSoldItems.Items.Add(lviStoreItem);
}
}
txtItemNumberSelected.Text = "";
if (itemFound == false)
{
MessageBox.Show("There is no item with that item number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
if (lvwSoldItems.Items.Count > 0)
{
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
txtOrderTotal.Text = dblTotal.ToString("F");
}
}
}
}
}
private void ShoppingSessionLoad(object sender, EventArgs e)
{
btnResetClick(sender, e);
}
private void txtEmployeeNumberLeave(object sender, EventArgs e)
{
bool employeeFound = false;
XmlDocument xdEmployees = new XmlDocument();
string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
{
return;
}
else
{
if (File.Exists(strEmployeesFile))
{
using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
{
xdEmployees.Load(fsEmployees);
XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
foreach (XmlNode xnEmployee in xnlEmployees)
{
if (txtEmployeeNumber.Text == xnEmployee.InnerText)
{
employeeFound = true;
txtEmployeeName.Text = string.Concat(xnEmployee.NextSibling.NextSibling.InnerText, ", ", xnEmployee.NextSibling.InnerText);
}
}
if (employeeFound == false)
{
MessageBox.Show("There is no employee with that number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
}
private void txtAmountTenderedKeyUp(object sender, KeyEventArgs e)
{
double orderTotal = 0.00;
double amountTendered = 0.00;
double change = 0.00;
if ((e.KeyCode == Keys.Tab) || (e.KeyCode == Keys.Enter))
{
if (string.IsNullOrEmpty(txtOrderTotal.Text))
{
return;
}
else if (string.IsNullOrEmpty(txtAmountTendered.Text))
{
return;
}
else
{
orderTotal = double.Parse(txtOrderTotal.Text);
amountTendered = double.Parse(txtAmountTendered.Text);
change = amountTendered - orderTotal;
txtChange.Text = change.ToString("F");
}
}
}
private void txtAmountTenderedLeave(object sender, EventArgs e)
{
double orderTotal = 0.00;
double amountTendered = 0.00;
double change = 0.00;
if (string.IsNullOrEmpty(txtOrderTotal.Text))
{
return;
}
else if (string.IsNullOrEmpty(txtAmountTendered.Text))
{
return;
}
else
{
orderTotal = double.Parse(txtOrderTotal.Text);
amountTendered = double.Parse(txtAmountTendered.Text);
change = amountTendered - orderTotal;
txtChange.Text = change.ToString("F");
}
}
private void btnRemoveItemClick(object sender, EventArgs e)
{
double dblTotal = 0.00;
bool itemFound = false;
if (lvwSoldItems.Items.Count == 0)
{
MessageBox.Show("The list view is empty.",
"FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (string.IsNullOrEmpty(txtItemNumberRemove.Text))
{
MessageBox.Show("You must enter an item number and that exists in the list view.",
"FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
{
if (lviStoreItem.SubItems[0].Text.Equals(txtItemNumberRemove.Text))
{
itemFound = true;
lvwSoldItems.Items.Remove(lviStoreItem);
}
}
if (itemFound == false)
{
MessageBox.Show("That item number is not in the list view.",
"FunDS", MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
if (lvwSoldItems.Items.Count > 0)
{
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
txtOrderTotal.Text = dblTotal.ToString("F");
txtAmountTendered.Text = "0.00";
txtChange.Text = "0.00";
}
else
{
txtOrderTotal.Text = "0.00";
txtAmountTendered.Text = "0.00";
txtChange.Text = "0.00";
}
txtItemNumberRemove.Text = "";
}
}
private void btnResetClick(object sender, EventArgs e)
{
int iReceiptNumber = 100000;
XmlDocument xdStoreSales = new XmlDocument();
string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
if (File.Exists(strStoreSalesFile))
{
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
{
xdStoreSales.Load(fsStoreSales);
XmlNodeList xnlStoreSales = xdStoreSales.GetElementsByTagName("ReceiptNumber");
foreach (XmlNode xnStoreSale in xnlStoreSales)
{
iReceiptNumber = int.Parse(xnStoreSale.InnerText);
}
}
}
txtReceiptNumber.Text = (iReceiptNumber + 1).ToString();
txtItemNumberSelected.Text = "";
lvwSoldItems.Items.Clear();
txtItemNumberRemove.Text = "";
dtpSaleDate.Value = DateTime.Now;
lblSaleTime.Text = DateTime.Now.ToString();
txtEmployeeNumber.Text = ""; ;
txtEmployeeName.Text = "";
txtOrderTotal.Text = "0.00";
txtAmountTendered.Text = "0.00";
txtChange.Text = "0.00";
}
private void btnSubmitClick(object sender, EventArgs e)
{
int iSoldItemID = 0;
XmlDocument xdSoldItems = new XmlDocument();
XmlDocument xdStoreSales = new XmlDocument();
XmlDocument xdStoreItems = new XmlDocument();
string strSoldItemsFile = @"C:\Fun Department Store\SoldItems.xml";
string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
string strStoreItemsFile = @"C:\Fun Department Store\StoreItems1.xml";
if (lvwSoldItems.Items.Count == 0)
{
MessageBox.Show("There is no customer order to save.",
"FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (!File.Exists(strStoreSalesFile))
{
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
{
xdStoreSales.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<StoreSales></StoreSales>");
xdStoreSales.Save(fsStoreSales);
}
}
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
{
xdStoreSales.Load(fsStoreSales);
}
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
{
XmlElement xeStoreSale = xdStoreSales.CreateElement("StoreSale");
xeStoreSale.InnerXml = "<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
"<EmployeeNumber>" + txtEmployeeNumber.Text + "</EmployeeNumber>" +
"<SaleDate>" + dtpSaleDate.Value.ToShortDateString() + "</SaleDate>" +
"<SaleTime>" + lblSaleTime.Text + "</SaleTime>" +
"<OrderTotal>" + txtOrderTotal.Text + "</OrderTotal>" +
"<AmountTendered>" + txtAmountTendered.Text + "</AmountTendered>" +
"<Change>" + txtChange.Text + "</Change>";
xdStoreSales.DocumentElement.AppendChild(xeStoreSale);
xdStoreSales.Save(fsStoreSales);
}
if (!File.Exists(strSoldItemsFile))
{
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
{
xdSoldItems.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SoldItems></SoldItems>");
xdSoldItems.Save(fsSoldItems);
}
}
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Open, FileAccess.Read))
{
xdSoldItems.Load(fsSoldItems);
XmlNodeList xnlSoldItems = xdSoldItems.GetElementsByTagName("SoldItemID");
foreach (XmlNode xnSoldItem in xnlSoldItems)
iSoldItemID = int.Parse(xnSoldItem.InnerText);
}
iSoldItemID = iSoldItemID + 1;
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
{
foreach (ListViewItem lviSoldItem in lvwSoldItems.Items)
{
XmlElement xeSoldItem = xdSoldItems.CreateElement("SoldItem");
xeSoldItem.InnerXml = "<SoldItemID>" + iSoldItemID.ToString() + "</SoldItemID>" +
"<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
"<ItemNumber>" + lviSoldItem.SubItems[0].Text + "</ItemNumber>" +
"<ItemName>" + lviSoldItem.SubItems[1].Text + "</ItemName>" +
"<ItemSize>" + lviSoldItem.SubItems[2].Text + "</ItemSize>" +
"<UnitPrice>" + lviSoldItem.SubItems[3].Text + "</UnitPrice>" +
"<DiscountRate>" + lviSoldItem.SubItems[4].Text + "</DiscountRate>" +
"<DiscountAmount>" + lviSoldItem.SubItems[5].Text + "</DiscountAmount>" +
"<SalePrice>" + lviSoldItem.SubItems[6].Text + "</SalePrice>";
xdSoldItems.DocumentElement.AppendChild(xeSoldItem);
iSoldItemID++;
}
xdSoldItems.Save(fsSoldItems);
MessageBox.Show("The customer's order has been saved.",
"FunDS", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
if (File.Exists(strStoreItemsFile))
{
using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
{
xdStoreItems.Load(fsStoreItems);
XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
// Remove the items of the list view from the StoreItems table
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
{
foreach (XmlNode xnStoreItem in xnlStoreItems)
{
if (xnStoreItem.InnerText == lviStoreItem.SubItems[0].Text)
xdStoreItems.DocumentElement.RemoveChild(xnStoreItem.ParentNode);
}
}
}
using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
{
xdStoreItems.Save(fsStoreItems);
}
}
btnResetClick(sender, e);
}
private void btnCloseClick(object sender, EventArgs e)
{
Close();
}
private void tmrDateTimeTick(object sender, EventArgs e)
{
dtpSaleDate.Value = DateTime.Today;
lblSaleTime.Text = DateTime.Now.ToLongTimeString();
}
}
- On the main menu, click File -> New
- When asked whether you want to save, click Save
- In the top
combo box, make sure the FunDS1 folder is displaying, Set the Save As Type
to All Files
- Set the File Name to PointOfSale.cs
- Click Save
Reviewing a Shopping Session
|
|
Some time to time, the company management must review store
sales. Also, a customer may come with a complaint about anything. The first
thing to do is to review a customer order. To handle this, we will create a form
in which a user can enter a receipt number and click a button to locate its
record. If the purchase is found, the form will display (that's all we will do
at this time; otherwise, there are many actions that can be taken such as
deleting an items from the receipt (and re-imbursing the customer) and putting
the information about the returned item to another list, etc).
Practical
Learning: Reviewing a Shopping Session
|
|
- In the empty document, type the following:
using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
public class ShoppingSessionReview : Form
{
private Label lblItemNumberSelected;
private TextBox txtItemNumberSelected;
private Label lblReceiptNumber;
private TextBox txtReceiptNumber;
private Button btnFind;
private ColumnHeader colItemNumber;
private ColumnHeader colItemName;
private ColumnHeader colItemSize;
private ColumnHeader colUnitPrice;
private ColumnHeader colDiscountRate;
private ColumnHeader colDiscountAmount;
private ColumnHeader colSalePrice;
private ListView lvwSoldItems;
private TextBox txtEmployeeNumber;
private Label lblEmployeeNumber;
private DateTimePicker dtpSaleDate;
private Label lblSaleTime;
private TextBox txtAmountTendered;
private Label lblTendered;
private TextBox txtChange;
private Label lblChange;
private TextBox txtOrderTotal;
private Label lblOrderTotal;
private TextBox txtEmployeeName;
private Button btnSubmit;
private TextBox txtItemNumberRemove;
private Label lblItemNumberToRemove;
private Button btnRemoveItem;
private Button btnClose;
private Timer tmrDateTime;
public ShoppingSessionReview()
{
InitializeComponent();
}
private void InitializeComponent()
{
// Label: Item Number to Add
lblItemNumberSelected = new Label();
lblItemNumberSelected.AutoSize = true;
lblItemNumberSelected.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblItemNumberSelected.Location = new Point(16, 22);
lblItemNumberSelected.TabIndex = 20;
lblItemNumberSelected.Text = "Item # to Add:";
// Text Box: Item Number to Add
txtItemNumberSelected = new TextBox();
txtItemNumberSelected.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtItemNumberSelected.Location = new Point(235, 18);
txtItemNumberSelected.Size = new System.Drawing.Size(155, 41);
txtItemNumberSelected.TabIndex = 21;
txtItemNumberSelected.KeyUp += new KeyEventHandler(txtItemNumberKeyUp);
// Label: Receipt Number
lblReceiptNumber = new Label();
lblReceiptNumber.AutoSize = true;
lblReceiptNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblReceiptNumber.Location = new Point(881, 22);
lblReceiptNumber.TabIndex = 0;
lblReceiptNumber.Text = "Receipt #:";
// Text Box: Receipt Number
txtReceiptNumber = new TextBox();
txtReceiptNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtReceiptNumber.Location = new Point(1060, 18);
txtReceiptNumber.Size = new System.Drawing.Size(105, 41);
txtReceiptNumber.TabIndex = 1;
// Button: Find
btnFind = new Button();
btnFind.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnFind.Location = new Point(1178, 16);
btnFind.Size = new System.Drawing.Size(168, 46);
btnFind.TabIndex = 2;
btnFind.Text = "Find";
btnFind.Click += new System.EventHandler(btnFindClick);
Controls.Add(btnFind);
// Column: umn: Item Number
colItemNumber = new ColumnHeader();
colItemNumber.Text = "Item #";
colItemNumber.Width = 100;
// Column: umn: Item Name
colItemName = new ColumnHeader();
colItemName.Text = "Item Name/Description";
colItemName.Width = 610;
// Column: umn: Item Size
colItemSize = new ColumnHeader();
colItemSize.Text = "Size";
colItemSize.Width = 150;
// Column: umn: Unit Price
colUnitPrice = new ColumnHeader();
colUnitPrice.Text = "Unit Price";
colUnitPrice.TextAlign = HorizontalAlignment.Right;
colUnitPrice.Width = 140;
// Column: umn: Discount Rate
colDiscountRate = new ColumnHeader();
colDiscountRate.Text = "Dscnt Rt";
colDiscountRate.TextAlign = HorizontalAlignment.Right;
colDiscountRate.Width = 120;
// Column: umn: Discount Amount
colDiscountAmount = new ColumnHeader();
colDiscountAmount.Text = "Dscnt Amt";
colDiscountAmount.TextAlign = HorizontalAlignment.Right;
colDiscountAmount.Width = 140;
// Column: umn: Sale Price
colSalePrice = new ColumnHeader();
colSalePrice.Text = "Sale Price";
colSalePrice.TextAlign = HorizontalAlignment.Right;
colSalePrice.Width = 130;
// List View: Selected Items
lvwSoldItems = new ListView();
lvwSoldItems.Columns.AddRange(new ColumnHeader[]
{
colItemNumber, colItemName, colItemSize, colUnitPrice,
colDiscountRate, colDiscountAmount, colSalePrice
});
lvwSoldItems.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lvwSoldItems.FullRowSelect = true;
lvwSoldItems.GridLines = true;
lvwSoldItems.Location = new Point(16, 71);
lvwSoldItems.Size = new System.Drawing.Size(1424, 321);
lvwSoldItems.TabIndex = 8;
lvwSoldItems.View = View.Details;
// Date/Time Picker: Sale Date
dtpSaleDate = new DateTimePicker();
dtpSaleDate.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
dtpSaleDate.Location = new Point(16, 469);
dtpSaleDate.Size = new System.Drawing.Size(402, 39);
dtpSaleDate.TabIndex = 9;
lblSaleTime = new Label();
lblSaleTime.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblSaleTime.Location = new Point(429, 469);
lblSaleTime.Text = "Sale Time";
lblSaleTime.Size = new System.Drawing.Size(177, 39);
lblSaleTime.TabIndex = 11;
// Label: Tendered
lblTendered = new Label();
lblTendered.AutoSize = true;
lblTendered.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblTendered.Location = new Point(1089, 449);
lblTendered.TabIndex = 15;
lblTendered.Text = "Tendered:";
// Text Box: Amount Tendered
txtAmountTendered = new TextBox();
txtAmountTendered.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtAmountTendered.Location = new Point(1278, 445);
txtAmountTendered.Size = new System.Drawing.Size(130, 41);
txtAmountTendered.TabIndex = 14;
txtAmountTendered.Text = "0.00";
txtAmountTendered.TextAlign = HorizontalAlignment.Right;
txtAmountTendered.KeyUp += new KeyEventHandler(txtAmountTenderedKeyUp);
txtAmountTendered.Leave += new EventHandler(txtAmountTenderedLeave);
// Label: Item Number to Remove
lblItemNumberToRemove = new Label();
lblItemNumberToRemove.AutoSize = true;
lblItemNumberToRemove.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblItemNumberToRemove.Location = new Point(16, 416);
lblItemNumberToRemove.TabIndex = 18;
lblItemNumberToRemove.Text = "Item # to Remove:";
// Text Box: Item Number to Remove
txtItemNumberRemove = new TextBox();
txtItemNumberRemove.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtItemNumberRemove.Location = new Point(290, 412);
txtItemNumberRemove.Size = new System.Drawing.Size(155, 41);
txtItemNumberRemove.TabIndex = 19;
// Button: Remove Item
btnRemoveItem = new Button();
btnRemoveItem.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnRemoveItem.Location = new Point(451, 408);
btnRemoveItem.Size = new System.Drawing.Size(155, 46);
btnRemoveItem.TabIndex = 20;
btnRemoveItem.Text = "Remove";
btnRemoveItem.Click += new System.EventHandler(btnRemoveItemClick);
// Label: Order Total
lblOrderTotal = new Label();
lblOrderTotal.AutoSize = true;
lblOrderTotal.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblOrderTotal.Location = new Point(1089, 401);
lblOrderTotal.TabIndex = 12;
lblOrderTotal.Text = "Order Total:";
// Text Box: Order Total
txtOrderTotal = new TextBox();
txtOrderTotal.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtOrderTotal.Location = new Point(1278, 396);
txtOrderTotal.Size = new System.Drawing.Size(130, 41);
txtOrderTotal.TabIndex = 13;
txtOrderTotal.Text = "0.00";
txtOrderTotal.TextAlign = HorizontalAlignment.Right;
// Label: Change
lblChange = new Label();
lblChange.AutoSize = true;
lblChange.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblChange.Location = new Point(1089, 496);
lblChange.TabIndex = 16;
lblChange.Text = "Change:";
// Text Box: Change
txtChange = new TextBox();
txtChange.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtChange.Location = new Point(1278, 492);
txtChange.Size = new System.Drawing.Size(129, 41);
txtChange.TabIndex = 17;
txtChange.Text = "0.00";
txtChange.TextAlign = HorizontalAlignment.Right;
// Label: Employee Number
lblEmployeeNumber = new Label();
lblEmployeeNumber.AutoSize = true;
lblEmployeeNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
lblEmployeeNumber.Location = new Point(10, 558);
lblEmployeeNumber.TabIndex = 0;
lblEmployeeNumber.Text = "Employee #:";
// Text Box: Employee Number
txtEmployeeNumber = new TextBox();
txtEmployeeNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtEmployeeNumber.Location = new Point(206, 554);
txtEmployeeNumber.Size = new System.Drawing.Size(155, 41);
txtEmployeeNumber.TabIndex = 1;
txtEmployeeNumber.Leave += new System.EventHandler(txtEmployeeNumberLeave);
// Text Box: Employee Name
txtEmployeeName = new TextBox();
txtEmployeeName.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
txtEmployeeName.Location = new Point(370, 554);
txtEmployeeName.Size = new System.Drawing.Size(499, 41);
txtEmployeeName.TabIndex = 7;
// Button: Submit
btnSubmit = new Button();
btnSubmit.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnSubmit.Location = new Point(1097, 551);
btnSubmit.Size = new System.Drawing.Size(177, 46);
btnSubmit.TabIndex = 3;
btnSubmit.Text = "Submit";
btnSubmit.Click += new System.EventHandler(btnSubmitClick);
Controls.Add(btnSubmit);
// Button: Close
btnClose = new Button();
btnClose.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnClose.Location = new Point(1281, 551);
btnClose.Size = new System.Drawing.Size(129, 46);
btnClose.TabIndex = 4;
btnClose.Text = "Close";
btnClose.Click += new System.EventHandler(btnCloseClick);
Controls.Add(btnClose);
tmrDateTime = new Timer();
tmrDateTime.Enabled = false;
tmrDateTime.Tick += new EventHandler(tmrDateTimeTick);
ClientSize = new System.Drawing.Size(1454, 611);
Controls.Add(txtItemNumberSelected);
Controls.Add(lblItemNumberSelected);
Controls.Add(txtReceiptNumber);
Controls.Add(lblReceiptNumber);
Controls.Add(btnRemoveItem);
Controls.Add(txtItemNumberRemove);
Controls.Add(lblItemNumberToRemove);
Controls.Add(txtEmployeeName);
Controls.Add(txtOrderTotal);
Controls.Add(lblOrderTotal);
Controls.Add(txtChange);
Controls.Add(lblChange);
Controls.Add(txtAmountTendered);
Controls.Add(lblTendered);
Controls.Add(lblSaleTime);
Controls.Add(dtpSaleDate);
Controls.Add(txtEmployeeNumber);
Controls.Add(lblEmployeeNumber);
Controls.Add(lvwSoldItems);
MaximizeBox = false;
MinimizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "FunDS - Shopping Session";
Load += new System.EventHandler(ShoppingSessionLoad);
}
private void btnFindClick(object sender, EventArgs e)
{
bool receiptFound = false;
XmlDocument xdSoldItems = new XmlDocument();
XmlDocument xdStoreSales = new XmlDocument();
string strSoldItemsFile = @"C:\Fun Department Store\SoldItems.xml";
string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
if (string.IsNullOrEmpty(txtReceiptNumber.Text))
{
MessageBox.Show("You must enter a receipt number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
if (File.Exists(strStoreSalesFile))
{
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
{
xdStoreSales.Load(fsStoreSales);
XmlNodeList xnlStoreSales = xdStoreSales.GetElementsByTagName("ReceiptNumber");
foreach (XmlNode xnStoreSale in xnlStoreSales)
{
if (xnStoreSale.InnerText == txtReceiptNumber.Text)
{
receiptFound = true;
txtEmployeeNumber.Text = xnStoreSale.NextSibling.InnerText;
dtpSaleDate.Value = DateTime.Parse(xnStoreSale.NextSibling.NextSibling.InnerText);
lblSaleTime.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.InnerText;
txtOrderTotal.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
txtAmountTendered.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
txtChange.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
txtEmployeeNumberLeave(sender, e);
}
}
}
}
if (File.Exists(strSoldItemsFile))
{
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Open, FileAccess.Read))
{
xdSoldItems.Load(fsSoldItems);
XmlNodeList xnlSoldItems = xdSoldItems.GetElementsByTagName("ReceiptNumber");
lvwSoldItems.Items.Clear();
foreach (XmlNode xnSoldItem in xnlSoldItems)
{
if (xnSoldItem.InnerText == txtReceiptNumber.Text)
{
ListViewItem lviStoreItem = new ListViewItem(xnSoldItem.NextSibling.InnerText); // Item Number
lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.InnerText); // Item Name
lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.InnerText); // Item Size
lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Unit Price
lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Discount Rate
lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Discount Amount
lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Sale Price
lvwSoldItems.Items.Add(lviStoreItem);
}
}
}
}
if (receiptFound == false)
{
MessageBox.Show("There is no sale order with that receipt number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
}
private void ResetForm()
{
txtReceiptNumber.Text = "";
txtItemNumberSelected.Text = "";
lvwSoldItems.Items.Clear();
txtItemNumberRemove.Text = "";
dtpSaleDate.Value = DateTime.Now;
lblSaleTime.Text = DateTime.Now.ToString();
txtEmployeeNumber.Text = ""; ;
txtEmployeeName.Text = "";
txtOrderTotal.Text = "0.00";
txtAmountTendered.Text = "0.00";
txtChange.Text = "0.00";
}
private void txtItemNumberKeyUp(object sender, KeyEventArgs e)
{
bool itemFound = false;
double dblTotal = 0.00;
XmlDocument xdStoreItems = new XmlDocument();
string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
if (e.KeyCode == Keys.Enter)
{
if (string.IsNullOrEmpty(txtItemNumberSelected.Text))
{
MessageBox.Show("You must enter an item number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
else
{
if (File.Exists(strStoreItemsFile))
{
using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
{
xdStoreItems.Load(fsStoreItems);
string itemNumber, itemName, itemSize, strDiscountRate;
double unitPrice, discountRate, discountAmount, salePrice;
XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
foreach (XmlNode xnStoreItem in xnlStoreItems)
{
if (xnStoreItem.InnerText == txtItemNumberSelected.Text)
{
itemFound = true;
itemNumber = xnStoreItem.InnerText;
itemName = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
itemSize = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
unitPrice = double.Parse(xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
strDiscountRate = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
if ((string.IsNullOrEmpty(strDiscountRate)) || (double.Parse(strDiscountRate) <= 0.00))
{
discountRate = 0.00;
discountAmount = 0.00;
salePrice = unitPrice;
}
else
{
discountRate = double.Parse(strDiscountRate) / 100.00;
discountAmount = unitPrice * discountRate;
salePrice = unitPrice - discountAmount;
}
ListViewItem lviStoreItem = new ListViewItem(itemNumber); // Item Number
lviStoreItem.SubItems.Add(itemName); // Item Name
lviStoreItem.SubItems.Add(itemSize); // Item Size
lviStoreItem.SubItems.Add(unitPrice.ToString("F"));
if (discountRate == 0.00)
{
lviStoreItem.SubItems.Add(""); // Discount Rate
lviStoreItem.SubItems.Add(""); // Discount Amount
lviStoreItem.SubItems.Add(unitPrice.ToString("F")); // Unit Price
}
else
{
lviStoreItem.SubItems.Add((discountRate * 100).ToString() + "%");
lviStoreItem.SubItems.Add(discountAmount.ToString("F"));
lviStoreItem.SubItems.Add(salePrice.ToString("F"));
}
lvwSoldItems.Items.Add(lviStoreItem);
}
}
txtItemNumberSelected.Text = "";
if (itemFound == false)
{
MessageBox.Show("There is no item with that item number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
if (lvwSoldItems.Items.Count > 0)
{
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
txtOrderTotal.Text = dblTotal.ToString("F");
}
}
}
}
}
private void ShoppingSessionLoad(object sender, EventArgs e)
{
}
private void txtEmployeeNumberLeave(object sender, EventArgs e)
{
bool employeeFound = false;
XmlDocument xdEmployees = new XmlDocument();
string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
{
return;
}
else
{
if (File.Exists(strEmployeesFile))
{
using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
{
xdEmployees.Load(fsEmployees);
XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
foreach (XmlNode xnEmployee in xnlEmployees)
{
if (txtEmployeeNumber.Text == xnEmployee.InnerText)
{
employeeFound = true;
txtEmployeeName.Text = string.Concat(xnEmployee.NextSibling.NextSibling.InnerText, ", ", xnEmployee.NextSibling.InnerText);
}
}
if (employeeFound == false)
{
MessageBox.Show("There is no employee with that number.",
"FunDS - Fun Department Store",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
}
private void txtAmountTenderedKeyUp(object sender, KeyEventArgs e)
{
double orderTotal = 0.00;
double amountTendered = 0.00;
double change = 0.00;
if ((e.KeyCode == Keys.Tab) || (e.KeyCode == Keys.Enter))
{
if (string.IsNullOrEmpty(txtOrderTotal.Text))
{
return;
}
else if (string.IsNullOrEmpty(txtAmountTendered.Text))
{
return;
}
else
{
orderTotal = double.Parse(txtOrderTotal.Text);
amountTendered = double.Parse(txtAmountTendered.Text);
change = amountTendered - orderTotal;
txtChange.Text = change.ToString("F");
}
}
}
private void txtAmountTenderedLeave(object sender, EventArgs e)
{
double orderTotal = 0.00;
double amountTendered = 0.00;
double change = 0.00;
if (string.IsNullOrEmpty(txtOrderTotal.Text))
{
return;
}
else if (string.IsNullOrEmpty(txtAmountTendered.Text))
{
return;
}
else
{
orderTotal = double.Parse(txtOrderTotal.Text);
amountTendered = double.Parse(txtAmountTendered.Text);
change = amountTendered - orderTotal;
txtChange.Text = change.ToString("F");
}
}
private void btnRemoveItemClick(object sender, EventArgs e)
{
double dblTotal = 0.00;
bool itemFound = false;
if (lvwSoldItems.Items.Count == 0)
{
MessageBox.Show("The list view is empty.",
"FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (string.IsNullOrEmpty(txtItemNumberRemove.Text))
{
MessageBox.Show("You must enter an item number and that exists in the list view.",
"FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
{
if (lviStoreItem.SubItems[0].Text.Equals(txtItemNumberRemove.Text))
{
itemFound = true;
lvwSoldItems.Items.Remove(lviStoreItem);
}
}
if (itemFound == false)
{
MessageBox.Show("That item number is not in the list view.",
"FunDS", MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
if (lvwSoldItems.Items.Count > 0)
{
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
txtOrderTotal.Text = dblTotal.ToString("F");
txtAmountTendered.Text = "0.00";
txtChange.Text = "0.00";
}
else
{
txtOrderTotal.Text = "0.00";
txtAmountTendered.Text = "0.00";
txtChange.Text = "0.00";
}
txtItemNumberRemove.Text = "";
}
}
private void btnSubmitClick(object sender, EventArgs e)
{
int iSoldItemID = 0;
XmlDocument xdSoldItems = new XmlDocument();
XmlDocument xdStoreSales = new XmlDocument();
XmlDocument xdStoreItems = new XmlDocument();
string strSoldItemsFile = @"C:\Fun Department Store\SoldItems.xml";
string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
if (lvwSoldItems.Items.Count == 0)
{
MessageBox.Show("There is no customer order to save.",
"FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (!File.Exists(strStoreSalesFile))
{
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
{
xdStoreSales.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<StoreSales></StoreSales>");
xdStoreSales.Save(fsStoreSales);
}
}
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
{
xdStoreSales.Load(fsStoreSales);
}
using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
{
XmlElement xeStoreSale = xdStoreSales.CreateElement("StoreSale");
xeStoreSale.InnerXml = "<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
"<EmployeeNumber>" + txtEmployeeNumber.Text + "</EmployeeNumber>" +
"<SaleDate>" + dtpSaleDate.Value.ToShortDateString() + "</SaleDate>" +
"<SaleTime>" + lblSaleTime.Text + "</SaleTime>" +
"<OrderTotal>" + txtOrderTotal.Text + "</OrderTotal>" +
"<AmountTendered>" + txtAmountTendered.Text + "</AmountTendered>" +
"<Change>" + txtChange.Text + "</Change>";
xdStoreSales.DocumentElement.AppendChild(xeStoreSale);
xdStoreSales.Save(fsStoreSales);
}
if (!File.Exists(strSoldItemsFile))
{
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
{
xdSoldItems.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SoldItems></SoldItems>");
xdSoldItems.Save(fsSoldItems);
}
}
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Open, FileAccess.Read))
{
xdSoldItems.Load(fsSoldItems);
XmlNodeList xnlSoldItems = xdSoldItems.GetElementsByTagName("SoldItemID");
foreach (XmlNode xnSoldItem in xnlSoldItems)
iSoldItemID = int.Parse(xnSoldItem.InnerText);
}
iSoldItemID = iSoldItemID + 1;
using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
{
foreach (ListViewItem lviSoldItem in lvwSoldItems.Items)
{
XmlElement xeSoldItem = xdSoldItems.CreateElement("SoldItem");
xeSoldItem.InnerXml = "<SoldItemID>" + iSoldItemID.ToString() + "</SoldItemID>" +
"<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
"<ItemNumber>" + lviSoldItem.SubItems[0].Text + "</ItemNumber>" +
"<ItemName>" + lviSoldItem.SubItems[1].Text + "</ItemName>" +
"<ItemSize>" + lviSoldItem.SubItems[2].Text + "</ItemSize>" +
"<UnitPrice>" + lviSoldItem.SubItems[3].Text + "</UnitPrice>" +
"<DiscountRate>" + lviSoldItem.SubItems[4].Text + "</DiscountRate>" +
"<DiscountAmount>" + lviSoldItem.SubItems[5].Text + "</DiscountAmount>" +
"<SalePrice>" + lviSoldItem.SubItems[6].Text + "</SalePrice>";
xdSoldItems.DocumentElement.AppendChild(xeSoldItem);
iSoldItemID++;
}
xdSoldItems.Save(fsSoldItems);
MessageBox.Show("The customer's order has been saved.",
"FunDS", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
if (File.Exists(strStoreItemsFile))
{
using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
{
xdStoreItems.Load(fsStoreItems);
XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
// Remove the items of the list view from the StoreItems table
foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
{
foreach (XmlNode xnStoreItem in xnlStoreItems)
{
if (xnStoreItem.InnerText == lviStoreItem.SubItems[0].Text)
xdStoreItems.DocumentElement.RemoveChild(xnStoreItem.ParentNode);
}
}
}
using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
{
xdStoreItems.Save(fsStoreItems);
}
}
ResetForm();
}
private void tmrDateTimeTick(object sender, EventArgs e)
{
dtpSaleDate.Value = DateTime.Today;
lblSaleTime.Text = DateTime.Now.ToLongTimeString();
}
private void btnCloseClick(object sender, EventArgs e)
{
Close();
}
}
- On the main menu, click File -> New
- When asked whether you want to save, click Save
- In the top
combo box, make sure the FunDS1 folder is displaying, Set the Save As Type
to All Files
- Set the File Name to ShoppingSessionReview.cs
- Click Save
A switchboard serves as the central point of an application.
It holds a list of the forms used in an application.
Practical
Learning: Creating a Switchboard
|
|
- In the empty document, type the following:
using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
public class FunDS : Form
{
private Button btnNewShoppingSession;
private Button btnStoreItems;
private Button btnShoppingSessionReview;
private Button btnNewStoreItem;
private Button btnEmployees;
private Button btnEditStoreItem;
private Button btnNewEmployee;
private Button btnDeleteStoteItem;
private Button btnClose;
public FunDS()
{
InitializeComponent();
}
private void InitializeComponent()
{
// Button: New Shopping Session
btnNewShoppingSession = new Button();
btnNewShoppingSession.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnNewShoppingSession.Location = new Point(22, 22);
btnNewShoppingSession.Size = new System.Drawing.Size(478, 67);
btnNewShoppingSession.TabIndex = 0;
btnNewShoppingSession.Text = "New Shopping Session ...";
btnNewShoppingSession.Click += new System.EventHandler(btnNewShoppingSessionClick);
Controls.Add(btnNewShoppingSession);
// Button: Store Items
btnStoreItems = new Button();
btnStoreItems.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnStoreItems.Location = new Point(518, 22);
btnStoreItems.Size = new System.Drawing.Size(478, 67);
btnStoreItems.TabIndex = 1;
btnStoreItems.Text = "Store Items ...";
btnStoreItems.Click += new System.EventHandler(btnStoreItemsClick);
Controls.Add(btnStoreItems);
// Button: Shopping Session Review
btnShoppingSessionReview = new Button();
btnShoppingSessionReview.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnShoppingSessionReview.Location = new Point(22, 108);
btnShoppingSessionReview.Size = new System.Drawing.Size(478, 67);
btnShoppingSessionReview.TabIndex = 2;
btnShoppingSessionReview.Text = "Shopping Session Review ...";
btnShoppingSessionReview.Click += new System.EventHandler(btnShoppingSessionReviewClick);
Controls.Add(btnShoppingSessionReview);
// Button: New Store Item
btnNewStoreItem = new Button();
btnNewStoreItem.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnNewStoreItem.Location = new Point(518, 108);
btnNewStoreItem.Size = new System.Drawing.Size(478, 67);
btnNewStoreItem.TabIndex = 3;
btnNewStoreItem.Text = "New Store Item ...";
btnNewStoreItem.Click += new System.EventHandler(btnNewStoreItemClick);
Controls.Add(btnNewStoreItem);
// Button: Employees
btnEmployees = new Button();
btnEmployees.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnEmployees.Location = new Point(22, 194);
btnEmployees.Size = new System.Drawing.Size(478, 67);
btnEmployees.TabIndex = 4;
btnEmployees.Text = "Employees ...";
btnEmployees.Click += new System.EventHandler(btnEmployeesClick);
Controls.Add(btnEmployees);
// Button: Edit Store Item
btnEditStoreItem = new Button();
btnEditStoreItem.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnEditStoreItem.Location = new Point(518, 194);
btnEditStoreItem.Size = new System.Drawing.Size(478, 67);
btnEditStoreItem.TabIndex = 5;
btnEditStoreItem.Text = "Edit Store Item ...";
btnEditStoreItem.Click += new System.EventHandler(btnEditStoreItemClick);
Controls.Add(btnEditStoreItem);
// Button: New Employee
btnNewEmployee = new Button();
btnNewEmployee.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnNewEmployee.Location = new Point(22, 283);
btnNewEmployee.Size = new System.Drawing.Size(478, 67);
btnNewEmployee.TabIndex = 6;
btnNewEmployee.Text = "New Employee ...";
btnNewEmployee.Click += new System.EventHandler(btnNewEmployeeClick);
Controls.Add(btnNewEmployee);
// Button: Delete Stote Item
btnDeleteStoteItem = new Button();
btnDeleteStoteItem.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnDeleteStoteItem.Location = new Point(518, 283);
btnDeleteStoteItem.Size = new System.Drawing.Size(478, 67);
btnDeleteStoteItem.TabIndex = 7;
btnDeleteStoteItem.Text = "Delete Stote Item ...";
btnDeleteStoteItem.Click += new System.EventHandler(btnDeleteStoteItemClick);
Controls.Add(btnDeleteStoteItem);
// Button: Close
btnClose = new Button();
btnClose.Font = new System.Drawing.Font("Palatino Linotype", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
btnClose.Location = new Point(22, 374);
btnClose.Size = new System.Drawing.Size(974, 67);
btnClose.TabIndex = 8;
btnClose.Text = "Close";
btnClose.Click += new System.EventHandler(btnCloseClick);
Controls.Add(btnClose);
// Form: Fun Department Store
ClientSize = new System.Drawing.Size(1018, 463);
StartPosition = FormStartPosition.CenterScreen;
Text = "FunDS - Fun Department Store";
Load += new EventHandler(FunDSLoad);
}
private void FunDSLoad(object sender, EventArgs e)
{
Directory.CreateDirectory(@"C:\Fun Department Store");
}
private void btnNewShoppingSessionClick(object sender, EventArgs e)
{
NewShoppingSession nss = new NewShoppingSession();
nss.Show();
}
private void btnStoreItemsClick(object sender, EventArgs e)
{
StoreItems sis = new StoreItems();
sis.Show();
}
private void btnShoppingSessionReviewClick(object sender, EventArgs e)
{
ShoppingSessionReview ssr = new ShoppingSessionReview();
ssr.Show();
}
private void btnNewStoreItemClick(object sender, EventArgs e)
{
StoreItemNew sin = new StoreItemNew();
sin.ShowDialog();
}
private void btnEmployeesClick(object sender, EventArgs e)
{
Employees clerks = new Employees();
clerks.Show();
}
private void btnEditStoreItemClick(object sender, EventArgs e)
{
StoreItemEditor sie = new StoreItemEditor();
sie.ShowDialog();
}
private void btnNewEmployeeClick(object sender, EventArgs e)
{
string strEmployeeNumber, strFirstName, strLastName, strCounty,
strAddress, strCity, strState, strZIPCode, strMaritalStatus;
int iExemptions;
double dblHourlySalary;
EmployeeNew en = new EmployeeNew();
Random rndEmployeeNumber = new Random();
XmlDocument xdEmployees = new XmlDocument();
string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
en.txtEmployeeNumber.Text = rndEmployeeNumber.Next(100000, 999999).ToString();
if (en.ShowDialog() == DialogResult.OK)
{
strEmployeeNumber = en.txtEmployeeNumber.Text;
strFirstName = en.txtFirstName.Text;
strLastName = en.txtLastName.Text;
strMaritalStatus = en.cbxMaritalsStatus.Text;
iExemptions = int.Parse(en.txtExemptions.Text);
strAddress = en.txtAddress.Text;
strCity = en.txtCity.Text;
strCounty = en.txtCounty.Text;
strState = en.txtState.Text;
strZIPCode = en.txtZIPCode.Text;
dblHourlySalary = double.Parse(en.txtHourlySalary.Text);
if (!File.Exists(strEmployeesFile))
{
xdEmployees.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Employees></Employees>");
xdEmployees.Save(strEmployeesFile);
}
xdEmployees.Load(strEmployeesFile);
XmlElement elmXML = xdEmployees.CreateElement("Employee");
// Create the XML code of the child element of Employee
string strNewEmployee = "<EmployeeNumber>" + strEmployeeNumber + "</EmployeeNumber>" +
"<FirstName>" + strFirstName + "</FirstName>" +
"<LastName>" + strLastName + "</LastName>" +
"<Address>" + strAddress + "</Address>" +
"<City>" + strCity + "</City>" +
"<County>" + strCounty + "</County>" +
"<State>" + strState + "</State>" +
"<ZIPCode>" + strZIPCode + "</ZIPCode>" +
"<MaritalStatus>" + strMaritalStatus + "</MaritalStatus>" +
"<Exemptions>" + iExemptions + "</Exemptions>" +
"<HourlySalary>" + dblHourlySalary + "</HourlySalary>";
elmXML.InnerXml = strNewEmployee;
// Append the new element as a child of employees
xdEmployees.DocumentElement.AppendChild(elmXML);
// Save the XML file
xdEmployees.Save(strEmployeesFile);
}
}
private void btnDeleteStoteItemClick(object sender, EventArgs e)
{
StoreItemDelete sid = new StoreItemDelete();
sid.ShowDialog();
}
private void btnCloseClick(object sender, EventArgs e)
{
Close();
}
[STAThread]
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FunDS());
return 0;
}
}
- On the main menu, click File -> Exit
- When asked whether you want to save, click Save
- In the top
combo box, make sure the FunDS1 folder is displaying, Set the Save As Type
to All Files
- Set the File Name to FunDS.cs
- Click Save
To build this project, you can use the compiler provides in
Microsoft Windows and from the Command Prompt.
Practical
Learning: Creating a Switchboard
|
|
- Display the Command Prompt (Start -> (All) Programs -> Accessories ->
Command Prompt)
- Type CD\ and press Enter to access the root
- Type CD FunDS1 and press Enter to access the folder for the
current project
- To build the project, type the following code:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /t:winexe FunDS.cs EmployeeNew.cs EmployeeEditor.cs Employees.cs Manufacturer.cs Category.cs SubCategory.cs StoreItemNew.cs StoreItemEditor.cs StoreItemDelete.cs Inventory.cs PointOfSale.cs ShoppingSessionReview.cs
- Press Enter
If you want to test the application, use the following
values.
|
|