Skip to content

Instantly share code, notes, and snippets.

@WilliamGarrow
Last active November 7, 2015 23:03
Show Gist options
  • Save WilliamGarrow/d7156abe24b55b3058b5 to your computer and use it in GitHub Desktop.
Save WilliamGarrow/d7156abe24b55b3058b5 to your computer and use it in GitHub Desktop.
DEV204x Programming with C# - Module Three Assignment
/// Module Three Assignment
using System;
namespace ModuleThreeAssignment
{
class Program
{
static void Main(string[] args)
{
// From within Main() call each of the methods to prompt for input from a user.
GetStudentInformation();
GetTeacherInformation();
GetCourseInformation();
GetProgramInformation();
GetDegreeInformation();
}
static void GetStudentInformation()
{
// Capture student information input from application user.
Console.WriteLine("--- Student Information ---");
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 (DD/MM/YYYY):");
string date = Console.ReadLine();
DateTime birthday = validateLegalAge(date);
DateTime birthDate = Convert.ToDateTime(birthday);
bool isStudent = true; // *Added Functionality Beyond What is Requested in Assignment*
string isThisAStudent; // True/False variable that will establish Student/Teacher relationship
if (isStudent)
{
isThisAStudent = "Student";
}
else
{
isThisAStudent = "Teacher";
}
// *Added Functionality Beyond What is Requested in Assignment*
int count = 01; string formatStr = string.Format("ID{0:00000000}", count); // Generates a sequential student identification number
PrintStudentDetails(isThisAStudent, count, firstName, lastName, birthDate);
}
static void PrintStudentDetails(string isThisAStudent, int count, string firstName, string lastName, DateTime birthDate)
{
Console.WriteLine("\r\n{0} Name: {1} {2}\r\nID number: {3}\r\nwas born in {4}.\r\n", isThisAStudent, firstName, lastName, count, birthDate.Year);
}
static void GetTeacherInformation()
{
// Capture teacher information input from application user.
Console.WriteLine("--- Teacher Information ---");
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 credentials: ");
string credentials = Console.ReadLine();
Console.WriteLine("Enter the teacher's class assignment: ");
string classAssignment = Console.ReadLine();
bool isStudent = false;
string isThisAStudent;
if (isStudent)
{
isThisAStudent = "Student";
}
else
{
isThisAStudent = "Teacher";
}
PrintTeacherDetails(isThisAStudent, firstName, lastName, credentials, classAssignment);
}
static void PrintTeacherDetails(string isThisAStudent, string firstName, string lastName, string credentials, string classAssignment)
{
Console.WriteLine("\r\n{0} Name: {1} {2}, {3}\r\nAssigned to class: {4}.\r\n", isThisAStudent, firstName, lastName, credentials, classAssignment);
}
static void GetCourseInformation()
{
// Capture course information input from application user.
Console.WriteLine("--- Course Information ---");
Console.WriteLine("Enter the course name: ");
string courseName = Console.ReadLine();
Console.WriteLine("Enter the number of credits earned with successful completion of course: ");
int courseCredits = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter the number of courses required to complete program: ");
int coursesRequired = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter the associated degree name: ");
string degreeName = Console.ReadLine();
PrintCourseDetails(courseName, courseCredits, coursesRequired, degreeName);
}
static void PrintCourseDetails(string courseName, int courseCredits, int coursesRequired, string degreeName)
{
Console.WriteLine("\r\nCourse name: {0}\r\nCredits for course: {1}\r\nDegree Association: {2}\r\n", courseName, courseCredits, coursesRequired, degreeName);
}
static void GetProgramInformation()
{
// Capture program information input from application user.
Console.WriteLine("--- Program Information ---");
Console.WriteLine("Enter the program name: ");
string programName = Console.ReadLine();
Console.WriteLine("Enter the associated degree name: ");
string degreeName = Console.ReadLine();
Console.WriteLine("Enter the associated field of study name: ");
string fieldName = Console.ReadLine();
PrintProgramDetails(programName, degreeName, fieldName);
}
static void PrintProgramDetails(string programName, string degreeName, string fieldName)
{
Console.WriteLine("\r\nProgram name: {0}\r\nDegree Association: {1}\r\nField of study: {2}\r\n", programName, degreeName, fieldName);
}
static void GetDegreeInformation()
{
// Capture degree information input from application user.
Console.WriteLine("--- Degree Information ---");
Console.WriteLine("Enter the degree name: ");
string degreeName = Console.ReadLine();
Console.WriteLine("Enter the number of credits required to complete degree: ");
int creditsRequired = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter the associated school name (College of...): ");
string collegeName = Console.ReadLine();
PrintDegreeDetails(degreeName, creditsRequired, collegeName);
}
static void PrintDegreeDetails(string degreeName, int creditsRequired, string collegeName)
{
Console.WriteLine("\r\nDegree name: {0}\r\nNumber of credits for completion: {1}\r\nCollege of {2}.\r\n", degreeName, creditsRequired, collegeName);
}
static DateTime validateLegalAge(string date)
{
try
{
DateTime legalAge = DateTime.Parse(date);
DateTime curDate = DateTime.Today;
//Validate student birth date input
if (legalAge > curDate)
{
Console.WriteLine("Invalid birth date provided");
}
if (legalAge.AddYears(18).CompareTo(DateTime.Today) > 0)
{
int age = curDate.Year - legalAge.Year;
if (legalAge.Month > curDate.Month)
{
age--;
}
Console.WriteLine("The student not legal age.\r\nThe student's age is {0}", age);
}
else
{
int age = curDate.Year - legalAge.Year;
if (legalAge.Month > curDate.Month)
{
age--;
}
Console.WriteLine("The student's age is {0}", age);
return legalAge;
}
return legalAge;
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message + "\r\nInvalid birthdate (DD/MM/YYYY) format.");
return DateTime.Today;
}
}
}
}
// Module Three Assignment
//
// In later modules, you will begin to create class files to represent the items we have
// been using so far. You will create class files for Students, Teachers, Courses, etc.
// The class files will consist of attributes and methods. To prepare for this, the
// assignment for this module will ask you to think about some basic methods that you can
// implement in your code.
//
// This assignment requires you to create some methods for getting data for your variables
// and sending that data to the console window.
//
// In the assignment, you are to practice getting values from a user and assigning the to
// local variables. As a result, move the variables into the appropriate methods. For
// example, you could create a method called GetStudentInformation() and in that method,
// you could prompt the user for each piece of student information and then assign it to
// the variables you created.
//
// Next, create methods such as PrintStudentDetails(string first, string last, string
// birthday) that accepts the proper variables, and use an appropriate message to print
// the content to the console window.
//
// The first example is a guide for you, the rest you will need to create on your own.
//
// Create a method that prompts a user of your console application to input the
// information for a student:
//
// static void GetStudentInformation()
// {
// 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, a uprogram, 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. From within Main(), call each of the methods to prompt for input from a user of your
// application.
// 5. Just enter enough information to show you understand how to use methods. (At least
// three attributes each).
// 6. Assign the values that are input, to the proper variables.
// 7. Output the values of each object using the "print" methods that you created.
//
// 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 (This challenge is for your own study and does not need to be submitted for
// peer review)
//
// 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)
// Hint: Look at methods for DateTime to determine ways to check if a valid date is
// entered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment