Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ConsoleMenu(System.IO.Stream outputStream, System.IO.Stream inputStream)
- {
- if (!typeof(T).IsEnum)
- {
- throw new TypeAccessException("Type" + typeof(T).Name + "must be of type Enum!");
- }
- OutputStream = outputStream;
- InputStream = inputStream;
- }
- public void ShowMenu(bool parseUnderlinetoWhitespace = true)
- {
- this.CheckStreams();
- var streamWriter = new System.IO.StreamWriter(this.OutputStream) { AutoFlush = true };
- var memberInfos = typeof(T).GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
- foreach (var memberInfo in memberInfos)
- {
- var menuEntryText = memberInfo.Name;
- var menuEntryNumber = (int)Enum.Parse(typeof(T), memberInfo.Name);
- streamWriter.WriteLine("{0} - {1}", menuEntryNumber, menuEntryText);
- }
- streamWriter.WriteLine();
- }
- private void CheckStreams()
- {
- if (InputStream == null)
- {
- throw new NullReferenceException("InputStream must be a valid stream!");
- }
- if (OutputStream == null)
- {
- throw new NullReferenceException("OutputStream must be a valid stream!");
- }
- }
- public enum Products
- {
- Display_Product = 1,
- Add_Product = 2,
- Edit_Product = 3,
- Delete_Product = 4,
- Search_Product = 5,
- Exit = 6
- }
- }
- public class Product
- {
- public string Name { get; set; }
- public decimal Quantity { get; set; }
- }
- public class Inventory
- {
- private List<Product> Products { get; set; }
- public int Count { get { return this.Products.Count; } }
- public IEnumerable<Product> GetAllProducts()
- {
- return this.Products.AsReadOnly();
- }
- public void RemoveByName(string productName)
- {
- var p = this.FindByName(productName);
- if (p != null) Products.Remove(p);
- }
- public void AddProduct(string productName, decimal productQuantity)
- {
- Products.Add(new Product() { Name = productName, Quantity = productQuantity });
- }
- public Product FindByName(string productName)
- {
- foreach (Product p in this.Products)
- {
- if (p.Name == productName)
- {
- return p;
- }
- }
- return null;
- }
- }
- class Program
- {
- //The program starts here.
- static void Main(string[] args)
- {
- string valueString = "Welcome to Swedish Mosaic AB Ordersystem";
- Console.WriteLine(valueString);
- string username, password;
- Console.Write("Administrator Username: ");
- username = Console.ReadLine();
- Console.Write("Enter your password: ");
- password = Console.ReadLine();
- Console.WriteLine("============");
- if (username == "Mosaic" && password == "123")
- {
- Console.WriteLine("Login successful");
- }
- else
- {
- Console.WriteLine("Login failed!");
- }
- if (bool true)
- }
- public static void MainMenu(string[] args)
- {
- var category = 0;
- var menuOption = new string[] { "Display Product", "Add Product", "Edit Product", "Delete Product", "Search Product", "Exit" };
- var methodCall = new string[] { "Display Product", "Add Product", "Edit Product", "Delete Product", "Search Product", "Exit" };
- do
- {
- category = Menu("MainMenu", menuOption, methodCall);
- Console.WriteLine("You choose: {0}", category);
- }
- while (category != 0);
- }
- static int Menu(string titel, string[] alternativ)
- {
- return DisplayAlternativ(titel, alternativ);
- }
- static int Menu(string titel, string[] alternativ, string [] metod)
- {
- var val = DisplayAlternativ(titel, alternativ);
- if (val == 0)
- return 0;
- Program p = new Program();
- Type t = p.GetType();
- var methodCall = metod[val - 1];
- MethodInfo mi = t.GetMethod(methodCall, BindingFlags.NonPublic | BindingFlags.Instance);
- string result = mi.Invoke(p, new object[] { 1 }).ToString();
- return val;
- }
- static int DisplayAlternativ(String titel, string[] alternativ)
- {
- Console.WriteLine(titel);
- Console.WriteLine();
- for (int i = 0; i < alternativ.Count(); i++)
- {
- Console.WriteLine("{0} - {1}", i + 1, alternativ[i]);
- }
- Console.WriteLine();
- Console.WriteLine("0 - Avsluta");
- bool conversionOK;
- var val = -1;
- do
- {
- var read = Console.ReadLine();
- conversionOK = int.TryParse(read, out val);
- if (conversionOK == true && val == 0)
- return 0;
- if (conversionOK == true && val >= 1 && val <= alternativ.Count())
- break;
- Console.WriteLine("Wrong input: " + read);
- } while (true);
- return val;
- }
- int DisplayProduct(int arg = 1)
- {
- Console.WriteLine("Display Product");
- return 1;
- }
- int AddProduct(int arg = 1)
- {
- Console.WriteLine("AddProduct");
- return 1;
- }
- int EditProduct(int arg = 1)
- {
- Console.WriteLine("EditProduct");
- return 1;
- }
- int DeleteProduct(int arg = 1)
- {
- Console.WriteLine("Delete Product");
- return 1;
- }
- int SearchProduct(int arg = 1)
- {
- Console.WriteLine("SearchProduct");
- return 1;
- }
- int Exit(int arg = 1)
- {
- Console.WriteLine("Exit");
- return 1;
- }
- Console.WriteLine("Category selected");
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement