bool AscendingSort(OldBook a, OldBook b) => a > b;
bool DescendingSort(OldBook a, OldBook b) => a < b;
public void Run() { // Try loading data PrintLoadMain(); int choice; FileStream fs; do { choice = GetChoice(); string filePath; switch (choice) { case 1: filePath = $"{_currentDir}\\{_fileName}.json"; if (!File.Exists(filePath)) { Console.WriteLine($"File {filePath} does not exist."); break; } var jsonUtf8Reader = new Utf8JsonReader(File.ReadAllBytes(filePath)); _books = JsonSerializer.Deserialize <SuperList <OldBook> >(ref jsonUtf8Reader, _serializerOptions); break; case 2: filePath = $"{_currentDir}\\{_fileName}.xml"; if (!File.Exists(filePath)) { Console.WriteLine($"File {filePath} does not exist."); break; } fs = File.OpenRead(filePath); if (_xmlSerializer.Deserialize(fs) is SuperList <OldBook> booksFromXml) { _books = booksFromXml; } fs.Close(); break; case 3: filePath = $"{_currentDir}\\{_fileName}.bin"; if (!File.Exists(filePath)) { Console.WriteLine($"File {filePath} does not exist."); break; } fs = File.OpenRead(filePath); if (_binaryFormatter.Deserialize(fs) is OldBook[] booksFromBinary) { _books = new SuperList <OldBook>(booksFromBinary); } fs.Close(); break; case 4: // Skip load savings phase break; } } while (choice < 1 || choice > 4); if (choice != 4) { Console.Write("Finish loading savings data. Press any key to continue."); Console.ReadLine(); } // Assigning events handlers _books.OnAdd += Book_OnAdd; _books.OnMutate += Book_OnMutate; // Run core program while (true) { Console.Clear(); PrintState(); Console.WriteLine("---------"); PrintMainMenu(); choice = GetChoice(); switch (choice) { case 1: Console.WriteLine(_books); Console.WriteLine("----------------"); Console.Write("Press any key to continue."); Console.ReadLine(); break; case 2: bool isContinue; var newBooks = new SuperList <OldBook>(); do { Console.WriteLine("------------------------"); Console.WriteLine("Please enter book info."); newBooks.Add(OldBook.Create()); Console.WriteLine("----------"); Console.Write("New book added successfully."); Console.WriteLine("Do you want to continue adding new book? (Y): Yes; (N): No;"); Console.Write("Your choice: "); isContinue = Console.ReadLine()?.Trim().ToUpper() == "Y"; } while (isContinue); _books += newBooks; Console.Write("Press any key to continue."); Console.ReadLine(); break; case 3: Console.WriteLine("----------"); if (_books.Count == 0) { Console.Write("Cannot copy since books list is empty. Press any key to continue."); } else { _books++; Console.Write("New book added successfully. Press any key to continue."); } Console.ReadLine(); break; case 4: _books.BubbleSort(AscendingSort); Console.WriteLine("Books are ascending sorted by Price. Press any key to continue."); Console.ReadLine(); break; case 5: _books.BubbleSort(DescendingSort); Console.WriteLine("Books are ascending sorted by Price. Press any key to continue."); Console.ReadLine(); break; case 6: byte[] serialized = JsonSerializer.SerializeToUtf8Bytes(_books, _serializerOptions); File.WriteAllBytes($"{_currentDir}\\{_fileName}.json", serialized); Console.WriteLine($"Books are saved in `{_fileName}.json`. Press any key to continue."); Console.ReadLine(); break; case 7: fs = File.Open($"{_currentDir}\\{_fileName}.xml", FileMode.Create); _xmlSerializer.Serialize(fs, _books); Console.WriteLine($"Books are saved in `{_fileName}.xml`. Press any key to continue."); Console.ReadLine(); break; case 8: fs = File.Open($"{_currentDir}\\{_fileName}.bin", FileMode.Create); _binaryFormatter.Serialize(fs, _books.ToArray()); Console.WriteLine($"Books are saved in `{_fileName}.bin`. Press any key to continue."); Console.ReadLine(); break; case 9: Console.WriteLine("Are you sure? (Y): Yes; (N): No;"); Console.Write("Enter: "); if (Console.ReadLine()?.Trim().ToUpper() == "Y") { break; } continue; default: Console.WriteLine("Unknown option, please try again."); Console.ReadLine(); break; } if (choice == 9) { break; } } }