From 72e00bd1961b110903624948f3996f2cd5d41c9c Mon Sep 17 00:00:00 2001 From: Yuhuan Huang Date: Fri, 2 Dec 2022 17:14:04 -0800 Subject: [PATCH 1/2] finishing the project --- src/com/generation/Main.java | 8 ++++++++ src/com/generation/model/Student.java | 15 +++++++++++++++ src/com/generation/service/StudentService.java | 18 ++++++++++++++++++ src/com/generation/utils/PrinterHelper.java | 11 ++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/com/generation/Main.java b/src/com/generation/Main.java index 9e24556..ae55651 100644 --- a/src/com/generation/Main.java +++ b/src/com/generation/Main.java @@ -7,6 +7,7 @@ import com.generation.utils.PrinterHelper; import java.text.ParseException; +import java.util.Date; import java.util.Scanner; public class Main @@ -17,6 +18,12 @@ public static void main( String[] args ) { StudentService studentService = new StudentService(); CourseService courseService = new CourseService(); + Student student1 = new Student("1", "Yuhuan", "yuhuan@gmail.com", new Date("02/01/1995")); + studentService.subscribeStudent(student1); + courseService.enrollStudent("INTRO-CS-1", student1); + courseService.enrollStudent("INTRO-CS-2", student1); + studentService.enrollToCourse(student1.getId(),courseService.getCourse("INTRO-CS-1")); + studentService.enrollToCourse(student1.getId(),courseService.getCourse("INTRO-CS-2")); Scanner scanner = new Scanner( System.in ); int option = 0; do @@ -104,4 +111,5 @@ private static void registerStudent( StudentService studentService, Scanner scan Student student = PrinterHelper.createStudentMenu( scanner ); studentService.subscribeStudent( student ); } + } diff --git a/src/com/generation/model/Student.java b/src/com/generation/model/Student.java index 0897ebe..178cd9c 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; @@ -24,6 +25,13 @@ public Student( String id, String name, String email, Date birthDate ) public void enrollToCourse( Course course ) { //TODO implement this method + if(courses.indexOf(course) == -1) { + courses.add(course); + System.out.println(courses); + } + else { + System.out.println("Course is already enrolled!"); + } } public void registerApprovedCourse( Course course ) @@ -35,9 +43,16 @@ public void registerApprovedCourse( Course course ) public boolean isAttendingCourse( String courseCode ) { //TODO implement this method + if(courses.indexOf(courseCode) != -1){ + return true; + } return false; } + public List getCourses() { + return courses; + } + @Override public double getAverage() { diff --git a/src/com/generation/service/StudentService.java b/src/com/generation/service/StudentService.java index f980e40..9e1ac91 100644 --- a/src/com/generation/service/StudentService.java +++ b/src/com/generation/service/StudentService.java @@ -12,6 +12,7 @@ public class StudentService public void subscribeStudent( Student student ) { + students.put( student.getId(), student ); } @@ -27,6 +28,23 @@ public Student findStudent( String studentId ) public void showSummary() { //TODO implement + System.out.println( "Students:" ); + for ( String key : students.keySet() ) + { + Student student = students.get( key ); + String studentStr = student.toString(); + String newStr = new String(); + String courseStr = student.getCourses().toString(); + for(int i = 0; i < studentStr.length(); i++) { + newStr += studentStr.charAt(i); + if(i == studentStr.length() - 2) { + newStr += ", " + courseStr; + } + + } + System.out.println(newStr); + } + } 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..01906c4 100644 --- a/src/com/generation/utils/PrinterHelper.java +++ b/src/com/generation/utils/PrinterHelper.java @@ -40,7 +40,16 @@ public static Student createStudentMenu( Scanner scanner ) 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()); + String date = scanner.next(); + String[] dateArr = date.split(""); + String str = "/"; + while(!dateArr[2].equals(str) | !dateArr[5].equals(str) | dateArr.length > 10) { + System.out.println("Invalid Date"); + System.out.println( "| Enter student birth date(mm/dd/yyyy)|" ); + date = scanner.next(); + dateArr = date.split(""); + } + Date birthDate = formatter.parse(date); System.out.println( "|-------------------------------------|" ); Student student = new Student( id, name, email, birthDate ); System.out.println( "Student Successfully Registered! " ); From 6c8f8947f8417894acb0db854dea1b7580e2a4f8 Mon Sep 17 00:00:00 2001 From: Yuhuan Huang Date: Fri, 2 Dec 2022 17:29:16 -0800 Subject: [PATCH 2/2] add comments --- src/com/generation/model/Student.java | 5 ++++- src/com/generation/service/StudentService.java | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/com/generation/model/Student.java b/src/com/generation/model/Student.java index 178cd9c..d2851dd 100644 --- a/src/com/generation/model/Student.java +++ b/src/com/generation/model/Student.java @@ -25,9 +25,12 @@ public Student( String id, String name, String email, Date birthDate ) public void enrollToCourse( Course course ) { //TODO implement this method + /* + To check if is already enroll to this course by getting the index of the course from the arrayList + will return -1 if not found. If not found then add the course to the list + */ if(courses.indexOf(course) == -1) { courses.add(course); - System.out.println(courses); } else { System.out.println("Course is already enrolled!"); diff --git a/src/com/generation/service/StudentService.java b/src/com/generation/service/StudentService.java index 9e1ac91..175eb3a 100644 --- a/src/com/generation/service/StudentService.java +++ b/src/com/generation/service/StudentService.java @@ -32,9 +32,14 @@ public void showSummary() for ( String key : students.keySet() ) { Student student = students.get( key ); - String studentStr = student.toString(); + String studentStr = student.toString(); //convert the student into a string String newStr = new String(); - String courseStr = student.getCourses().toString(); + String courseStr = student.getCourses().toString(); //convert the course list into a string + /* + loop through the student string and add each character to the new string, once it reach the second last index, + add a comma and followed by course string to the new string and end the string with the last index of the + student string (}). Lastly, print out the new string + */ for(int i = 0; i < studentStr.length(); i++) { newStr += studentStr.charAt(i); if(i == studentStr.length() - 2) {