static void Main(string[] args) { // separation of concerns // different "concerns" or "considerations" in code shouldn't be tangled together // single responsibility principle (S in SOLID) // a unit of code (e.g. class, method) should have just one responsibility. // DRY principle // don't repeat yourself // KISS // keep it simple stupid (unless it's on codesignal) // have a list of products IEnumerable <Product> catalog = GetProducts(); // allow for some customization of that display to the user string input = null; while (input != "s" && input != "d") { Console.WriteLine("Enter s for simple, d for detailed: "); input = Console.ReadLine(); } string input2 = null; while (input2 != "y" && input2 != "n") { Console.WriteLine("Sort? y/n: "); input2 = Console.ReadLine(); } ISorter sorter; if (input2 == "y") { sorter = new PriceSorter(); } else { sorter = new NonSorter(); } IWriter writer; // display them to the user if (input == "s") { writer = new SimpleWriter(sorter); } else { writer = new DetailedWriter(sorter); } writer.FormatAndDisplay(catalog); // dependency inversion principle (D in SOLID) // - don't have classes depend on each other directly // instead, have them depend on interfaces }
/// <summary> /// Asks the user for which sort they would like to use /// </summary> /// <returns>The Sorter</returns> public static ISorter GetSorter() { ISorter sorter = new NonSorter(); string input = ""; while (input != "dn" && input != "do" && input != "oh" && input != "ol" && input != "ond" && input != "ona" && input != "d") { Console.WriteLine("Please Enter Sort Method"); Console.WriteLine("(dn) Date Newest | (do) Date Oldest | (oh) Order Total Highest | (ol) Order Total Lowest"); Console.WriteLine("(ond) Order Number Descending | (ona) Order Number Ascending | (d) Default"); Console.Write("Selection: "); input = Console.ReadLine().Trim().ToLower(); } Console.Clear(); switch (input) { case "dn": sorter = new NewestOrderSorter(); break; case "do": sorter = new OldestOrderSorter(); break; case "oh": sorter = new HighestOrderTotalSorter(); break; case "ol": sorter = new LowestOrderTotalSorter(); break; case "ond": sorter = new OrderNumberDescendingSorter(); break; case "ona": sorter = new OrderNumberAscendingSorter(); break; } return(sorter); }
static void Main(string[] args) { // separation of concerns // different concerns in code shouldn't be tangled together // single responsibility principle (S in SOLID) // a unit of code like class method should have just one responsibility // DRY principle do not repeat yourself // KISS Keep it simple stupid // functionalities // have a list of products List <Product> catalog = GetProducts(); // user's choice of sort and display string userInput = null; while (userInput != "s" && userInput != "d") { Console.WriteLine("Enter s for simple, d for detailed"); userInput = Console.ReadLine(); } // dependency inversion principle (D in SOLID) // dont have classes depend on each other directly // instead, have them depend on // A(B) , A(Interface B) // polymorphism string userInput2 = null; while (userInput2 != "y" && userInput2 != "n") { Console.WriteLine("Have the list sorted? y/n "); userInput2 = Console.ReadLine(); } ISorter sorter; if (userInput2 == "y") { sorter = new PriceSorter(); } else { sorter = new NonSorter(); } // sorted catalog goes in to writer IWriter writer; if (userInput == "s") { writer = new SimpleWriter(sorter); } else { writer = new DetailedWriter(sorter); } writer.FormatAndDisplay(catalog); }