Ejemplo n.º 1
0
        /*
         |----------------------------------------------------------------------
         | Public Methods
         |----------------------------------------------------------------------
         */

        // Public method to Import the CSV
        public bool ImportCSV(BeverageCollection beverages, string pathToCSVFile)
        {
            // Declare the streamreader
            StreamReader streamReader = null;

            // If it has already been imported
            if (hasBeenImported)
            {
                return(false);
            }

            // Has not been imported yet, so do the import.
            try
            {
                // Declare a string for the line
                string line;

                // Instantiate a new StreamReader class instance
                streamReader = new StreamReader(pathToCSVFile);

                // While still reading a line from the file
                while ((line = streamReader.ReadLine()) != null)
                {
                    // Process the line
                    this.processLine(line, beverages);
                }

                // Set hasBeenImported to true now that it is imported
                hasBeenImported = true;

                // Return true to represent success
                return(true);
            }
            catch (Exception e)
            {
                // Output the Error that occured.
                // This is the only output that is NOT
                // being done in the UI class
                Console.WriteLine(e.ToString());
                Console.WriteLine();
                Console.WriteLine(e.StackTrace);

                // Return false to signify that it failed
                return(false);
            }
            finally
            {
                // If the stream reader was instanciated,
                // make sure it is closed before exiting.
                if (streamReader != null)
                {
                    streamReader.Close();
                }
            }
        }
Ejemplo n.º 2
0
        /*
         |----------------------------------------------------------------------
         | Private Methods
         |----------------------------------------------------------------------
         */

        private void processLine(string line, BeverageCollection beverageCollection)
        {
            // Declare array of parts that will contain the
            // results of splitting the read in string.
            string[] parts = line.Split(',');

            // Assign each part to a variable
            string  id     = parts[0];
            string  name   = parts[1];
            string  pack   = parts[2];
            decimal price  = decimal.Parse(parts[3]);
            bool    active = (parts[4] == "True");

            // Add a new beverage into the collection with the properties
            // of what was read in.
            beverageCollection.AddNewItem(id, name, pack, price, active);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            // Set Console Window Size
            Console.BufferHeight = Int16.MaxValue - 1;
            Console.WindowHeight = 40;
            Console.WindowWidth  = 120;

            // Set a constant for the size of the collection
            const int beverageCollectionSize = 4000;

            // Set a constant for the path to the CSV File
            const string pathToCSVFile = "../../../../datafiles/beverage_list.csv";

            // Create an instance of the UserInterface class
            UserInterface userInterface = new UserInterface();

            // Create an instance of the BeverageCollection class
            BeverageCollection beverageCollection = new BeverageCollection(beverageCollectionSize);

            // Create an instance of the CSVProcessor class
            CSVProcessor csvProcessor = new CSVProcessor();

            // Display the Welcome Message to the user
            userInterface.DisplayWelcomeGreeting();

            // Display the Menu and get the response. Store the response in the choice integer
            // This is the 'primer' run of displaying and getting.
            int choice = userInterface.DisplayMenuAndGetResponse();

            // While the choice is not exit program
            while (choice != 5)
            {
                switch (choice)
                {
                case 1:
                    // Load the CSV File
                    bool success = csvProcessor.ImportCSV(beverageCollection, pathToCSVFile);
                    if (success)
                    {
                        // Display Success Message
                        userInterface.DisplayImportSuccess();
                    }
                    else
                    {
                        // Display Fail Message
                        userInterface.DisplayImportError();
                    }
                    break;

                case 2:
                    // Print Entire List Of Items
                    string allItemsString = beverageCollection.ToString();
                    if (!String.IsNullOrWhiteSpace(allItemsString))
                    {
                        // Display all of the items
                        userInterface.DisplayAllItems(allItemsString);
                    }
                    else
                    {
                        // Display error message for all items
                        userInterface.DisplayAllItemsError();
                    }
                    break;

                case 3:
                    // Search For An Item
                    string searchQuery     = userInterface.GetSearchQuery();
                    string itemInformation = beverageCollection.FindById(searchQuery);
                    if (itemInformation != null)
                    {
                        userInterface.DisplayItemFound(itemInformation);
                    }
                    else
                    {
                        userInterface.DisplayItemFoundError();
                    }
                    break;

                case 4:
                    // Add A New Item To The List
                    string[] newItemInformation = userInterface.GetNewItemInformation();
                    if (beverageCollection.FindById(newItemInformation[0]) == null)
                    {
                        beverageCollection.AddNewItem(
                            newItemInformation[0],
                            newItemInformation[1],
                            newItemInformation[2],
                            decimal.Parse(newItemInformation[3]),
                            (newItemInformation[4] == "True")
                            );
                        userInterface.DisplayAddWineItemSuccess();
                    }
                    else
                    {
                        userInterface.DisplayItemAlreadyExistsError();
                    }
                    break;
                }

                // Get the new choice of what to do from the user
                choice = userInterface.DisplayMenuAndGetResponse();
            }
        }