Esempio n. 1
0
        static void Main(string[] args)
        {
            Init();// this method fills the arrays above with data


            // QUERIES!

            // - 1) how many Songs start with the letter 'a' (case insensitive)
            // - 2) how many artists end with letter 'a' (case insensitive)
            // - 3) whats the name of the song with longest duration
            // - 4) whats the total Duration of all Songs
            // - 5) how many albums have Songs longer than 300 seconds
            // - 6) print the names of the artists(separated with "--"), that have more than one album of PopRock genre
            // - 7) print the name of the album that has highest Average duration of a song
            // - 8) how many characters has the song that has the shortest Duration
            // - 9) print the name and the genre of the album that has most songs
            // - 10) print the name of the artist that has most songs
            // - 11) print the type of the artist(SoloArtist/Band) that has most albums published before year 2000
            // - 12) print the average song duration, of the album that has most songs

            // Bonus:
            // - print the longest song duration of the album that has least songs
            // - print the name of the album that has most songs that contain letter 'a' in the name
            // - print the name of the artist that has most songs that end with letter 'd'



            // ************ Don't mind the structure, focus on the lists declared on the beginning of Program.cs ****************

            // 3, 2, 1.... GO! :)
            #region 1)
            IEnumerable <Song> SongsThatStartWithLowercaseA = from song in Songs
                                                              where song.Name.ToLower().StartsWith('a')
                                                              select song;
            int NumberOfSongsThatStartWithLowercaseA = SongsThatStartWithLowercaseA.Count();
            Console.WriteLine($"1)  The number of songs that start with the letter 'a' is {NumberOfSongsThatStartWithLowercaseA}.");
            #endregion
            #region 2)
            IEnumerable <Artist> ArtistWithNameEndingInA = from artist in Artists
                                                           where artist.FullName.ToLower().EndsWith('a')
                                                           select artist;
            int NumberOfArtistsWithNameEndingInA = ArtistWithNameEndingInA.Count();
            Console.WriteLine($"2)  The number of artists whose name ends with the letter 'a' is {NumberOfArtistsWithNameEndingInA}.");
            #endregion
            #region 3)
            Song LongestDurationSong = Songs.Aggregate((song1, song2) => song1.Duration > song2.Duration ? song1 : song2);
            Console.WriteLine($"3)  The song with the longest duration is {LongestDurationSong.Name}.");
            #endregion
            #region 4)
            var TotalDurationOfAllSongs = Songs.Select(song => song.Duration)
                                          .Aggregate((dur1, dur2) => dur1 + dur2);
            Console.WriteLine($"4)  The total duration of all songs is {TotalDurationOfAllSongs} seconds.");
            #endregion
            #region 5)
            IEnumerable <Album> AlbumsWithSongsLongerThan5min = from album in Albums
                                                                where album.Songs.Where(song => song.Duration > 300).ToList().Any()
                                                                select album;
            int NumOfAlbumsWithSongsLongerThan5min = AlbumsWithSongsLongerThan5min.Count();
            Console.WriteLine($"5)  There are {NumOfAlbumsWithSongsLongerThan5min} albums that contain at least a single song that lasts longer than 5 minutes.");
            #endregion
            #region 6)
            IEnumerable <Artist> ArtistsWithAtLeast2PopRockAlbums = from artist in Artists
                                                                    where artist.Albums.Where(alb => alb.Genre == Genre.PopRock).Count() > 1
                                                                    select artist;
            Artist LastArtistWithAtLeast2PopRockAlbums = ArtistsWithAtLeast2PopRockAlbums.Last();
            Console.Write("6)  Artists with 2 or more PopRock Albums: ");
            foreach (Artist artist in ArtistsWithAtLeast2PopRockAlbums)
            {
                if (artist.Equals(LastArtistWithAtLeast2PopRockAlbums))
                {
                    Console.Write($"{artist.FullName}.");
                    Console.WriteLine("");
                }
                else
                {
                    Console.Write($"{artist.FullName}--");
                }
            }

            #endregion
            #region 7)
            var HighestAverageDurationAlbum = Albums.Aggregate((alb1, alb2) =>
                                                               alb1.Songs.Select(song => song.Duration)
                                                               .Aggregate(0, (dur1, dur2) => dur1 + dur2, sum => sum / alb1.Songs.Count()) >
                                                               alb2.Songs.Select(song => song.Duration)
                                                               .Aggregate(0, (dur1, dur2) => dur1 + dur2, sum => sum / alb2.Songs.Count())
                                                                ? alb1 : alb2);
            Console.WriteLine($"7)  The album with the highest average duration of a song is {HighestAverageDurationAlbum.Name}.");


            #endregion
            #region 8)
            Song ShortestDurationSong           = Songs.Aggregate((song1, song2) => song1.Duration < song2.Duration ? song1 : song2);
            int  ShortestDurationSongNameLength = ShortestDurationSong.Name.Length;
            Console.WriteLine($"8)  The song with the shortest duration has {ShortestDurationSongNameLength} characters in its name.");
            #endregion
            #region 9)
            Album AlbumWithMostSongs = Albums.Aggregate((alb1, alb2) => alb1.Songs.Count() > alb2.Songs.Count() ? alb1 : alb2);
            Console.WriteLine($"9)  The album with most songs is the {AlbumWithMostSongs.Genre} album: {AlbumWithMostSongs.Name}");
            #endregion
            #region 10)
            Artist ArtistWithMostSongs = Artists.Aggregate((art1, art2) => art1.Albums.SelectMany(alb => alb.Songs).Count() > art2.Albums.SelectMany(alb => alb.Songs).Count() ? art1 : art2);
            Console.WriteLine($"10) The artist with the most songs is {ArtistWithMostSongs.FullName}.");
            #endregion
            #region 11)
            Artist TypeOfArtistWithMostAlbumsBeforeYear2000 = Artists.Aggregate((art1, art2) =>
                                                                                art1.Albums.Where(alb => alb.Year < 2000).Count() > art2.Albums.Where(alb => alb.Year < 2000).Count()
                                                              ? art1 : art2);
            Console.WriteLine($"11) The artist with the most albums released before the year 2000 is of type {TypeOfArtistWithMostAlbumsBeforeYear2000.ArtistType}.");
            #endregion
            #region 12)
            int AvgSongDurationOfAlbumWithMostSongs = AlbumWithMostSongs.Songs
                                                      .Select(song => song.Duration)
                                                      .Aggregate(0, (dur1, dur2) => (dur1 + dur2), dursum => dursum / AlbumWithMostSongs.Songs.Count());
            Console.WriteLine($"12) The average length of a song in the album with the most songs lasts {AvgSongDurationOfAlbumWithMostSongs} seconds.");
            #endregion
            Console.WriteLine("\n --------------------------------BONUS--------------------------------\n ");
            #region Bonus 1)
            Album AlbumWithLeastSongs = Albums.Aggregate((alb1, alb2) => alb1.Songs
                                                         .Select(song => song.Duration).Aggregate((dur1, dur2) => dur1 + dur2) <
                                                         alb2.Songs
                                                         .Select(song => song.Duration).Aggregate((dur1, dur2) => dur1 + dur2)
                                                                       ? alb1 : alb2);
            Song LongestSongFromAlbumWithLeastSongs = AlbumWithLeastSongs.Songs.Aggregate((song1, song2) =>
                                                                                          song1.Duration > song2.Duration
                                                                          ? song1 : song2);
            Console.WriteLine($"1)  The longest song in the album with least songs lasts {LongestSongFromAlbumWithLeastSongs.Duration} seconds.");
            #endregion
            #region Bonus 2)
            Album AlbumWithMostAs = Albums.Aggregate((alb1, alb2) =>
                                                     alb1.Songs.Select(song => song.Name.ToLower().Contains('a')).Count() >
                                                     alb2.Songs.Select(song => song.Name.ToLower().Contains('a')).Count()
                                            ? alb1 : alb2);
            Console.WriteLine($"2)  The album which has the most songs that contain the letter 'a' in its name is {AlbumWithMostAs.Name}.");
            #endregion
            #region Bonus 3)
            Artist ArtistWithMostSongsThatEndInD = Artists.Aggregate((art1, art2) =>
                                                                     art1.Albums.SelectMany(alb => alb.Songs)
                                                                     .Select(song => song.Name)
                                                                     .Where(name => name.ToLower().EndsWith('d')).Count() >
                                                                     art2.Albums.SelectMany(alb => alb.Songs)
                                                                     .Select(song => song.Name)
                                                                     .Where(name => name.ToLower().EndsWith('d')).Count()
                                                           ? art1 : art2);
            Console.WriteLine($"3)  The artist that has the most songs that end in 'd' is {ArtistWithMostSongsThatEndInD.FullName}.");
            #endregion


            Console.ReadKey();
        }