From a84c80d97f90062449b8044f4b746760e2534f91 Mon Sep 17 00:00:00 2001 From: Kimberly MB Date: Sun, 11 Dec 2022 23:13:14 -0500 Subject: [PATCH] complete assessments with comments --- .idea/misc.xml | 2 +- src/com/generation/Main.java | 3 +- src/com/generation/model/Student.java | 32 ++++++++++++++++--- .../generation/service/StudentService.java | 25 ++++++++++++++- src/com/generation/utils/PrinterHelper.java | 22 +++++++++---- 5 files changed, 71 insertions(+), 13 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 4e2f650..1ce27b8 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/src/com/generation/Main.java b/src/com/generation/Main.java index 9e24556..08df07d 100644 --- a/src/com/generation/Main.java +++ b/src/com/generation/Main.java @@ -79,7 +79,8 @@ private static void showCoursesSummary( CourseService courseService, Scanner sca private static void showStudentsSummary( StudentService studentService, Scanner scanner ) { - studentService.showSummary(); + String key = null; + studentService.showSummary(key); } private static void findStudent( StudentService studentService, Scanner scanner ) diff --git a/src/com/generation/model/Student.java b/src/com/generation/model/Student.java index 0897ebe..ba89504 100644 --- a/src/com/generation/model/Student.java +++ b/src/com/generation/model/Student.java @@ -1,5 +1,6 @@ package com.generation.model; +import java.sql.SQLOutput; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -13,17 +14,33 @@ public class Student private double average; private final List courses = new ArrayList<>(); - + //Student class instance field courses assigned to a new array list private final Map approvedCourses = new HashMap<>(); + private Course course; public Student( String id, String name, String email, Date birthDate ) { super( id, name, email, birthDate ); } - public void enrollToCourse( Course course ) + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + public void enrollToCourse(Course course ) { - //TODO implement this method + if (!isAttendingCourse(course.getCode())) { + courses.add(course); + } + else { + System.out.println("Student previously enrolled in course, enter different course."); + } + //Method that will add course objects to the instance field courses from above array list. + // Implementing conditional statement to prevent double enrollment with else sout message } public void registerApprovedCourse( Course course ) @@ -34,8 +51,15 @@ public void registerApprovedCourse( Course course ) public boolean isAttendingCourse( String courseCode ) { - //TODO implement this method + for (Course c : courses) { + if (c.getCode().equals(courseCode)) { + return true; + } + } return false; + //Boolean method that will return true if parameter courseCode is true for student with the argument that will be passed into it + //Create enhanced for loop/for each loop that will loop over entry of courses from courses instance field array list above + //Getting code of Course c to check if it equals to the argument that was passed to courseCode parameter } @Override diff --git a/src/com/generation/service/StudentService.java b/src/com/generation/service/StudentService.java index f980e40..af2bdd3 100644 --- a/src/com/generation/service/StudentService.java +++ b/src/com/generation/service/StudentService.java @@ -24,8 +24,13 @@ public Student findStudent( String studentId ) return null; } - public void showSummary() + public void showSummary(String key) { + System.out.println("Students: "); + for (Student s: students.values()) { + Student stud = students.get(key); + System.out.println(s.toString()); + } //TODO implement } @@ -39,3 +44,21 @@ public void enrollToCourse( String studentId, Course course ) } + + + +// System.out.println( "Available Courses:" ); +// for ( String key : courses.keySet() ) +// { +// Course course = courses.get( key ); +// System.out.println( course ); +// } +// System.out.println( "Enrolled Students" ); +// for ( String key : enrolledStudents.keySet() ) +// { +// List students = enrolledStudents.get( key ); +// System.out.println( "Students on Course " + key + ": " ); +// for ( Student student : students ) +// { +// System.out.println( student ); +// } diff --git a/src/com/generation/utils/PrinterHelper.java b/src/com/generation/utils/PrinterHelper.java index 6f1ca9b..a709416 100644 --- a/src/com/generation/utils/PrinterHelper.java +++ b/src/com/generation/utils/PrinterHelper.java @@ -11,6 +11,8 @@ public class PrinterHelper { + private static Date birthDate; + public static void showMainMenu(){ System.out.println( "|-------------------------------|" ); System.out.println( "| Welcome to StudentGen |" ); @@ -37,15 +39,23 @@ 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"); - //TODO validate date format and catch exception to avoid crash - Date birthDate = formatter.parse( scanner.next()); + DateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy"); + boolean validDate = false; + Date birthdate = null; + while (!validDate) { + System.out.println("| Enter birthdate in following format (mm/dd/yy) |"); + try { + birthdate = formatter.parse(scanner.next()); + validDate= true; + } catch (ParseException error) { + System.out.println("Invalid birthdate!"); + } + } ; + // While loop with try and catch statement to catch wrong birthdate input System.out.println( "|-------------------------------------|" ); - Student student = new Student( id, name, email, birthDate ); + Student student = new Student( id, name, email, birthdate ); System.out.println( "Student Successfully Registered! " ); System.out.println(student); return student; } - }