Advertisement
Dosk3n

CODEMARTC101

Mar 15th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. public class Item
  8. {
  9.     /// <summary>
  10.     /// A class that is used for products in a store.
  11.     /// Each product has the cost, stock level and product name.
  12.     /// </summary>
  13.     public int cost = 0;
  14.     public int stock = 0;
  15.     public string name = ""; // name is set blank as set with Constructor Method
  16.  
  17.     // This is a constructor method. It is part of the class code and used so
  18.     // that when you creat an object from your class you can also assign
  19.     // their stats within the brackets.
  20.     public Item(int itemCost, int itemStock, string itemName)
  21.     {
  22.         cost = itemCost;
  23.         stock = itemStock;
  24.         name = itemName;
  25.     }
  26. }
  27.  
  28. namespace ShoppingCartExample
  29. {
  30.     /// <summary>
  31.     /// A simple shopping cart program with preset product and stock values
  32.     /// that uses all the subjects that have been covered on Coding 101.
  33.     ///
  34.     /// This program does use arrays which have not yet been covered however
  35.     /// as a personal learning adventure for myself who was having difficulties
  36.     /// with arrays, this has been very usefull.
  37.     /// </summary>
  38.     class Program
  39.     {
  40.         static void Main(string[] args)
  41.         {
  42.             PrintWelcome();
  43.             // Set up our stock of items (Cost / Stock / Name)
  44.             // Thanks to the way the class was coded above I can set
  45.             // the value of each criteria of the object as I go.
  46.             Item banana = new Item(1, 5, "Banana");
  47.             Item apple = new Item(1, 3, "Apple");
  48.             Item pear = new Item(1, 7, "Pear");
  49.             Item microBurger = new Item(4, 3, "Microwave Burger");
  50.             Item microRoast = new Item(6, 2, "Beef Dinner");
  51.             Item cola = new Item(2, 7, "Can of Cola");
  52.             Item lemonade = new Item(2, 9, "Can of Lemonade");
  53.             Item oJ = new Item(2, 7, "Orange Juice");
  54.             Item milk = new Item(2, 4, "Fresh Local Milk");
  55.             Item chicken = new Item(3, 3, "Fresh Chicken");
  56.             Item beef = new Item(3, 2, "Fresh Beef");
  57.             Item unknownmeat = new Item(300, 1, "Unknown Meat...");
  58.             Item lamb = new Item(3, 6, "Fresh Lamb");
  59.             // Create an Object Array to hold our item objects
  60.             // I do this as it makes it easier to group our similar items
  61.             // together as well as for easier manipulation
  62.             // and listing our items later on.
  63.             Item[] fruits = { banana, apple, pear };
  64.             Item[] microwaves = { microBurger, microRoast };
  65.             Item[] drinks = { cola, lemonade, oJ, milk };
  66.             Item[] meats = { chicken, beef, unknownmeat, lamb };
  67.  
  68.             // The visual stuff starts from this point here.
  69.             Console.Clear();
  70.             while (true) // Run the program on a loop so it only ends when we want it to
  71.             {
  72.                 PrintWelcome(); // Method / Function that is create at the bottom of the code
  73.                 Console.ForegroundColor = ConsoleColor.Blue;
  74.                 Console.WriteLine("PLEASE SELECT A CATAGORY TO VIEW");
  75.                 Console.ResetColor();
  76.                 Console.WriteLine();
  77.                 Console.WriteLine("FRUIT \t\t(1)");
  78.                 Console.WriteLine("READY MEALS \t(2)");
  79.                 Console.WriteLine("DRINKS \t\t(3)");
  80.                 Console.WriteLine("MEATS \t\t(4)");
  81.                 Console.WriteLine("exit \t\t(5)");
  82.                 Console.WriteLine();
  83.                 Console.Write("YOUR CHOICE (FROM 1 TO 5): ");
  84.                 int choice = Int16.Parse(Console.ReadLine()); // Expect an Int
  85.                 Console.WriteLine();
  86.  
  87.                 if (choice == 1)
  88.                 {
  89.                     ListItems(fruits); // Send our array of fruit objects to ListItem
  90.                 }
  91.                 else if (choice == 2)
  92.                 {
  93.                     ListItems(microwaves);
  94.                 }
  95.                 else if (choice == 3)
  96.                 {
  97.                     ListItems(drinks);
  98.                 }
  99.                 else if (choice == 4)
  100.                 {
  101.                     ListItems(meats);
  102.                 }
  103.                 else if (choice == 5)
  104.                 {
  105.                     break; // Ends the program
  106.                 }
  107.                 else
  108.                 {
  109.                     Console.Clear();
  110.                     continue; // If anything else is entered then loop
  111.                 }
  112.             }
  113.         }
  114.  
  115.         // ### Methods / Functions start here ######################
  116.  
  117.         static void ListItems(Item[] item) // Recieve what ever array is sent (fruits, meats etc)
  118.         {
  119.             Console.Clear(); // Clear in case anything is in console to make it look nicer
  120.             PrintWelcome();
  121.             Console.ForegroundColor = ConsoleColor.Blue;
  122.             Console.WriteLine("LIST OF STOCK");
  123.             Console.ResetColor();
  124.             Console.WriteLine();
  125.             // This names all the objects in the array as products and loops
  126.             // through them all printing their stats
  127.             foreach (var product in item)
  128.             {
  129.                 Console.WriteLine("Name: {0} \tStock: {1} \tPrice: ${2}", product.name, product.stock, product.cost);
  130.             }
  131.             Console.WriteLine();
  132.             Console.ForegroundColor = ConsoleColor.Blue;
  133.             Console.WriteLine("WHAT WOULD YOU LIKE TO DO?");
  134.             Console.ResetColor();
  135.             Console.WriteLine();
  136.             Console.WriteLine("PURCHASE AN ITEM \t(1)");
  137.             Console.WriteLine("RETURN TO MAIN MENU \t(2)");
  138.             Console.WriteLine();
  139.             Console.Write("YOUR CHOICE (1 OR 2): ");
  140.             int choice = Int16.Parse(Console.ReadLine());
  141.             if (choice == 1)
  142.             {
  143.                 PurchaseItem(item); // Send the same Arra of objects that was sent to us
  144.                                     // previously to the PurchaseItem Method / Function
  145.             }
  146.             else
  147.             {
  148.                 Console.Clear();
  149.                 return; // Return to the main menu loop
  150.             }
  151.         }
  152.  
  153.  
  154.         static void PurchaseItem(Item[] item)
  155.         {
  156.             Console.Clear();
  157.             PrintWelcome();
  158.             Console.ForegroundColor = ConsoleColor.Blue;
  159.             Console.WriteLine("WHICH ITEM DO YOU WISH TO PURCHASE?");
  160.             Console.ResetColor();
  161.             Console.WriteLine();
  162.             int listNum = 1; // Create list number to add to the menu choice
  163.             // This loops through each object (product) and lists the specified info
  164.             // that we ask it to and then adds a menu choice number next to it.
  165.             // Since each array could have any amount of products we get it to use
  166.             // listNum which will start at one and increase for each product and put
  167.             // the increased number next to it so we have a number next to every
  168.             // product without knowing how many products are sent to us.
  169.             foreach (var product in item)
  170.             {
  171.                 Console.WriteLine("Name: {0} \tStock: {1}", product.name, product.stock + " \t(" + listNum + ")");
  172.                 listNum++;
  173.             }
  174.             Console.WriteLine();
  175.             listNum--;
  176.             Console.Write("YOUR CHOICE (FROM 1 TO {0}): ", listNum); // Use List num for the same
  177.                                                                      // reason above. We dont know
  178.                                                                      // how many we have
  179.             int choice = Int16.Parse(Console.ReadLine());
  180.             choice--; // Since C# starts lists at 0 but we have the first option as 1 we
  181.                       // choice-- so that it matches with the computers listing methods
  182.             Console.WriteLine();
  183.             Console.Write("HOW MANY?: ");
  184.             int many = Int16.Parse(Console.ReadLine());
  185.  
  186.             if (item[choice].stock >= many) // if we have enough stock then deduct from stock total
  187.             {
  188.                 item[choice].stock = item[choice].stock - many;
  189.                 Console.WriteLine();
  190.                 Console.WriteLine("PURCHASE SUCCESFUL");
  191.                 Console.WriteLine();
  192.                 Console.WriteLine("PRESS ENTER TO RETURN TO THE MAIN MENU");
  193.                 Console.ReadLine();
  194.             }
  195.             else
  196.             {
  197.                 Console.WriteLine();
  198.                 Console.WriteLine("SORRY THERE IS NOT ENOUGH STOCK AVAILABLE");
  199.                 Console.WriteLine();
  200.                 Console.WriteLine("PRESS ENTER TO RETURN TO THE MAIN MENU");
  201.                 Console.ReadLine();
  202.             }
  203.             Console.Clear();
  204.         }
  205.  
  206.         static void PrintWelcome()
  207.         {
  208.             Console.ForegroundColor = ConsoleColor.Blue;
  209.             Console.WriteLine("############################################CODE*MART ver.1##");
  210.             Console.WriteLine("##  _____ _____ ____  _____ _____ _____ _____ _____ _____  ##");
  211.             Console.WriteLine("## |     |     |    '|   __| | | |     |  _  | __  |_   _| ##");
  212.             Console.WriteLine("## |   --|  |  |  |  |   __|-   -| | | |     |    -| | |   ##");
  213.             Console.WriteLine("## |_____|_____|____/|_____|_|_|_|_|_|_|__|__|__|__| |_|   ##");
  214.             Console.WriteLine("## EXAMPLE CODE DESIGNED BY DEAN T (@Dosk3n) FOR CODING101 ##");
  215.             Console.WriteLine("##                                                         ##");
  216.             Console.WriteLine("#############################################################");
  217.             Console.WriteLine();
  218.             Console.ResetColor();
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement