Visual C# – The Sandwich Shop

This is a sandwich shop app that creats sandwich objects, has hidden menus, keeps track of inventory, will not allow you to order an item that is not in stock, and validates credit cards.

Download and try the app.

The first sandwich in the order
1 - FirstSandwich

The 10th sandwich in the order 2 - 10thSandwich

At checkout

3 - atCheckout

Approved credit card4 - ApprovedCreditCard

Inventory after order5 - InventoryAfterOrder

Out of stock for next customer6 - OutOfStockForNextCustomer


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;
using System.IO;
using System.Text.RegularExpressions;

namespace SandwichShop
{
   public partial class SandwichShopForm1 : Form
   {
      /* cost of items */
      private double breadPrice = 3.00;
      private double meatPrice = 1.00;
      private double cheesePrice = 0.50;
      private double vegetablePrice = 0.25;
      private double saucePrice = 0.10;
      private double subTotal;
      private double totalCost;
      private double allSubTotal;
      private double currentSandwichSubTotal;
      /* prevent fraud */
      private int fraudControl;
      /* must choose a bread first */
      private bool breadSelected = false;
      /* the sandwich that is being made */
      private List<string> sandwichIngredients = new List<string>();
      private List<double> sandwichPrices = new List<double>();
      /* multiple sandwich prices anfter being made */
      private List<string> allSandwiches = new List<string> { "1st Sandwich", "2nd Sandwich", 
         "3rd Sandwich", "4th Sandwich", "5th Sandwich", "6th Sandwich", "7th Sandwich", 
         "8th Sandwich", "9th Sandwich", "10th Sandwich" };
      private List<double> allSandwichPrices = new List<double>();
      /* inventory items */
      private string[] breads = new string[] { "White", "Wheat", "Rye", "Multi-Grain",
         "Ezekiel", "Sourdough", "Pumpernickel", "Ciabatta", "Focaccia", "Cinnamon Raisin" };
      private string[] meats = new string[] { "Turkey", "Ham", "Roast Beef", "Chicken", 
         "Bacon", "Bologna", "Salami", "Sausage", "Pepperoni", "Prosciutto" };
      private string[] cheeses = new string[] { "Cheddar", "Swiss", "Provolone", 
         "Pepper Jack", "American", "Gouda", "Colby", "Muenster", "Monterey Jack", "Brie" };
      private string[] vegetables = new string[] { "Cucumbers", "Bell Peppers", "Lettuce", 
         "Onions", "Spinach", "Tomatoes", "Jalapenos", "Olives", "Pickles", "Avocado" };
      private string[] sauces = new string[] { "Mustard", "Mayonnaise", "Barbecue", "Blue Cheese", 
         "Buffalo", "Italian", "Honey Mustard", "Vinaigrette", "Ranch", "Ketchup" };
      /* main invantory */
      private string[] breadInventory = new string[10];
      private string[] meatInventory = new string[10];
      private string[] cheeseInventory = new string[10];
      private string[] vegetableInventory = new string[10];
      private string[] sauceInvantory = new string[10];
      /* incase of cancel order with multiple sandwiches */
      private string[] tempBreadInventory = new string[10];
      private string[] tempMeatInventory = new string[10];
      private string[] tempCheeseInventory = new string[10];
      private string[] tempVegetableInventory = new string[10];
      private string[] tempSauceInventory = new string[10];
      /* text file where invantory will be saved */
      private string filename = "Invantory.txt";



      public SandwichShopForm1()
      {
         InitializeComponent();
         tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
         /* hide checkout Tab Page */
         tabControl1.TabPages.Remove(checkoutTabPage);
         /* center text on card aprooval page */
         creditDisplayTextBox.SelectionAlignment = HorizontalAlignment.Center;
      }

      private void SandwichShopForm1_Load(object sender, EventArgs e)
      {
         //loadFile();
         loadInvantory();
         checkAllInvantory();
      }

      //private void loadFile()
      //{
      //   if (!File.Exists(filename))
      //   {
      //      DialogResult result;
      //      using (OpenFileDialog fileChooser = new OpenFileDialog())
      //      {
      //         result = fileChooser.ShowDialog();
      //         filename = fileChooser.FileName;
      //      }
      //      if (result == DialogResult.OK)
      //      {
      //         if (filename == string.Empty)
      //            MessageBox.Show("Invalid File Name", "Error",
      //               MessageBoxButtons.OK, MessageBoxIcon.Error);
      //      }
      //   }
      //}

      /* LOAD INVENTORY */
      private void loadInvantory()
      {
         if (File.Exists(filename)) // use inventory in file
         {
            string line = File.ReadLines(filename).Skip(0).Take(1).First();
            breadInventory = line.Split(',');
            line = File.ReadLines(filename).Skip(1).Take(1).First();
            meatInventory = line.Split(',');
            line = File.ReadLines(filename).Skip(2).Take(1).First();
            cheeseInventory = line.Split(',');
            line = File.ReadLines(filename).Skip(3).Take(1).First();
            vegetableInventory = line.Split(',');
            line = File.ReadLines(filename).Skip(4).Take(1).First();
            sauceInvantory = line.Split(',');

            tempBreadInventory = (string[])breadInventory.Clone();
            tempMeatInventory = (string[])meatInventory.Clone();
            tempCheeseInventory = (string[])cheeseInventory.Clone();
            tempVegetableInventory = (string[])vegetableInventory.Clone();
            tempSauceInventory = (string[])sauceInvantory.Clone();
         }
         else // create new inventory if there is no file
         {
            breadInventory = new string[] { "5", "5", "5", "5", "5", "5", "5", "5", "5", "5" };
            meatInventory = new string[] { "5", "5", "5", "5", "5", "5", "5", "5", "5", "5" };
            cheeseInventory = new string[] { "5", "5", "5", "5", "5", "5", "5", "5", "5", "5" };
            vegetableInventory = new string[] { "5", "5", "5", "5", "5", "5", "5", "5", "5", "5" };
            sauceInvantory = new string[] { "5", "5", "5", "5", "5", "5", "5", "5", "5", "5" };
            saveInvantory();
         }
      }

      /* DISPLAY ALL INVENTORY */
      private void displayAllInventory()
      {
         displayInvantoryTextBox.Text = string.Empty;
         displayInvantory("BREAD", breads, breadInventory);
         displayInvantory("MEAT", meats, meatInventory);
         displayInvantory("CHEESE", cheeses, cheeseInventory);
         displayInvantory("VEGETABLE", vegetables, vegetableInventory);
         displayInvantory("SAUCE", sauces, sauceInvantory);
      }

      /* DISPLAY INDIVIDUAL INVENTORY */
      private void displayInvantory(string name, string[] group, string[] invantory)
      {
         displayInvantoryTextBox.AppendText(name + " INVENTORY:\n");
         displayInvantoryTextBox.AppendText(String.Format(
            "{0,-17}{1,-17}{2,-17}{3,-17}{4,-17}{5,-17}\n"+
            "{6,-17}{7,-17}{8,-17}{9,-17}{10,-17}{11,-17}\n"+
            "{12,-17}{13,-17}{14,-17}{15,-17}{16,-17}{17,-17}\n"+
            "{18,-17}{19,-17}\n\n",
            group[0], invantory[0], group[1], invantory[1],
            group[2], invantory[2], group[3], invantory[3],
            group[4], invantory[4], group[5], invantory[5],
            group[6], invantory[6], group[7], invantory[7],
            group[8], invantory[8], group[9], invantory[9]));
      }

      /* CHECK INDIVIDUAL INVENTORY FOR OUT OF STOCK ITEM */
      private void checkInvantory(string[] invantory)
      {
         for (int i = 0; i < invantory.Length; i++)
         {
            if (invantory[i] != "OUT OF STOCK")
               if (int.Parse(invantory[i]) < 1)
                  invantory[i] = "OUT OF STOCK";
         }
      }

      /* CHECK ALL INVENTORY FOR OUT OF STOCK ITEM */
      private void checkAllInvantory()
      {
         checkInvantory(breadInventory);
         checkInvantory(meatInventory);
         checkInvantory(cheeseInventory);
         checkInvantory(vegetableInventory);
         checkInvantory(sauceInvantory);
      }

      /* PREPAIR INVENTORY ARRAY FOR SAVING */
      private string arrayToString(Array array)
      {
         int count = 0;
         string temp = "";
         foreach (var item in array)
         {
            if (count < 9)
               temp += String.Format("{0},", item);
            else
               temp += String.Format("{0}\r\n", item);
            count++;
         }
         return temp;
      }

      /* SAVE INVENTORY TO FILE */
      private void saveInvantory()
      {
         checkAllInvantory();
         string temp = "";
         temp += arrayToString(breadInventory);
         temp += arrayToString(meatInventory);
         temp += arrayToString(cheeseInventory);
         temp += arrayToString(vegetableInventory);
         temp += arrayToString(sauceInvantory);
         File.WriteAllText(filename, temp);
         loadInvantory(); // get a fresh reload
      }

      /* USE ONE ITEM FROM THE INVENTORY */
      private void useInvantory(string[] group, int item)
      {
         if (group[item] != "OUT OF STOCK")
            group[item] = Convert.ToString((int.Parse(group[item]) - 1));
      }

      /* ADDS/TOPPS-OFF INVENTORY */
      private void resetInvantory(int quantity)
      {
         for (int i = 0; i <= 9; i++)
         {
             breadInventory[i] = quantity.ToString();
             meatInventory[i] = quantity.ToString();
             cheeseInventory[i] = quantity.ToString();
             vegetableInventory[i] = quantity.ToString();
             sauceInvantory[i] = quantity.ToString();
         }
         uncheckAll();
         enableCheckRadio();
         saveInvantory();
         displayAllInventory();
      }

      /* CHOOSE A BREAD */
      private void chooseBread(string[] item, string[] itemInvantory, int itemNum, double price, RadioButton thisButton)
      {
         if (thisButton.Checked == true) // add bread to sandwich
         {
            if (itemInvantory[itemNum] != "OUT OF STOCK") // make sure its in stock
            {
               sandwichIngredients.Add(item[itemNum]); // add item to the sanwich
               sandwichPrices.Add(price);  // add the price of the item to the sandwich
               useInvantory(itemInvantory, itemNum); // remove item from inventory
               displaySelectedItems();
               displayReciept();
               breadSelected = true; // allow other ingredence to be added to sandwich
            }
            else
            {
               thisButton.Enabled = false;
               thisButton.Checked = false;
               displayOrderTextBox.Text = "Sorry, That item is Out of Stock.";
            }

         }
         else // remove bread from sandwich
         {
            int index = sandwichIngredients.IndexOf(item[itemNum]); // find wich item to return
            if (index >= 0)
            {
               sandwichIngredients.RemoveAt(index); // remove item from the sanwich
               sandwichPrices.RemoveAt(index); // remove price of the item
               displayReciept();
               if (itemInvantory[itemNum] == "OUT OF STOCK") // encase used last bread
                  itemInvantory[itemNum] = "1";
               else // return bread to inventory
                  itemInvantory[itemNum] = Convert.ToString((int.Parse(itemInvantory[itemNum]) + 1));
            }
         }
      }

      /* ADD INGRIEDENT TO THE SANDWICH */
      private void addIngredient(string[] item, string[] itemInvantory, int itemNum, double price, CheckBox thisButton)
      {
         if (thisButton.Checked == true) // add ingredient to sandwic
         {
            if (breadIsSelected()) // checks if a bread has been chosen
            {
               if (itemInvantory[itemNum] != "OUT OF STOCK") // makes sure item is not out of stock
               {
                  sandwichIngredients.Add(item[itemNum]); // adds item to the sandwich list
                  sandwichPrices.Add(price); // adds item price to the sanwich list
                  useInvantory(itemInvantory, itemNum); // removes items used from invantory
                  displaySelectedItems();  
                  displayReciept();
               }
               else
               {
                  thisButton.Enabled = false;
                  thisButton.Checked = false;
                  displayOrderTextBox.Text = "Sorry, That item is Out of Stock.";
               }
            }
         }
         else // remove ingredient from sandwic
         {
            int index = sandwichIngredients.IndexOf(item[itemNum]); // find wich item to return
            if (index >= 0)
            {
               displayOrderTextBox.Text = (String.Format("Removed: {0,-15}\n",sandwichIngredients[index].ToString() ));
               sandwichIngredients.RemoveAt(index); // remove item from the sanwich
               sandwichPrices.RemoveAt(index); // remove price of the item
               displayReciept();
               if (itemInvantory[itemNum] == "OUT OF STOCK")
                  itemInvantory[itemNum] = "1";
               else // return item to inventory
                  itemInvantory[itemNum] = Convert.ToString((int.Parse(itemInvantory[itemNum]) + 1));
            }
         }
      }

      /* DISPLAY DELECTED ITEMS */
      private void displaySelectedItems()
      {
         displayOrderTextBox.Text = (String.Format("Add: {0,-15}{1,-15:C}\n",
               sandwichIngredients.Last().ToString(), Convert.ToDouble(sandwichPrices.Last())));
      }

      /* DISPLAY THE RECIEPT */
      private void displayReciept()
      {
         displayRecieptTextBox.Text = string.Empty;
         displayMadeSandwiches();
         if (sandwichPrices.Count > 0)
            displayRecieptTextBox.Text += "    Current Sandwich:\n";
         for (int i = 0; i < sandwichIngredients.Count(); i++)
         {
            displayRecieptTextBox.Text += (String.Format("      {0,-20}{1,8:C}\n",
               sandwichIngredients[i].ToString(), Convert.ToDouble(sandwichPrices[i])));
         }
         totalPrice();
      }

      /* DISPLAY THE ALREADY MADE SANDWICHES ON THE RECIEPT */
      private void displayMadeSandwiches()
      {
         allSubTotal = 0.00;
         if (allSandwichPrices.Count > 0)
         {
            for (int i = 0; i < allSandwichPrices.Count; i++)
            {
               displayRecieptTextBox.Text += (String.Format("  {0,-24}{1,8:C}\n",
                  allSandwiches[i].ToString(), allSandwichPrices[i]));
               allSubTotal += allSandwichPrices[i];
            }
            displayRecieptTextBox.Text += "\n";
         }
      }

      /* DISPLAY THE SUBTOTAL, TAX, AND TOTAL ON THE RECIEPT */
      private void totalPrice()
      {
         currentSandwichSubTotal = 0.00;
         subTotal = 0.00;
         totalCost = 0.00;
         displayRecieptTextBox.Text += "\n\n";
         foreach (var item in sandwichPrices)
            currentSandwichSubTotal += Convert.ToDouble(item);
         subTotal = currentSandwichSubTotal + allSubTotal;
         totalCost = (subTotal + (subTotal * 0.045));
         displayRecieptTextBox.Text += (String.Format("  {0,-24}{1,8:C}\n  {2,-24}{3,8:C}\n  {4}\n  {5,-24}{6,8:C}\n", 
            "Sub Total", subTotal,
            "Tax  (4.5%)", (subTotal * 0.045),
            "================================",
            "TOTAL", totalCost ));
      }

      /* ENABLE ALL CHECKBOXES AND RADIO BUTTONS */
      private void enableCheckRadio()
      {
         /* breads */
         whiteRadioButton.Enabled = true;
         wheatRadioButton.Enabled = true;
         ryeRadioButton.Enabled = true;
         multiGrainRadioButton.Enabled = true;
         ezekielRadioButton.Enabled = true;
         sourdoughRadioButton.Enabled = true;
         pumpernickelRadioButton.Enabled = true;
         ciabattaRadioButton.Enabled = true;
         focacciaRadioButton.Enabled = true;
         cinnamonRaisinRadioButton.Enabled = true;
         /*meats*/
         checkBoxTurkey.Enabled = true;
         checkBoxHam.Enabled = true;
         checkBoxRoastBeef.Enabled = true;
         checkBoxChicken.Enabled = true;
         checkBoxBacon.Enabled = true;
         checkBoxBologna.Enabled = true;
         checkBoxSalami.Enabled = true;
         checkBoxSausage.Enabled = true;
         checkBoxPepperoni.Enabled = true;
         checkBoxProsciutto.Enabled = true;
         /*cheeses*/
         checkBoxCheddar.Enabled = true;
         checkBoxSwiss.Enabled = true;
         checkBoxProvolone.Enabled = true;
         checkBoxPepperJack.Enabled = true;
         checkBoxAmerican.Enabled = true;
         checkBoxGouda.Enabled = true;
         checkBoxColby.Enabled = true;
         checkBoxMuenster.Enabled = true;
         checkBoxMontereyJack.Enabled = true;
         checkBoxBrie.Enabled = true;
         /*Vegetables*/
         checkBoxCucumbers.Enabled = true;
         checkBoxBellPeppers.Enabled = true;
         checkBoxLettuce.Enabled = true;
         checkBoxOnions.Enabled = true;
         checkBoxSpinach.Enabled = true;
         checkBoxTomatoes.Enabled = true;
         checkBoxJalapenos.Enabled = true;
         checkBoxOlives.Enabled = true;
         checkBoxPickles.Enabled = true;
         checkBoxAvocado.Enabled = true;
         /*sauces*/
         checkBoxMustard.Enabled = true;
         checkBoxMayonnaise.Enabled = true;
         checkBoxBarbecue.Enabled = true;
         checkBoxBlueCheese.Enabled = true;
         checkBoxBuffalo.Enabled = true;
         checkBoxItalian.Enabled = true;
         checkBoxHoneyMustard.Enabled = true;
         checkBoxVinaigrette.Enabled = true;
         checkBoxRanch.Enabled = true;
         checkBoxKetchup.Enabled = true;
      }

      /* UN-CHECK ALL BREADS */
      private void uncheckBreads()
      {
         whiteRadioButton.Checked = false;
         wheatRadioButton.Checked = false;
         ryeRadioButton.Checked = false;
         multiGrainRadioButton.Checked = false;
         ezekielRadioButton.Checked = false;
         sourdoughRadioButton.Checked = false;
         pumpernickelRadioButton.Checked = false;
         ciabattaRadioButton.Checked = false;
         focacciaRadioButton.Checked = false;
         cinnamonRaisinRadioButton.Checked = false;
      }

      /* UN-CHECK ALL MEATS */
      private void uncheckMeats()
      {
         checkBoxTurkey.Checked = false;
         checkBoxHam.Checked = false;
         checkBoxRoastBeef.Checked = false;
         checkBoxChicken.Checked = false;
         checkBoxBacon.Checked = false;
         checkBoxBologna.Checked = false;
         checkBoxSalami.Checked = false;
         checkBoxSausage.Checked = false;
         checkBoxPepperoni.Checked = false;
         checkBoxProsciutto.Checked = false;
      }

      /* UN-CHECK ALL CHEESES */
      private void uncheckCheeses()
      {
         checkBoxCheddar.Checked = false;
         checkBoxSwiss.Checked = false;
         checkBoxProvolone.Checked = false;
         checkBoxPepperJack.Checked = false;
         checkBoxAmerican.Checked = false;
         checkBoxGouda.Checked = false;
         checkBoxColby.Checked = false;
         checkBoxMuenster.Checked = false;
         checkBoxMontereyJack.Checked = false;
         checkBoxBrie.Checked = false;
      }

      /* UN-CHECK ALL VEGETABLES */
      private void uncheckVegetables()
      {
         checkBoxCucumbers.Checked = false;
         checkBoxBellPeppers.Checked = false;
         checkBoxLettuce.Checked = false;
         checkBoxOnions.Checked = false;
         checkBoxSpinach.Checked = false;
         checkBoxTomatoes.Checked = false;
         checkBoxJalapenos.Checked = false;
         checkBoxOlives.Checked = false;
         checkBoxPickles.Checked = false;
         checkBoxAvocado.Checked = false;
      }

      /* UN-CHECK ALL SAUSES */
      private void uncheckSauces()
      {
         checkBoxMustard.Checked = false;
         checkBoxMayonnaise.Checked = false;
         checkBoxBarbecue.Checked = false;
         checkBoxBlueCheese.Checked = false;
         checkBoxBuffalo.Checked = false;
         checkBoxItalian.Checked = false;
         checkBoxHoneyMustard.Checked = false;
         checkBoxVinaigrette.Checked = false;
         checkBoxRanch.Checked = false;
         checkBoxKetchup.Checked = false;
      }

      /* UN-CHECK EVERYTHING */
      private void uncheckAll()
      {
         uncheckBreads();
         uncheckMeats();
         uncheckCheeses();
         uncheckVegetables();
         uncheckSauces();
         breadSelected = false;
      }

      /* CHECK IF A BREAD HAS BEEN CHOSEN */
      private bool breadIsSelected()
      {
         if (breadSelected == true)
            return true;
         else
         {
            displayOrderTextBox.Text = "You must choose a bread first.";
            uncheckAll();
            return false;
         }
            
      }

      /* CANCEL THE ORDER */
      private void cancelOrder()
      {
         /* hide Checkout Tab pages */
         tabControl1.TabPages.Remove(checkoutTabPage);
         tabControl1.TabPages.Remove(orderTabPage);
         tabControl1.TabPages.Remove(inventoryTabPage);
         /* show Order Tab page */
         tabControl1.TabPages.Insert(0, orderTabPage);
         tabControl1.TabPages.Insert(1, inventoryTabPage);
         /* move main view back to breads tab page */
         tabControl2.SelectedTab = breadTabPage;
         /* clear and reset */
         uncheckAll();
         enableCheckRadio();
         clearMadeSandwiches();
         displayRecieptTextBox.Text = string.Empty;
         displayOrderTextBox.Text = "Order Cancled";
         creditDisplayTextBox.Text = string.Empty;
         /* reset feilds card fields */
         buttonSubmit.Enabled = true;
         textBoxCardNumber.Text = string.Empty;
         textBoxCVV.Text = string.Empty;
         comboBoxMonth.SelectedIndex = -1;
         comboBoxYear.SelectedIndex = -1;

         /* return origanal invantory */
         //breadInventory = tempBreadInventory;
         //meatInventory = tempMeatInventory;
         //cheeseInventory = tempCheeseInventory;
         //vegetableInventory = tempVegetableInventory;
         //sauceInvantory = tempSauceInventory;

         breadInventory = (string[])tempBreadInventory.Clone();
         meatInventory = (string[])tempMeatInventory.Clone();
         cheeseInventory = (string[])tempCheeseInventory.Clone();
         vegetableInventory = (string[])tempVegetableInventory.Clone();
         sauceInvantory = (string[])tempSauceInventory.Clone();

      }

      /* ADD A MADE SANDWICH */
      private void addSandwich()
      {
         allSandwichPrices.Add(currentSandwichSubTotal); // add sandwich to list of all sandwiches
         checkAllInvantory(); // Update invantory check
         /* copy invantory to temp - the next step (uncheckAll) will re-enable items uses */
         string[] tempB = (string[])breadInventory.Clone();
         string[] tempM = (string[])meatInventory.Clone();
         string[] tempC = (string[])cheeseInventory.Clone();
         string[] tempV = (string[])vegetableInventory.Clone();
         string[] tempS = (string[])sauceInvantory.Clone();

         /* clear order form for next sandwich */
         uncheckAll();
         enableCheckRadio();
         displayOrderTextBox.Text = "Ready for your next sandwich.";
         tabControl2.SelectedTab = breadTabPage;

         /* copy back invatory used */
         breadInventory = (string[])tempB.Clone();
         meatInventory = (string[])tempM.Clone();
         cheeseInventory = (string[])tempC.Clone();
         vegetableInventory = (string[])tempV.Clone();
         sauceInvantory = (string[])tempS.Clone();
      }

      /* CLEAR THE ALREADY MADE SANDWICHES */
      private void clearMadeSandwiches()
      {
         allSandwichPrices.Clear();
         totalCost = 0;
      }

      /* VALIDATE THE CREDIT CARD FOR APPROVAL */
      private bool validateCard()
      {
         if (textBoxCardNumber.Text != string.Empty) // check if card has been entered
            if (textBoxCVV.Text != string.Empty) // check if CVV has been entered
               if (comboBoxMonth.SelectedIndex > -1) // check if month has been selected
                  if (comboBoxYear.SelectedIndex > -1) // check if year has been selected
                     if (checkCardNumbers()) // check for valid card number
                        if (checkCVV()) // check for valid CVV
                           if (checkDate()) // check for valid experation date
                              return true; // ALL CHECKS OUT, THE CARD IS APPROVED!
                           else return false;
                        else return false;
                     else return false;
                  else
                  {
                     creditDisplayTextBox.Text = "ERROR! - Year.\nMake sure you select a year.";
                     return false;
                  }
               else
               {
                  creditDisplayTextBox.Text = "ERROR! - Month.\nMake sure you select a month.";
                  return false;
               }
            else
            {
               creditDisplayTextBox.Text = "ERROR! - CVV Number.\nMake sure you enter a credit card CVV number (Card Verification Value).";
               return false;
            }
         else
         {
            creditDisplayTextBox.Text = "ERROR! - Credit Card Number.\nMake sure you enter a credit card number.";
            return false;
         }
      }

      /* VALIDATE THE CREDIT CARD NUMBERS */
      private bool checkCardNumbers()
      {
         string temp = Regex.Replace(textBoxCardNumber.Text, @"[^\d]", ""); // remove all but numbers
          string firstFour = new string( temp.Take(4).ToArray());
          if (temp.Count() == 16) // valid cards begin with 1298, 1267, 4512, 4567, 8901, or 8933
              if (firstFour == "1298" | firstFour == "1267" | firstFour == "4512" |
                  firstFour == "4567" | firstFour == "8901" | firstFour == "8933")
                  return true;
              else
              {
                  creditDisplayTextBox.Text = "ERROR! - Credit Card Type.\nThe Credit Card Type you are using is incompatable.\nFirst 4 digits must be one of: 1298, 1267, 4512, 4567, 8901, 8933.";
                  return false;
              }
          else
          {
              creditDisplayTextBox.Text = "ERROR! - Credit Card Number.\nEnter 16 digit credit card number without spaces or dashes.\nExample: 1111222233334444";
              return false;
          }
      }

      /* VALIDATE THE CVV NUMBERS */
      private bool checkCVV()
      {
          string temp = Regex.Replace(textBoxCVV.Text, @"[^\d]", ""); // remove all but numbers
          if (temp.Count() == 3) return true;
          else
          {
              creditDisplayTextBox.Text = "ERROR! - CVV Number.\nEnter 3 digit CVV number (Card Verification Value).\nExample: 123";
              return false;
          }
      }

      /* VALIDATE THE EXPERATION DATE */
      private bool checkDate()
      {
         /* turn combo boxs into a DateTime type */
         DateTime cardDate = new DateTime(
            int.Parse(comboBoxYear.Text),
            int.Parse(comboBoxMonth.Text),
            DateTime.DaysInMonth(int.Parse(comboBoxYear.Text), int.Parse(comboBoxMonth.Text)) );
         /* minimum start date */
         DateTime minimumDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
            DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
         /* maximum end date */
         DateTime maxDate = minimumDate.AddYears(6);
         /* verify date is between min and max */
         if (cardDate >= minimumDate & cardDate <= maxDate)
            return true;
         else
         {
            creditDisplayTextBox.Text = "ERROR! - Invalid Experation Date.\nDate not within range.\nDate cannot be more tha 6 years out";
            return false;
         }
      }

      /* APPROVED TEXT ADDED TO RECIEPT */
      private void approvedText()
      {
         Random randNum = new Random(); // random number for Approval and Transaction #s
         DateTime today = DateTime.Now; // get todays time and date
         string temp = textBoxCardNumber.Text;
         string lastFour = temp.Substring(temp.Length - 4);
         recieptCheckoutTextBox.Text += (String.Format("\n\n\n  {0,-24}{1,8:C}\n  {2,-24}{3,8:C}\n  {4,-24}{5,8:C}\n\n  {6}",
            "Master Card", totalCost,
            "BALANCE:", 0.00,
            "CHANGE DUE:",0.00,
            "--------------------------------" ));

         recieptCheckoutTextBox.Text += (String.Format("\n\n  {0,-13}{1,8}\n  {2,-24}{3,8}\n  {4,-24}{5,8}\n\n  {6,-21}{7,8}\n\n\n\n{8,26}\n{9,26}",
            "Account #:", "**** **** **** "+ lastFour,
            "Approval #:", randNum.Next(9999,100000),
            "Transaction ID", randNum.Next(99, 10000),
            today.ToString("MM-dd-yyyy"), today.ToString("h:mm:ss tt"),
            "Have a blessed day!", 
            "Please come again!" ));
      }

      private void resetInvantoryButton_Click(object sender, EventArgs e)
      {
         resetInvantory(5);
      }

      private void testInventoryButton_Click(object sender, EventArgs e)
      {
         resetInvantory(1);
      }

      private void tabControl1_Selected(object sender, TabControlEventArgs e)
      {
         if (e.TabPage == inventoryTabPage)
         {
            checkAllInvantory();
            displayAllInventory();
         }
      }

      private void whiteRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 0, breadPrice, whiteRadioButton);
      }

      private void wheatRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 1, breadPrice, wheatRadioButton);
      }

      private void ryeRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 2, breadPrice, ryeRadioButton);
      }

      private void multiGrainRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 3, breadPrice, multiGrainRadioButton);
      }

      private void ezekielRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 4, breadPrice, ezekielRadioButton);
      }

      private void sourdoughRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 5, breadPrice, sourdoughRadioButton);
      }

      private void pumpernickelRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 6, breadPrice, pumpernickelRadioButton);
      }

      private void ciabattaRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 7, breadPrice, ciabattaRadioButton);
      }

      private void focacciaRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 8, breadPrice, focacciaRadioButton);
      }

      private void cinnamonRaisinRadioButton_CheckedChanged(object sender, EventArgs e)
      {
         chooseBread(breads, breadInventory, 9, breadPrice, cinnamonRaisinRadioButton);
      }

      private void checkBoxTurkey_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 0, meatPrice, checkBoxTurkey);
      }

      private void checkBoxHam_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 1, meatPrice, checkBoxHam);
      }

      private void checkBoxRoastBeef_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 2, meatPrice, checkBoxRoastBeef);
      }

      private void checkBoxChicken_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 3, meatPrice, checkBoxChicken);
      }

      private void checkBoxBacon_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 4, meatPrice, checkBoxBacon);
      }

      private void checkBoxBologna_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 5, meatPrice, checkBoxBologna);
      }

      private void checkBoxSalami_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 6, meatPrice, checkBoxSalami);
      }

      private void checkBoxSausage_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 7, meatPrice, checkBoxSausage);
      }

      private void checkBoxPepperoni_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 8, meatPrice, checkBoxPepperoni);
      }

      private void checkBoxProsciutto_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(meats, meatInventory, 9, meatPrice, checkBoxProsciutto);
      }

      private void checkBoxCheddar_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 0, cheesePrice, checkBoxCheddar);
      }

      private void checkBoxSwiss_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 1, cheesePrice, checkBoxSwiss);
      }

      private void checkBoxProvolone_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 2, cheesePrice, checkBoxProvolone);
      }

      private void checkBoxPepperJack_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 3, cheesePrice, checkBoxPepperJack);
      }

      private void checkBoxAmerican_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 4, cheesePrice, checkBoxAmerican);
      }

      private void checkBoxGouda_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 5, cheesePrice, checkBoxGouda);
      }

      private void checkBoxColby_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 6, cheesePrice, checkBoxColby);
      }

      private void checkBoxMuenster_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 7, cheesePrice, checkBoxMuenster);
      }

      private void checkBoxMontereyJack_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 8, cheesePrice, checkBoxMontereyJack);
      }

      private void checkBoxBrie_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(cheeses, cheeseInventory, 9, cheesePrice, checkBoxBrie);
      }

      private void checkBoxCucumbers_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 0, vegetablePrice, checkBoxCucumbers);
      }

      private void checkBoxBellPeppers_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 1, vegetablePrice, checkBoxBellPeppers);
      }

      private void checkBoxLettuce_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 2, vegetablePrice, checkBoxLettuce);
      }

      private void checkBoxOnions_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 3, vegetablePrice, checkBoxOnions);
      }

      private void checkBoxSpinach_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 4, vegetablePrice, checkBoxSpinach);
      }

      private void checkBoxTomatoes_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 5, vegetablePrice, checkBoxTomatoes);
      }

      private void checkBoxJalapenos_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 6, vegetablePrice, checkBoxJalapenos);
      }

      private void checkBoxOlives_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 7, vegetablePrice, checkBoxOlives);
      }

      private void checkBoxPickles_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 8, vegetablePrice, checkBoxPickles);
      }

      private void checkBoxAvocado_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(vegetables, vegetableInventory, 9, vegetablePrice, checkBoxAvocado);
      }

      private void checkBoxMustard_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 0, saucePrice, checkBoxMustard);
      }

      private void checkBoxMayonnaise_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 1, saucePrice, checkBoxMayonnaise);
      }

      private void checkBoxBarbecue_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 2, saucePrice, checkBoxBarbecue);
      }

      private void checkBoxBlueCheese_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 3, saucePrice, checkBoxBlueCheese);
      }

      private void checkBoxBuffalo_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 4, saucePrice, checkBoxBuffalo);
      }

      private void checkBoxItalian_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 5, saucePrice, checkBoxItalian);
      }

      private void checkBoxHoneyMustard_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 6, saucePrice, checkBoxHoneyMustard);
      }

      private void checkBoxVinaigrette_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 7, saucePrice, checkBoxVinaigrette);
      }

      private void checkBoxRanch_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 8, saucePrice, checkBoxRanch);
      }

      private void checkBoxKetchup_CheckedChanged(object sender, EventArgs e)
      {
         addIngredient(sauces, sauceInvantory, 9, saucePrice, checkBoxKetchup);
      }

      private void nextBreadsButton_Click(object sender, EventArgs e)
      {
         tabControl2.SelectedTab = meatTabPage;
      }

      private void nextMeatsButton_Click(object sender, EventArgs e)
      {
         tabControl2.SelectedTab = cheeseTabPage;
      }

      private void nextCheesesButton_Click(object sender, EventArgs e)
      {
         tabControl2.SelectedTab = additionalTabPage;
      }

      private void prevAdditionalButton_Click(object sender, EventArgs e)
      {
         tabControl2.SelectedTab = cheeseTabPage;
      }

      private void prevCheesesButton_Click(object sender, EventArgs e)
      {
         tabControl2.SelectedTab = meatTabPage;
      }

      private void prevMeatsButton_Click(object sender, EventArgs e)
      {
         tabControl2.SelectedTab = breadTabPage;
      }

      private void clearMeatsButton_Click(object sender, EventArgs e)
      {
         uncheckMeats();
      }

      private void clearCheesesButton_Click(object sender, EventArgs e)
      {
         uncheckCheeses();
      }

      private void clearAdditionalsButton_Click(object sender, EventArgs e)
      {
         uncheckVegetables();
         uncheckSauces();
      }

      private void buttonCancelOreder_Click(object sender, EventArgs e)
      {
         cancelOrder();
      }

      private void buttonAddSandwich_Click(object sender, EventArgs e)
      {
         if (allSandwichPrices.Count < 9) // only allowed 10 sandwiches per order
         {
            if (currentSandwichSubTotal > 0) // make sure sandwich has content before making a new one
            {
               addSandwich();
            }
            else
               displayOrderTextBox.Text = "Ready for your next sandwich.\nPlease choose your bread.";
         }
         else
            displayOrderTextBox.Text = "Sorry, ten sandwiches max.\nYou have already made ten." +
               "\n\nPlease proceed to checkout.";
      }

      private void buttonCheckout_Click(object sender, EventArgs e)
      {
         if (totalCost > 0) // make sure sandwich has content before making a new one
         {

            addSandwich();
            /* reset feilds card fields */
            buttonSubmit.Enabled = true;
            textBoxCardNumber.Text = string.Empty;
            textBoxCVV.Text = string.Empty;
            comboBoxMonth.SelectedIndex = -1;
            comboBoxYear.SelectedIndex = -1;
            /* hide finish button */
            buttonFinished.Visible = false;
            /* hide PlaceOrder and Invantory Tab pages */
            tabControl1.TabPages.Remove(orderTabPage);
            tabControl1.TabPages.Remove(inventoryTabPage);
            /* show Checkout Tab page */
            tabControl1.TabPages.Insert(0, checkoutTabPage);
            fraudControl = 0;


            /* Display reciept */
            recieptCheckoutTextBox.Text = string.Empty;
            recieptCheckoutTextBox.Text = displayRecieptTextBox.Text;
            labelAmount.Text = string.Format("Amount to be charged:\n{0:C}", totalCost);
         }
         else
            displayOrderTextBox.Text = "Please create at least one sandwich before proceeding to checkout.";
      }

      private void buttonSubmit_Click(object sender, EventArgs e)
      {
         fraudControl++; // add attempt to procces card
         if (validateCard())
         {
            /* show invantory Tab page */
            tabControl1.TabPages.Insert(1, inventoryTabPage);
            buttonFinished.Visible = true;
            buttonSubmit.Enabled = false;
            creditDisplayTextBox.Text = "APPROVED!\n\nThank you for your order!\nPlease come again!\n\nAPPROVED!";
            approvedText();
            /* save inventory */
            saveInvantory();
         }
         else
         {
            if (fraudControl < 3) // cancel order on 3rd failed attempt to run card
               creditDisplayTextBox.Text += "\n\nWARNING - Fraud Control!\nOrder is caneled after 3rd attempt.";
            else
               cancelOrder();
         }
      }

      private void buttonFinished_Click(object sender, EventArgs e)
      {
         /* show Order Tab page */
         tabControl1.TabPages.Insert(0, orderTabPage);
         /* hide Checkout Tab pages */
         tabControl1.TabPages.Remove(checkoutTabPage);
         /* clear and reset */
         uncheckAll();
         enableCheckRadio();
         clearMadeSandwiches();
         displayRecieptTextBox.Text = string.Empty;
         displayOrderTextBox.Text = string.Empty;
         /* move main view back to breads tab page */
         tabControl2.SelectedTab = breadTabPage;
      }
   }
}