diff --git a/assignment1/CSVProcessor.cs b/assignment1/CSVProcessor.cs index 5e5660693bf16dbf3cb3261eb7fa59ef568b1b63..a9f7413eb51c410e2a6b580baa9faf2eeacf1035 100644 --- a/assignment1/CSVProcessor.cs +++ b/assignment1/CSVProcessor.cs @@ -15,14 +15,8 @@ namespace assignment1 WineItem wineItem; WineItemCollection wineItemCollection; - // Input Variables - private int wineIDInt; - private string wineDescriptionString; - private string wineSizeString; - // Working Variables - private static bool hasLoadedBool; - private static int wineListSizeInt; + private static int loadListSizeInt; private int indexInt; private StreamReader inputFile; @@ -45,6 +39,7 @@ namespace assignment1 /// Constructor to read from file. /// </summary> /// <param name="wineItemCollection">The current instance of WineItemCollection Class.</param> + /// <param name="index">Index of first spot in array without a WineItem.</param> public CSVProcessor(WineItemCollection wineCollection, int index) { Collection = wineCollection; @@ -75,35 +70,11 @@ namespace assignment1 } } - public int WineListSize - { - get - { - return wineListSizeInt; - } - } - - public int WineID - { - get - { - return wineIDInt; - } - } - - public string WineDescription - { - get - { - return wineDescriptionString; - } - } - - public string WineSize + public int LoadListSize { get { - return wineSizeString; + return loadListSizeInt; } } @@ -111,7 +82,7 @@ namespace assignment1 - #region Private Methods + #region Methods /// <summary> /// Deterimines size of items to handle. @@ -122,14 +93,14 @@ namespace assignment1 while (inputFile.EndOfStream == false) { inputFile.ReadLine(); - wineListSizeInt++; + loadListSizeInt++; } CloseFile(); - // Forces wineList to at least be 10 items long. - if (wineListSizeInt < 10) + // Forces wineList to at least be 10 items long. For future-proofing. + if (loadListSizeInt < 10) { - wineListSizeInt = 10; + loadListSizeInt = 10; } } @@ -158,7 +129,9 @@ namespace assignment1 { OpenFile(); - while (indexInt < wineListSizeInt) + int arrayEndSizeInt = loadListSizeInt + wineItemCollection.WineListSize; + + while (indexInt < arrayEndSizeInt) { string inputString = inputFile.ReadLine(); var flds = inputString.Split(','); @@ -168,7 +141,7 @@ namespace assignment1 wineItem.WineDescription = flds[1].Trim(); wineItem.WineSize = flds[2].Trim(); - wineItemCollection.LoadWineItem(wineItem, indexInt); + wineItemCollection.LoadWineItem(wineItem, indexInt, arrayEndSizeInt); indexInt++; } @@ -183,11 +156,5 @@ namespace assignment1 #endregion - - - #region Public Methods - - - #endregion } } diff --git a/assignment1/Program.cs b/assignment1/Program.cs index 1e654b12d86e512add9608224f4ccd6c06bb4f7f..587be2592d18d0bcd9c3b825c19d584703b5aa78 100644 --- a/assignment1/Program.cs +++ b/assignment1/Program.cs @@ -32,7 +32,7 @@ namespace assignment1 //Initializes Classes and Variables runProgramBool = true; processFiles = new CSVProcessor(); - loadListSizeInt = processFiles.WineListSize; + loadListSizeInt = processFiles.LoadListSize; wineItemCollection = new WineItemCollection(loadListSizeInt); diff --git a/assignment1/UserInterface.cs b/assignment1/UserInterface.cs index 1876087accacb0db258496fd4e11252fe6c3a577..f42ad7845dcf1898b5e74022bccb805206e31ffe 100644 --- a/assignment1/UserInterface.cs +++ b/assignment1/UserInterface.cs @@ -35,12 +35,13 @@ namespace assignment1 } - public UserInterface(bool runProgram) - { - RunProgram = runProgram; - RunMenu(); - } - + /// <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; @@ -109,6 +110,7 @@ namespace assignment1 /// </summary> private void RunMenu() { + wineItem = new WineItem(); while (runProgramBool) { DisplayMainMenu(); @@ -128,7 +130,8 @@ namespace assignment1 " 2) Print Wine List" + Environment.NewLine + " 3) Search for Item" + Environment.NewLine + " 4) Add New Item to List" + Environment.NewLine + - " 5) Exit" + Environment.NewLine); + " 5) Remove Item from List" + Environment.NewLine + + " 6) Exit" + Environment.NewLine); } /// <summary> @@ -138,10 +141,12 @@ namespace assignment1 { string userSelectionString = Console.ReadLine().Trim(); Console.WriteLine(); + WineItem wineItem = new WineItem(); + int IDint; switch (userSelectionString) { - case "1": + case "1": // Load Wine List. // While has not been loaded yet. if (collectionLoadedBool == false) { @@ -167,7 +172,7 @@ namespace assignment1 } break; - case "2": + case "2": // Print Wine List. if (wineItemCollection.WineItemArray[0] != null) { Console.WriteLine(wineItemCollection.GetCollectionToString()); @@ -178,16 +183,42 @@ namespace assignment1 } break; - case "3": + 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()); - break; + 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."); + } - case "4": - WineItem wineItem = new WineItem(); + + break; + case "4": // Add item to Wine List. try { - Console.WriteLine(Environment.NewLine + "Enter Wine ID:"); + 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:"); @@ -205,7 +236,22 @@ namespace assignment1 break; - case "5": + 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; diff --git a/assignment1/WineItem.cs b/assignment1/WineItem.cs index 9fdfdcc67a48b70328271e7d7e7ae6e7af1206e8..dda896aafe1f7228dfa74e29d0d17e5d928920e7 100644 --- a/assignment1/WineItem.cs +++ b/assignment1/WineItem.cs @@ -21,11 +21,20 @@ namespace assignment1 #region Constructors + /// <summary> + /// Base Constructor. + /// </summary> public WineItem() { } + /// <summary> + /// Constructor that fills in WineItem information. + /// </summary> + /// <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) { WineID = wineID; diff --git a/assignment1/WineItemCollection.cs b/assignment1/WineItemCollection.cs index 0b93753d6f77b4729ccb02fe0de8839e70a4b974..96bff157200bf15397303c8ac0749b9185dee3b6 100644 --- a/assignment1/WineItemCollection.cs +++ b/assignment1/WineItemCollection.cs @@ -14,6 +14,7 @@ namespace assignment1 WineItem wineItem; // Input Variables + private int loadListSizeInt; private int wineListSizeInt; private int lenghtOfArrayInt; private int indexInt; @@ -38,12 +39,13 @@ namespace assignment1 /// <summary> /// Create Array Constructor with room for additional items. /// </summary> - /// <param name="wineListSize">Size of wine List.</param> - public WineItemCollection(int wineListSize) + /// <param name="loadListSize">Size of wine List.</param> + public WineItemCollection(int loadListSize) { - WineListSize = wineListSize; + LoadListSize = loadListSize; - lenghtOfArrayInt = wineListSizeInt + (wineListSizeInt / 2); + wineListSizeInt = 0; + lenghtOfArrayInt = loadListSizeInt + (loadListSizeInt / 2); wineItemArray = new WineItem[lenghtOfArrayInt]; } @@ -53,12 +55,15 @@ namespace assignment1 #region Properties - public int WineListSize + public int LoadListSize { set { - wineListSizeInt = value; + this.loadListSizeInt = value; } + } + public int WineListSize + { get { return wineListSizeInt; @@ -96,7 +101,7 @@ namespace assignment1 #region Private Methods /// <summary> - /// Expands Collection if user fills up entire array. Generally only happens if manually creating a new list. + /// Expands Collection if user fills up entire array. /// </summary> private void ExpandArraySize() { @@ -106,8 +111,15 @@ namespace assignment1 indexInt = 0; while (indexInt < wineListSizeInt) { - tempArray[indexInt] = WineItemArray[indexInt]; - indexInt++; + if (wineItemArray[indexInt] == null) + { + indexInt = wineListSizeInt; + } + else + { + tempArray[indexInt] = WineItemArray[indexInt]; + indexInt++; + } } wineItemArray = tempArray; } @@ -123,9 +135,20 @@ namespace assignment1 /// </summary> /// <param name="wineItem">The individual WineItem to add.</param> /// <param name="indexInt">Index the item will be added to.</param> - public void LoadWineItem(WineItem wineItem, int indexInt) + /// <param name="arrayEndSize">Total number of non-null items array will have.</param> + public void LoadWineItem(WineItem wineItem, int index, int arrayEndSize) { - wineItemArray[indexInt] = wineItem; + wineListSizeInt = arrayEndSize; + + if (arrayEndSize < lenghtOfArrayInt) + { + wineItemArray[index] = wineItem; + } + else + { + ExpandArraySize(); + LoadWineItem(wineItem, index, arrayEndSize); + } } /// <summary> @@ -136,30 +159,28 @@ namespace assignment1 { string outputString = ""; + indexInt = 0; foreach (WineItem wineItem in wineItemArray) { if (wineItem != null) { - outputString += wineItem.ToString() + Environment.NewLine; + indexInt++; + outputString += " " + (indexInt + ")").PadRight(5) + wineItem.ToString() + Environment.NewLine; } } - - if (outputString == "") - { - outputString = "Collection is currently empty."; - } return outputString; } /// <summary> - /// Adds new WineItem to first availible spot in collection. + /// Adds new WineItem to first available spot in collection. /// </summary> - /// <param name="wineItem"></param> + /// <param name="wineItem">Item to be added.</param> public void AddWineItem(WineItem wineItem) { - // Recursive if to account for no previous loaded list. + // Test to see if there is room to add more items. if (wineListSizeInt < lenghtOfArrayInt) { + // Recursive search for first empty index spot. if (wineItemArray[indexInt] == null) { wineItemArray[indexInt] = wineItem; @@ -175,7 +196,90 @@ namespace assignment1 else { ExpandArraySize(); + AddWineItem(wineItem); + } + } + + /// <summary> + /// Search for matching Wine ID. + /// </summary> + /// <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) + { + // Checks for end of array items. + if (index < wineListSizeInt) + { + // Recursive search for match. + if (wineItemArray[index].WineID == wineID) + { + wineItem = wineItemArray[index]; + index = wineListSizeInt; + } + else + { + index++; + SearchWineItem(wineID, index); + } + } + else + { + wineItem = new WineItem(wineID, "ID is Not Found", "ID is Not Found"); + } + + return wineItem; + } + + public bool RemoveWineItem(int wineID, int index) + { + bool removedIDBool = false; + + // Checks for end of array. + if (index < wineListSizeInt) + { + // If matching ID found. + if (wineItemArray[index].WineID == wineID) + { + int removalIndex = index; + + indexInt = 0; + tempArray = new WineItem[wineListSizeInt + wineListSizeInt/2]; + // Recreate array, minus index to remove. + while (indexInt < wineListSizeInt) + { + // Skip removalIndex. + if (indexInt == removalIndex) + { + indexInt++; + } + else + { + // If already passed removalIndex. + if (indexInt > removalIndex) + { + tempArray[indexInt - 1] = wineItemArray[indexInt]; + indexInt++; + } + else + { + tempArray[indexInt] = wineItemArray[indexInt]; + indexInt++; + } + } + } + + wineItemArray = tempArray; + removedIDBool = true; + wineListSizeInt--; + } + else + { + index++; + removedIDBool = RemoveWineItem(wineID, index); + } } + return removedIDBool; } #endregion