Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Item
- {
- /// <summary>
- /// A class that is used for products in a store.
- /// Each product has the cost, stock level and product name.
- /// </summary>
- public int cost = 0;
- public int stock = 0;
- public string name = ""; // name is set blank as set with Constructor Method
- // This is a constructor method. It is part of the class code and used so
- // that when you creat an object from your class you can also assign
- // their stats within the brackets.
- public Item(int itemCost, int itemStock, string itemName)
- {
- cost = itemCost;
- stock = itemStock;
- name = itemName;
- }
- }
- namespace ShoppingCartExample
- {
- /// <summary>
- /// A simple shopping cart program with preset product and stock values
- /// that uses all the subjects that have been covered on Coding 101.
- ///
- /// This program does use arrays which have not yet been covered however
- /// as a personal learning adventure for myself who was having difficulties
- /// with arrays, this has been very usefull.
- /// </summary>
- class Program
- {
- static void Main(string[] args)
- {
- PrintWelcome();
- // Set up our stock of items (Cost / Stock / Name)
- // Thanks to the way the class was coded above I can set
- // the value of each criteria of the object as I go.
- Item banana = new Item(1, 5, "Banana");
- Item apple = new Item(1, 3, "Apple");
- Item pear = new Item(1, 7, "Pear");
- Item microBurger = new Item(4, 3, "Microwave Burger");
- Item microRoast = new Item(6, 2, "Beef Dinner");
- Item cola = new Item(2, 7, "Can of Cola");
- Item lemonade = new Item(2, 9, "Can of Lemonade");
- Item oJ = new Item(2, 7, "Orange Juice");
- Item milk = new Item(2, 4, "Fresh Local Milk");
- Item chicken = new Item(3, 3, "Fresh Chicken");
- Item beef = new Item(3, 2, "Fresh Beef");
- Item unknownmeat = new Item(300, 1, "Unknown Meat...");
- Item lamb = new Item(3, 6, "Fresh Lamb");
- // Create an Object Array to hold our item objects
- // I do this as it makes it easier to group our similar items
- // together as well as for easier manipulation
- // and listing our items later on.
- Item[] fruits = { banana, apple, pear };
- Item[] microwaves = { microBurger, microRoast };
- Item[] drinks = { cola, lemonade, oJ, milk };
- Item[] meats = { chicken, beef, unknownmeat, lamb };
- // The visual stuff starts from this point here.
- Console.Clear();
- while (true) // Run the program on a loop so it only ends when we want it to
- {
- PrintWelcome(); // Method / Function that is create at the bottom of the code
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("PLEASE SELECT A CATAGORY TO VIEW");
- Console.ResetColor();
- Console.WriteLine();
- Console.WriteLine("FRUIT \t\t(1)");
- Console.WriteLine("READY MEALS \t(2)");
- Console.WriteLine("DRINKS \t\t(3)");
- Console.WriteLine("MEATS \t\t(4)");
- Console.WriteLine("exit \t\t(5)");
- Console.WriteLine();
- Console.Write("YOUR CHOICE (FROM 1 TO 5): ");
- int choice = Int16.Parse(Console.ReadLine()); // Expect an Int
- Console.WriteLine();
- if (choice == 1)
- {
- ListItems(fruits); // Send our array of fruit objects to ListItem
- }
- else if (choice == 2)
- {
- ListItems(microwaves);
- }
- else if (choice == 3)
- {
- ListItems(drinks);
- }
- else if (choice == 4)
- {
- ListItems(meats);
- }
- else if (choice == 5)
- {
- break; // Ends the program
- }
- else
- {
- Console.Clear();
- continue; // If anything else is entered then loop
- }
- }
- }
- // ### Methods / Functions start here ######################
- static void ListItems(Item[] item) // Recieve what ever array is sent (fruits, meats etc)
- {
- Console.Clear(); // Clear in case anything is in console to make it look nicer
- PrintWelcome();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("LIST OF STOCK");
- Console.ResetColor();
- Console.WriteLine();
- // This names all the objects in the array as products and loops
- // through them all printing their stats
- foreach (var product in item)
- {
- Console.WriteLine("Name: {0} \tStock: {1} \tPrice: ${2}", product.name, product.stock, product.cost);
- }
- Console.WriteLine();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("WHAT WOULD YOU LIKE TO DO?");
- Console.ResetColor();
- Console.WriteLine();
- Console.WriteLine("PURCHASE AN ITEM \t(1)");
- Console.WriteLine("RETURN TO MAIN MENU \t(2)");
- Console.WriteLine();
- Console.Write("YOUR CHOICE (1 OR 2): ");
- int choice = Int16.Parse(Console.ReadLine());
- if (choice == 1)
- {
- PurchaseItem(item); // Send the same Arra of objects that was sent to us
- // previously to the PurchaseItem Method / Function
- }
- else
- {
- Console.Clear();
- return; // Return to the main menu loop
- }
- }
- static void PurchaseItem(Item[] item)
- {
- Console.Clear();
- PrintWelcome();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("WHICH ITEM DO YOU WISH TO PURCHASE?");
- Console.ResetColor();
- Console.WriteLine();
- int listNum = 1; // Create list number to add to the menu choice
- // This loops through each object (product) and lists the specified info
- // that we ask it to and then adds a menu choice number next to it.
- // Since each array could have any amount of products we get it to use
- // listNum which will start at one and increase for each product and put
- // the increased number next to it so we have a number next to every
- // product without knowing how many products are sent to us.
- foreach (var product in item)
- {
- Console.WriteLine("Name: {0} \tStock: {1}", product.name, product.stock + " \t(" + listNum + ")");
- listNum++;
- }
- Console.WriteLine();
- listNum--;
- Console.Write("YOUR CHOICE (FROM 1 TO {0}): ", listNum); // Use List num for the same
- // reason above. We dont know
- // how many we have
- int choice = Int16.Parse(Console.ReadLine());
- choice--; // Since C# starts lists at 0 but we have the first option as 1 we
- // choice-- so that it matches with the computers listing methods
- Console.WriteLine();
- Console.Write("HOW MANY?: ");
- int many = Int16.Parse(Console.ReadLine());
- if (item[choice].stock >= many) // if we have enough stock then deduct from stock total
- {
- item[choice].stock = item[choice].stock - many;
- Console.WriteLine();
- Console.WriteLine("PURCHASE SUCCESFUL");
- Console.WriteLine();
- Console.WriteLine("PRESS ENTER TO RETURN TO THE MAIN MENU");
- Console.ReadLine();
- }
- else
- {
- Console.WriteLine();
- Console.WriteLine("SORRY THERE IS NOT ENOUGH STOCK AVAILABLE");
- Console.WriteLine();
- Console.WriteLine("PRESS ENTER TO RETURN TO THE MAIN MENU");
- Console.ReadLine();
- }
- Console.Clear();
- }
- static void PrintWelcome()
- {
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("############################################CODE*MART ver.1##");
- Console.WriteLine("## _____ _____ ____ _____ _____ _____ _____ _____ _____ ##");
- Console.WriteLine("## | | | '| __| | | | | _ | __ |_ _| ##");
- Console.WriteLine("## | --| | | | | __|- -| | | | | -| | | ##");
- Console.WriteLine("## |_____|_____|____/|_____|_|_|_|_|_|_|__|__|__|__| |_| ##");
- Console.WriteLine("## EXAMPLE CODE DESIGNED BY DEAN T (@Dosk3n) FOR CODING101 ##");
- Console.WriteLine("## ##");
- Console.WriteLine("#############################################################");
- Console.WriteLine();
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement