// Finds the specified Item within the Library's Catalog and checks to see if it is checked out to then be checked back in. // If checked out, the Item's status is changed to OnShelf (checked in). // If it is already checked in, the console states that the item is already checked in. public void CheckIn(List <Item> itemsList) { bool proceed = CnslFormatter.AskYesOrNo($"Would you like to return an item? "); while (proceed) { Console.Clear(); for (int i = 0; i < itemsList.Count; i++) { Console.Write($"Item {i + 1}. "); itemsList[i].PrintInfo(); Console.WriteLine(); } string input = CnslFormatter.PromptForInput($"Please select the item you would like to return? [{CnslFormatter.MoreThanOne(itemsList)}]: "); if (Int32.TryParse(input, out int num)) { int index = num - 1; if (index < 0 || index >= itemsList.Count) { Console.WriteLine($"Input out of range. {CnslFormatter.MoreThanOne(itemsList)}."); } else { if (itemsList[index].Status == ItemStatus.CheckedOut || itemsList[index].Status == ItemStatus.Overdue) { Console.WriteLine(Environment.NewLine + "You have checked in: "); Console.WriteLine($" {itemsList[index].Title} by {itemsList[index].Author}"); itemsList[index].Status = ItemStatus.OnShelf; proceed = false; } else { Console.WriteLine("Cannot complete checkout at this time."); CnslFormatter.PauseByAnyKey(); } } } else { Console.WriteLine("Non-Integer input detected. Please enter an integer."); } } CnslFormatter.PauseByAnyKey(); }
static void Main(string[] args) { List <Item> itemsLoaded = new List <Item>(); StreamReader reader = new StreamReader("../../../SavedItems.txt"); string line = reader.ReadLine(); while (line != null) { string[] itemEntryInfo = line.Split("|"); // Record line Item newItem = GenerateItem(itemEntryInfo); itemsLoaded.Add(newItem); line = reader.ReadLine(); } reader.Close(); Library L = new Library(itemsLoaded); bool userContinue = true; Console.WriteLine("Welcome to the Library!"); while (userContinue) { LibraryMenu(); string input = CnslFormatter.PromptForInput($"\nWhat would you like to do? "); Console.WriteLine(); if (input == "1") //prompts user to ask if they want to check out an item { L.PrintItems(); bool proceed = CnslFormatter.AskYesOrNo($"Would you like to check out an item?"); if (proceed) { L.Checkout(L.Catalog); } else { Console.Clear(); } } else if (input == "2") //prompt for user to search by author name { input = CnslFormatter.PromptForInput("Please enter name to search: "); List <Item> resultsAuthor = L.SearchByAuthor(input); if (resultsAuthor.Count == 0) { Console.WriteLine("\nNo items found."); CnslFormatter.PauseByAnyKey(); } else if (resultsAuthor.Count >= 1) { Console.WriteLine(); foreach (Item result in resultsAuthor) { result.PrintInfo(); Console.WriteLine(); } bool proceed = CnslFormatter.AskYesOrNo($"Would you like to check out an item?"); if (proceed) { L.Checkout(resultsAuthor); } else { Console.Clear(); } } } else if (input == "3") //prompt for user to search by title { input = CnslFormatter.PromptForInput("Please enter title to search: "); List <Item> resultsTitle = L.SearchByTitle(input); if (resultsTitle.Count == 0) { Console.WriteLine("\nNo items found."); CnslFormatter.PauseByAnyKey(); } else if (resultsTitle.Count >= 1) { foreach (Item result in resultsTitle) { result.PrintInfo(); Console.WriteLine(); } bool proceed = CnslFormatter.AskYesOrNo($"Would you like to check out an item?"); if (proceed) { L.Checkout(resultsTitle); } else { Console.Clear(); } } } else if (input == "4") //lists all items that has been checked out { Console.Clear(); Console.WriteLine("Items Currently Checked Out:\n"); List <Item> results = new List <Item>(); foreach (Item itemMatch in L.Catalog) { if (itemMatch.Status == ItemStatus.CheckedOut || itemMatch.Status == ItemStatus.Overdue) { results.Add(itemMatch); } } if (results.Count <= 0) { Console.WriteLine("No matches found"); CnslFormatter.PauseByAnyKey(); } else if (results.Count >= 1) { foreach (Item result in results) { result.PrintInfo(); Console.WriteLine(); } L.CheckIn(results); } } else if (input == "5") //Brings up a series of prompts for the user to create new items { L.AddNewItem(); } else if (input == "6") //displays item list and asks if user want to check in an item { userContinue = false; } else { Console.WriteLine("Invalid input. Please try again. "); CnslFormatter.PauseByAnyKey(); } } Console.WriteLine("Saving Library Content!"); File.WriteAllText("../../../SavedItems.txt", string.Empty); // Clear the File StreamWriter writer = new StreamWriter("../../../SavedItems.txt"); // Should generate new file if deleted List <Item> itemsSaved = L.Catalog; foreach (Item item in itemsSaved) { string itemEntry = GenerateEntry(item); writer.WriteLine(itemEntry); } writer.Close(); Console.WriteLine("Goodbye!"); }