From 4582102c90b8d3d72174cfab6433e57ba5a5bcb1 Mon Sep 17 00:00:00 2001 From: mitch Date: Tue, 29 Nov 2022 10:46:28 -0600 Subject: [PATCH 1/3] meets requirements but needs test --- .idea/codeStyles/Project.xml | 7 ++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ JavaFinalProject.iml | 17 ++++ src/com/generation/Main.java | 1 + src/com/generation/model/Instructor.java | 2 +- src/com/generation/model/Person.java | 2 +- src/com/generation/model/Student.java | 48 +++++++---- src/com/generation/service/CourseService.java | 14 ++-- .../generation/service/StudentService.java | 14 +++- src/com/generation/utils/PrinterHelper.java | 84 ++++++++++++------- .../generation/service/CourseServiceTest.java | 17 ++++ .../service/StudentServiceTest.java | 22 +++++ 12 files changed, 178 insertions(+), 55 deletions(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 test/com/generation/service/CourseServiceTest.java create mode 100644 test/com/generation/service/StudentServiceTest.java diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/JavaFinalProject.iml b/JavaFinalProject.iml index c90834f..86d3dd7 100644 --- a/JavaFinalProject.iml +++ b/JavaFinalProject.iml @@ -4,8 +4,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/com/generation/Main.java b/src/com/generation/Main.java index 9e24556..67e2738 100644 --- a/src/com/generation/Main.java +++ b/src/com/generation/Main.java @@ -45,6 +45,7 @@ public static void main( String[] args ) while ( option != 6 ); } + private static void enrollStudentToCourse( StudentService studentService, CourseService courseService, Scanner scanner ) { diff --git a/src/com/generation/model/Instructor.java b/src/com/generation/model/Instructor.java index 9dccdfa..ed4caf8 100644 --- a/src/com/generation/model/Instructor.java +++ b/src/com/generation/model/Instructor.java @@ -12,7 +12,7 @@ public class Instructor private final List teachingCourses = new ArrayList<>(); - protected Instructor( String id, String name, String email, Date birthDate ) + protected Instructor(String id, String name, String email, Date birthDate ) { super( id, name, email, birthDate ); } diff --git a/src/com/generation/model/Person.java b/src/com/generation/model/Person.java index 56cb7fd..11eb3f2 100644 --- a/src/com/generation/model/Person.java +++ b/src/com/generation/model/Person.java @@ -12,7 +12,7 @@ abstract public class Person private final Date birthDate; - protected Person( String id, String name, String email, Date birthDate ) + protected Person(String id, String name, String email, Date birthDate ) { this.id = id; this.name = name; diff --git a/src/com/generation/model/Student.java b/src/com/generation/model/Student.java index 0897ebe..b08319d 100644 --- a/src/com/generation/model/Student.java +++ b/src/com/generation/model/Student.java @@ -1,10 +1,6 @@ package com.generation.model; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class Student extends Person @@ -13,17 +9,47 @@ public class Student private double average; private final List courses = new ArrayList<>(); + //temp holds the courses each student is enrolled + public final List temp = new ArrayList<>(); private final Map approvedCourses = new HashMap<>(); - public Student( String id, String name, String email, Date birthDate ) + public Student(String id, String name, String email, Date birthDate ) { super( id, name, email, birthDate ); } - + //getter method to return the enrolled courses to display in student summary + public List getTemp(){ + return temp; + } public void enrollToCourse( Course course ) { - //TODO implement this method + //check if student is already enrolled in course + if(!isAttendingCourse(course.getCode())) { + //add the user's input course into the arrayList course + courses.add(course); + //add the course into the arrayList + temp.add(String.valueOf(course)); + } else { + System.out.println("Student is already enrolled in course"); + } + } + + + public boolean isAttendingCourse( String courseCode ) { + //iterate through the courses arrayList that consists of Course object. + // IE: [Course{Code,Name,Credits,Module},Course{Code,Name,Credits,Module},Course{Code,Name,Credits,Module}] + //for each element(temp) in arrayList(courses) + for (Course temp : courses) { + //use getCode method to get the course code of each element in the arrayList + //.equals check if it matches with the user's input(courseCode) + if (temp.getCode().equals(courseCode)) { + //return true if it equals + return true; + //return false if not found + } + } + return false; } public void registerApprovedCourse( Course course ) @@ -32,12 +58,6 @@ public void registerApprovedCourse( Course course ) } - public boolean isAttendingCourse( String courseCode ) - { - //TODO implement this method - return false; - } - @Override public double getAverage() { diff --git a/src/com/generation/service/CourseService.java b/src/com/generation/service/CourseService.java index 24df98b..f858e62 100644 --- a/src/com/generation/service/CourseService.java +++ b/src/com/generation/service/CourseService.java @@ -4,10 +4,7 @@ import com.generation.model.Module; import com.generation.model.Student; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class CourseService { @@ -63,7 +60,8 @@ public void enrollStudent( String courseId, Student student ) enrolledStudents.get( courseId ).add( student ); } - public void showEnrolledStudents( String courseId ) + + public void showEnrolledStudents(String courseId) { if ( enrolledStudents.containsKey( courseId ) ) { @@ -75,7 +73,6 @@ public void showEnrolledStudents( String courseId ) } } - public void showSummary() { System.out.println( "Available Courses:" ); @@ -84,15 +81,16 @@ public void showSummary() 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 ); + System.out.println(student); } } } + } + diff --git a/src/com/generation/service/StudentService.java b/src/com/generation/service/StudentService.java index f980e40..4b0bf02 100644 --- a/src/com/generation/service/StudentService.java +++ b/src/com/generation/service/StudentService.java @@ -4,6 +4,7 @@ import com.generation.model.Student; import java.util.HashMap; +import java.util.List; import java.util.Map; public class StudentService @@ -26,7 +27,18 @@ public Student findStudent( String studentId ) public void showSummary() { - //TODO implement + System.out.println( "Total Students:" ); + //loop through the students hashmap using mapName.keySet() + //for each key in the student... + for ( String key : students.keySet() ) { + // save each key (element) in the hashMap into a student obj + Student student = students.get( key ); + String temp = student.getTemp().toString(); + //print out each student until there's no more keys + //temp is the courses each student is enrolled into + System.out.println( student + temp); + + } } 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..99f13c8 100644 --- a/src/com/generation/utils/PrinterHelper.java +++ b/src/com/generation/utils/PrinterHelper.java @@ -8,44 +8,68 @@ import java.util.Date; import java.util.Scanner; -public class PrinterHelper -{ +public class PrinterHelper { - public static void showMainMenu(){ - System.out.println( "|-------------------------------|" ); - System.out.println( "| Welcome to StudentGen |" ); - System.out.println( "|-------------------------------|" ); - System.out.println( "| Select 1 option: |" ); - System.out.println( "| . 1 Register Student |" ); - System.out.println( "| . 2 Find Student |" ); - System.out.println( "| . 3 Enroll Student to Course |" ); - System.out.println( "| . 4 Show Students Summary |" ); - System.out.println( "| . 5 Show Courses Summary |" ); - System.out.println( "| . 6 Exit |" ); - System.out.println( "|-------------------------------|" ); + public static void showMainMenu() { + System.out.println("|-------------------------------|"); + System.out.println("| Welcome to StudentGen |"); + System.out.println("|-------------------------------|"); + System.out.println("| Select 1 option: |"); + System.out.println("| . 1 Register Student |"); + System.out.println("| . 2 Find Student |"); + System.out.println("| . 3 Enroll Student to Course |"); + System.out.println("| . 4 Show Students Summary |"); + System.out.println("| . 5 Show Courses Summary |"); + System.out.println("| . 6 Exit |"); + System.out.println("|-------------------------------|"); } - 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: |" ); + 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: |" ); + System.out.println("| Enter student ID: |"); String id = scanner.next(); - System.out.println( "| Enter student email: |" ); + 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("| Enter student birth date(mm/dd/yyyy)|"); + // set date format to mm/dd/yyyy + DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); + // declare next input as dob + String dob = scanner.next(); + // declare isValidDate to the return the value of isValidFormat method(if the date format is correct) + boolean isValidDate = isValidFormat(dob); + // while the format is not true, loop until the user input the correct format + while (!isValidDate){ + System.out.println("| Enter student birth date(mm/dd/yyyy)|"); + // retaking the user for DOB again + dob = scanner.next(); + // resetting value on isValidDate and stops when true. + isValidDate = isValidFormat(dob); + } + // declare birthDate as the user's input into a DateFormat + Date birthDate = formatter.parse(dob); + System.out.println("|-------------------------------------|"); + Student student = new Student(id, name, email, birthDate); + System.out.println("Student Successfully Registered! "); System.out.println(student); return student; } + public static boolean isValidFormat(String dob) { + try { + // set the expected date format + DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); + // check if date is matches the format + formatter.parse(dob); + return true; + } catch (Exception e) { + // throw error message if format is not matched + System.out.println("Wrong Date Format, Error: " +e.getMessage()); + return false; + } + } } diff --git a/test/com/generation/service/CourseServiceTest.java b/test/com/generation/service/CourseServiceTest.java new file mode 100644 index 0000000..818cc3b --- /dev/null +++ b/test/com/generation/service/CourseServiceTest.java @@ -0,0 +1,17 @@ +package com.generation.service; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CourseServiceTest { + //import CourseService object + public CourseService courseService; + @Test + void getCourse() { + } + + @Test + void showEnrolledStudents() { + } +} \ No newline at end of file diff --git a/test/com/generation/service/StudentServiceTest.java b/test/com/generation/service/StudentServiceTest.java new file mode 100644 index 0000000..5a0b6a6 --- /dev/null +++ b/test/com/generation/service/StudentServiceTest.java @@ -0,0 +1,22 @@ +package com.generation.service; + +import com.generation.model.Student; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class StudentServiceTest { + //import student object + public Student student; + Student mitch = new Student("1", "mitch", "email", null); + Student vector = new Student("2","vector", "email2", null); + + @Test + void findStudent() { + + } + + @Test + void StudentServiceTest() { + + } +} \ No newline at end of file From f39b925dfaa299facea52433d1b26ecc805afcff Mon Sep 17 00:00:00 2001 From: mitch Date: Tue, 29 Nov 2022 12:04:26 -0600 Subject: [PATCH 2/3] adds one test per file --- .../generation/service/CourseServiceTest.java | 22 +++++++++++++++++-- .../service/StudentServiceTest.java | 20 +++++++++++------ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/test/com/generation/service/CourseServiceTest.java b/test/com/generation/service/CourseServiceTest.java index 818cc3b..4bcd6d5 100644 --- a/test/com/generation/service/CourseServiceTest.java +++ b/test/com/generation/service/CourseServiceTest.java @@ -1,5 +1,7 @@ package com.generation.service; +import com.generation.model.Course; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -7,11 +9,27 @@ class CourseServiceTest { //import CourseService object public CourseService courseService; + //implement this constructor before each test + @BeforeEach + void setUp(){ + courseService = new CourseService(); + } @Test void getCourse() { + //assigning a var to getCourse's answer and converting it as a string + String code = String.valueOf(courseService.getCourse("INTRO-WEB-1")); + //assigning the answer string as a var + String answer = "Course{code='INTRO-WEB-1', name='Introduction to Web Applications', credits=9, module=Module{name='Web Development Fundamentals'}}"; + assertEquals(answer,code); } - @Test - void showEnrolledStudents() { + void getCourseNull() { + //assigning a var to getCourse's answer and converting it as a string + String code = String.valueOf(courseService.getCourse("INTRO-W3B-2")); + //assigning the expected answer string as a var + String answer = "null"; + //null is in a string because we made the valueOf null into a string within code assignment + assertEquals(answer,code); } + } \ No newline at end of file diff --git a/test/com/generation/service/StudentServiceTest.java b/test/com/generation/service/StudentServiceTest.java index 5a0b6a6..832fbdf 100644 --- a/test/com/generation/service/StudentServiceTest.java +++ b/test/com/generation/service/StudentServiceTest.java @@ -1,22 +1,28 @@ package com.generation.service; import com.generation.model.Student; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class StudentServiceTest { - //import student object + //import student and studentService object public Student student; - Student mitch = new Student("1", "mitch", "email", null); - Student vector = new Student("2","vector", "email2", null); + public StudentService studentService; + //execute before each test + @BeforeEach + void setUp(){ + studentService = new StudentService(); + //initialize students + Student mitch = new Student("1", "mitch", "email", null); + Student vector = new Student("2","vector", "email2", null); + } @Test void findStudent() { + Student temp = studentService.findStudent("2"); + assertEquals(null, temp ); } - @Test - void StudentServiceTest() { - - } } \ No newline at end of file From 6091855c43b57aa6b9f591c795dbc07c1970d03d Mon Sep 17 00:00:00 2001 From: mitch Date: Tue, 29 Nov 2022 21:16:33 -0600 Subject: [PATCH 3/3] adds comment --- src/com/generation/model/Student.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/com/generation/model/Student.java b/src/com/generation/model/Student.java index b08319d..11e8bfa 100644 --- a/src/com/generation/model/Student.java +++ b/src/com/generation/model/Student.java @@ -1,14 +1,12 @@ package com.generation.model; - import java.util.*; - public class Student extends Person implements Evaluation { private double average; - - private final List courses = new ArrayList<>(); + // an array list that holds the objects of Course named:courses + private final List courses = new ArrayList<>(); //temp holds the courses each student is enrolled public final List temp = new ArrayList<>();