In: Computer Science
programming language is c#.
Create a method that prompts a user of your console application to
input the information for a student:
static void GetStudentInfo()
{
Console.WriteLine("Enter the student's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the student's last name");
string lastName = Console.ReadLine();
// Code to finish getting the rest of the student data
.....
}
static void PrintStudentDetails(string first, string last,
string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", first, last,
birthday);
}
1. Using the above partial code sample, complete the method for
getting student data.
2. Create a method to get information for a teacher, a course, and
program, and a degree using a
similar method as above.
3. Create methods to print the information to the screen for each
object such as static
void PrintStudentDetails(...).
4. Just enter enough information to show you understand how to use
methods. (At least three
attributes each).
5. Call the Get methods from the Main method in the
application.
6. Call the Print methods from within each Get method.
Exceptions
1. At times, developers create method signatures early on in the
development process but leave the
implementation until later. This can lead to methods that are not
complete if a developer forgets
about these empty methods. One way to help overcome the issue of
not remembering to complete
a method is to throw an exception in that method if no
implementation details are present.
2. For this task, use MSDN to research the NotImplementedException
exception.
3. Create a new method for validating a student's birthday. You
won't write any validation code in this
method, but you will throw the NotImplementedException in this
method.
4. Call the method from Main() to verify your exception is
thrown.
Challenge
Using MSDN, research the System.DateTime type. Using the
information you learn, modify your birth
date field for the student and/or teacher to ensure it used a
DateTime type if you did not already
include that in your data for these objects.
• Remove your NotImplementedException statement in the validate
method you created above.
• Create a try/catch block to catch invalid date entries and
display a message to the user if this
occurs. (Console output)
• Assume that your student must be at least 18 years of age to
enroll at a university.
• Write code that validates the student is at least 18 years old.
You can use birth year and math or
you can calcuate from today's date and work back.
• Output an error message to the console window if the student's
age is less than 18
Hi,
Please find the below output for the program
using System;
namespace ConsoleApp1
{
class Program
{
public static string sUpdatedbirthDate = string.Empty;
static void Main(string[] args)
{
//Fetch and show student Details
GetStudentInfo();
//Fetch and show Teachers Details
GetTeachersInfo();
//Fetch and show Course Details
GetcourseInfo();
//Fetch and show Program Details
GetProgramInfo();
//Fetch and show Degree Details
GetDegreeInfo();
}
/// <summary>
/// students infor
/// </summary>
static void GetStudentInfo()
{
Console.WriteLine("Enter the student's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the student's last name");
string lastName = Console.ReadLine();
Console.WriteLine("Enter the student's Birthdate in dd/MM/yyyy format");
string dtBirthDate = (Console.ReadLine());
// validate Students Age
int age = ValidateStudentsBirthday(dtBirthDate);
string str = string.Empty;
if (age < 18)
{
Console.WriteLine("student's age is : "+ age + ", less than 18");
}
PrintStudentDetails(firstName, lastName, sUpdatedbirthDate.Length > 0 ? sUpdatedbirthDate : dtBirthDate, age);
}
/// <summary>
/// Print Student Details
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="birthday"></param>
static void PrintStudentDetails(string first, string last, string birthday, int age)
{
Console.WriteLine("{0} {1} was born on: {2} and age is: {3}", first, last, birthday, age);
}
/// <summary>
/// teachers info
/// </summary>
static void GetTeachersInfo()
{
Console.WriteLine("Enter the Teacher's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the Teacher's last name");
string lastName = Console.ReadLine();
Console.WriteLine("Enter the Teacher's Birthdate in MM/DD/YYYY format");
string dtBirthDate = (Console.ReadLine());
PrintTeachersDetails(firstName, lastName, dtBirthDate);
}
/// <summary>
/// Print Teacher Details
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="birthday"></param>
static void PrintTeachersDetails(string first, string last, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
}
/// <summary>
/// Course Info
/// </summary>
static void GetcourseInfo()
{
Console.WriteLine("Enter the course name: ");
string coursename = Console.ReadLine();
Console.WriteLine("Enter the course duration in Years");
string duration = Console.ReadLine();
Console.WriteLine("Enter the cost of the course");
string courseCost = (Console.ReadLine());
PrintCourseDetails(coursename, duration, courseCost);
}
/// <summary>
/// Print Course Details
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="birthday"></param>
static void PrintCourseDetails(string coursename, string duration, string courseCost)
{
Console.WriteLine("{0} course is having duration - {1} and cose of that Course is: {2}", coursename, duration, courseCost);
}
/// <summary>
/// Program Info
/// </summary>
static void GetProgramInfo()
{
Console.WriteLine("Enter the Program name: ");
string ProgramName = Console.ReadLine();
Console.WriteLine("Enter the Program Start Date");
string ProgramStartDate = Console.ReadLine();
Console.WriteLine("Enter the Program Duration");
string ProgramDuratioin = (Console.ReadLine());
PrintProgramDetails(ProgramName, ProgramStartDate, ProgramDuratioin);
}
/// <summary>
/// Print Student Details
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="birthday"></param>
static void PrintProgramDetails(string ProgramName, string ProgramStartDate, string ProgramDuratioin)
{
Console.WriteLine("Program {0} will be starting on {1} and duration is: {2}", ProgramName, ProgramStartDate, ProgramDuratioin);
}
/// <summary>
/// Degree Info
/// </summary>
static void GetDegreeInfo()
{
Console.WriteLine("Enter the Name of Degree: ");
string DegreeName = Console.ReadLine();
Console.WriteLine("Enter the Duration of Degree in years");
string DurationOfDegree = Console.ReadLine();
Console.WriteLine("Enter the semester parrtern");
string DegreeSemeseterPatters = (Console.ReadLine());
PrintDegreeDetails(DegreeName, DurationOfDegree, DegreeSemeseterPatters);
}
/// <summary>
/// Print Degree Details
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="birthday"></param>
static void PrintDegreeDetails(string DegreeName, string DurationOfDegree, string DegreeSemeseterPatters)
{
Console.WriteLine("Degree Name - {0} will {1} Years of duration and Semester pattern is: {2}", DegreeName, DurationOfDegree, DegreeSemeseterPatters);
}
/// <summary>
/// Calculate Students BirthDay and return the result
/// </summary>
/// <param name="iBirthYear"></param>
/// <returns></returns>
static private int ValidateStudentsBirthday(string sBirthdate)
{
try
{
//Set today's date as a variable
DateTime todayDate = DateTime.Today;
DateTime dateOfBirth = new DateTime();
while (!DateTime.TryParseExact(sBirthdate, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out dateOfBirth))
{
Console.WriteLine("Invalid date, please retry");
sBirthdate = Console.ReadLine();
sUpdatedbirthDate = sBirthdate;
//In case of invalid date, again check the valid date in recursive loop
ValidateStudentsBirthday(sBirthdate);
}
//Calculate user age
var today = DateTime.Today;
var a = (today.Year * 100 + today.Month) * 100 + today.Day;
var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;
int userAge = (a - b) / 10000;
return userAge;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
}
}
Test Cases: