In: Computer Science
Code in C#
Part 1
Build a Series
class that has the following attributes and behaviors:
Attributes
1.) A name -- this can be any string value (i.e. “Firefly”)
2.) A number of episodes -- this can be any non-negative integer value
3.) An episode length -- this can be a double value, in hours, representing the length of an episode (i.e. 0.5 for a half-hour episode, or 0.37 for a 23 minute episode)
4.) A forKids attribute that holds a boolean value representing whether the show is suitable for kids. A boolean value true means the show is kid-friendly.
Behaviors
1.)Create a method named GetViewingHours that returns the total amount of hours of content for this series. This can be calculated by multiplying the number of episodes by the episode length.
2.) Create a method named PrintDetails that prints to the console the name, number of episodes, and the length of each episode.
Part 2
: In Main(), create an application that allows for the following:
1.) Output a line of text that shows the name of your Streaming Service.
2.) Prompt the user to enter a name of a series, the number of episodes in the series, the length of each episode, and whether or not the series is kid-friendly. For simplicity, assume each episode of the series has the same length.
3.) Keep prompting the user to enter more series’ information until the total viewing hours available is greater than 1000 hours. (This means your channel has ~ 2 years of content if consumed at 1.5 hours a day).
4.) After all series have been entered, print out the details of all kid-friendly series.
5.) After all series have been entered, print the name of the series with the most episodes.
6.)After all series have been entered, print the average length of each series
/* C # program that prompts user to enter the name of the
series,
* number of episodes, and length of the each episode. Then
* print the most viewed episode, average length of the
episode,
* kids friendly episodes to console output */
//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Series
{
class Program
{
static void
Main(string[] args)
{
string name;
int numEpisodes;
double length;
string userInput;
bool forKids;
//create a List of Series class
List<Series> seriesList = new List<Series>();
Console.WriteLine("Live streaming company ltd");
int MAX_HOURS = 1000;
int totalHours = 0;
//Loop until hours hours less than MAX_HOURS
while (totalHours < MAX_HOURS)
{
Console.Write("Enter name of Series: ");
name = Console.ReadLine();
Console.Write("Enter # of episodes: ");
int.TryParse(Console.ReadLine(), out numEpisodes);
Console.Write("Enter length of each episode: ");
double.TryParse(Console.ReadLine(), out length);
Console.Write("Enter y for kids friendly or n for not kids friendly
");
userInput = Console.ReadLine();
if (userInput.Equals("y") || userInput.Equals("Y"))
forKids = true;
else
forKids = false;
//create a series object with name, number of episodes, length and
forkids
Series series = new Series(name, numEpisodes, length,
forKids);
seriesList.Add(series );
//add total hours to total hours
totalHours+=series.GetViewingHours();
}
//calling methods
Console.WriteLine("Printing most episodes ");
printMostEpisodes(seriesList);
Console.WriteLine();
Console.WriteLine("Printing kids friendly episodes ");
printKidsFriendly(seriesList);
Console.WriteLine();
Console.WriteLine("Printing aveage length of episodes ");
printAverageLength(seriesList);
Console.ReadLine();
}
/*Method to print the
average length of the series */
public static void
printAverageLength(List<Series> seriesList)
{
for (int index = 0; index < seriesList.Count; index++)
{
Console.WriteLine("Name : {0} ", seriesList.ElementAt(index).Name
);
Console.WriteLine("# of episodes : {0} ",
seriesList.ElementAt(index).NumEpisodes );
Console.WriteLine("Average length of episode : {0:#.##} ",
seriesList.ElementAt(index).NumEpisodes /
seriesList.ElementAt(index).Length);
}
}//end of
method
/*Method to display the
most episodes of the series */
public static void
printMostEpisodes(List<Series> seriesList)
{
Series mostEpisodeSeries = seriesList.ElementAt(0);
for(int index=1;index<seriesList.Count ;index++)
{
if (seriesList.ElementAt(index).NumEpisodes
>mostEpisodeSeries.NumEpisodes)
{
mostEpisodeSeries = seriesList.ElementAt(index);
}
}
mostEpisodeSeries.PrintDetails();
}//end of method
/*Method to
display only kids friendly*/
public static void
printKidsFriendly(List<Series> seriesList)
{
foreach (var item in seriesList)
{
if (item.KidsFriendly)
{
item.PrintDetails();
}
}
}//end of method
}
}
------------------------------------------------------------------------------------------------------
//Series.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Series
{
class Series
{
private string
name;
private int
numEpisodes;
private double
length;
private bool
forKids;
public
Series(string name, int numEpisodes,
double length, bool forKids)
{
this.name = name;
this.numEpisodes = numEpisodes;
this.length = length;
this.forKids = forKids;
}
public string
Name
{
set { value = name; }
get { return name; }
}
public double
Length
{
set { value = length; }
get { return length; }
}
public bool
KidsFriendly
{
set { value = forKids; }
get { return forKids; }
}
public int
NumEpisodes
{
set { value = numEpisodes ; }
get { return numEpisodes; }
}
public int
GetViewingHours()
{
int totalHours = 0;
length = Math.Ceiling(length * 60);
totalHours = (int)(numEpisodes * length);
return totalHours;
}
public void
PrintDetails()
{
Console.WriteLine("Name : {0} ", name);
Console.WriteLine("# of episodes : {0} ", numEpisodes );
Console.WriteLine("Length of Each episode : {0} ",
length.ToString("F2") );
}
}
}
------------------------------------------------------------------------------------------------------
Sample Output: