From 79d17ba559a8325b53fa7b1d341e1fa7758e1b5e Mon Sep 17 00:00:00 2001 From: Tiffany Ngo Date: Wed, 30 Nov 2022 10:32:18 -0800 Subject: [PATCH] completed README tasks. --- src/com/generation/model/Student.java | 10 +++++++++- src/com/generation/service/CourseService.java | 1 + src/com/generation/service/StudentService.java | 8 ++++++++ src/com/generation/utils/PrinterHelper.java | 18 +++++++++++++++--- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/com/generation/model/Student.java b/src/com/generation/model/Student.java index 0897ebe..852b778 100644 --- a/src/com/generation/model/Student.java +++ b/src/com/generation/model/Student.java @@ -24,6 +24,8 @@ public Student( String id, String name, String email, Date birthDate ) public void enrollToCourse( Course course ) { //TODO implement this method + // add course to courses list + courses.add(course); } public void registerApprovedCourse( Course course ) @@ -35,6 +37,12 @@ public void registerApprovedCourse( Course course ) public boolean isAttendingCourse( String courseCode ) { //TODO implement this method + // check to see if the course code corresponds to any entry in the courses list + for(int i = 0; i < courses.size(); i++) { + if(courseCode.equals(courses.get(i))) { + return true; + } + } return false; } @@ -47,6 +55,6 @@ public double getAverage() @Override public String toString() { - return "Student {" + super.toString() + "}"; + return "Student {" + super.toString() + " " + courses.toString() + "}"; } } diff --git a/src/com/generation/service/CourseService.java b/src/com/generation/service/CourseService.java index 24df98b..a31c147 100644 --- a/src/com/generation/service/CourseService.java +++ b/src/com/generation/service/CourseService.java @@ -15,6 +15,7 @@ public class CourseService private final Map> enrolledStudents = new HashMap<>(); + public CourseService() { Module module = new Module( "INTRO-CS", "Introduction to Computer Science", diff --git a/src/com/generation/service/StudentService.java b/src/com/generation/service/StudentService.java index f980e40..956e9f9 100644 --- a/src/com/generation/service/StudentService.java +++ b/src/com/generation/service/StudentService.java @@ -4,6 +4,7 @@ import com.generation.model.Student; import java.util.HashMap; +import java.util.List; import java.util.Map; public class StudentService @@ -27,6 +28,13 @@ public Student findStudent( String studentId ) public void showSummary() { //TODO implement + // implement showSummary by using showSummary method in CourseService class as a basis to what the output should look like + + System.out.println("Registered Students:"); + for (Student student : students.values() ) { + System.out.println(student.toString()); + } + } public void enrollToCourse( String studentId, Course course ) diff --git a/src/com/generation/utils/PrinterHelper.java b/src/com/generation/utils/PrinterHelper.java index 6f1ca9b..7568ab3 100644 --- a/src/com/generation/utils/PrinterHelper.java +++ b/src/com/generation/utils/PrinterHelper.java @@ -37,10 +37,22 @@ public static Student createStudentMenu( Scanner scanner ) String id = scanner.next(); System.out.println( "| Enter student email: |" ); String email = scanner.next(); - System.out.println( "| Enter student birth date(mm/dd/yyyy)|" ); - DateFormat formatter = new SimpleDateFormat( "mm/dd/yyyy"); + DateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy"); //TODO validate date format and catch exception to avoid crash - Date birthDate = formatter.parse( scanner.next()); + boolean validFormat = false; + Date birthDate = null; + while(!validFormat) { + System.out.println( "| Enter student birth date(mm/dd/yyyy)|" ); + try { + birthDate = formatter.parse(scanner.next()); + validFormat = true; + + } catch (ParseException e) { + System.out.println("Please input a valid date."); + } + + } + System.out.println( "|-------------------------------------|" ); Student student = new Student( id, name, email, birthDate ); System.out.println( "Student Successfully Registered! " );