コード例 #1
0
        static void Main(string[] args)
        {
            Console.Title = "COMP2614 Assignment 04 A00838629";

            string path = @"...\\...\\groceries.csv";

            if (!File.Exists(path))
            {
                Console.WriteLine("File does not exist.");
            }

            if (path == null)
            {
                Console.WriteLine("File Path not found.");
            }

            string[] readLinesArray = File.ReadAllLines(path);

            decimal totalPrice = 0;

            GroceryItemCollection groceryItems = new GroceryItemCollection();

            foreach (string line in readLinesArray)
            {
                string[] splitLine                  = line.Split(',');
                string   description                = GetValueOrEmpty(splitLine, 0);
                string   price                      = GetValueOrEmpty(splitLine, 1);
                string   dateTime                   = GetValueOrEmpty(splitLine, 2);
                decimal  convertedGroceryPrice      = Convert.ToDecimal(price);
                DateTime convertedGroceryExpriation = Convert.ToDateTime(dateTime);
                groceryItems.Add(new GroceryItem {
                    Description = description, Price = convertedGroceryPrice, ExpriationDate = convertedGroceryExpriation
                });
                totalPrice += convertedGroceryPrice;
            }

            Console.WriteLine("Natural Order:");
            Console.WriteLine($"{"Grocery Item",-20} {"Price",20:N2}   {"Expires",-15}");
            Console.WriteLine(new string('-', 60));
            ConsolePrinter.PrintGroceryCollection(groceryItems, "");
            Console.WriteLine(new string('-', 60));
            Console.WriteLine($"{"Total:",-20} {totalPrice,20}");
            Console.WriteLine("\n");

            groceryItems.Sort();

            Console.WriteLine("Sorder Order: [Price Descending]");
            Console.WriteLine($"{"Grocery Item",-20} {"Price",20:N2}   {"Expires",-15}");
            Console.WriteLine(new string('-', 60));
            ConsolePrinter.PrintGroceryCollection(groceryItems, "");
            Console.WriteLine(new string('-', 60));
            Console.WriteLine($"{"Total:",-20} {totalPrice,20}");
        }
        /// <summary>
        /// Method to populates the customers list
        /// </summary>
        private void populateCustomers()
        {
            try
            {
                filterByProvince = Console.ReadLine();
                customers        = CustomerRepository.GetCustomers();

                if (customers != null && customers.Count() > NULL_CUSTOMERS)
                {
                    if (filterByProvince.Equals("1"))
                    {
                        ConsolePrinter.Print(customers, "AB");
                    }
                    else if (filterByProvince.Equals("2"))
                    {
                        ConsolePrinter.Print(customers, "BC");
                    }
                    else if (filterByProvince.Equals("3"))
                    {
                        ConsolePrinter.Print(customers, "ON");
                    }
                    else if (filterByProvince.Equals("4"))
                    {
                        ConsolePrinter.Print(customers, "SK");
                    }
                    else if (filterByProvince.Equals("5"))
                    {
                        ConsolePrinter.Print(customers, "ALL");
                    }
                    else
                    {
                        Console.WriteLine("Error please pick a number between 1 to {0}", provinces.Count + COUNT_START);
                    }
                }
                else
                {
                    Console.WriteLine("No customers to print");
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Data Access Error\n\n{0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Processing Error\n\n{0}", ex.Message);
            }
        }
        /// <summary>
        /// Method to populateCustomers list through populateSelectionList method
        /// </summary>
        private void populateCustomers()
        {
            int selection = SELECTION_START;

            do
            {
                try
                {
                    populateSelectionList();

                    string provinceFilter = Console.ReadLine();
                    bool   isNumber       = int.TryParse(provinceFilter, out selection);
                    customers = CustomertRepository.GetCustomers(selection);

                    if (selection > 0 && selection <= provinces.Count - ELEMENT)
                    {
                        if (customers != null && customers.Count() > NULL_CUSTOMERS)
                        {
                            ConsolePrinter.Print(customers, selection);
                        }
                        else
                        {
                            Console.WriteLine("No customers to print");
                            Environment.Exit(EXIT_CODE);
                        }
                    }
                    else if (selection == provinces.Count)
                    {
                        Console.WriteLine("COMP2614Assign04 will now exit . . .");
                        Environment.Exit(EXIT_CODE);
                    }
                }
                catch (SqlException ex)
                {
                    Console.WriteLine("Data Access Error\n\n{0}", ex.Message);
                }
                catch (Exception)
                {
                    Console.WriteLine("**********INVALID SELECTION**********\nPlease select a number between 1 to {0}\n\n", provinces.Count);
                }
            } while (selection >= SELECTION_START);
        }