示例#1
0
        static void Main(string[] args)
        {
            //List to contain items
            GroceryItemCollection list = new GroceryItemCollection();

            //Items created using explicit constructor
            list.Add(new GroceryItem("4L Milk", 3.87, new DateTime(2017, 5, 28)));
            list.Add(new GroceryItem("500g Cottage Cheese", 4.28, new DateTime(2017, 7, 15)));
            list.Add(new GroceryItem("1 Tin Mushroom Soup", 0.99, DateTime.MaxValue));
            list.Add(new GroceryItem("15kg Dog Food", 29.99, new DateTime(2019, 9, 23)));

            GroceryItem item = new GroceryItem("Test", 29.99, new DateTime(2019, 9, 23));

            Console.WriteLine(item.Description);

            //Natural order
            Console.WriteLine("Natural Order:");
            OutputHelper.PrintList(list);

            //Line feeds between two different display methods
            Console.WriteLine("\n\n\n");

            //Sorted order
            list.Sort();
            Console.WriteLine("Sorted Order: [Price Descending]");
            OutputHelper.PrintList(list);
        }
示例#2
0
        public static void ShowItem(GroceryItemCollection groceryItems, string header)
        {
            //output header
            Console.WriteLine($"{header}");
            Console.WriteLine($"{"Grocery Item",-30} {"Price",5}   {"Expires",-15}");
            Console.WriteLine(new string('-', 55));
            //output Invoice Items
            foreach (GroceryItem item in groceryItems)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(new string('-', 55));

            //Calculate Total by call method Total in GroceryItemCollection class
            Console.WriteLine($"{"Total:",-30} {groceryItems.Total,5}");
        }
        /// <summary>
        /// Prints the GroceryItems in predefined format
        /// </summary>
        /// <param name="items">the items to print</param>
        /// <param name="order">the order to print</param>
        public static void Print(GroceryItemCollection items, string order)
        {
            Console.WriteLine(order);
            Console.WriteLine("{0, -30} {1, 5}  {2, 6}", "Grocery Item", "Price", "Expires");
            Console.WriteLine(new string('-', SEPARATOR_LENGTH));

            foreach (GroceryItem item in items)
            {
                if (item.ExpirationDate == DateTime.MaxValue.Date)
                {
                    Console.WriteLine("{0, -30} {1, 5:F2}  {2, 6}", item.Description, item.Price, item.ExpirationDate.ToString("<Never>"));
                }
                else
                {
                    Console.WriteLine("{0, -30} {1, 5:F2}  {2, 6}", item.Description, item.Price, item.ExpirationDate.ToString("ddd MMM d, yyyy"));
                }
            }

            Console.WriteLine(new string('-', SEPARATOR_LENGTH));
            Console.WriteLine($"Total: {items.Total,29:F2}\n\n");
        }
示例#4
0
        static void Main(string[] args)
        {
            //declare GroceryItemCollection
            GroceryItemCollection groceryItems = new GroceryItemCollection();

            //Add an object
            groceryItems.Add(new GroceryItem("4L Milk", 3.87m, DateTime.Parse("2018,02,13")));
            groceryItems.Add(new GroceryItem("500g Cottage Cheese", 4.28m, DateTime.Parse("2018,03,5")));
            groceryItems.Add(new GroceryItem("1 Tin Mushroom Soup", 0.99m, DateTime.MaxValue));
            groceryItems.Add(new GroceryItem("15kg Dog Food", 29.99m, DateTime.Parse("2019,12,19")));

            //Ouput Natural Order to screen
            ConsolePrinter.ShowItem(groceryItems, "Natural Order:");

            //Sort Order by Price
            groceryItems.Sort();
            Console.WriteLine(Environment.NewLine);

            //Output Sorted Order to screen
            ConsolePrinter.ShowItem(groceryItems, "Sorted Order: [Price Descending]");

            Console.ReadLine();
        }