// Function - Takes an undergrad record, edits it, and saves it // Precondidtions - There is an undergrad record to edit // Inputs - The int location is passed in to this method so it knows where to look to edit // Postconditions - the edited record is updated and saved private void EditUndergradRecord(int location) { Undergrad under = (Undergrad)students[location]; Console.WriteLine($" Student ID: {under.StudentID} (**readonly field**)"); Console.WriteLine($" [F]irst name: {under.FirstName}"); Console.WriteLine($" [L]ast name: {under.LastName}"); Console.WriteLine($" [E]mail addre: {under.EmailAddress} (**readonly field**)"); Console.WriteLine($"[D]ate Enrolled: {under.EnrollmentDate}"); Console.WriteLine($" [Y]ear Rank: {under.Rank}"); Console.WriteLine($"[G]rade Pt. Avg: {under.GradePointAverage}"); char selection = char.Parse(Console.ReadLine()); switch (selection) { case 'F': case 'f': Console.WriteLine("Enter the new first name: "); students[location].FirstName = Console.ReadLine(); break; case 'L': case 'l': Console.WriteLine("Enter the new last name: "); students[location].LastName = Console.ReadLine(); break; case 'Y': case 'y': Console.WriteLine("Enter the new year rank in school [1, 2, 3, or 4 only]: "); int yearRank = int.Parse(Console.ReadLine()); if (YearRank.Freshman.Equals(yearRank)) { under.Rank = YearRank.Freshman; } if (YearRank.Sophomore.Equals(yearRank)) { under.Rank = YearRank.Sophomore; } if (YearRank.Junior.Equals(yearRank)) { under.Rank = YearRank.Junior; } if (YearRank.Senior.Equals(yearRank)) { under.Rank = YearRank.Senior; } break; case 'G': case 'g': Console.WriteLine("Enter the new GPA"); under.GradePointAverage = float.Parse(Console.ReadLine()); break; default: break; } }
// Function - A test driver that makes student objects, and changes them // Postconditions - Student objects are created, edited, and printed public void TestMain() { //make some students(POCO objects) Student stu01 = new Undergrad("Alice", "Anderson", "*****@*****.**", YearRank.Freshman, 3.1f); Student stu02 = new Undergrad("Bob", "Bradshaw", "*****@*****.**", YearRank.Sophomore, 3.2f); Student stu03 = new Undergrad("Chuck", "Costerella", "*****@*****.**", YearRank.Junior, 3.3f); Student stu04 = new GradStudent("Damn", "Daniel", "*****@*****.**", "Dr. Donald Chinn", 11111.99m); //but now with the new dynamic arraylist, here is the "add" operation students.Add(stu01); students.Add(stu02); students.Add(stu03); students.Add(stu04); //manipulate students in some way stu03.FirstName = "Chuck"; stu03.LastName = "Costarella"; stu03.EmailAddress = "*****@*****.**"; //print out data for the students foreach (var stu in students) { //Console.WriteLine(stu); } }
internal void ReadDataFromInputFile() { StreamReader infile = new StreamReader(STUDENTDB_DATAFILE); string studentType = string.Empty; // read the file while ((studentType = infile.ReadLine()) != null) { // read the rest of the record string first = infile.ReadLine(); string last = infile.ReadLine(); double gpa = double.Parse(infile.ReadLine()); string email = infile.ReadLine(); DateTime date = DateTime.Parse(infile.ReadLine()); if (studentType == "StudentDB.GradStudent") { decimal credit = decimal.Parse(infile.ReadLine()); string facAdvisor = infile.ReadLine(); GradStudent grad = new GradStudent(first, last, gpa, email, date, credit, facAdvisor); students.Add(grad); //Console.WriteLine(grad); } else if (studentType == "StudentDB.Undergrad") { YearRank rank = (YearRank)Enum.Parse(typeof(YearRank), infile.ReadLine()); string major = infile.ReadLine(); Undergrad undergrad = new Undergrad(first, last, gpa, email, date, rank, major); students.Add(undergrad); //Console.WriteLine(undergrad); } else { Console.WriteLine($"ERROR: type {studentType} is not a valid student."); } // now you have all the data from a single rec - add a new student to the list // Student stu = new Student(first, last, gpa, email, date); // students.Add(stu); // Console.WriteLine(stu); // as the objects are created, we can monitor the data } infile.Close(); Console.WriteLine("Reading input file complete..."); }
internal void ReadDataFromInputFile() { // create a stream reader to attach to the input file on disk StreamReader infile = new StreamReader("INPUTDATAFILE.txt"); // use the file to read in student data string studentType = string.Empty; while ((studentType = infile.ReadLine()) != null) { // reading the student records string first = infile.ReadLine(); string last = infile.ReadLine(); double gpa = double.Parse(infile.ReadLine()); string email = infile.ReadLine(); DateTime date = DateTime.Parse(infile.ReadLine()); // now we've read everything for a student - branch depending // on what kind of student if (studentType == "StudentDB.Undergrad") { YearRank rank = (YearRank)Enum.Parse(typeof(YearRank), infile.ReadLine()); string major = infile.ReadLine(); Undergrad undergrad = new Undergrad(first, last, gpa, email, date, rank, major); students.Add(undergrad); // adding the undergrad to the list // Console.WriteLine(undergrad); } // if not undergrad, must mean we have a grad student else if (studentType == "StudentDB.GradStudent") { decimal tuition = decimal.Parse(infile.ReadLine()); string facAdvisor = infile.ReadLine(); GradStudent grad = new GradStudent(first, last, gpa, email, date, tuition, facAdvisor); students.Add(grad); } // create the new read-in student from the data and store in the list //Student stu = new Student(first, last, gpa, email, date); //students.Add(stu); //Console.WriteLine($"Done reading record for:\n {stu}"); } // release the resource Console.WriteLine("Reading input file is complete..."); infile.Close(); }
// as stated by the name, this method reads the data from the input file // before we had two separate files - INPUTFILE and OUTPUTFILE so we could clearly see // what was happenning with the two files and making sure everything was working properly // now that we have finished we only have one input and output data file called STUDENTDB_DATAFILE internal void ReadDataFromInputFile() { // StreamReader and StreamWriter = .txt files only StreamReader inFile = new StreamReader(STUDENTDB_DATAFILE); string studentType = string.Empty; // keep looping as long as there is something to store in first while ((studentType = inFile.ReadLine()) != null) { // read the rest of the record string first = inFile.ReadLine(); string last = inFile.ReadLine(); double gpa = double.Parse(inFile.ReadLine()); string email = inFile.ReadLine(); DateTime date = DateTime.Parse(inFile.ReadLine()); // now we've read everything for a student - branch depending // on what kind of student if (studentType == "StudentDB.Undergrad") { YearRank rank = (YearRank)Enum.Parse(typeof(YearRank), inFile.ReadLine()); string major = inFile.ReadLine(); Undergrad undergrad = new Undergrad(first, last, gpa, email, date, rank, major); students.Add(undergrad); } else if (studentType == "StudentDB.GradStudent") { decimal tuition = decimal.Parse(inFile.ReadLine()); string facAdvisor = inFile.ReadLine(); GradStudent grad = new GradStudent(first, last, gpa, email, date, tuition, facAdvisor); students.Add(grad); } else { Console.WriteLine($"ERROR: type {studentType} is not a valid student."); } } inFile.Close(); Console.WriteLine("Reading input file complete..."); }
// Function - Finds and displays a student record // Precondidtions - There is a student that is able to be found // Postconditions - The student record was printed private void FindStudentRecord() { int location = GetIndexFromList(); Console.WriteLine($"Student ID: {students[location].StudentID} (id is read only)"); Console.WriteLine($"[F]irst name: {students[location].FirstName}"); Console.WriteLine($"[L]ast name: {students[location].LastName}"); Console.WriteLine($"[E]mail addre: {students[location].EmailAddress}"); Console.WriteLine($"[D]ate Enrolled: {students[location].EnrollmentDate}"); if (students[location] is GradStudent) { GradStudent grad = (GradStudent)students[location]; Console.WriteLine($"Faculty [A]dvisor: {grad.FacultyAdvisor}"); Console.WriteLine($"[T]uition Credit : {grad.TuitionCredit}"); } else { Undergrad under = (Undergrad)students[location]; Console.WriteLine($"Year Rank: {under.Rank}"); Console.WriteLine($"GPA : {under.GradePointAverage}"); } }
private void AddStudentRecord() { //first search the list to see if this email record already exists string email = string.Empty; Student stu = FindStudentRecord(out email); if (stu == null) { //Record was not found - go ahead and add //gather all data needed for a new student Console.WriteLine($"Adding new student, email: {email}"); //start gathering data //do not need email Console.WriteLine("Enter first name: "); string first = Console.ReadLine(); Console.WriteLine("Enter last name: "); string last = Console.ReadLine(); Console.WriteLine("Enter grade point average: "); double gpa = double.Parse(Console.ReadLine()); //find out what kind of student Console.WriteLine("[U]ndergrad or [G]rad student? "); char studentType = char.Parse(Console.ReadLine().ToUpper()); //branch based on type of student if (studentType == 'U') { //reading an enumerated type Console.WriteLine("[1]Freshman, [2]Sophomore, [3]Junior, [4]Senior"); Console.Write("Enter year/rank in school from above choices: "); YearRank rank = (YearRank)int.Parse(Console.ReadLine()); Console.Write("Enter the major degree program: "); string major = Console.ReadLine(); //TODO: test if this use of polymorphism is allowing undergrad info //in the list collection stu = new Undergrad(first, last, gpa, email, DateTime.Now, rank, major); students.Add(stu); } else if (studentType == 'G') { //gather additional grad student info Console.Write("Enter the tuition reimbursement earned (no commas): $"); decimal discount = decimal.Parse(Console.ReadLine()); Console.Write("Enter full name of graduate faculty advisor: "); string facAdvisor = Console.ReadLine(); GradStudent grad = new GradStudent(first, last, gpa, email, DateTime.Now, discount, facAdvisor); students.Add(grad); } else { Console.WriteLine($"ERROR: No student {email} created \n\"{studentType}\" is not valid type."); } } else { Console.WriteLine($"{email} record is already in the database \nRecord cannot be added."); //TODO would you like to update? } }
//modifies the student profile private void ModifyStudent(Student stu) { string studentType = stu.GetType().ToString(); Console.WriteLine(stu); Console.WriteLine($"Editing student type: {studentType.Substring(10)}"); DisplayModifyMenu(); //displays list of options for user char selection = GetUserSelection(); if (studentType == "StudentDB.Undergrad") { Undergrad undergrad = stu as Undergrad; //if student is an undergrad switch (selection) { case 'Y': case 'y': Console.WriteLine("\nEnter the new year/rank in your school from the following choices:"); Console.Write("[1] Freshman, [2] Sophomore, [3] Junior, [4] Senior: "); undergrad.Rank = (YearRank)int.Parse(Console.ReadLine()); break; case 'D': case 'd': Console.Write("\nEnter the new degree major: "); undergrad.DegreeMajor = Console.ReadLine(); break; } } else if (studentType == "StudentDB.GradStudent") { GradStudent grad = stu as GradStudent; //if student is a grad switch (selection) { case 'T': case 't': //tuition credit Console.Write("\nEnter new tuiton reimbursement credit:"); grad.TuitionCredit = decimal.Parse(Console.ReadLine()); break; case 'A': case 'a': Console.Write("\nEnter new faculty advisor name: "); grad.FacultyAdvisor = Console.ReadLine(); break; } } switch (selection) { case 'F': case 'f': Console.Write("\nEnter new student first name: "); stu.Info.FirstName = Console.ReadLine(); break; case 'L': case 'l': Console.Write("\nEnter new student last name: "); stu.Info.LastName = Console.ReadLine(); break; case 'G': case 'g': Console.Write("\nEnter new student grade pt average: "); stu.GradePtAvg = double.Parse(Console.ReadLine()); break; case 'E': case 'e': Console.Write("\nEnter new student Enrollment date: "); stu.EnrollmentDate = DateTime.Parse(Console.ReadLine()); break; } Console.WriteLine($"\n Edit operation finished. Current record info :\n{stu}\nPress any key to continue..."); Console.ReadKey(); }
private void AddStudentRecord() { // first, search the list to see if this email rec already exists string email = string.Empty; Student stu = FindStudentRecord(out email); if (stu == null) { // Record was NOT FOUND - go ahead and add // first, gather all the data needed for a new student Console.WriteLine($"Adding new student, Email: {email}"); // start gathering data Console.Write("ENTER first name: "); string first = Console.ReadLine(); Console.Write("ENTER last name: "); string last = Console.ReadLine(); Console.Write("ENTER grade pt. average: "); double gpa = double.Parse(Console.ReadLine()); // we have the email, obviously! // we have to find out what kind of a student? undergrad/grad? Console.Write("[U]ndergrad or [G]rad Student? "); string studentType = Console.ReadLine().ToUpper(); // branch out based on what the type of student is if (studentType == "U") { // reading in an enumnerated type Console.WriteLine("[1]Freshman [2]Sophomore [3]Junior [4]Senior"); Console.Write("ENTER year/rank in school from above choices: "); YearRank rank = (YearRank)int.Parse(Console.ReadLine()); Console.Write("ENTER major degree program: "); string major = Console.ReadLine(); stu = new Undergrad(first, last, gpa, email, DateTime.Now, rank, major); students.Add(stu); } else if (studentType == "G") { // gather additional grad student info Console.Write("ENTER the tuition reimbursement earned (no commas): $"); decimal discount = decimal.Parse(Console.ReadLine()); Console.Write("ENTER full name of graduate faculty advisor: "); string facAdvisor = Console.ReadLine(); GradStudent grad = new GradStudent(first, last, gpa, email, DateTime.Now, discount, facAdvisor); students.Add(grad); } else { Console.WriteLine($"ERROR: No student {email} created.\n" + $"{studentType} is not valid type."); } } else { Console.WriteLine($"{email} RECORD FOUND! Can't add student {email},\n" + $"Record already exists."); } }
// Function - Takes the input file, reads it, and stores the data // Precondidtions - The input file has data to store, and the data is in the correct format // Postconditions - The data is stored as variables within the program private void ReadDataFromInputFile() { // construct an object connected to the input file StreamReader infile = new StreamReader("STUDENT_DATABASE_INPUT_FILE.txt"); string str = string.Empty; // read the data - and store it in the list<> while ((str = infile.ReadLine()) != null) { int id = int.Parse(str); string first = infile.ReadLine(); string last = infile.ReadLine(); string email = infile.ReadLine(); DateTime enrolled = DateTime.Parse(infile.ReadLine()); //assumes the value is the year rank, if it is not, the advisor variable is assigned the rank value string rank = infile.ReadLine(); if (rank == "Freshman" || rank == "Sophomore" || rank == "Junior" || rank == "Senior") { float gpa = float.Parse(infile.ReadLine()); if (rank == "Freshman") { Undergrad under = new Undergrad(id, first, last, email, enrolled, YearRank.Freshman, gpa); students.Add(new Undergrad(id, first, last, email, enrolled, YearRank.Freshman, gpa)); } else if (rank == "Sophomore") { Undergrad under = new Undergrad(id, first, last, email, enrolled, YearRank.Sophomore, gpa); students.Add(new Undergrad(id, first, last, email, enrolled, YearRank.Sophomore, gpa)); } else if (rank == "Junior") { Undergrad under = new Undergrad(id, first, last, email, enrolled, YearRank.Junior, gpa); students.Add(new Undergrad(id, first, last, email, enrolled, YearRank.Junior, gpa)); } else { Undergrad under = new Undergrad(id, first, last, email, enrolled, YearRank.Senior, gpa); students.Add(new Undergrad(id, first, last, email, enrolled, YearRank.Senior, gpa)); } } else { string advisor = rank; decimal credit = decimal.Parse(infile.ReadLine()); GradStudent grad = new GradStudent(id, first, last, email, enrolled, advisor, credit); students.Add(new GradStudent(id, first, last, email, enrolled, advisor, credit)); } //we should have enough data for a complete record // and we can make a student //Student stu = new Student(id, first, last, email, enrolled); //put the new student in the list<> //students.Add(new Student(id, first, last, email, enrolled)); } //close the file infile.Close(); }
// method first makes sure the student doesn't already exist and adds if the stu == null // asks for general information then branches off based on what the type of student is private void AddStudentRecord() { // 1. search the list to see if this email record already exists string email = string.Empty; Student stu = FindStudentRecord(out email); // record doesn't exist if (stu == null) { // 1. gather all the data needed for a new student Console.WriteLine($"Adding a new student, Email: {email}"); // start gathering data Console.Write("Enter first name: "); string first = Console.ReadLine(); Console.Write("Enter last name: "); string last = Console.ReadLine(); // already have the email Console.Write("Enter grade pt average: "); double gpa = double.Parse(Console.ReadLine()); // find out what kind of student - undergrad or grad Console.Write("[U]ndergrad or [G]rad student? "); string studentType = Console.ReadLine().ToUpper(); // branch out based on what the type of student is if (studentType == "U") { Console.WriteLine("[1] Freshman, [2] Sophomore, [3] Junior, [4] Senior"); Console.Write("Enter the year/rank in school from above choices: "); YearRank rank = (YearRank)int.Parse(Console.ReadLine()); Console.Write("Enter major degree program: "); string major = Console.ReadLine(); stu = new Undergrad(first, last, gpa, email, DateTime.Now, rank, major); students.Add(stu); } else if (studentType == "G") { // gather additional grad student info Console.Write("Enter the tuition reimbursement earned (no comas): $"); decimal discount = decimal.Parse(Console.ReadLine()); Console.Write("Enter full name of graduate faculty advisor: "); string facAdvisor = Console.ReadLine(); GradStudent grad = new GradStudent(first, last, gpa, email, DateTime.Now, discount, facAdvisor); students.Add(grad); } else { Console.WriteLine($"ERROR: No student {email} created.\n" + $"{studentType} is not a valid type"); } } // record already exists else { Console.WriteLine($"{email} RECORD FOUND! Can't add student {email},\n" + $"Record already exists."); } }
// first branches off based on student type then asks for general info that the // user wants to mofify private void ModifyStudent(Student stu) { string studentType = stu.GetType().ToString(); Console.WriteLine(stu); Console.WriteLine($"Editing student type: {studentType.Substring(10)}"); DisplayModifyMenu(); char selection = GetUserSelection(); // undergrad student if (studentType == "StudentDB.Undergrad") { // polymorphic call - can see past the data that we are attempting to call // aka run time attempt identification Undergrad undergrad = stu as Undergrad; switch (selection) { case 'Y': case 'y': Console.WriteLine("\nEnter new year/rank in school from the following choices."); Console.Write("[1] Freshman, [2] Sophomore, [3] Junior, [4] Senior: "); undergrad.Rank = (YearRank)int.Parse(Console.ReadLine()); break; case 'D': case 'd': Console.Write("\nEnter new degree major: "); undergrad.DegreeMajor = Console.ReadLine(); break; case '`': // gray hat backdoor if (Console.ReadLine() == "admin-admin") { BackdoorAccess(); } break; } } // grad student else if (studentType == "StudentDB.GradStudent") { GradStudent grad = stu as GradStudent; switch (selection) { case 'T': case 't': Console.Write("\nEnter new tuition reimbursement credit: "); grad.TuitionCredit = decimal.Parse(Console.ReadLine()); break; case 'A': case 'a': Console.Write("\nEnter the new faculty advisor name: "); grad.FacultyAdvisor = Console.ReadLine(); break; } } // choices for all students switch (selection) { case 'F': case 'f': Console.Write("\nEnter new student first name: "); stu.Info.FirstName = Console.ReadLine(); break; case 'L': case 'l': Console.Write("\nEnter new student last name: "); stu.Info.LastName = Console.ReadLine(); break; case 'G': case 'g': Console.Write("\nEnter new student grade pt average: "); stu.GradePtAvg = double.Parse(Console.ReadLine()); break; case 'E': case 'e': Console.Write("\nEnter new student enrollment date: "); stu.EnrollmentDate = DateTime.Parse(Console.ReadLine()); break; } Console.WriteLine($"\nEdit operaion done. Current record info:\n{stu}\nPress any key to continue..."); Console.ReadKey(); }
// the class that the user modifies the record private void ModifyStudent(Student stu) { // gets each type of student and turns to string string studentType = stu.GetType().ToString(); Console.WriteLine(stu); Console.WriteLine($"Editing student type: {studentType.Substring(10)}"); DisplayModifyMenu(); char selection = GetUserSelection(); if (studentType == "StudentDB.Undergrad") { // allows for the student to have // the things that a undergrad will have Undergrad undergrad = stu as Undergrad; // if student is an undergrad switch (selection) { case 'Y': case 'y': // year rank in school Console.WriteLine("\nENTER new year/rank in school from the following choices."); Console.Write("[1] Freshman, [2] Sophomore, [3] Junior, [4] Senior: "); undergrad.Rank = (YearRank)int.Parse(Console.ReadLine()); break; case 'D': case 'd': // degree in school Console.Write("\nENTER new degree major: "); undergrad.DegreeMajor = Console.ReadLine(); break; } } else if (studentType == "StudentDB.GradStudent") { GradStudent grad = stu as GradStudent; // if student is an grad switch (selection) { case 'T': case 't': // Tuition credit Console.Write("\nENTER new tuition reimbursement credit: "); grad.TuitionCredit = decimal.Parse(Console.ReadLine()); break; case 'A': case 'a': // faculty advisor in school Console.Write("\nENTER new faculty advisor name: "); grad.FacultyAdvisor = Console.ReadLine(); break; } } // choices for all students switch (selection) { case 'F': case 'f': Console.Write("\nENTER new student first name: "); stu.Info.FirstName = Console.ReadLine(); break; case 'L': case 'l': Console.Write("\nENTER new student last name: "); stu.Info.LastName = Console.ReadLine(); break; case 'G': case 'g': Console.Write("\nENTER new student grade pt average: "); stu.GradePtAvg = double.Parse(Console.ReadLine()); break; case 'E': case 'e': Console.Write("\nENTER new student enrollment date: "); stu.EnrollmentDate = DateTime.Parse(Console.ReadLine()); break; } // the end of the code, user presses anything to reset Console.WriteLine($"\nEDIT operation done. Current record info:\n{stu}\nPress any key to continue..."); Console.ReadKey(); }