public Katalog(StreamReader tracksFile, StreamReader genresFile) { genres = new Genre(genresFile); try{ string[] separator = { "-" }; string line; while ((line = tracksFile.ReadLine()) != null) { string[] infoFromFile = line.Split(separator, StringSplitOptions.None); for (var i = 0; i < infoFromFile.Length; i++) { infoFromFile[i] = infoFromFile[i].Trim(' '); } if (infoFromFile.Length < 3 || infoFromFile[0] == "" || infoFromFile[2] == "") { continue; } string genre = infoFromFile[3] == "" ? "none" : infoFromFile[3]; Artist artist = new Artist(infoFromFile[0], genre); Album album; if (infoFromFile[1] == "") { infoFromFile[1] = infoFromFile[2]; } if (albums.TryGetValue(infoFromFile[1].ToLower().GetHashCode(), out var outAlbum)) { if (outAlbum.Artist.Name.ToLower() != infoFromFile[0] || outAlbum.Artist.Name.ToLower() == "various artists") { artist.AddAlbum(ref outAlbum); if (outAlbum.Artist.Name != "various artists") { outAlbum.Type = katalogTypes.compilation; outAlbum.Artist = new Artist("various artists"); } } album = outAlbum; } else { album = new Album(infoFromFile[1], ref artist); artist.AddAlbum(ref album); } int year; Int32.TryParse(infoFromFile[4], out year); song Song = new song(year, infoFromFile[2], artist, album, genre); Addsong(Song); } } catch (System.Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
public void Addsong(song Song) { if (Song.Album == null || Song.Artist == null || Song.Genre == null) { Console.WriteLine("Bad song metainfo \nnot aded"); return; } if (!this.songs.TryGetValue(Song.GetHashCode(), out var outSong)) { AddAlbum(Song.Album); outSong = Song; if (this.albums.TryGetValue(Song.Album.GetHashCode(), out var outAlbum)) { outAlbum.songs.Add(Song); } this.songs.Add(Song.GetHashCode(), outSong); } else { Console.WriteLine("track already in catalogue"); } }