In this step, we will refactor the code to address some issues and improve its quality. We will leverage GitHub Copilot to assist with the refactoring process.
-
Make Search Methods Case-Insensitive
The search methods implemented in the previous exercise are case-sensitive. Let's refactor these methods to ignore case when comparing strings.
Use GitHub Copilot to refactor the
SearchAlbumsByArtist
,SearchAlbumsByTitle
, andSearchAlbumsByGenre
methods to perform case-insensitive comparisons.The
fix
slash command can be used to refactor the code. Select the code and use the following prompt to update theSearchAlbumsByArtist
method:/fix Make SearchAlbumsByArtist case-insensitive
-
Refactor
AddAlbum
MethodRefactor the
AddAlbum
method to check if an album already exists before adding it to the list. If an album with the sameTitle
already exists, throw an exception with the message "Album already exists".The refactored
AddAlbum
method should look like this:public void AddAlbum(Album album) { if (_albums.Any(a => a.Title.Equals(album.Title, StringComparison.OrdinalIgnoreCase))) { throw new Exception("Album already exists"); } _albums.Add(album); }
-
Introduce Custom Exception
Create a custom exception class
AlbumAlreadyExistsException
. Refactor theAddAlbum
method to throw this custom exception when an album already exists. -
Update Application for User Interaction
We will now use the search methods to allow users to search for albums by artist, title, or genre.
Let us start with adding the
PrintAlbums
method to print the search results in a tabular format. Copy thePrintAlbums
method below in theProgram.cs
file:public static void PrintAlbums(List<Album> albums) { Console.WriteLine("--------------------------------------------------------------"); Console.WriteLine("| ID | Title | Artist | Genre |"); Console.WriteLine("--------------------------------------------------------------"); foreach (var album in albums) { Console.WriteLine($"| {album.Id,-6} | {album.Title,-19} | {album.Artist,-18} | {album.Genre,-9} |"); } Console.WriteLine("--------------------------------------------------------------"); }
Now use Copilot to update the
Main
method in theProgram.cs
file to include the following options. Based on the user's choice, the application should display the list of matching albums.- Search albums by artist
- Search albums by title
- Search albums by genre
public static void Main(string[] args) { var musicStoreService = new MusicStoreService(); var albums = musicStoreService.GetAlbums(); Console.WriteLine("Choose an option:"); Console.WriteLine("1. Search albums by artist"); Console.WriteLine("2. Search albums by title"); Console.WriteLine("3. Search albums by genre"); var option = Console.ReadLine(); switch (option) { case "1": Console.WriteLine("Enter artist name:"); var artist = Console.ReadLine(); var albumsByArtist = musicStoreService.SearchAlbumsByArtist(artist); PrintAlbums(albumsByArtist); break; case "2": Console.WriteLine("Enter title:"); var title = Console.ReadLine(); var albumsByTitle = musicStoreService.SearchAlbumsByTitle(title); PrintAlbums(albumsByTitle); break; case "3": Console.WriteLine("Enter genre:"); var genre = Console.ReadLine(); var albumsByGenre = musicStoreService.SearchAlbumsByGenre(genre); PrintAlbums(albumsByGenre); break; default: Console.WriteLine("Invalid option"); break; } }
If you have used the
Main
method from the sample code above, you can use GitHub Copilot'sExplain
feature to understand the logic behind the code. -
Test the Application
Run the application and test the search functionality by artist, title, and genre. You should see the matching albums displayed based on the search criteria entered by the user in the console.
Congrats on reaching this point! Here are some additional challenges you can try to hone your skills further:
- Add required validation to the
AddAlbum
method to ensure that theTitle
, andArtist
properties are not empty. - Update the search methods to allow partial matches.
- Sort the albums by release date before displaying them.
- Add logging to log any exceptions that occur during the search process.
Previous - Step 2: Building the Music Store Application | Next - Step 4: Adding Unit Tests to MusicStoreService