Some interesting things I found while doing this:
1) Scroll bar required when the order list is too long
2) Window should resize on Adding a pizza to the order
Everything is in the code. It might be confusing so let me know if you need some help on that.
PS: I am using some methods/functions. They work like events, so I don't think you'll struggle with understanding that :)
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
int[] iPostCode = { 2000, 2002, 2312, 2492, 2121, 2765};
string[] sSuburb = { "City", "Chippendale", "Rose Hill", "Balmain", "Edgecliff", "Bondi" };
int[] iPizzaQuantity = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int iOrder = 0;
string sPizzaOrdered= "";
double dPizzaPrice = 0.0;
double dTotalAmount = 0.0;
string sPizzaExtras = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fnClearForm();
fnLoadDefaultValues();
}
/* Update Suburb on PostCode change */
private void cmbPostCode_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < iPostCode.Length; i++)
{
if (iPostCode[i].ToString() == cmbPostCode.Text)
{
txtSuburb.Text = sSuburb[i];
}
}
}
private void btnAddPizza_Click(object sender, EventArgs e)
{
if (fnValidateAllFields() == 0)
{
fnResizeMax();
string sOrder = "Order #" + lblOrder.Text + " Date: " + lblDate.Text +
"\n\nClient Name: " + txtFName.Text + " " + txtLName.Text ;
double dPizzaCost = fnCountPizzaCost();
dTotalAmount += dPizzaCost;
string sDetails = cmbQuantity.Text.ToString() + " - " + sPizzaOrdered + " - $" + dPizzaCost;
sDetails += "\n" + sPizzaExtras;
lblPizzaSummary.Text +="\n\n" + sDetails;
lblResult.Text = sOrder;
sPizzaExtras = ""; //reset the pizza toppings and extra
lblSubTotal.Text = dTotalAmount.ToString("F2");
lblTax.Text = (dTotalAmount * 0.13).ToString("F2");
lblGrandTotal.Text = (dTotalAmount * 1.13).ToString("F2");
}
}
private void radLarge_CheckedChanged(object sender, EventArgs e)
{
sPizzaOrdered = "Large";
dPizzaPrice = 25;
}
private void radRegular_CheckedChanged(object sender, EventArgs e)
{
sPizzaOrdered = "Regular";
dPizzaPrice = 18;
}
private void radSmall_CheckedChanged(object sender, EventArgs e)
{
sPizzaOrdered = "Small";
dPizzaPrice = 15;
}
private void radPersonal_CheckedChanged(object sender, EventArgs e)
{
sPizzaOrdered = "Personal";
dPizzaPrice = 11;
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
fnClearForm();
fnLoadDefaultValues();
fnResizeMin();
}
private void radVisa_Click(object sender, EventArgs e)
{
MessageBox.Show("Request to see ID and Visa Card", "Visa Verification", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCC.Enabled = true;
txtCVC.Enabled = true;
dtpExpDate.Enabled = true;
txtCC.BackColor = Color.White;
txtCVC.BackColor = Color.White;
dtpExpDate.BackColor = Color.White;
}
private void radMasterCard_Click(object sender, EventArgs e)
{
MessageBox.Show("Request to see ID and MasterCard", "Mastercard Verification", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCC.Enabled = true;
txtCVC.Enabled = true;
dtpExpDate.Enabled = true;
txtCC.BackColor = Color.White;
txtCVC.BackColor = Color.White;
dtpExpDate.BackColor = Color.White;
}
private void radDebit_Click(object sender, EventArgs e)
{
MessageBox.Show("Customer may swipe card now", "Debit", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCC.Enabled = false;
txtCVC.Enabled = false;
dtpExpDate.Enabled = false;
txtCC.BackColor = Color.Black;
txtCVC.BackColor = Color.Black;
dtpExpDate.BackColor = Color.Black;
}
private void radCheque_Click(object sender, EventArgs e)
{
MessageBox.Show("-Please request ID\n-Verify address on Cheque\n-Write LIC # on Cheque\n-Write Phone Number on Cheque", "Cheque Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCC.Enabled = false;
txtCVC.Enabled = false;
dtpExpDate.Enabled = false;
txtCC.BackColor = Color.Black;
txtCVC.BackColor = Color.Black;
dtpExpDate.BackColor = Color.Black;
}
private void radCash_Click(object sender, EventArgs e)
{
txtCC.Enabled = false;
txtCVC.Enabled = false;
dtpExpDate.Enabled = false;
txtCC.BackColor = Color.Black;
txtCVC.BackColor = Color.Black;
dtpExpDate.BackColor = Color.Black;
}
/******************************* FUNCTIONS LIST ***************************/
private void fnClearForm()
{
txtFName.Clear();
txtLName.Clear();
txtApt.Clear();
txtPhone.Clear();
txtStreet.Clear();
txtSuburb.Clear();
lblDate.Text = "";
chkAnchovies.Checked = false;
chkOnion.Checked = false;
chkPepperoni.Checked = false;
chkSausage.Checked = false;
radCheeseNo.Checked = false;
radCheeseYes.Checked = false;
radLarge.Checked = false;
radPersonal.Checked = false;
radRegular.Checked = false;
radSmall.Checked = false;
cmbQuantity.Items.Clear();
cmbPostCode.Items.Clear();
lblResult.Text = "";
lblPizzaSummary.Text = "";
sPizzaExtras = "";
lblSubTotal.Text = "";
lblTax.Text = "";
lblGrandTotal.Text = "";
dTotalAmount = 0;
txtCC.Enabled = false;
txtCVC.Enabled = false;
dtpExpDate.Enabled = false;
txtCC.BackColor = Color.Black;
txtCVC.BackColor = Color.Black;
dtpExpDate.BackColor = Color.Black;
}
private void fnLoadDefaultValues()
{
lblDate.Text = DateTime.Now.ToString();
lblOrder.Text = (++iOrder).ToString();
radCheeseNo.Checked = true;
radCash.Checked = true;
/* Load Combo values for PostCode */
for (int i = 0; i < iPostCode.Length; i++)
{
cmbPostCode.Items.Add(iPostCode[i]);
}
for (int i = 0; i < iPizzaQuantity.Length; i++)
{
cmbQuantity.Items.Add(iPizzaQuantity[i]);
}
}
private int fnValidateAllFields()
{
if (txtFName.Text == "")
{
MessageBox.Show("Enter first name", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtFName.Focus();
return -1;
}
if (txtLName.Text == "")
{
MessageBox.Show("Enter last name", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtLName.Focus();
return -1;
}
if (txtApt.Text == "")
{
MessageBox.Show("Enter appartment number", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtApt.Focus();
return -1;
}
if (txtStreet.Text == "")
{
MessageBox.Show("Enter street address", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtStreet.Focus();
return -1;
}
if (txtPhone.Text == "")
{
MessageBox.Show("Enter phone number", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhone.Focus();
return -1;
}
if (txtSuburb.Text == "")
{
MessageBox.Show("Enter suburb", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtSuburb.Focus();
return -1;
}
if (cmbPostCode.Text == "")
{
MessageBox.Show("Enter Postcode", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
cmbPostCode.Focus();
return -1;
}
if (cmbQuantity.Text == "")
{
MessageBox.Show("Enter Quantity", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
cmbQuantity.Focus();
return -1;
}
if ((chkAnchovies.Checked == false) && (chkOnion.Checked == false) && (chkPepperoni.Checked == false) && (chkSausage.Checked == false))
{
MessageBox.Show("Choose at least 1 topping", "Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
chkSausage.Focus();
return -1;
}
return 0; //success
}
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
if (fnValidateAllFields() == -1)
MessageBox.Show("Please click Add Pizza before placing Order", "Order Rejected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
MessageBox.Show("Order has been placed", "Order Confirmed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private double fnCountPizzaCost()
{
double dPizzaOnlyPrice = int.Parse(cmbQuantity.Text) * dPizzaPrice;
if (chkAnchovies.Checked == true)
{
dPizzaOnlyPrice += 0.75;
sPizzaExtras += "Anchovies\n";
}
if (chkSausage.Checked == true)
{
dPizzaOnlyPrice += 0.75;
sPizzaExtras += "Sausage\n";
}
if (chkPepperoni.Checked == true)
{
dPizzaOnlyPrice += 0.75;
sPizzaExtras += "Pepperoni\n";
}
if (chkOnion.Checked == true)
{
dPizzaOnlyPrice += 0.75;
sPizzaExtras += "Onion\n";
}
if (radCheeseYes.Checked == true)
{
dPizzaOnlyPrice += 0.50;
sPizzaExtras += "Extra Cheese\n";
}
return dPizzaOnlyPrice;
}
private void fnResizeMax()
{
Form1.ActiveForm.Size = new Size(700, 762);
}
private void fnResizeMin()
{
Form1.ActiveForm.Size = new Size(399, 762);
}
}
}
No comments:
Post a Comment