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
8 changes: 8 additions & 0 deletions src/com/generation/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.generation.utils.PrinterHelper;

import java.text.ParseException;
import java.util.Date;
import java.util.Scanner;

public class Main
Expand All @@ -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
Expand Down Expand Up @@ -104,4 +111,5 @@ private static void registerStudent( StudentService studentService, Scanner scan
Student student = PrinterHelper.createStudentMenu( scanner );
studentService.subscribeStudent( student );
}

}
18 changes: 18 additions & 0 deletions src/com/generation/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.generation.model;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -24,6 +25,16 @@ 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);
}
else {
System.out.println("Course is already enrolled!");
}
}

public void registerApprovedCourse( Course course )
Expand All @@ -35,9 +46,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<Course> getCourses() {
return courses;
}

@Override
public double getAverage()
{
Expand Down
23 changes: 23 additions & 0 deletions src/com/generation/service/StudentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class StudentService

public void subscribeStudent( Student student )
{

students.put( student.getId(), student );
}

Expand All @@ -27,6 +28,28 @@ 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(); //convert the student into a string
String newStr = new String();
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) {
newStr += ", " + courseStr;
}

}
System.out.println(newStr);
}

}

public void enrollToCourse( String studentId, Course course )
Expand Down
11 changes: 10 additions & 1 deletion src/com/generation/utils/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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! " );
Expand Down