From ef3c68390db2929c2eaea2648913be482b7eb870 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@mail.kvcc.edu> Date: Tue, 22 Sep 2015 00:02:25 -0400 Subject: [PATCH] Version 1.1 Removes unecessary work from UserInterface. UserInterface now /only/ handles interface-related functions. Also removes work from Main, because main is mean. Creates new RunProgram class, which handles cross-coordination between classes. Allows ID's to include letters and symbols if desired. Now only needs to be 5 characters. Adds handling for user attempting to remove items when there are none. --- assignment1/CSVProcessor.cs | 7 +- assignment1/Program.cs | 104 +-------- assignment1/RunProgram.cs | 361 ++++++++++++++++++++++++++++++ assignment1/UserInterface.cs | 225 +++---------------- assignment1/WineItem.cs | 9 +- assignment1/WineItemCollection.cs | 17 +- assignment1/assignment1.csproj | 1 + 7 files changed, 416 insertions(+), 308 deletions(-) create mode 100644 assignment1/RunProgram.cs diff --git a/assignment1/CSVProcessor.cs b/assignment1/CSVProcessor.cs index a9f7413..b03101b 100644 --- a/assignment1/CSVProcessor.cs +++ b/assignment1/CSVProcessor.cs @@ -7,6 +7,9 @@ using System.IO; namespace assignment1 { + /// <summary> + /// Handles loading/reading of file. + /// </summary> class CSVProcessor { #region Variables @@ -109,7 +112,7 @@ namespace assignment1 /// </summary> private void OpenFile() { - inputFile = File.OpenText("../../../datafiles/TestWineList.csv"); + inputFile = File.OpenText("../../../datafiles/WineList.csv"); } /// <summary> @@ -137,7 +140,7 @@ namespace assignment1 var flds = inputString.Split(','); wineItem = new WineItem(); - wineItem.WineID = Convert.ToInt32(flds[0].Trim()); + wineItem.WineID = flds[0].Trim(); wineItem.WineDescription = flds[1].Trim(); wineItem.WineSize = flds[2].Trim(); diff --git a/assignment1/Program.cs b/assignment1/Program.cs index 587be25..a3ed62d 100644 --- a/assignment1/Program.cs +++ b/assignment1/Program.cs @@ -9,112 +9,12 @@ namespace assignment1 class Program { /// <summary> - /// Main + /// Main. /// </summary> /// <param name="args">Command-Line args</param> static void Main(string[] args) { - #region Variables - - // Classes - UserInterface mainMenu; - CSVProcessor processFiles; - WineItemCollection wineItemCollection; - - // Input Variables - int loadListSizeInt; // Number of items in file to load. - - // Working Variables - bool runProgramBool; // Is true until user selects exit. Forces loop to keep program running. - - #endregion - - //Initializes Classes and Variables - runProgramBool = true; - processFiles = new CSVProcessor(); - loadListSizeInt = processFiles.LoadListSize; - wineItemCollection = new WineItemCollection(loadListSizeInt); - - - - mainMenu = new UserInterface(runProgramBool, loadListSizeInt, processFiles, wineItemCollection); - - #region Methods - - #endregion - - - - - - #region TestRegion - /* - #region WineItemTest - - Console.WriteLine(Environment.NewLine + "******************************" + Environment.NewLine + - "Test For WineItem functionality." + Environment.NewLine + - "******************************" + Environment.NewLine); - - int ID1 = 123; - int ID2 = 456; - string aDesc1 = "aDesc"; - string aDesc2 = "OtherDesc"; - string aSize1 = "Size 1"; - string aSize2 = "Size 500"; - - WineItem WineTest1 = new WineItem(ID1, aDesc1, aSize1); - WineItem WineTest2 = new WineItem(ID2, aDesc2, aSize2); - - Console.WriteLine("TEST- ForcePrint:" + Environment.NewLine + - WineTest1.WineID + " " + WineTest1.WineDescription + " " + WineTest1.WineSize + Environment.NewLine); - - Console.WriteLine("TEST- ToString():"); - Console.WriteLine(WineTest1.ToString()); - Console.WriteLine(WineTest2.ToString()); - - Console.WriteLine(Environment.NewLine); - - #endregion - - - - #region WineItemCollection - - Console.WriteLine(Environment.NewLine + "******************************" + Environment.NewLine + - "Test For Collection functionality." + Environment.NewLine + - "******************************" + Environment.NewLine); - - WineItemCollection collectionTest = new WineItemCollection(5); - collectionTest.LoadWineItem(WineTest1, 0); - collectionTest.LoadWineItem(WineTest2, 1); - - Console.WriteLine("TEST- Above items added to collection and displayed:"); - Console.WriteLine(collectionTest.GetCollectionToString()); - - #endregion - - - - #region CSVProcessorTest - - Console.WriteLine(Environment.NewLine + "******************************" - + Environment.NewLine + "Test For CSVProcessor functionality." + Environment.NewLine + - "******************************" + Environment.NewLine); - - CSVProcessor loadThings = new CSVProcessor(); - - int ArraySize = loadThings.WineListSize; - Console.WriteLine("TEST- # of Items loaded: " + Environment.NewLine + ArraySize + Environment.NewLine); - - WineItemCollection wineCollection = new WineItemCollection(ArraySize); - loadThings = new CSVProcessor(wineCollection); - - Console.WriteLine("TEST- Collection Contents After Load:"); - Console.WriteLine(wineCollection.GetCollectionToString()); - - #endregion - */ - #endregion + RunProgram run = new RunProgram(); } } } diff --git a/assignment1/RunProgram.cs b/assignment1/RunProgram.cs new file mode 100644 index 0000000..c32f544 --- /dev/null +++ b/assignment1/RunProgram.cs @@ -0,0 +1,361 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace assignment1 +{ + /// <summary> + /// Cordinates classes in program to behave properly. + /// </summary> + class RunProgram + { + #region Variables + + // Classes + UserInterface mainMenu; + CSVProcessor processFiles; + WineItemCollection wineItemCollection; + WineItem wineItem; + + // Input Variables + private int loadListSizeInt; // Number of items in file to load. + + // Working Variables + private bool runProgramBool; // Is true until user selects exit. Forces loop to keep program running. + private string IDString; + private bool collectionLoadedBool; // Saves if file has been loaded or not. + private int indexInt; + + // Constant Variables + private int MAX_ID_LENGTH = 5; + + #endregion + + + + #region Constructor + + + + /// <summary> + /// Base Constructor to run program. + /// </summary> + public RunProgram() + { + wineItem = new WineItem(); + runProgramBool = true; + processFiles = new CSVProcessor(); + loadListSizeInt = processFiles.LoadListSize; + wineItemCollection = new WineItemCollection(loadListSizeInt); + mainMenu = new UserInterface(); + + wineItem = new WineItem(); + while (runProgramBool) + { + mainMenu.PrintUserMenu(); + UserSelection(); + } + } + + /// <summary> + /// Constructor to force testing of individual classes. + /// </summary> + /// <param name="TestTrueBool">Bool to specify testing should occur.</param> + public RunProgram(bool TestTrueBool) + { + Testing(); + } + + #endregion + + + + #region Methods + + /// <summary> + /// Reads user input and takes appropriate action. + /// </summary> + private void UserSelection() + { + string userSelectionString = mainMenu.GetUserInput().ToLower(); + Console.WriteLine(); + + switch (userSelectionString) + { + case "1": + LoadWineList(); + break; + + case "2": + PrintWineList(); + break; + + case "3": + SearchWineList(); + break; + + case "4": + AddItemToList(); + break; + + case "5": + RemoveItemFromList(); + break; + + case "6": + CloseProgram(); + break; + + case "esc": + CloseProgram(); + break; + + default: + mainMenu.DisplayLine(Environment.NewLine + " Error, Invalid Selection!" + Environment.NewLine + " Please enter a number between 1 and 5."); + break; + } + } + + /// <summary> + /// Handles Menu option 1. Loading from Wine List. + /// </summary> + private void LoadWineList() + { + // While has not been loaded yet. + if (collectionLoadedBool == false) + { + indexInt = wineItemCollection.CurrentIndex; + // If no items are present in the collection. + if (wineItemCollection.WineItemArray[0] == null) + { + processFiles = new CSVProcessor(wineItemCollection, indexInt); + } + else + { + while (wineItemCollection.WineItemArray[indexInt] != null) + { + indexInt++; + } + processFiles = new CSVProcessor(wineItemCollection, indexInt); + } + collectionLoadedBool = true; + } + else + { + mainMenu.DisplayLine("Wine list has already been loaded."); + } + } + + /// <summary> + /// Handles Menu option 2. Printing of Wine List. + /// </summary> + private void PrintWineList() + { + if (wineItemCollection.WineItemArray[0] != null) + { + mainMenu.DisplayLine(wineItemCollection.GetCollectionToString()); + } + else + { + mainMenu.DisplayLine("List is currently empty."); + } + } + + /// <summary> + /// Handles Menu option 3. Searching of Wine List. + /// </summary> + private void SearchWineList() + { + // Determines if there is a list to even search. + if (wineItemCollection.WineItemArray[0] != null) + { + try + { + mainMenu.DisplayLine("Enter ID to search for: "); + IDString = mainMenu.GetUserInput(); + + // Allows user to back out. + if (IDString.ToLower() != "esc") + { + wineItem = wineItemCollection.SearchWineItem(IDString, 0); + if (wineItem.WineDescription == "ID is Not Found") + { + mainMenu.DisplayLine("The entered ID (" + wineItem.WineID + ") is not found."); + } + else + { + mainMenu.DisplayLine("Match Found: " + wineItem.ToString()); + } + } + } + catch + { + mainMenu.DisplayLine("Error, invalid ID. Please enter 5 characters."); + } + } + else + { + mainMenu.DisplayLine("There are no items to search. Please load a file or add items first."); + } + + } + + /// <summary> + /// Handles Menu option 4. Manually adding items to Wine List. + /// </summary> + private void AddItemToList() + { + try + { + mainMenu.DisplayLine(Environment.NewLine + "Enter Wine ID: (ID should be 5 characters long)"); + IDString = mainMenu.GetUserInput(); + + // Allows user to back out. + if (IDString.ToLower() != "esc") + { + if (IDString.Length == MAX_ID_LENGTH) + { + wineItem.WineID = IDString; + + mainMenu.DisplayLine("Enter Wine Description:"); + wineItem.WineDescription = mainMenu.GetUserInput(); + + mainMenu.DisplayLine("Enter Wine Pack Size:"); + wineItem.WineSize = mainMenu.GetUserInput(); + + wineItemCollection.AddWineItem(wineItem); + } + else + { + mainMenu.DisplayLine("ID must be exactly 5 characters long."); + } + } + } + catch + { + mainMenu.DisplayLine("Invalid input. ID must be a number."); + } + } + + /// <summary> + /// Handles Menu option 5. Removing items from Wine List. + /// </summary> + private void RemoveItemFromList() + { + // Checks to see if there is anything to remove. + if (wineItemCollection.WineItemArray[0] != null) + { + Console.WriteLine(Environment.NewLine + "Enter ID to remove: "); + IDString = mainMenu.GetUserInput(); + + // Allows user to back out. + if (IDString.ToLower() != "esc") + { + if (wineItemCollection.RemoveWineItem(IDString, 0)) + { + mainMenu.DisplayLine("ID " + IDString + " successfully removed."); + } + else + { + mainMenu.DisplayLine("Could not locate ID to remove."); + } + } + } + else + { + mainMenu.DisplayLine("There are no items to remove. Please load a file or add items first."); + } + } + + /// <summary> + /// Handles Menu option 6. Closing the program. + /// </summary> + private void CloseProgram() + { + runProgramBool = false; + } + + #endregion + + + + #region TestRegion + + /// <summary> + /// For debugging/testing purposes. Bypasses UserInterface class and tests individual classes directly. + /// </summary> + private void Testing() + { + + #region WineItemTest + + Console.WriteLine(Environment.NewLine + "******************************" + Environment.NewLine + + "Test For WineItem functionality." + Environment.NewLine + + "******************************" + Environment.NewLine); + + string ID1 = "12345"; + string ID2 = "67890"; + string aDesc1 = "aDesc"; + string aDesc2 = "OtherDesc"; + string aSize1 = "Size 1"; + string aSize2 = "Size 500"; + + WineItem WineTest1 = new WineItem(ID1, aDesc1, aSize1); + WineItem WineTest2 = new WineItem(ID2, aDesc2, aSize2); + + Console.WriteLine("TEST- ForcePrint:" + Environment.NewLine + + WineTest1.WineID + " " + WineTest1.WineDescription + " " + WineTest1.WineSize + Environment.NewLine); + + Console.WriteLine("TEST- ToString():"); + Console.WriteLine(WineTest1.ToString()); + Console.WriteLine(WineTest2.ToString()); + + Console.WriteLine(Environment.NewLine); + + #endregion + + + + #region WineItemCollectionTest + + Console.WriteLine(Environment.NewLine + "******************************" + Environment.NewLine + + "Test For Collection functionality." + Environment.NewLine + + "******************************" + Environment.NewLine); + + WineItemCollection collectionTest = new WineItemCollection(5); + collectionTest.LoadWineItem(WineTest1, 0, 1); + collectionTest.LoadWineItem(WineTest2, 1, 1); + + Console.WriteLine("TEST- Above items added to collection and displayed:"); + Console.WriteLine(collectionTest.GetCollectionToString()); + + #endregion + + + + #region CSVProcessorTest + + Console.WriteLine(Environment.NewLine + "******************************" + + Environment.NewLine + "Test For CSVProcessor functionality." + Environment.NewLine + + "******************************" + Environment.NewLine); + + CSVProcessor loadThings = new CSVProcessor(); + + int ArraySize = loadThings.LoadListSize; + Console.WriteLine("TEST- # of Items loaded: " + Environment.NewLine + ArraySize + Environment.NewLine); + + WineItemCollection wineCollection = new WineItemCollection(ArraySize); + loadThings = new CSVProcessor(wineCollection, 0); + + Console.WriteLine("TEST- Collection Contents After Load:"); + Console.WriteLine(wineCollection.GetCollectionToString()); + + #endregion + + } + + #endregion + } +} diff --git a/assignment1/UserInterface.cs b/assignment1/UserInterface.cs index f42ad78..eae2046 100644 --- a/assignment1/UserInterface.cs +++ b/assignment1/UserInterface.cs @@ -6,20 +6,15 @@ using System.Threading.Tasks; namespace assignment1 { + /// <summary> + /// Controls the entire interface for user. + /// </summary> class UserInterface { #region Variables - // Classes - WineItem wineItem; - WineItemCollection wineItemCollection; - CSVProcessor processFiles; - // Working Variables - private bool runProgramBool; // Keeps program running. - private bool collectionLoadedBool; // Saves if file has been loaded or not. - private int loadListSizeInt; - private int indexInt; + private string userInputString; #endregion @@ -32,25 +27,7 @@ namespace assignment1 /// </summary> public UserInterface() { - - } - /// <summary> - /// Constructor to launch interface. - /// </summary> - /// <param name="runProgram">Continues running program if Boolean is true.</param> - /// <param name="loadListSize">The size of the list in loadFile.</param> - /// <param name="fileLoader">Class which loads files.</param> - /// <param name="collection">Collection of Wine Items.</param> - public UserInterface(bool runProgram, int loadListSize, CSVProcessor fileLoader, WineItemCollection collection) - { - RunProgram = runProgram; - LoadListSize = loadListSize; - FileLoader = fileLoader; - Collection = collection; - - RunProgram = runProgram; - RunMenu(); } #endregion @@ -59,43 +36,11 @@ namespace assignment1 #region Properties - public bool RunProgram - { - set - { - this.runProgramBool = value; - } - get - { - return runProgramBool; - } - } - - public int LoadListSize + public string UserSelection { - set - { - this.loadListSizeInt = value; - } get { - return loadListSizeInt; - } - } - - private CSVProcessor FileLoader - { - set - { - this.processFiles = value; - } - } - - private WineItemCollection Collection - { - set - { - this.wineItemCollection = value; + return userInputString; } } @@ -106,26 +51,13 @@ namespace assignment1 #region Methods /// <summary> - /// Main loop to keep user in program until exit. + /// Displays Main Menu for user. /// </summary> - private void RunMenu() - { - wineItem = new WineItem(); - while (runProgramBool) - { - DisplayMainMenu(); - UserSelection(); - } - } - - /// <summary> - /// Reusable menu to display to user. - /// </summary> - private void DisplayMainMenu() + public void PrintUserMenu() { Console.WriteLine(Environment.NewLine + Environment.NewLine + - " Choose an Option:" + Environment.NewLine + - " ~~~~~~~~~~~~~~~~~" + Environment.NewLine + + " Choose an Option:" + " Note:"+ Environment.NewLine + + " ~~~~~~~~~~~~~~~~~" + " You may type 'esc' at any point to back out." + Environment.NewLine + " 1) Load Wine List" + Environment.NewLine + " 2) Print Wine List" + Environment.NewLine + " 3) Search for Item" + Environment.NewLine + @@ -135,130 +67,29 @@ namespace assignment1 } /// <summary> - /// Reads user input and takes appropriate action. + /// Method to display line to user. Single method creates consistancy in UI. /// </summary> - private void UserSelection() + /// <param name="displayString">The string to display.</param> + public void DisplayLine(string displayString) { - string userSelectionString = Console.ReadLine().Trim(); - Console.WriteLine(); - WineItem wineItem = new WineItem(); - int IDint; + Console.WriteLine(displayString); + } - switch (userSelectionString) + /// <summary> + /// Reads user input from console. + /// </summary> + /// <returns>String of user input.</returns> + public string GetUserInput() + { + try { - case "1": // Load Wine List. - // While has not been loaded yet. - if (collectionLoadedBool == false) - { - indexInt = wineItemCollection.CurrentIndex; - // If no items are present in the collection. - if (wineItemCollection.WineItemArray[0] == null) - { - processFiles = new CSVProcessor(wineItemCollection, indexInt); - } - else - { - while (wineItemCollection.WineItemArray[indexInt] != null) - { - indexInt++; - } - processFiles= new CSVProcessor(wineItemCollection, indexInt); - } - collectionLoadedBool = true; - } - else - { - Console.WriteLine("Wine list has already been loaded."); - } - break; - - case "2": // Print Wine List. - if (wineItemCollection.WineItemArray[0] != null) - { - Console.WriteLine(wineItemCollection.GetCollectionToString()); - } - else - { - Console.WriteLine("List is currently empty."); - } - break; - - case "3": // Search Wine List. - // Determines if there is a list to even search. - if (wineItemCollection.WineItemArray[0] != null) - { - try - { - Console.WriteLine("Enter ID to search for: "); - IDint = Convert.ToInt32(Console.ReadLine().Trim()); - - wineItem = wineItemCollection.SearchWineItem(IDint, 0); - if (wineItem.WineDescription == "ID is Not Found") - { - Console.WriteLine("The entered ID (" + wineItem.WineID + ") is not found."); - } - else - { - Console.WriteLine("Match Found: " + wineItem.ToString()); - } - } - catch - { - Console.WriteLine("Error, invalid ID. Please enter a 5 digit number."); - } - } - else - { - Console.WriteLine("There are no items to search. Please load a file or add items first."); - } - - - break; - - case "4": // Add item to Wine List. - try - { - Console.WriteLine(Environment.NewLine + "Enter Wine ID: (ID should be a 5 digit number)"); - wineItem.WineID = Convert.ToInt32(Console.ReadLine().Trim()); - - Console.WriteLine("Enter Wine Description:"); - wineItem.WineDescription = Console.ReadLine().Trim(); - - Console.WriteLine("Enter Wine Pack Size:"); - wineItem.WineSize = Console.ReadLine().Trim(); - } - catch - { - Console.WriteLine("Invalid input. ID must be a number."); - } - - wineItemCollection.AddWineItem(wineItem); - - break; - - case "5": // Remove item from Wine List. - Console.WriteLine(Environment.NewLine + "Enter ID to remove: "); - IDint = Convert.ToInt32(Console.ReadLine()); - if (wineItemCollection.RemoveWineItem(IDint, 0)) - { - Console.WriteLine("ID " + IDint + " successfully removed."); - } - else - { - Console.WriteLine("Could not locate ID to remove."); - } - - - break; - - case "6": // Exit. - runProgramBool = false; - break; - - default: - Console.WriteLine(Environment.NewLine + " Error, Invalid Selection!" + Environment.NewLine + " Please enter a number between 1 and 5."); - break; + userInputString = Console.ReadLine().Trim(); + } + catch (Exception errmsg) + { + userInputString = "Error," + Environment.NewLine + errmsg; } + return userInputString; } #endregion diff --git a/assignment1/WineItem.cs b/assignment1/WineItem.cs index dda896a..1839678 100644 --- a/assignment1/WineItem.cs +++ b/assignment1/WineItem.cs @@ -6,12 +6,15 @@ using System.Threading.Tasks; namespace assignment1 { + /// <summary> + /// Creates and holds information for individual Wine Items. + /// </summary> class WineItem { #region Variables // Input Variables - private int wineIDInt; + private string wineIDInt; private string wineDescriptionString; private string wineSizeString; @@ -35,7 +38,7 @@ namespace assignment1 /// <param name="wineID">Item's desired ID.</param> /// <param name="wineDescription">Item's desired Description.</param> /// <param name="wineSize">Item's desired Pack Size.</param> - public WineItem(int wineID, string wineDescription, string wineSize) + public WineItem(string wineID, string wineDescription, string wineSize) { WineID = wineID; WineDescription = wineDescription; @@ -48,7 +51,7 @@ namespace assignment1 #region Properties - public int WineID + public string WineID { set { diff --git a/assignment1/WineItemCollection.cs b/assignment1/WineItemCollection.cs index 96bff15..df2a072 100644 --- a/assignment1/WineItemCollection.cs +++ b/assignment1/WineItemCollection.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace assignment1 { + /// <summary> + /// Stores and handles full collection of Wine Items. + /// </summary> class WineItemCollection { #region Variables @@ -134,7 +137,7 @@ namespace assignment1 /// Loads Wine Items from file into Colletion Array. /// </summary> /// <param name="wineItem">The individual WineItem to add.</param> - /// <param name="indexInt">Index the item will be added to.</param> + /// <param name="index">Index the item will be added to.</param> /// <param name="arrayEndSize">Total number of non-null items array will have.</param> public void LoadWineItem(WineItem wineItem, int index, int arrayEndSize) { @@ -165,7 +168,7 @@ namespace assignment1 if (wineItem != null) { indexInt++; - outputString += " " + (indexInt + ")").PadRight(5) + wineItem.ToString() + Environment.NewLine; + outputString += " " + (indexInt + ")").PadRight(6) + wineItem.ToString() + Environment.NewLine; } } return outputString; @@ -206,7 +209,7 @@ namespace assignment1 /// <param name="wineID">ID to search for.</param> /// <param name="index">Current index of search.</param> /// <returns>Full information of matching item.</returns> - public WineItem SearchWineItem(int wineID, int index) + public WineItem SearchWineItem(string wineID, int index) { // Checks for end of array items. if (index < wineListSizeInt) @@ -231,7 +234,13 @@ namespace assignment1 return wineItem; } - public bool RemoveWineItem(int wineID, int index) + /// <summary> + /// Search for and removal of matching Wine ID. + /// </summary> + /// <param name="wineID">ID to search for.</param> + /// <param name="index">Current index of search.</param> + /// <returns>Information regarding item removal.</returns> + public bool RemoveWineItem(string wineID, int index) { bool removedIDBool = false; diff --git a/assignment1/assignment1.csproj b/assignment1/assignment1.csproj index 8b60dcb..f0f5c39 100644 --- a/assignment1/assignment1.csproj +++ b/assignment1/assignment1.csproj @@ -44,6 +44,7 @@ <Compile Include="CSVProcessor.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="RunProgram.cs" /> <Compile Include="UserInterface.cs" /> <Compile Include="WineItem.cs" /> <Compile Include="WineItemCollection.cs" /> -- GitLab