Skip to content

Commit ba6d428

Browse files
committed
Implement functionalities in Student, StudentService, PrinterHelper, and Main classes.
1 parent a70b9bf commit ba6d428

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/com/generation/model/Instructor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.generation.model;
22

3-
import java.util.ArrayList;
43
import java.util.Date;
4+
import java.util.ArrayList;
55
import java.util.List;
66

77
public class Instructor
@@ -12,7 +12,7 @@ public class Instructor
1212

1313
private final List<Course> teachingCourses = new ArrayList<>();
1414

15-
protected Instructor( String id, String name, String email, Date birthDate )
15+
protected Instructor(String id, String name, String email, Date birthDate )
1616
{
1717
super( id, name, email, birthDate );
1818
}

src/com/generation/model/Student.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.generation.model;
22

3-
import java.util.ArrayList;
43
import java.util.Date;
4+
import java.util.ArrayList;
55
import java.util.HashMap;
66
import java.util.List;
77
import java.util.Map;
@@ -16,14 +16,19 @@ public class Student
1616

1717
private final Map<String, Course> approvedCourses = new HashMap<>();
1818

19-
public Student( String id, String name, String email, Date birthDate )
19+
public Student(String id, String name, String email, Date birthDate )
2020
{
2121
super( id, name, email, birthDate );
2222
}
2323

2424
public void enrollToCourse( Course course )
2525
{
2626
//TODO implement this method
27+
if(!isAttendingCourse(course.getCode())) {
28+
courses.add(course);
29+
} else {
30+
System.out.println("Student already enrolled.");
31+
}
2732
}
2833

2934
public void registerApprovedCourse( Course course )
@@ -35,6 +40,11 @@ public void registerApprovedCourse( Course course )
3540
public boolean isAttendingCourse( String courseCode )
3641
{
3742
//TODO implement this method
43+
for(Course _course : courses) {
44+
if(_course.getCode().equals(courseCode)) {
45+
return true;
46+
}
47+
}
3848
return false;
3949
}
4050

src/com/generation/service/StudentService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public Student findStudent( String studentId )
2727
public void showSummary()
2828
{
2929
//TODO implement
30+
System.out.println("Students: ");
31+
for(Student _student : students.values()){
32+
System.out.println(_student.toString());
33+
}
3034
}
3135

3236
public void enrollToCourse( String studentId, Course course )

src/com/generation/utils/PrinterHelper.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ public static Student createStudentMenu( Scanner scanner )
3737
String id = scanner.next();
3838
System.out.println( "| Enter student email: |" );
3939
String email = scanner.next();
40-
System.out.println( "| Enter student birth date(mm/dd/yyyy)|" );
41-
DateFormat formatter = new SimpleDateFormat( "mm/dd/yyyy");
40+
41+
DateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy");
4242
//TODO validate date format and catch exception to avoid crash
43-
Date birthDate = formatter.parse( scanner.next());
43+
formatter.setLenient(false);
44+
Date birthDate = null;
45+
46+
while(birthDate == null) {
47+
System.out.println( "| Enter student birth date(mm/dd/yyyy)|" );
48+
try {
49+
birthDate = formatter.parse(scanner.next());
50+
} catch (ParseException e) {
51+
System.out.println("Please input valid date");
52+
}
53+
}
4454
System.out.println( "|-------------------------------------|" );
4555
Student student = new Student( id, name, email, birthDate );
4656
System.out.println( "Student Successfully Registered! " );

0 commit comments

Comments
 (0)