Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. public ConsoleMenu(System.IO.Stream outputStream, System.IO.Stream inputStream)
  2. {
  3. if (!typeof(T).IsEnum)
  4. {
  5. throw new TypeAccessException("Type" + typeof(T).Name + "must be of type Enum!");
  6. }
  7.  
  8. OutputStream = outputStream;
  9. InputStream = inputStream;
  10. }
  11.  
  12. public void ShowMenu(bool parseUnderlinetoWhitespace = true)
  13. {
  14. this.CheckStreams();
  15.  
  16. var streamWriter = new System.IO.StreamWriter(this.OutputStream) { AutoFlush = true };
  17. var memberInfos = typeof(T).GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
  18.  
  19. foreach (var memberInfo in memberInfos)
  20. {
  21. var menuEntryText = memberInfo.Name;
  22. var menuEntryNumber = (int)Enum.Parse(typeof(T), memberInfo.Name);
  23.  
  24. streamWriter.WriteLine("{0} - {1}", menuEntryNumber, menuEntryText);
  25. }
  26.  
  27. streamWriter.WriteLine();
  28. }
  29.  
  30. private void CheckStreams()
  31. {
  32. if (InputStream == null)
  33. {
  34. throw new NullReferenceException("InputStream must be a valid stream!");
  35. }
  36. if (OutputStream == null)
  37. {
  38. throw new NullReferenceException("OutputStream must be a valid stream!");
  39. }
  40. }
  41.  
  42. public enum Products
  43. {
  44. Display_Product = 1,
  45. Add_Product = 2,
  46. Edit_Product = 3,
  47. Delete_Product = 4,
  48. Search_Product = 5,
  49. Exit = 6
  50. }
  51. }
  52.  
  53. public class Product
  54. {
  55. public string Name { get; set; }
  56. public decimal Quantity { get; set; }
  57. }
  58.  
  59. public class Inventory
  60. {
  61. private List<Product> Products { get; set; }
  62.  
  63. public int Count { get { return this.Products.Count; } }
  64.  
  65. public IEnumerable<Product> GetAllProducts()
  66. {
  67. return this.Products.AsReadOnly();
  68. }
  69.  
  70. public void RemoveByName(string productName)
  71. {
  72. var p = this.FindByName(productName);
  73. if (p != null) Products.Remove(p);
  74. }
  75.  
  76. public void AddProduct(string productName, decimal productQuantity)
  77. {
  78. Products.Add(new Product() { Name = productName, Quantity = productQuantity });
  79. }
  80.  
  81. public Product FindByName(string productName)
  82. {
  83. foreach (Product p in this.Products)
  84. {
  85. if (p.Name == productName)
  86. {
  87. return p;
  88. }
  89. }
  90. return null;
  91. }
  92. }
  93.  
  94. class Program
  95. {
  96. //The program starts here.
  97. static void Main(string[] args)
  98. {
  99. string valueString = "Welcome to Swedish Mosaic AB Ordersystem";
  100. Console.WriteLine(valueString);
  101.  
  102. string username, password;
  103. Console.Write("Administrator Username: ");
  104. username = Console.ReadLine();
  105.  
  106. Console.Write("Enter your password: ");
  107. password = Console.ReadLine();
  108. Console.WriteLine("============");
  109. if (username == "Mosaic" && password == "123")
  110. {
  111. Console.WriteLine("Login successful");
  112. }
  113. else
  114. {
  115. Console.WriteLine("Login failed!");
  116. }
  117. if (bool true)
  118.  
  119. }
  120.  
  121.  
  122.  
  123. public static void MainMenu(string[] args)
  124. {
  125. var category = 0;
  126. var menuOption = new string[] { "Display Product", "Add Product", "Edit Product", "Delete Product", "Search Product", "Exit" };
  127. var methodCall = new string[] { "Display Product", "Add Product", "Edit Product", "Delete Product", "Search Product", "Exit" };
  128. do
  129. {
  130. category = Menu("MainMenu", menuOption, methodCall);
  131.  
  132. Console.WriteLine("You choose: {0}", category);
  133. }
  134. while (category != 0);
  135. }
  136.  
  137. static int Menu(string titel, string[] alternativ)
  138. {
  139. return DisplayAlternativ(titel, alternativ);
  140. }
  141.  
  142. static int Menu(string titel, string[] alternativ, string [] metod)
  143. {
  144. var val = DisplayAlternativ(titel, alternativ);
  145. if (val == 0)
  146. return 0;
  147.  
  148. Program p = new Program();
  149. Type t = p.GetType();
  150. var methodCall = metod[val - 1];
  151. MethodInfo mi = t.GetMethod(methodCall, BindingFlags.NonPublic | BindingFlags.Instance);
  152. string result = mi.Invoke(p, new object[] { 1 }).ToString();
  153.  
  154. return val;
  155. }
  156.  
  157. static int DisplayAlternativ(String titel, string[] alternativ)
  158. {
  159. Console.WriteLine(titel);
  160. Console.WriteLine();
  161. for (int i = 0; i < alternativ.Count(); i++)
  162. {
  163. Console.WriteLine("{0} - {1}", i + 1, alternativ[i]);
  164. }
  165. Console.WriteLine();
  166. Console.WriteLine("0 - Avsluta");
  167. bool conversionOK;
  168. var val = -1;
  169. do
  170. {
  171. var read = Console.ReadLine();
  172. conversionOK = int.TryParse(read, out val);
  173. if (conversionOK == true && val == 0)
  174. return 0;
  175. if (conversionOK == true && val >= 1 && val <= alternativ.Count())
  176. break;
  177. Console.WriteLine("Wrong input: " + read);
  178. } while (true);
  179. return val;
  180. }
  181.  
  182. int DisplayProduct(int arg = 1)
  183. {
  184. Console.WriteLine("Display Product");
  185. return 1;
  186. }
  187.  
  188. int AddProduct(int arg = 1)
  189. {
  190. Console.WriteLine("AddProduct");
  191. return 1;
  192. }
  193.  
  194. int EditProduct(int arg = 1)
  195. {
  196. Console.WriteLine("EditProduct");
  197. return 1;
  198. }
  199.  
  200. int DeleteProduct(int arg = 1)
  201. {
  202. Console.WriteLine("Delete Product");
  203. return 1;
  204. }
  205.  
  206. int SearchProduct(int arg = 1)
  207. {
  208. Console.WriteLine("SearchProduct");
  209. return 1;
  210. }
  211.  
  212. int Exit(int arg = 1)
  213. {
  214. Console.WriteLine("Exit");
  215. return 1;
  216. }
  217.  
  218. Console.WriteLine("Category selected");
  219.  
  220. Console.ReadKey();
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement