Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions JavaFinalProject.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
1 change: 0 additions & 1 deletion src/com/generation/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private static void enrollStudentToCourse( StudentService studentService, Course
courseService.enrollStudent( courseId, student );
studentService.enrollToCourse( studentId, course );
System.out.println( "Student with ID: " + studentId + " enrolled successfully to " + courseId );

}

private static void showCoursesSummary( CourseService courseService, Scanner scanner )
Expand Down
22 changes: 20 additions & 2 deletions src/com/generation/model/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ public Student( String id, String name, String email, Date birthDate )
super( id, name, email, birthDate );
}

public void enrollToCourse( Course course )

public void enrollToCourse(Course course )
{
//TODO implement this method
if(!(isAttendingCourse(course.getCode()))) {
//Initially I did correctly. I overthought add all registered courses to approvedCourses.
//Modified back again
courses.add(course);

//Used for isAttendingCourse
registerApprovedCourse(course);
}
else {
System.out.println("The student has been enrolled");
}
}

public void registerApprovedCourse( Course course )
Expand All @@ -35,7 +47,13 @@ public void registerApprovedCourse( Course course )
public boolean isAttendingCourse( String courseCode )
{
//TODO implement this method
return false;
if(approvedCourses.containsKey(courseCode)) {
return true;
}
else {
return false;
}

}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/com/generation/service/CourseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ public void showSummary()
}
}
}

}
53 changes: 52 additions & 1 deletion src/com/generation/service/StudentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
import com.generation.model.Course;
import com.generation.model.Student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentService
{
private final Map<String, Student> students = new HashMap<>();

//Keep track of the courses that each student registered.
private final Map<String, ArrayList<Course>> enrolledStudents = new HashMap<>();

public void subscribeStudent( Student student )
{
students.put( student.getId(), student );
}

public Student findStudent( String studentId )
{
if ( students.containsKey( studentId ) )
if ( students.containsKey( studentId ))
{
return students.get( studentId );
}
Expand All @@ -27,13 +32,59 @@ public Student findStudent( String studentId )
public void showSummary()
{
//TODO implement
System.out.println( "List of Students:" );

//Enhanced for loop for the students
for ( String key : students.keySet()) {
Student student = students.get(key);
List<Course> courseList = enrolledStudents.get(student.getId());

//Print out the student information
System.out.print(student);
if (courseList != null) {
System.out.println(courseList);
} else {
System.out.println("[{}]");
}
System.out.println();
}
}

//Keeping track of the course for the student
public void addEnrollStudents(String studentId, Course course) {


if ( (enrolledStudents.containsKey( studentId ) ))
{
ArrayList<Course> courseList = enrolledStudents.get(studentId);
System.out.println("CourseList" + courseList);

//For avoiding ConcurrentModificationException,used for loop
boolean flag = false;
for(int i = 0; i < courseList.size(); i++) {
if (courseList.get(i).getCode().equals(course.getCode())) {
flag = true;
}
}
if(!(flag)) {
enrolledStudents.get(studentId).add( course );
}
}
else {
enrolledStudents.put( studentId, new ArrayList<>() );
enrolledStudents.get(studentId).add( course );
}

}

public void enrollToCourse( String studentId, Course course )
{
if ( students.containsKey( studentId ) )
{
students.get( studentId ).enrollToCourse( course );

//Keeping the information of enrolled course for the student
addEnrollStudents(studentId, course);
}
}

Expand Down
49 changes: 29 additions & 20 deletions src/com/generation/utils/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,35 @@ public static void showMainMenu(){
}

public static Student createStudentMenu( Scanner scanner )
throws ParseException
{
System.out.println( "|-------------------------------------|" );
System.out.println( "| . 1 Register Student |" );
System.out.println( "|-------------------------------------|" );
System.out.println( "| Enter student name: |" );
String name = scanner.next();
System.out.println( "| Enter student ID: |" );
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());
System.out.println( "|-------------------------------------|" );
Student student = new Student( id, name, email, birthDate );
System.out.println( "Student Successfully Registered! " );
System.out.println(student);
return student;
}
System.out.println("|-------------------------------------|");
System.out.println("| . 1 Register Student |");
System.out.println("|-------------------------------------|");
System.out.println("| Enter student name: |");
String name = scanner.next();
System.out.println("| Enter student ID: |");
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
//Initialize the birthday
Date birthDate = null;

//While the invalid format is added, birthDate is null
while(birthDate == null) {
try {
birthDate = formatter.parse(scanner.next());
} catch (ParseException e) {
System.err.println("ParseException: " + e.getMessage());
System.out.println("| Enter student birth date(mm/dd/yyyy)|");
}
}
Student student = new Student(id, name, email, birthDate);
System.out.println("|-------------------------------------|");
System.out.println("Student Successfully Registered! ");
System.out.println(student);
return student;
}
}
24 changes: 24 additions & 0 deletions test/com/generation/service/CourseServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.generation.service;

import com.generation.model.Course;
import com.generation.model.Module;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CourseServiceTest {

//Declare courseService variable for the test
CourseService courseService = new CourseService();

////Only method's type that is not void can be tested. So, I created two for getting course information below
@Test
void getCourse() {
assertEquals(courseService.getCourse("INTRO-CS-1").getCredits(), 9);
}

@Test
void getCourse2() {
assertEquals(courseService.getCourse("INTRO-CS-4").getName(), "Algorithm Design and Problem Solving - Advanced");
}
}
38 changes: 38 additions & 0 deletions test/com/generation/service/StudentServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.generation.service;

import com.generation.model.Student;
import org.junit.jupiter.api.Test;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.*;


class StudentServiceTest {

//Declare instances for tests
Date date = new Date(12/12/1212);
Date date2 = new Date(11/11/1111);
Student student = new Student("1", "Makiko", "makiko@gmail.com", date);
Student student2 = new Student("2", "Randy", "randyo@gmail.com", date2);
StudentService studentService = new StudentService();

//Only method's type that is not void can be tested. So, I created two for find students below
@Test
void findStudent() {

//Add the student in the students(Map)
studentService.subscribeStudent(student);

//Test the method
assertEquals(studentService.findStudent(student.getId()).getId(), "1");
}

@Test
void findStudent2() {
//Add the student in the students(Map)
studentService.subscribeStudent(student2);

//Test the method
assertEquals(studentService.findStudent(student2.getId()).getName(), "Randy");
}

}