Skip to content

Instantly share code, notes, and snippets.

@WilliamGarrow
Created November 7, 2015 23:44
Show Gist options
  • Save WilliamGarrow/8b8c3179ece6e9b9fb3c to your computer and use it in GitHub Desktop.
Save WilliamGarrow/8b8c3179ece6e9b9fb3c to your computer and use it in GitHub Desktop.
DEV204x Programming with C# - Module Three Assignment (Better Example)
using System;
namespace ModuleThreeAssignment
{
class Program
{
static void Main(string[] args)
{
GetStudentInformation();
GetTeacherInformation();
GetCourseInformation();
GetDegreeInformation();
GetProgramInformation();
}
#region Student
// get student information
static void GetStudentInformation()
{
Console.WriteLine("-----------------------------");
Console.WriteLine(" Getting Student Information ");
Console.WriteLine("-----------------------------");
Console.WriteLine();
// get user input
Console.Write("First Name: ");
string firstName = Console.ReadLine();
Console.Write("Last Name: ");
string lastName = Console.ReadLine();
Console.Write("Birthdate: ");
DateTime birthDate = ValidateDate(Console.ReadLine());
// print student details
PrintStudentDetails(first: firstName, last: lastName, birthday: birthDate);
}
// print student information
static void PrintStudentDetails(string first, string last, DateTime birthday)
{
Console.WriteLine("\nStudent Details:");
Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday.ToString("MMMM dd, yyyy"));
Console.WriteLine();
}
#endregion
#region Teacher
// get teacher information
static void GetTeacherInformation()
{
Console.WriteLine("-----------------------------");
Console.WriteLine(" Getting Teacher Information ");
Console.WriteLine("-----------------------------");
Console.WriteLine();
// get user input
Console.Write("First Name: ");
string firstName = Console.ReadLine();
Console.Write("Last Name: ");
string lastName = Console.ReadLine();
Console.Write("Birthdate: ");
DateTime birthDate = ValidateDate(Console.ReadLine());
// print teacher details
PrintTeacherDetails(first: firstName, last: lastName, birthday: birthDate);
}
// print teacher information
static void PrintTeacherDetails(string first, string last, DateTime birthday)
{
Console.WriteLine("\nTeacher Details:");
Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday.ToString("MMMM dd, yyyy"));
Console.WriteLine();
}
#endregion
#region Course
// get course information
static void GetCourseInformation()
{
Console.WriteLine("-----------------------------");
Console.WriteLine(" Getting Course Information ");
Console.WriteLine("-----------------------------");
Console.WriteLine();
// get user input
Console.Write("Course Name: ");
string courseName = Console.ReadLine();
Console.Write("Duration in Weeks: ");
int duration = GetInt(Console.ReadLine(), "Duration in Weeks: ");
Console.Write("Teacher: ");
string teacher = Console.ReadLine();
// print degree details
PrintCourseDetails(courseName, duration, teacher);
}
// print course details
static void PrintCourseDetails(string courseName, int duration, string teacher)
{
Console.WriteLine("\nCourse Details:");
Console.WriteLine("A {1} week course, '{0}' taught by {2}", courseName, duration, teacher);
Console.WriteLine();
}
#endregion
#region Degree
// get degree information
static void GetDegreeInformation()
{
Console.WriteLine("-----------------------------");
Console.WriteLine(" Getting Degree Information ");
Console.WriteLine("-----------------------------");
Console.WriteLine();
// get user input
Console.Write("Degree Name: ");
string degreeName = Console.ReadLine();
Console.Write("Credits Required: ");
int creditsRequired = GetInt(Console.ReadLine(), "Credits Required: ");
// print degree details
PrintDegreeDetails(degreeName, creditsRequired);
}
// print degree details
static void PrintDegreeDetails(string degreeName, int creditsRequired)
{
Console.WriteLine("\nDegree Details:");
Console.WriteLine("Degree in {0} requires {1} credits", degreeName, creditsRequired);
Console.WriteLine();
}
#endregion
#region Program
// get Program information
static void GetProgramInformation()
{
Console.WriteLine("-----------------------------");
Console.WriteLine(" Getting Program Information ");
Console.WriteLine("-----------------------------");
Console.WriteLine();
// get user input
Console.Write("Program Name: ");
string ProgramName = Console.ReadLine();
Console.Write("Department Head: ");
string head = Console.ReadLine();
Console.Write("Degrees: ");
string degrees = Console.ReadLine();
// print degree details
PrintProgramDetails(ProgramName, head, degrees);
}
// print degree details
static void PrintProgramDetails(string ProgramName, string head, string degrees)
{
Console.WriteLine("\nProgram Details:");
Console.WriteLine("{0} managed by {1} provides degree in {2}", ProgramName, head, degrees);
Console.WriteLine();
}
#endregion
// recursive validate date
static DateTime ValidateDate(string birthDate)
{
try
{
return DateTime.Parse(birthDate);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message + "\nERROR: Invalid Date!\n");
Console.Write("Birthdate: ");
return ValidateDate(Console.ReadLine());
}
}
// recursive validate integer
static int GetInt(string integer, string msg)
{
try
{
return Convert.ToInt32(integer);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message + "\nERROR: Not an Integer\n");
Console.Write(msg);
return GetInt(Console.ReadLine(), msg);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment