This is done in c#: In this problem you will be asking the user for the number o
ID: 3875116 • Letter: T
Question
This is done in c#:
In this problem you will be asking the user for the number of different movie types they own and add them all up. Prompt the user for the number of DVDs, Blu-Rays and Digital UltraViolet copies they own. Validate each of the 3 prompts and create a number array that will hold the converted values. Next use a for loop to cycle through the array and add up the total number of movies that the user owns.
Once you have the total, use a conditional to test if the user has 100 movies or more. If they have less, give them their total. If they 100 or more, tell the user that you are impressed with their collection.
• User Inputs:
o Amount of DVDs
o Amount of Blu-Rays
o Amount of UltraViolets
• Result To Print Out:
o “You have a total of X movies in your collection.” OR “Wow, I am impressed with your collection of X movies!”
• Data Sets To Test:
o DVDs – 45 Blu-Rays – 15 UltraViolets – 2 :Results - “You have a total of 62 movies in your collection.” o DVDs – 60 Blu-Rays – 75 UltraViolets – 45 :Results - “Wow, I am impressed with your collection of 180 movies!”
Explanation / Answer
using System;
namespace ConsoleApplication1
{
class Program
{
//Array for holding converted value.
int[] movieArray = new int[3];
//Validating whether the user is entering integer value or not. If the user is not entering integer value, user is asked again to enter correct value.
//This will happen till the time user enters correct value.
public void ValidateInput(string movieType, int arrayIndex)
{
var result = false;
int value;
do
{
result = int.TryParse(movieType, out value);
if (result == true)
{
movieArray[arrayIndex] = value;
}
else
{
Console.WriteLine("Enter the correct value as an integer");
movieType = Console.ReadLine();
}
}
while (result != true);
}
static void Main(string[] args)
{
Program obj = new Program();
var movieCount = 0;
//User Inputs for dvd
Console.WriteLine("Enter the number of DVDs");
string dvd = Console.ReadLine();
obj.ValidateInput(dvd,0);
//User Inputs for blu ray
Console.WriteLine("Enter the number of Blu-rays");
string bluRay = Console.ReadLine();
obj.ValidateInput(bluRay,1);
//User Inputs for ultraviolet
Console.WriteLine("Enter the number of ultraviolets");
string ultraViolet = Console.ReadLine();
obj.ValidateInput(ultraViolet,2);
//Adding up total movies
foreach (var data in obj.movieArray)
{
movieCount += data;
}
//To check whether user has 100 moview or more
if (movieCount >= 100)
{
Console.WriteLine("Wow, I am impressed with your collection of " + movieCount + " movies!");
}
else
{
Console.WriteLine("You have a total of " + movieCount + " movies in your collection.");
}
Console.ReadKey();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.