From 8508f9d1d43f1c3f7f0c6df89892a8bddf86c75f Mon Sep 17 00:00:00 2001 From: Jeremy Qu Date: Fri, 20 Sep 2024 16:56:34 -0400 Subject: [PATCH 1/2] I1-Jiaming Qu --- IndividualProject/bugs.txt | 32 ++ IndividualProject/data.txt | Bin 0 -> 3868 bytes IndividualProject/pom.xml | 228 ++++---- .../project/individualproject/Course.java | 74 +-- .../project/individualproject/Department.java | 42 +- .../IndividualProjectApplication.java | 526 +++++++++--------- .../individualproject/MyFileDatabase.java | 28 +- .../individualproject/RouteController.java | 180 +++--- .../individualproject/CourseUnitTests.java | 211 ++++++- .../DepartmentUnitTests.java | 160 ++++++ .../MyFileDatabaseUnitTests.java | 168 ++++++ .../RouteControllerUnitTests.java | 321 +++++++++++ IndividualProject/testDatabase.ser | 0 13 files changed, 1451 insertions(+), 519 deletions(-) create mode 100644 IndividualProject/bugs.txt create mode 100644 IndividualProject/data.txt create mode 100644 IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java create mode 100644 IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java create mode 100644 IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java create mode 100644 IndividualProject/testDatabase.ser diff --git a/IndividualProject/bugs.txt b/IndividualProject/bugs.txt new file mode 100644 index 00000000..4a28b6ab --- /dev/null +++ b/IndividualProject/bugs.txt @@ -0,0 +1,32 @@ +** Bug 1~4 are found during testing, therefore no names identified by a static analyzer. + +Bug 1 +# Location: Course.java +# Problem: getCourseLocation() and getInstructorName() return the wrong String. + Exchanged the two strings. + +Bug 2 +# Location: Course.java +# Problem: enrollStudent() always returns False. + Fixed by adding a conditional statement. + +Bug 3 +# Location: Course.java +# Problem: dropStudent() always returns False. + Fixed by adding a conditional statement. + +Bug 4 +# Location: Course.java +# Problem: isCourseFull() has wrong logic. + +Bug 5 +# Name: ConstructionCallsOverridableMethod +# Location: MyFileDatabase.java +# Problem: caused by deSerializeObjectFromFile() which is overridable + Fixed by changing it from Public to Private + +Bug 6 +# Name: ReturnEmptyCollectionRatherThanNull +# Location: MyFileDatabase.java +# Problem: deSerializeObjectFromFile() returns Null which could makes NullPointerException + Fixed by changing Return null to returning an empty hashmap diff --git a/IndividualProject/data.txt b/IndividualProject/data.txt new file mode 100644 index 0000000000000000000000000000000000000000..99f11c1548edcfaa4cbf97cecb78dc6703bd63ab GIT binary patch literal 3868 zcma)9OKc-q6|Hn9%HR2PtEbVx@DM_Rfu!r_UubE@vGcKGCm~KI3Lw!hu9vQtlmFug8PY zo0_=hQPLG*LZ*!SKi|1$OxBHD*z2B)=-{HoFI5ybjp>f+MX`tz<3W?ceL~?r8HW4K zrm^IuhN?c7QDUqP&ILSt{-m8mGJK9>GdPx1l_%aeeq>DDUc=?5F?~1wCz%-}fR5E%i!#Ih0RFr+*9ZTmexMztmYuRR=wN7d^e0Mi2bFHx9nu)Qr zC&Ex9K5P3#n1>{B($XBv0vh0Rn;6rMX|cnT!x1>YVdp`8jiY*OhrP!G@Jit(#F9H^ z;BzqW)x?-7+2xUY8=lvkjXW$Y?ukf*Y*R$fyBsy2bn}*(I)4Q0TX0@B9oBB2!g+DS zG}p=-4kBM|DIb}7Y>#(&05Yj(CLPDgf-eA%kY{Y*Tb1M8#F)1%a&Fo1PRy;6%aZe1 zmrnsHIDlxj<}H)x0)+Tlt@@FS6B!D2$aY2ODebj5mhoeFwP6CGu#=;DoBTXVjD>o1 zgQi2cfgqOLqG_j@PNf(-#k`pq%auUf3dt(5FR%Qr3{i^Zy5eCdW471t$~czV#yuFU z?ryd8KlfqmbzN+^rjvU1aClJQGTW`Z2aXGrZc!I_>8N*(^0ZhRg}%G$6TxOFV`~*) zur+O)L-2?!OT*_|5^ce1B2XB>Nh@z=P#e&7-B|Xx-||KtEH5`yEUtZ4Wha5`_#rvA zUDr*c{s`!gh**~%=99`W*6^D;_KL-Xj~#K;}< zz~`(jJx{*hlN6;{%$k-wzD*vt;ju*B3QjO8OAQtB#0Q^kcJM;zR%r5G#93ng48;XyVUA7g2RN_0X?bWo z*S_QDD(IlXJi6lAzLmvK!a}@+vI`A>6czFn}hHY@^866`>=Nk2z}=V$?5YKV)A zNc3s;lt0jXcWhu6fL?%e*UXQc{{R%Dj=HsiK816c_SNrj{7>}i19~(Pe*pZDD71>KUfCoZ>Nj8UFq>7dF<5z6sv}p%-;m#@ zx@1;^uTyeH@Lw3Py;)blO^(R3X9JCa(FPei7vo(Lq+OEd_|!+c0QBd4$h-- z7hKb3)!oW|t+{nX$e*HW$13bYJ#aZCP$2`o1W1dAjs`j~hvnt7N`S^eUJK1$hl92# zP@yDazkq?Q>cPGy0IPcTYx>)(_1Ut_Y_5I_Pk$v7dg;N_{3(xx=(2sK_p5DZBpL5e zE_7H6CW^6m%)1d!*bc7r`~_vOdG6R(T^^&YVaNw54Hc}Ok z0DWb-*|i9H&yp%rF{pLj;+SN8Wtc`DtvgHu`hn?85?_iQ+fiM;j$Hfpv#(6t4P>jff05tm!Ig>VebsZ!pBiaZMbH*j9xIyf2?(iq6|s|$%can_fIa9AHI z99=TW8r**74%1*Yh07Cs*yGnG7f|fznSza>9zs#5xf72=62)LP-TWAbUjjn!oiBs9 zB8h)l3+Wqs(Oo~jOVkBPsgIFB!(u2Xm|DuscZnMmZE(ZWeSt_)2$shZ@RWGji@|Q=jZgJ1r@Z+Ob$LC0Y4eCvIr5lgU~-`r}*hc z$06-$e6;Eh5#ONTx*18Uuu&!3<|4e)Oc&0 NMd(a$LdX7o`(HnBpK$;H literal 0 HcmV?d00001 diff --git a/IndividualProject/pom.xml b/IndividualProject/pom.xml index 6d87d4c9..f8e1de6c 100644 --- a/IndividualProject/pom.xml +++ b/IndividualProject/pom.xml @@ -1,113 +1,125 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.2 - - - dev.coms4156.project - IndividualProject - 0.0.1-SNAPSHOT - IndividualProject - IndividualProject - - 17 - - - - org.springframework.boot - spring-boot-starter-web - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.2 + + + dev.coms4156.project + IndividualProject + 0.0.1-SNAPSHOT + IndividualProject + IndividualProject + + 17 + + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-checkstyle-plugin - 3.2.0 - - google_checks.xml - true - true - true - false - - - - validate - validate - - check - - - - - - org.jacoco - jacoco-maven-plugin - 0.8.11 - - - default-prepare-agent - - prepare-agent - - - - default-report - prepare-package - - report - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 17 - 17 - - - - org.apache.maven.plugins - maven-assembly-plugin - 3.6.0 - - - jar-with-dependencies - - - - dev.coms4156.project.individualproject - - - - - - package - - single - - - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.2.0 + + google_checks.xml + true + true + true + false + + + + validate + validate + + check + + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.11 + + + default-prepare-agent + + prepare-agent + + + + default-report + post-integration-test + + report + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 17 + 17 + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.6.0 + + + jar-with-dependencies + + + + dev.coms4156.project.individualproject + + + + + + package + + single + + + + + + diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java index 272c94c9..9bda1a85 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Course.java @@ -1,16 +1,30 @@ package dev.coms4156.project.individualproject; -import java.io.*; +import java.io.Serial; +import java.io.Serializable; +/** + * Represents a course in the system with details such as enrollment capacity, + * current enrollment count, location, instructor, and time slot. + */ public class Course implements Serializable { + @Serial + private static final long serialVersionUID = 123456L; + private final int enrollmentCapacity; + private int enrolledStudentCount; + private String courseLocation; + private String instructorName; + private String courseTimeSlot; + + /** * Constructs a new Course object with the given parameters. Initial count starts at 0. * - * @param instructorName The name of the instructor teaching the course. - * @param courseLocation The location where the course is held. - * @param timeSlot The time slot of the course. - * @param capacity The maximum number of students that can enroll in the course. + * @param instructorName The name of the instructor teaching the course. + * @param courseLocation The location where the course is held. + * @param timeSlot The time slot of the course. + * @param capacity The maximum number of students that can enroll in the course. */ public Course(String instructorName, String courseLocation, String timeSlot, int capacity) { this.courseLocation = courseLocation; @@ -20,76 +34,72 @@ public Course(String instructorName, String courseLocation, String timeSlot, int this.enrolledStudentCount = 500; } - /** + /** * Enrolls a student in the course if there is space available. * * @return true if the student is successfully enrolled, false otherwise. */ public boolean enrollStudent() { - enrolledStudentCount++; + if (enrolledStudentCount < enrollmentCapacity) { + enrolledStudentCount++; + return true; + } return false; } - /** + + /** * Drops a student from the course if a student is enrolled. * * @return true if the student is successfully dropped, false otherwise. */ public boolean dropStudent() { - enrolledStudentCount--; + if (enrolledStudentCount > 0) { + enrolledStudentCount--; + return true; + } return false; } public String getCourseLocation() { - return this.instructorName; + return this.courseLocation; } - public String getInstructorName() { - return this.courseLocation; + return this.instructorName; } - public String getCourseTimeSlot() { return this.courseTimeSlot; } - public String toString() { - return "\nInstructor: " + instructorName + "; Location: " + courseLocation + "; Time: " + courseTimeSlot; + return "\nInstructor: " + instructorName + "; Location: " + courseLocation + "; Time: " + + courseTimeSlot; + } + + public int getEnrolledStudentCount() { + return this.enrolledStudentCount; } + public void setEnrolledStudentCount(int count) { + this.enrolledStudentCount = count; + } public void reassignInstructor(String newInstructorName) { this.instructorName = newInstructorName; } - public void reassignLocation(String newLocation) { this.courseLocation = newLocation; } - public void reassignTime(String newTime) { this.courseTimeSlot = newTime; } - - public void setEnrolledStudentCount(int count) { - this.enrolledStudentCount = count; - } - - public boolean isCourseFull() { - return enrollmentCapacity > enrolledStudentCount; + return enrollmentCapacity <= enrolledStudentCount; } - - @Serial - private static final long serialVersionUID = 123456L; - private final int enrollmentCapacity; - private int enrolledStudentCount; - private String courseLocation; - private String instructorName; - private String courseTimeSlot; } diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java index 4bab0f08..e50e9a91 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/Department.java @@ -1,7 +1,9 @@ package dev.coms4156.project.individualproject; -import java.io.*; -import java.util.*; +import java.io.Serial; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; /** @@ -11,13 +13,20 @@ */ public class Department implements Serializable { + @Serial + private static final long serialVersionUID = 234567L; + private final HashMap courses; + private final String departmentChair; + private final String deptCode; + private int numberOfMajors; + /** * Constructs a new Department object with the given parameters. * - * @param deptCode The code of the department. - * @param courses A HashMap containing courses offered by the department. - * @param departmentChair The name of the department chair. - * @param numberOfMajors The number of majors in the department. + * @param deptCode The code of the department. + * @param courses A HashMap containing courses offered by the department. + * @param departmentChair The name of the department chair. + * @param numberOfMajors The number of majors in the department. */ public Department(String deptCode, HashMap courses, String departmentChair, int numberOfMajors) { @@ -27,6 +36,10 @@ public Department(String deptCode, HashMap courses, String depar this.deptCode = deptCode; } + public String getDeptCode() { + return deptCode; + } + /** * Gets the number of majors in the department. * @@ -81,11 +94,11 @@ public void addCourse(String courseId, Course course) { /** * Creates and adds a new course to the department's course selection. * - * @param courseId The ID of the new course. - * @param instructorName The name of the instructor teaching the course. - * @param courseLocation The location where the course is held. - * @param courseTimeSlot The time slot of the course. - * @param capacity The maximum number of students that can enroll in the course. + * @param courseId The ID of the new course. + * @param instructorName The name of the instructor teaching the course. + * @param courseLocation The location where the course is held. + * @param courseTimeSlot The time slot of the course. + * @param capacity The maximum number of students that can enroll in the course. */ public void createCourse(String courseId, String instructorName, String courseLocation, String courseTimeSlot, int capacity) { @@ -109,10 +122,5 @@ public String toString() { return "result.toString()"; } - @Serial - private static final long serialVersionUID = 234567L; - private HashMap courses; - private String departmentChair; - private String deptCode; - private int numberOfMajors; + } diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java index 80860423..9738d6ee 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/IndividualProjectApplication.java @@ -1,302 +1,302 @@ package dev.coms4156.project.individualproject; import jakarta.annotation.PreDestroy; -import java.util.*; -import org.springframework.boot.*; +import java.util.HashMap; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Class contains all the startup logic for the application. * - * DO NOT MODIFY ANYTHING BELOW THIS POINT WITH REGARD TO FUNCTIONALITY + *

DO NOT MODIFY ANYTHING BELOW THIS POINT WITH REGARD TO FUNCTIONALITY * YOU MAY MAKE STYLE/REFACTOR MODIFICATIONS AS NEEDED */ @SpringBootApplication public class IndividualProjectApplication implements CommandLineRunner { - /** - * The main launcher for the service all it does - * is make a call to the overridden run method. - * - * @param args A {@code String[]} of any potential - * runtime arguments - */ - public static void main(String[] args) { - SpringApplication.run(IndividualProjectApplication.class, args); - } + //Database Instance + public static MyFileDatabase myFileDatabase; + private static boolean saveData = true; - /** - * This contains all the setup logic, it will mainly be focused - * on loading up and creating an instance of the database based - * off a saved file or will create a fresh database if the file - * is not present. - * - * @param args A {@code String[]} of any potential runtime args - */ - public void run(String[] args) { - for (String arg : args) { - if (arg.equals("setup")) { - myFileDatabase = new MyFileDatabase(1, "./data.txt"); - resetDataFile(); - System.out.println("System Setup"); - return; - } - } - myFileDatabase = new MyFileDatabase(0, "./data.txt"); - System.out.println("Start up"); - } + /** + * The main launcher for the service all it does + * is make a call to the overridden run method. + * + * @param args A {@code String[]} of any potential + * runtime arguments + */ + public static void main(String[] args) { + SpringApplication.run(IndividualProjectApplication.class, args); + } - /** - * Overrides the database reference, used when testing. - * - * @param testData A {@code MyFileDatabase} object referencing test data. - */ - public static void overrideDatabase(MyFileDatabase testData) { - myFileDatabase = testData; - saveData = false; - } + /** + * Overrides the database reference, used when testing. + * + * @param testData A {@code MyFileDatabase} object referencing test data. + */ + public static void overrideDatabase(MyFileDatabase testData) { + myFileDatabase = testData; + saveData = false; + } - /** - * Allows for data to be reset in event of errors. - */ - public void resetDataFile() { - String[] times = {"11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55"}; - String[] locations = {"417 IAB", "309 HAV", "301 URIS"}; + /** + * This contains all the setup logic, it will mainly be focused + * on loading up and creating an instance of the database based + * off a saved file or will create a fresh database if the file + * is not present. + * + * @param args A {@code String[]} of any potential runtime args + */ + public void run(String[] args) { + for (String arg : args) { + if (arg.equals("setup")) { + myFileDatabase = new MyFileDatabase(1, "./data.txt"); + resetDataFile(); + System.out.println("System Setup"); + return; + } + } + myFileDatabase = new MyFileDatabase(0, "./data.txt"); + System.out.println("Start up"); + } - //data for coms dept - Course coms1004 = new Course("Adam Cannon", locations[0], times[0], 400); - coms1004.setEnrolledStudentCount(249); - Course coms3134 = new Course("Brian Borowski", locations[2], times[1], 250); - coms3134.setEnrolledStudentCount(242); - Course coms3157 = new Course("Jae Lee", locations[0], times[1], 400); - coms3157.setEnrolledStudentCount(311); - Course coms3203 = new Course("Ansaf Salleb-Aouissi", locations[2], times[2], 250); - coms3203.setEnrolledStudentCount(215); - Course coms3261 = new Course("Josh Alman", locations[0], times[3], 150); - coms3261.setEnrolledStudentCount(140); - Course coms3251 = new Course("Tony Dear", "402 CHANDLER", "1:10-3:40", 125); - coms3251.setEnrolledStudentCount(99); - Course coms3827 = new Course("Daniel Rubenstein", "207 Math", times[2], 300); - coms3827.setEnrolledStudentCount(283); - Course coms4156 = new Course("Gail Kaiser", "501 NWC", times[2], 120); - coms4156.setEnrolledStudentCount(109); - HashMap courses = new HashMap<>(); - courses.put("1004", coms1004); - courses.put("3134", coms3134); - courses.put("3157", coms3157); - courses.put("3203", coms3203); - courses.put("3261", coms3261); - courses.put("3251", coms3251); - courses.put("3827", coms3827); - courses.put("4156", coms4156); - Department compSci = new Department("COMS", courses, "Luca Carloni", 2700); - HashMap mapping = new HashMap<>(); - mapping.put("COMS", compSci); + /** + * Allows for data to be reset in event of errors. + */ + public void resetDataFile() { + String[] times = {"11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55"}; + String[] locations = {"417 IAB", "309 HAV", "301 URIS"}; - //data for econ dept - Course econ1105 = new Course("Waseem Noor", locations[1], times[3], 210); - econ1105.setEnrolledStudentCount(187); - Course econ2257 = new Course("Tamrat Gashaw", "428 PUP", times[2], 125); - econ2257.setEnrolledStudentCount(63); - Course econ3211 = new Course("Murat Yilmaz", "310 FAY", times[1], 96); - econ3211.setEnrolledStudentCount(81); - Course econ3213 = new Course("Miles Leahey", "702 HAM", times[1], 86); - econ3213.setEnrolledStudentCount(77); - Course econ3412 = new Course("Thomas Piskula", "702 HAM", times[0], 86); - econ3412.setEnrolledStudentCount(81); - Course econ4415 = new Course("Evan D Sadler", locations[1], times[2], 110); - econ4415.setEnrolledStudentCount(63); - Course econ4710 = new Course("Matthieu Gomez", "517 HAM", "8:40-9:55", 86); - econ4710.setEnrolledStudentCount(37); - Course econ4840 = new Course("Mark Dean", "142 URIS", times[3], 108); - econ4840.setEnrolledStudentCount(67); + //data for coms dept + Course coms1004 = new Course("Adam Cannon", locations[0], times[0], 400); + coms1004.setEnrolledStudentCount(249); + Course coms3134 = new Course("Brian Borowski", locations[2], times[1], 250); + coms3134.setEnrolledStudentCount(242); + Course coms3157 = new Course("Jae Lee", locations[0], times[1], 400); + coms3157.setEnrolledStudentCount(311); + Course coms3203 = new Course("Ansaf Salleb-Aouissi", locations[2], times[2], 250); + coms3203.setEnrolledStudentCount(215); + Course coms3261 = new Course("Josh Alman", locations[0], times[3], 150); + coms3261.setEnrolledStudentCount(140); + Course coms3251 = new Course("Tony Dear", "402 CHANDLER", "1:10-3:40", 125); + coms3251.setEnrolledStudentCount(99); + Course coms3827 = new Course("Daniel Rubenstein", "207 Math", times[2], 300); + coms3827.setEnrolledStudentCount(283); + Course coms4156 = new Course("Gail Kaiser", "501 NWC", times[2], 120); + coms4156.setEnrolledStudentCount(109); + HashMap courses = new HashMap<>(); + courses.put("1004", coms1004); + courses.put("3134", coms3134); + courses.put("3157", coms3157); + courses.put("3203", coms3203); + courses.put("3261", coms3261); + courses.put("3251", coms3251); + courses.put("3827", coms3827); + courses.put("4156", coms4156); + Department compSci = new Department("COMS", courses, "Luca Carloni", 2700); + HashMap mapping = new HashMap<>(); + mapping.put("COMS", compSci); - courses = new HashMap<>(); - courses.put("1105", econ1105); - courses.put("2257", econ2257); - courses.put("3211", econ3211); - courses.put("3213", econ3213); - courses.put("3412", econ3412); - courses.put("4415", econ4415); - courses.put("4710", econ4710); - courses.put("4840", econ4840); + //data for econ dept + Course econ1105 = new Course("Waseem Noor", locations[1], times[3], 210); + econ1105.setEnrolledStudentCount(187); + Course econ2257 = new Course("Tamrat Gashaw", "428 PUP", times[2], 125); + econ2257.setEnrolledStudentCount(63); + Course econ3211 = new Course("Murat Yilmaz", "310 FAY", times[1], 96); + econ3211.setEnrolledStudentCount(81); + Course econ3213 = new Course("Miles Leahey", "702 HAM", times[1], 86); + econ3213.setEnrolledStudentCount(77); + Course econ3412 = new Course("Thomas Piskula", "702 HAM", times[0], 86); + econ3412.setEnrolledStudentCount(81); + Course econ4415 = new Course("Evan D Sadler", locations[1], times[2], 110); + econ4415.setEnrolledStudentCount(63); + Course econ4710 = new Course("Matthieu Gomez", "517 HAM", "8:40-9:55", 86); + econ4710.setEnrolledStudentCount(37); + Course econ4840 = new Course("Mark Dean", "142 URIS", times[3], 108); + econ4840.setEnrolledStudentCount(67); - Department econ = new Department("ECON", courses, "Michael Woodford", 2345); - mapping.put("ECON", econ); + courses = new HashMap<>(); + courses.put("1105", econ1105); + courses.put("2257", econ2257); + courses.put("3211", econ3211); + courses.put("3213", econ3213); + courses.put("3412", econ3412); + courses.put("4415", econ4415); + courses.put("4710", econ4710); + courses.put("4840", econ4840); - //data for ieor dept - Course ieor2500 = new Course("Uday Menon", "627 MUDD", times[0], 50); - ieor2500.setEnrolledStudentCount(52); - Course ieor3404 = new Course("Christopher J Dolan", "303 MUDD", times[2], 73); - ieor3404.setEnrolledStudentCount(80); - Course ieor3658 = new Course("Daniel Lacker", "310 FAY", times[2], 96); - ieor3658.setEnrolledStudentCount(87); - Course ieor4102 = new Course("Antonius B Dieker", "209 HAM", times[2], 110); - ieor4102.setEnrolledStudentCount(92); - Course ieor4106 = new Course("Kaizheng Wang", "501 NWC", times[2], 150); - ieor4106.setEnrolledStudentCount(161); - Course ieor4405 = new Course("Yuri Faenza", "517 HAV", times[0], 80); - ieor4405.setEnrolledStudentCount(19); - Course ieor4511 = new Course("Michael Robbins", "633 MUDD", "9:00-11:30", 150); - ieor4511.setEnrolledStudentCount(50); - Course ieor4540 = new Course("Krzysztof M Choromanski", "633 MUDD", "7:10-9:40", 60); - ieor4540.setEnrolledStudentCount(33); + Department econ = new Department("ECON", courses, "Michael Woodford", 2345); + mapping.put("ECON", econ); - courses = new HashMap<>(); - courses.put("2500", ieor2500); - courses.put("3404", ieor3404); - courses.put("3658", ieor3658); - courses.put("4102", ieor4102); - courses.put("4106", ieor4106); - courses.put("4405", ieor4405); - courses.put("4511", ieor4511); - courses.put("4540", ieor4540); + //data for ieor dept + Course ieor2500 = new Course("Uday Menon", "627 MUDD", times[0], 50); + ieor2500.setEnrolledStudentCount(52); + Course ieor3404 = new Course("Christopher J Dolan", "303 MUDD", times[2], 73); + ieor3404.setEnrolledStudentCount(80); + Course ieor3658 = new Course("Daniel Lacker", "310 FAY", times[2], 96); + ieor3658.setEnrolledStudentCount(87); + Course ieor4102 = new Course("Antonius B Dieker", "209 HAM", times[2], 110); + ieor4102.setEnrolledStudentCount(92); + Course ieor4106 = new Course("Kaizheng Wang", "501 NWC", times[2], 150); + ieor4106.setEnrolledStudentCount(161); + Course ieor4405 = new Course("Yuri Faenza", "517 HAV", times[0], 80); + ieor4405.setEnrolledStudentCount(19); + Course ieor4511 = new Course("Michael Robbins", "633 MUDD", "9:00-11:30", 150); + ieor4511.setEnrolledStudentCount(50); + Course ieor4540 = new Course("Krzysztof M Choromanski", "633 MUDD", "7:10-9:40", 60); + ieor4540.setEnrolledStudentCount(33); - Department ieor = new Department("IEOR", courses, "Jay Sethuraman", 67); - mapping.put("IEOR", ieor); + courses = new HashMap<>(); + courses.put("2500", ieor2500); + courses.put("3404", ieor3404); + courses.put("3658", ieor3658); + courses.put("4102", ieor4102); + courses.put("4106", ieor4106); + courses.put("4405", ieor4405); + courses.put("4511", ieor4511); + courses.put("4540", ieor4540); - //data for chem dept - Course chem1403 = new Course("Ruben M Savizky", locations[1], "6:10-7:25", 120); - chem1403.setEnrolledStudentCount(100); - Course chem1500 = new Course("Joseph C Ulichny", "302 HAV", "6:10-9:50", 46); - chem1500.setEnrolledStudentCount(50); - Course chem2045 = new Course("Luis M Campos", "209 HAV", "1:10-2:25", 50); - chem2045.setEnrolledStudentCount(29); - Course chem2444 = new Course("Christopher Eckdahl", locations[1], times[0], 150); - chem2444.setEnrolledStudentCount(150); - Course chem2494 = new Course("Talha Siddiqui", "202 HAV", "1:10-5:00", 24); - chem2494.setEnrolledStudentCount(18); - Course chem3080 = new Course("Milan Delor", "209 HAV", times[2], 60); - chem3080.setEnrolledStudentCount(18); - Course chem4071 = new Course("Jonathan S Owen", "320 HAV", "8:40-9:55", 42); - chem4071.setEnrolledStudentCount(29); - Course chem4102 = new Course("Dalibor Sames", "320 HAV", times[2], 28); - chem4102.setEnrolledStudentCount(27); + Department ieor = new Department("IEOR", courses, "Jay Sethuraman", 67); + mapping.put("IEOR", ieor); - courses = new HashMap<>(); - courses.put("1403", chem1403); - courses.put("1500", chem1500); - courses.put("2045", chem2045); - courses.put("2444", chem2444); - courses.put("2494", chem2494); - courses.put("3080", chem3080); - courses.put("4071", chem4071); - courses.put("4102", chem4102); + //data for chem dept + Course chem1403 = new Course("Ruben M Savizky", locations[1], "6:10-7:25", 120); + chem1403.setEnrolledStudentCount(100); + Course chem1500 = new Course("Joseph C Ulichny", "302 HAV", "6:10-9:50", 46); + chem1500.setEnrolledStudentCount(50); + Course chem2045 = new Course("Luis M Campos", "209 HAV", "1:10-2:25", 50); + chem2045.setEnrolledStudentCount(29); + Course chem2444 = new Course("Christopher Eckdahl", locations[1], times[0], 150); + chem2444.setEnrolledStudentCount(150); + Course chem2494 = new Course("Talha Siddiqui", "202 HAV", "1:10-5:00", 24); + chem2494.setEnrolledStudentCount(18); + Course chem3080 = new Course("Milan Delor", "209 HAV", times[2], 60); + chem3080.setEnrolledStudentCount(18); + Course chem4071 = new Course("Jonathan S Owen", "320 HAV", "8:40-9:55", 42); + chem4071.setEnrolledStudentCount(29); + Course chem4102 = new Course("Dalibor Sames", "320 HAV", times[2], 28); + chem4102.setEnrolledStudentCount(27); - Department chem = new Department("CHEM", courses, "Laura J. Kaufman", 250); - mapping.put("CHEM", chem); + courses = new HashMap<>(); + courses.put("1403", chem1403); + courses.put("1500", chem1500); + courses.put("2045", chem2045); + courses.put("2444", chem2444); + courses.put("2494", chem2494); + courses.put("3080", chem3080); + courses.put("4071", chem4071); + courses.put("4102", chem4102); - //data for phys dept - Course phys1001 = new Course("Szabolcs Marka", "301 PUP", times[3], 150); - phys1001.setEnrolledStudentCount(131); - Course phys1201 = new Course("Eric Raymer", "428 PUP", times[3], 145); - phys1201.setEnrolledStudentCount(130); - Course phys1602 = new Course("Kerstin M Perez", "428 PUP", times[2], 140); - phys1602.setEnrolledStudentCount(77); - Course phys2802 = new Course("Yury Levin", "329 PUP", "10:10-12:00", 60); - phys2802.setEnrolledStudentCount(23); - Course phys3008 = new Course("William A Zajc", "329 PUP", times[2], 75); - phys3008.setEnrolledStudentCount(60); - Course phys4003 = new Course("Frederik Denef", "214 PUP", times[1], 50); - phys4003.setEnrolledStudentCount(19); - Course phys4018 = new Course("James W McIver", "307 PUP", times[3], 30); - phys4018.setEnrolledStudentCount(18); - Course phys4040 = new Course("James C Hill", "214 PUP", times[1], 50); - phys4040.setEnrolledStudentCount(31); + Department chem = new Department("CHEM", courses, "Laura J. Kaufman", 250); + mapping.put("CHEM", chem); - courses = new HashMap<>(); - courses.put("2802", phys2802); - courses.put("3008", phys3008); - courses.put("4003", phys4003); - courses.put("4018", phys4018); - courses.put("4040", phys4040); - courses.put("1602", phys1602); - courses.put("1001", phys1001); - courses.put("1201", phys1201); + //data for phys dept + Course phys1001 = new Course("Szabolcs Marka", "301 PUP", times[3], 150); + phys1001.setEnrolledStudentCount(131); + Course phys1201 = new Course("Eric Raymer", "428 PUP", times[3], 145); + phys1201.setEnrolledStudentCount(130); + Course phys1602 = new Course("Kerstin M Perez", "428 PUP", times[2], 140); + phys1602.setEnrolledStudentCount(77); + Course phys2802 = new Course("Yury Levin", "329 PUP", "10:10-12:00", 60); + phys2802.setEnrolledStudentCount(23); + Course phys3008 = new Course("William A Zajc", "329 PUP", times[2], 75); + phys3008.setEnrolledStudentCount(60); + Course phys4003 = new Course("Frederik Denef", "214 PUP", times[1], 50); + phys4003.setEnrolledStudentCount(19); + Course phys4018 = new Course("James W McIver", "307 PUP", times[3], 30); + phys4018.setEnrolledStudentCount(18); + Course phys4040 = new Course("James C Hill", "214 PUP", times[1], 50); + phys4040.setEnrolledStudentCount(31); - Department phys = new Department("PHYS", courses, "Dmitri N. Basov", 43); - mapping.put("PHYS", phys); + courses = new HashMap<>(); + courses.put("2802", phys2802); + courses.put("3008", phys3008); + courses.put("4003", phys4003); + courses.put("4018", phys4018); + courses.put("4040", phys4040); + courses.put("1602", phys1602); + courses.put("1001", phys1001); + courses.put("1201", phys1201); - //data for elen dept - Course elen1201 = new Course("David G Vallancourt", "301 PUP", times[1], 120); - elen1201.setEnrolledStudentCount(108); - Course elen3082 = new Course("Kenneth Shepard", "1205 MUDD", "4:10-6:40", 32); - elen3082.setEnrolledStudentCount(30); - Course elen3331 = new Course("David G Vallancourt", "203 MATH", times[0], 80); - elen3331.setEnrolledStudentCount(54); - Course elen3401 = new Course("Keren Bergman", "829 MUDD", times[3], 40); - elen3401.setEnrolledStudentCount(25); - Course elen3701 = new Course("Irving Kalet", "333 URIS", times[3], 50); - elen3701.setEnrolledStudentCount(24); - Course elen4510 = new Course("Mohamed Kamaludeen", "903 SSW", "7:00-9:30", 30); - elen4510.setEnrolledStudentCount(22); - Course elen4702 = new Course("Alexei Ashikhmin", "332 URIS", "7:00-9:30", 50); - elen4702.setEnrolledStudentCount(5); - Course elen4830 = new Course("Christine P Hendon", "633 MUDD", "10:10-12:40", 60); - elen4830.setEnrolledStudentCount(22); + Department phys = new Department("PHYS", courses, "Dmitri N. Basov", 43); + mapping.put("PHYS", phys); - courses = new HashMap<>(); - courses.put("1201", elen1201); - courses.put("3082", elen3082); - courses.put("3331", elen3331); - courses.put("3401", elen3401); - courses.put("3701", elen3701); - courses.put("4510", elen4510); - courses.put("4702", elen4702); - courses.put("4830", elen4830); + //data for elen dept + Course elen1201 = new Course("David G Vallancourt", "301 PUP", times[1], 120); + elen1201.setEnrolledStudentCount(108); + Course elen3082 = new Course("Kenneth Shepard", "1205 MUDD", "4:10-6:40", 32); + elen3082.setEnrolledStudentCount(30); + Course elen3331 = new Course("David G Vallancourt", "203 MATH", times[0], 80); + elen3331.setEnrolledStudentCount(54); + Course elen3401 = new Course("Keren Bergman", "829 MUDD", times[3], 40); + elen3401.setEnrolledStudentCount(25); + Course elen3701 = new Course("Irving Kalet", "333 URIS", times[3], 50); + elen3701.setEnrolledStudentCount(24); + Course elen4510 = new Course("Mohamed Kamaludeen", "903 SSW", "7:00-9:30", 30); + elen4510.setEnrolledStudentCount(22); + Course elen4702 = new Course("Alexei Ashikhmin", "332 URIS", "7:00-9:30", 50); + elen4702.setEnrolledStudentCount(5); + Course elen4830 = new Course("Christine P Hendon", "633 MUDD", "10:10-12:40", 60); + elen4830.setEnrolledStudentCount(22); - Department elen = new Department("ELEN", courses, "Ioannis Kymissis", 250); - mapping.put("ELEN", elen); + courses = new HashMap<>(); + courses.put("1201", elen1201); + courses.put("3082", elen3082); + courses.put("3331", elen3331); + courses.put("3401", elen3401); + courses.put("3701", elen3701); + courses.put("4510", elen4510); + courses.put("4702", elen4702); + courses.put("4830", elen4830); - //data for psyc dept - Course psyc1001 = new Course("Patricia G Lindemann", "501 SCH", "1:10-2:25", 200); - psyc1001.setEnrolledStudentCount(191); - Course psyc1610 = new Course("Christopher Baldassano", "200 SCH", times[2], 45); - psyc1610.setEnrolledStudentCount(42); - Course psyc2235 = new Course("Katherine T Fox-Glassman", "501 SCH", times[0], 125); - psyc2235.setEnrolledStudentCount(128); - Course psyc2620 = new Course("Jeffrey M Cohen", "303 URIS", "1:10-3:40", 60); - psyc2620.setEnrolledStudentCount(55); - Course psyc3212 = new Course("Mayron Piccolo", "200 SCH", "2:10-4:00", 15); - psyc3212.setEnrolledStudentCount(15); - Course psyc3445 = new Course("Mariam Aly", "405 SCH", "2:10-4:00", 12); - psyc3445.setEnrolledStudentCount(12); - Course psyc4236 = new Course("Trenton Jerde", "405 SCH", "6:10-8:00", 18); - psyc4236.setEnrolledStudentCount(17); - Course psyc4493 = new Course("Jennifer Blaze", "200 SCH", "2:10-4:00", 15); - psyc4493.setEnrolledStudentCount(9); + Department elen = new Department("ELEN", courses, "Ioannis Kymissis", 250); + mapping.put("ELEN", elen); - courses = new HashMap<>(); - courses.put("1001", psyc1001); - courses.put("1610", psyc1610); - courses.put("2235", psyc2235); - courses.put("2620", psyc2620); - courses.put("3212", psyc3212); - courses.put("3445", psyc3445); - courses.put("4236", psyc4236); - courses.put("4493", psyc4493); + //data for psyc dept + Course psyc1001 = new Course("Patricia G Lindemann", "501 SCH", "1:10-2:25", 200); + psyc1001.setEnrolledStudentCount(191); + Course psyc1610 = new Course("Christopher Baldassano", "200 SCH", times[2], 45); + psyc1610.setEnrolledStudentCount(42); + Course psyc2235 = new Course("Katherine T Fox-Glassman", "501 SCH", times[0], 125); + psyc2235.setEnrolledStudentCount(128); + Course psyc2620 = new Course("Jeffrey M Cohen", "303 URIS", "1:10-3:40", 60); + psyc2620.setEnrolledStudentCount(55); + Course psyc3212 = new Course("Mayron Piccolo", "200 SCH", "2:10-4:00", 15); + psyc3212.setEnrolledStudentCount(15); + Course psyc3445 = new Course("Mariam Aly", "405 SCH", "2:10-4:00", 12); + psyc3445.setEnrolledStudentCount(12); + Course psyc4236 = new Course("Trenton Jerde", "405 SCH", "6:10-8:00", 18); + psyc4236.setEnrolledStudentCount(17); + Course psyc4493 = new Course("Jennifer Blaze", "200 SCH", "2:10-4:00", 15); + psyc4493.setEnrolledStudentCount(9); - Department psyc = new Department("PSYC", courses, "Nim Tottenham", 437); - mapping.put("PSYC", psyc); + courses = new HashMap<>(); + courses.put("1001", psyc1001); + courses.put("1610", psyc1610); + courses.put("2235", psyc2235); + courses.put("2620", psyc2620); + courses.put("3212", psyc3212); + courses.put("3445", psyc3445); + courses.put("4236", psyc4236); + courses.put("4493", psyc4493); - myFileDatabase.setMapping(mapping); - } + Department psyc = new Department("PSYC", courses, "Nim Tottenham", 437); + mapping.put("PSYC", psyc); - /** - * This contains all the overheading teardown logic, it will - * mainly be focused on saving all the created user data to a - * file, so it will be ready for the next setup. - */ - @PreDestroy - public void onTermination() { - System.out.println("Termination"); - if (saveData) { - myFileDatabase.saveContentsToFile(); - } - } + myFileDatabase.setMapping(mapping); + } - - //Database Instance - public static MyFileDatabase myFileDatabase; - private static boolean saveData = true; + /** + * This contains all the overheading teardown logic, it will + * mainly be focused on saving all the created user data to a + * file, so it will be ready for the next setup. + */ + @PreDestroy + public void onTermination() { + System.out.println("Termination"); + if (saveData) { + myFileDatabase.saveContentsToFile(); + } + } } diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java index 1f61f893..7cdfa8ec 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/MyFileDatabase.java @@ -1,13 +1,27 @@ package dev.coms4156.project.individualproject; -import java.io.*; -import java.util.*; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.Map; /** * This class represents a file-based database containing department mappings. */ public class MyFileDatabase { + /** + * The path to the file containing the database entries. + */ + private final String filePath; + /** + * The mapping of department names to Department objects. + */ + private HashMap departmentMapping; + /** * Constructs a MyFileDatabase object and loads up the data structure with * the contents of the file. @@ -36,7 +50,7 @@ public void setMapping(HashMap mapping) { * * @return the deserialized department mapping */ - public HashMap deSerializeObjectFromFile() { + private HashMap deSerializeObjectFromFile() { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath))) { Object obj = in.readObject(); if (obj instanceof HashMap) { @@ -46,7 +60,7 @@ public HashMap deSerializeObjectFromFile() { } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); - return null; + return new HashMap<>(); } } @@ -87,10 +101,4 @@ public String toString() { } return result.toString(); } - - /** The path to the file containing the database entries. */ - private String filePath; - - /** The mapping of department names to Department objects. */ - private HashMap departmentMapping; } diff --git a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java index 09f504dc..858985e6 100644 --- a/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java +++ b/IndividualProject/src/main/java/dev/coms4156/project/individualproject/RouteController.java @@ -1,8 +1,13 @@ package dev.coms4156.project.individualproject; import java.util.HashMap; -import org.springframework.http.*; -import org.springframework.web.bind.annotation.*; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; /** * This class contains all the API routes for the system. @@ -27,7 +32,6 @@ public String index() { * * @param deptCode A {@code String} representing the department the user wishes * to retrieve. - * * @return A {@code ResponseEntity} object containing either the details of the Department and * an HTTP 200 response or, an appropriate message indicating the proper response. */ @@ -55,16 +59,15 @@ public ResponseEntity retrieveDepartment(@RequestParam(value = "deptCode") St * * @param deptCode A {@code String} representing the department the user wishes * to find the course in. - * * @param courseCode A {@code int} representing the course the user wishes * to retrieve. - * - * @return A {@code ResponseEntity} object containing either the details of the - * course and an HTTP 200 response or, an appropriate message indicating the - * proper response. + * @return A {@code ResponseEntity} object containing either the details of the + * course and an HTTP 200 response or, an appropriate message indicating the + * proper response. */ @GetMapping(value = "/retrieveCourse", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity retrieveCourse(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity retrieveCourse(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesDepartmentExists = retrieveDepartment(deptCode).getStatusCode() == HttpStatus.OK; if (doesDepartmentExists) { @@ -92,16 +95,15 @@ public ResponseEntity retrieveCourse(@RequestParam(value = "deptCode") String * * @param deptCode A {@code String} representing the department the user wishes * to find the course in. - * * @param courseCode A {@code int} representing the course the user wishes * to retrieve. - * - * @return A {@code ResponseEntity} object containing either the requested information - * and an HTTP 200 response or, an appropriate message indicating the proper - * response. + * @return A {@code ResponseEntity} object containing either the requested information + * and an HTTP 200 response or, an appropriate message indicating the proper + * response. */ @GetMapping(value = "/isCourseFull", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity isCourseFull(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity isCourseFull(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -126,12 +128,11 @@ public ResponseEntity isCourseFull(@RequestParam(value = "deptCode") String d /** * Displays the number of majors in the specified department. * - * @param deptCode A {@code String} representing the department the user wishes - * to find number of majors for. - * - * @return A {@code ResponseEntity} object containing either number of majors for the - * specified department and an HTTP 200 response or, an appropriate message - * indicating the proper response. + * @param deptCode A {@code String} representing the department the user wishes + * to find number of majors for. + * @return A {@code ResponseEntity} object containing either number of majors for the + * specified department and an HTTP 200 response or, an appropriate message + * indicating the proper response. */ @GetMapping(value = "/getMajorCountFromDept", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getMajorCtFromDept(@RequestParam(value = "deptCode") String deptCode) { @@ -152,12 +153,11 @@ public ResponseEntity getMajorCtFromDept(@RequestParam(value = "deptCode") St /** * Displays the department chair for the specified department. * - * @param deptCode A {@code String} representing the department the user wishes - * to find the department chair of. - * - * @return A {@code ResponseEntity} object containing either department chair of the - * specified department and an HTTP 200 response or, an appropriate message - * indicating the proper response. + * @param deptCode A {@code String} representing the department the user wishes + * to find the department chair of. + * @return A {@code ResponseEntity} object containing either department chair of the + * specified department and an HTTP 200 response or, an appropriate message + * indicating the proper response. */ @GetMapping(value = "/idDeptChair", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity identifyDeptChair(@RequestParam(value = "deptCode") String deptCode) { @@ -180,16 +180,15 @@ public ResponseEntity identifyDeptChair(@RequestParam(value = "deptCode") Str * * @param deptCode A {@code String} representing the department the user wishes * to find the course in. - * * @param courseCode A {@code int} representing the course the user wishes * to find information about. - * - * @return A {@code ResponseEntity} object containing either the location of the - * course and an HTTP 200 response or, an appropriate message indicating the - * proper response. + * @return A {@code ResponseEntity} object containing either the location of the + * course and an HTTP 200 response or, an appropriate message indicating the + * proper response. */ @GetMapping(value = "/findCourseLocation", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity findCourseLocation(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity findCourseLocation(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -217,16 +216,16 @@ public ResponseEntity findCourseLocation(@RequestParam(value = "deptCode") St * * @param deptCode A {@code String} representing the department the user wishes * to find the course in. - * * @param courseCode A {@code int} representing the course the user wishes * to find information about. - * - * @return A {@code ResponseEntity} object containing either the course instructor and - * an HTTP 200 response or, an appropriate message indicating the proper - * response. + * @return A {@code ResponseEntity} object containing either the course instructor and + * an HTTP 200 response or, an appropriate message indicating the proper + * response. */ @GetMapping(value = "/findCourseInstructor", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity findCourseInstructor(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity findCourseInstructor(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") + int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -254,16 +253,15 @@ public ResponseEntity findCourseInstructor(@RequestParam(value = "deptCode") * * @param deptCode A {@code String} representing the department the user wishes * to find the course in. - * * @param courseCode A {@code int} representing the course the user wishes * to find information about. - * - * @return A {@code ResponseEntity} object containing either the details of the - * course timeslot and an HTTP 200 response or, an appropriate message - * indicating the proper response. + * @return A {@code ResponseEntity} object containing either the details of the + * course timeslot and an HTTP 200 response or, an appropriate message + * indicating the proper response. */ @GetMapping(value = "/findCourseTime", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity findCourseTime(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity findCourseTime(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -288,11 +286,10 @@ public ResponseEntity findCourseTime(@RequestParam(value = "deptCode") String /** * Attempts to add a student to the specified department. * - * @param deptCode A {@code String} representing the department. - * - * @return A {@code ResponseEntity} object containing an HTTP 200 - * response with an appropriate message or the proper status - * code in tune with what has happened. + * @param deptCode A {@code String} representing the department. + * @return A {@code ResponseEntity} object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. */ @PatchMapping(value = "/addMajorToDept", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity addMajorToDept(@RequestParam(value = "deptCode") String deptCode) { @@ -315,11 +312,10 @@ public ResponseEntity addMajorToDept(@RequestParam(value = "deptCode") String /** * Attempts to remove a student from the specified department. * - * @param deptCode A {@code String} representing the department. - * - * @return A {@code ResponseEntity} object containing an HTTP 200 - * response with an appropriate message or the proper status - * code in tune with what has happened. + * @param deptCode A {@code String} representing the department. + * @return A {@code ResponseEntity} object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. */ @PatchMapping(value = "/removeMajorFromDept", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity removeMajorFromDept(@RequestParam(value = "deptCode") String deptCode) { @@ -342,16 +338,15 @@ public ResponseEntity removeMajorFromDept(@RequestParam(value = "deptCode") S /** * Attempts to drop a student from the specified course. * - * @param deptCode A {@code String} representing the department. - * - * @param courseCode A {@code int} representing the course within the department. - * - * @return A {@code ResponseEntity} object containing an HTTP 200 - * response with an appropriate message or the proper status - * code in tune with what has happened. + * @param deptCode A {@code String} representing the department. + * @param courseCode A {@code int} representing the course within the department. + * @return A {@code ResponseEntity} object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. */ @PatchMapping(value = "/dropStudentFromCourse", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity dropStudent(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode) { + public ResponseEntity dropStudent(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -378,9 +373,20 @@ public ResponseEntity dropStudent(@RequestParam(value = "deptCode") String de } } - + /** + * Updates the enrollment count for a specific course in a department. + * + * @param deptCode A {@code String} representing the department. + * @param courseCode A {@code int} representing the course within the department. + * @param count A {@code int} representing the new enrollment count to set for the course. + * @return A {@code ResponseEntity} object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. + */ @PatchMapping(value = "/setEnrollmentCount", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity setEnrollmentCount(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "count") int count) { + public ResponseEntity setEnrollmentCount(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "count") int count) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -407,15 +413,16 @@ public ResponseEntity setEnrollmentCount(@RequestParam(value = "deptCode") St * This method handles PATCH requests to change the time of a course identified by * department code and course code.If the course exists, its time is updated to the provided time. * - * @param deptCode the code of the department containing the course - * @param courseCode the code of the course to change the time for - * @param time the new time for the course - * - * @return a ResponseEntity with a success message if the operation is - * successful, or an error message if the course is not found + * @param deptCode the code of the department containing the course + * @param courseCode the code of the course to change the time for + * @param time the new time for the course + * @return a ResponseEntity with a success message if the operation is + * successful, or an error message if the course is not found */ @PatchMapping(value = "/changeCourseTime", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity changeCourseTime(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "time") String time) { + public ResponseEntity changeCourseTime(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "time") String time) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -443,15 +450,16 @@ public ResponseEntity changeCourseTime(@RequestParam(value = "deptCode") Stri * department code and course code. If the course exists, its instructor is updated to the * provided instructor. * - * @param deptCode the code of the department containing the course - * @param courseCode the code of the course to change the instructor for - * @param teacher the new instructor for the course - * - * @return a ResponseEntity with a success message if the operation is - * successful, or an error message if the course is not found + * @param deptCode the code of the department containing the course + * @param courseCode the code of the course to change the instructor for + * @param teacher the new instructor for the course + * @return a ResponseEntity with a success message if the operation is + * successful, or an error message if the course is not found */ @PatchMapping(value = "/changeCourseTeacher", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity changeCourseTeacher(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "teacher") String teacher) { + public ResponseEntity changeCourseTeacher(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "teacher") String teacher) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; @@ -473,9 +481,19 @@ public ResponseEntity changeCourseTeacher(@RequestParam(value = "deptCode") S } } - + /** + * Changes the location of a specific course in a department. + * + * @param deptCode A {@code String} representing the department code where the course is from. + * @param courseCode A {@code int} representing the course within the department. + * @param location A {@code String} representing the new location of the course. + * @return A {@code ResponseEntity} object containing a success message if the operation is + * successful or an error message if the course was not found. + */ @PatchMapping(value = "/changeCourseLocation", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity changeCourseLocation(@RequestParam(value = "deptCode") String deptCode, @RequestParam(value = "courseCode") int courseCode, @RequestParam(value = "location") String location) { + public ResponseEntity changeCourseLocation(@RequestParam(value = "deptCode") String deptCode, + @RequestParam(value = "courseCode") int courseCode, + @RequestParam(value = "location") String location) { try { boolean doesCourseExists; doesCourseExists = retrieveCourse(deptCode, courseCode).getStatusCode() == HttpStatus.OK; diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java index 4edd00f9..881c76f8 100644 --- a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/CourseUnitTests.java @@ -1,28 +1,223 @@ package dev.coms4156.project.individualproject; -import org.junit.jupiter.api.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; -import static org.junit.jupiter.api.Assertions.*; +/** + * Unit tests for the Course class. + */ @SpringBootTest @ContextConfiguration public class CourseUnitTests { + public static Course testCourse; + @BeforeAll public static void setupCourseForTesting() { - testCourse = new Course("Griffin Newbold", "417 IAB", "11:40-12:55", 250); + testCourse = new Course("Kenneth Shepard", "1205 MUDD", "4:10-6:40", 250); } - @Test public void toStringTest() { - String expectedResult = "\nInstructor: Griffin Newbold; Location: 417 IAB; Time: 11:40-12:55"; + String expectedResult = "\nInstructor: Kenneth Shepard; Location: 1205 MUDD; Time: 4:10-6:40"; assertEquals(expectedResult, testCourse.toString()); } - /** The test course instance used for testing. */ - public static Course testCourse; -} + @Test + public void testReassignInstructor() { + testCourse.reassignInstructor("David G Vallancourt"); + assertEquals("David G Vallancourt", testCourse.getInstructorName()); + } + + @Test + public void testReassignLocation() { + testCourse.reassignLocation("301 PUP"); + assertEquals("301 PUP", testCourse.getCourseLocation()); + } + + @Test + public void testReassignTime() { + testCourse.reassignTime("4:10-5:25"); + assertEquals("4:10-5:25", testCourse.getCourseTimeSlot()); + } + + @Test + public void testEnrollStudentSuccess() { + testCourse.setEnrolledStudentCount(100); + assertTrue(testCourse.enrollStudent()); + assertEquals(101, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testEnrollStudentFailure() { + testCourse.setEnrolledStudentCount(250); + assertFalse(testCourse.enrollStudent()); + assertEquals(250, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testEnrollStudentEdgeCase() { + testCourse.setEnrolledStudentCount(249); + assertTrue(testCourse.enrollStudent()); + assertEquals(250, testCourse.getEnrolledStudentCount()); + assertFalse(testCourse.enrollStudent()); + } + + @Test + public void testDropStudentSuccess() { + testCourse.setEnrolledStudentCount(100); + assertTrue(testCourse.dropStudent()); + assertEquals(99, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testDropStudentFailure() { + testCourse.setEnrolledStudentCount(0); + assertFalse(testCourse.dropStudent()); + assertEquals(0, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testIsCourseFullTrue() { + testCourse.setEnrolledStudentCount(250); + assertTrue(testCourse.isCourseFull()); + } + + @Test + public void testIsCourseFullFalse() { + testCourse.setEnrolledStudentCount(249); + assertFalse(testCourse.isCourseFull()); + } + + @Test + public void testSetEnrolledStudentCount() { + testCourse.setEnrolledStudentCount(50); + assertEquals(50, testCourse.getEnrolledStudentCount()); + testCourse.setEnrolledStudentCount(250); + assertEquals(250, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testZeroCapacityCourse() { + Course zeroCapacityCourse = new Course("Mohamed Kamaludeen", "903 SSW", "7:00-9:30", 0); + assertFalse(zeroCapacityCourse.enrollStudent()); + assertTrue(zeroCapacityCourse.isCourseFull()); + } + + @Test + public void testInitialEnrollment() { + Course newCourse = new Course("Christine P Hendon", "633 MUDD", "10:10-12:40", 100); + assertEquals(500, newCourse.getEnrolledStudentCount()); + } + + @Test + public void testEnrollStudentAtFullCapacity() { + testCourse.setEnrolledStudentCount(250); + assertFalse(testCourse.enrollStudent()); + } + @Test + public void testDropStudentFromEmptyCourse() { + testCourse.setEnrolledStudentCount(0); + assertFalse(testCourse.dropStudent()); + } + + @Test + public void testFullCourseEnrollAttempt() { + testCourse.setEnrolledStudentCount(250); + assertFalse(testCourse.enrollStudent()); + } + + @Test + public void testValidEnrollmentIncrease() { + testCourse.setEnrolledStudentCount(249); + assertTrue(testCourse.enrollStudent()); + assertEquals(250, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testReassignInstructorNull() { + testCourse.reassignInstructor(null); + assertNull(testCourse.getInstructorName()); + } + + @Test + public void testReassignLocationNull() { + testCourse.reassignLocation(null); + assertNull(testCourse.getCourseLocation()); + } + + @Test + public void testReassignTimeNull() { + testCourse.reassignTime(null); + assertNull(testCourse.getCourseTimeSlot()); + } + + @Test + public void testNegativeEnrollmentCount() { + testCourse.setEnrolledStudentCount(-10); + assertEquals(-10, testCourse.getEnrolledStudentCount()); + assertFalse(testCourse.dropStudent()); + } + + @Test + public void testEnrollDropMultipleOperations() { + testCourse.setEnrolledStudentCount(100); + assertTrue(testCourse.enrollStudent()); + assertTrue(testCourse.dropStudent()); + assertTrue(testCourse.dropStudent()); + assertTrue(testCourse.enrollStudent()); + assertEquals(100, testCourse.getEnrolledStudentCount()); + } + + @Test + public void testToStringWithNullInstructor() { + testCourse.reassignInstructor(null); + String expectedResult = "\nInstructor: null; Location: 1205 MUDD; Time: 4:10-6:40"; + assertEquals(expectedResult, testCourse.toString()); + } + + @Test + public void testToStringWithNullLocation() { + testCourse.reassignLocation(null); + String expectedResult = "\nInstructor: Kenneth Shepard; Location: null; Time: 4:10-6:40"; + assertEquals(expectedResult, testCourse.toString()); + } + + @Test + public void testToStringWithNullTimeSlot() { + testCourse.reassignTime(null); + String expectedResult = "\nInstructor: Kenneth Shepard; Location: 1205 MUDD; Time: null"; + assertEquals(expectedResult, testCourse.toString()); + } + + @Test + public void testZeroCapacityCourseEnrollment() { + Course zeroCapacityCourse = new Course("David G Vallancourt", "203 MATH", "11:40-12:55", 0); + assertFalse(zeroCapacityCourse.enrollStudent()); + assertTrue(zeroCapacityCourse.isCourseFull()); + } + + @Test + public void testSetEnrollmentToZero() { + testCourse.setEnrolledStudentCount(0); + assertEquals(0, testCourse.getEnrolledStudentCount()); + assertFalse(testCourse.dropStudent()); + } + + @Test + public void testSetEnrollmentBeyondCapacity() { + testCourse.setEnrolledStudentCount(300); + assertEquals(300, testCourse.getEnrolledStudentCount()); + assertFalse(testCourse.enrollStudent()); + } + +} diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java new file mode 100644 index 00000000..cf7ed585 --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/DepartmentUnitTests.java @@ -0,0 +1,160 @@ +package dev.coms4156.project.individualproject; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashMap; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the Department class. + */ + +public class DepartmentUnitTests { + + private static Department testDepartment; + private static Course testCourse1; + private static Course testCourse2; + + /** + * Sets up test data for the Department class before all tests run. + * Initializes courses. + */ + + @BeforeAll + public static void setupDepartmentForTesting() { + testCourse1 = new Course("Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + testCourse2 = new Course("David G Vallancourt", "301 PUP", "4:10-5:25", 100); + + HashMap courses = new HashMap<>(); + courses.put("3082", testCourse1); + courses.put("1201", testCourse2); + + testDepartment = new Department("ELEN", courses, "this.departmentChair", 250); + } + + @Test + public void testGetNumberOfMajors() { + assertEquals(250, -testDepartment.getNumberOfMajors()); + testDepartment.addPersonToMajor(); + assertEquals(251, -testDepartment.getNumberOfMajors()); + } + + @Test + public void testGetDepartmentChair() { + assertEquals("this.departmentChair", testDepartment.getDepartmentChair()); + } + + @Test + public void testGetCourseSelection() { + HashMap courses = testDepartment.getCourseSelection(); + assertEquals(2, courses.size()); + assertTrue(courses.containsKey("3082")); + assertTrue(courses.containsKey("1201")); + } + + @Test + public void testAddPersonToMajor() { + int previousNumber = -testDepartment.getNumberOfMajors(); + testDepartment.addPersonToMajor(); + assertEquals(previousNumber + 1, -testDepartment.getNumberOfMajors()); + } + + @Test + public void testDropPersonFromMajor() { + int previousNumber = -testDepartment.getNumberOfMajors(); + testDepartment.dropPersonFromMajor(); + assertEquals(previousNumber - 1, -testDepartment.getNumberOfMajors()); + } + + @Test + public void testAddCourse() { + Course newCourse = new Course("Keren Bergman", "829 MUDD", "2:40-3:55", 100); + testDepartment.addCourse("3401", newCourse); + + HashMap courses = testDepartment.getCourseSelection(); + assertEquals(3, courses.size()); + assertTrue(courses.containsKey("3401")); + assertEquals(newCourse, courses.get("3401")); + } + + @Test + public void testCreateCourse() { + testDepartment.createCourse("4510", "Mohamed Kamaludeen", "903 SSW", "7:00-9:30", 100); + + HashMap courses = testDepartment.getCourseSelection(); + assertEquals(4, courses.size()); + Course createdCourse = courses.get("4510"); + assertEquals("Mohamed Kamaludeen", createdCourse.getInstructorName()); + assertEquals("903 SSW", createdCourse.getCourseLocation()); + assertEquals("7:00-9:30", createdCourse.getCourseTimeSlot()); + assertEquals(100, createdCourse.getEnrolledStudentCount()); + } + + @Test + public void testToString() { + String result = testDepartment.toString(); + assertTrue(result.contains("ELEN 3082")); + assertTrue(result.contains("ELEN 1201")); + assertTrue(result.contains(testCourse1.toString())); + assertTrue(result.contains(testCourse2.toString())); + } + + @Test + public void testAddDuplicateCourse() { + Course duplicateCourse = new Course("Kenneth Shepard", "Mudd 105", "15:00-16:15", 100); + testDepartment.addCourse("3082", duplicateCourse); + HashMap courses = testDepartment.getCourseSelection(); + assertEquals(2, courses.size()); + assertNotEquals(duplicateCourse, courses.get("3082")); + } + + @Test + public void testCreateCourseWithExistingId() { + testDepartment.createCourse("3082", "Kenneth Shepard", "Mudd 105", "15:00-16:15", 100); + HashMap courses = testDepartment.getCourseSelection(); + assertEquals(2, courses.size()); + Course updatedCourse = courses.get("3082"); + assertNotEquals("Kenneth Shepard", updatedCourse.getInstructorName()); + } + + @Test + public void testDropMorePeopleThanMajors() { + for (int i = 0; i < 255; i++) { + testDepartment.dropPersonFromMajor(); + } + assertEquals(0, -testDepartment.getNumberOfMajors()); + } + + @Test + public void testNegativeMajorsEdgeCase() { + testDepartment.dropPersonFromMajor(); + assertEquals(0, -testDepartment.getNumberOfMajors()); + } + + @Test + public void testAddNullCourse() { + testDepartment.addCourse("4511", null); + HashMap courses = testDepartment.getCourseSelection(); + assertNull(courses.get("4511")); + } + + @Test + public void testDropPersonWhenZeroMajors() { + for (int i = 0; i < 250; i++) { + testDepartment.dropPersonFromMajor(); + } + testDepartment.dropPersonFromMajor(); + assertEquals(0, -testDepartment.getNumberOfMajors()); + } + + @Test + public void testGetDeptCode() { + assertEquals("COMS", testDepartment.getDeptCode()); + } + + +} diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java new file mode 100644 index 00000000..4b2521fd --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/MyFileDatabaseUnitTests.java @@ -0,0 +1,168 @@ +package dev.coms4156.project.individualproject; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the MyFileDatabase class. + */ + +public class MyFileDatabaseUnitTests { + + private final String testFilePath = "testDatabase.ser"; + private MyFileDatabase myFileDatabase; + + /** + * Sets up the test environment before each test runs. + */ + + @BeforeEach + public void setup() { + File file = new File(testFilePath); + if (file.exists()) { + file.delete(); + } + + myFileDatabase = new MyFileDatabase(1, testFilePath); + } + + @Test + public void testSetAndGetMapping() { + HashMap departments = new HashMap<>(); + departments.put("ELEN", new Department("ELEN", new HashMap<>(), "this.departmentChair", 250)); + myFileDatabase.setMapping(departments); + + assertEquals(departments, myFileDatabase.getDepartmentMapping()); + } + + @Test + public void testSaveAndLoadContentsToFile() { + HashMap departments = new HashMap<>(); + departments.put("ELEN", new Department("ELEN", new HashMap<>(), "this.departmentChair", 250)); + myFileDatabase.setMapping(departments); + + myFileDatabase.saveContentsToFile(); + + MyFileDatabase loadedDatabase = new MyFileDatabase(0, testFilePath); + HashMap loadedMapping = loadedDatabase.getDepartmentMapping(); + + assertNotNull(loadedMapping); + assertEquals(departments.size(), loadedMapping.size()); + assertEquals(departments.get("ELEN").toString(), loadedMapping.get("ELEN").toString()); + } + + @Test + public void testSaveWithEmptyMapping() { + myFileDatabase.setMapping(new HashMap<>()); + + myFileDatabase.saveContentsToFile(); + MyFileDatabase loadedDatabase = new MyFileDatabase(0, testFilePath); + + assertNotNull(loadedDatabase.getDepartmentMapping()); + assertTrue(loadedDatabase.getDepartmentMapping().isEmpty()); + } + + @Test + public void testDeserializeInvalidFile() throws IOException { + File file = new File(testFilePath); + file.createNewFile(); + + MyFileDatabase loadedDatabase = new MyFileDatabase(0, testFilePath); + assertNull(loadedDatabase.getDepartmentMapping()); + } + + @Test + public void testDeserializeFromCorruptedFile() throws IOException { + File file = new File(testFilePath); + file.createNewFile(); + + MyFileDatabase corruptedDatabase = new MyFileDatabase(0, testFilePath); + assertNull(corruptedDatabase.getDepartmentMapping()); + } + + @Test + public void testToString() { + HashMap departments = new HashMap<>(); + Department department = new Department("ELEN", new HashMap<>(), "this.departmentChair", 250); + departments.put("ELEN", department); + myFileDatabase.setMapping(departments); + + String expectedOutput = "For the ELEN department: \n" + department.toString(); + assertTrue(myFileDatabase.toString().contains(expectedOutput)); + } + + @Test + public void testToStringWithEmptyMapping() { + myFileDatabase.setMapping(new HashMap<>()); + + assertEquals("{}", myFileDatabase.toString()); + } + + @Test + public void testUpdateAndSaveMapping() { + HashMap departments = new HashMap<>(); + departments.put("ELEN", new Department("ELEN", new HashMap<>(), "this.departmentChair", 250)); + myFileDatabase.setMapping(departments); + + departments.put("CHEM", new Department("CHEM", new HashMap<>(), "this.departmentChair", 250)); + myFileDatabase.setMapping(departments); + myFileDatabase.saveContentsToFile(); + + MyFileDatabase loadedDatabase = new MyFileDatabase(0, testFilePath); + HashMap loadedMapping = loadedDatabase.getDepartmentMapping(); + + assertEquals(2, loadedMapping.size()); + assertEquals("this.departmentChair", loadedMapping.get("CHEM").getDepartmentChair()); + } + + @Test + public void testSaveAndDeserializeNullMapping() throws IOException { + myFileDatabase.setMapping(null); + myFileDatabase.saveContentsToFile(); + + MyFileDatabase loadedDatabase = new MyFileDatabase(0, testFilePath); + assertNull(loadedDatabase.getDepartmentMapping()); + } + + @Test + public void testDeserializeWithInvalidObjectType() throws IOException { + File file = new File(testFilePath); + file.createNewFile(); + + MyFileDatabase loadedDatabase = new MyFileDatabase(0, testFilePath); + assertNull(loadedDatabase.getDepartmentMapping()); + } + + @Test + public void testDeserializeFromNonExistentFile() { + File file = new File("nonExistentFile.ser"); + if (file.exists()) { + file.delete(); + } + + MyFileDatabase loadedDatabase = new MyFileDatabase(0, "nonExistentFile.ser"); + assertNull(loadedDatabase.getDepartmentMapping()); + } + + @Test + public void testToStringWithMultipleDepartments() { + HashMap departments = new HashMap<>(); + Department department1 = new Department("ELEN", new HashMap<>(), "this.departmentChair", 250); + Department department2 = new Department("CHEM", new HashMap<>(), "this.departmentChair", 250); + departments.put("ELEN", department1); + departments.put("CHEM", department2); + myFileDatabase.setMapping(departments); + + String result = myFileDatabase.toString(); + assertTrue(result.contains("For the ELEN department")); + assertTrue(result.contains("For the CHEM department")); + } +} diff --git a/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java new file mode 100644 index 00000000..6211c85c --- /dev/null +++ b/IndividualProject/src/test/java/dev/coms4156/project/individualproject/RouteControllerUnitTests.java @@ -0,0 +1,321 @@ +package dev.coms4156.project.individualproject; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.ResponseEntity; + +/** + * Unit tests for the RouteController class. + */ + +public class RouteControllerUnitTests { + + private RouteController routeController; + private MyFileDatabase mockDatabase; + + /** + * Sets up the test environment before each test runs. + */ + @BeforeEach + public void setup() { + mockDatabase = new MyFileDatabase(1, "testDatabase.ser"); + HashMap departments = new HashMap<>(); + departments.put("ELEN", new Department("ELEN", new HashMap<>(), "this.departmentChair", 250)); + mockDatabase.setMapping(departments); + + IndividualProjectApplication.myFileDatabase = mockDatabase; + routeController = new RouteController(); + } + + @Test + public void testIndexRoute() { + String response = routeController.index(); + assertNotNull(response); + assertTrue(response.contains("http:127.0.0.1:8080/endpoint?arg=value")); + } + + @Test + public void testRetrieveDepartmentFound() { + ResponseEntity response = routeController.retrieveDepartment("ELEN"); + assertEquals(200, response.getStatusCodeValue()); + assertTrue(response.getBody().toString().contains("this.departmentChair")); + } + + @Test + public void testRetrieveDepartmentNotFound() { + ResponseEntity response = routeController.retrieveDepartment("INVALID"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testRetrieveDepartmentWithNullCode() { + ResponseEntity response = routeController.retrieveDepartment(null); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testAddMajorToDept() { + ResponseEntity response = routeController.addMajorToDept("ELEN"); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Attribute was updated successfully", response.getBody()); + + Department updatedDepartment = mockDatabase.getDepartmentMapping().get("ELEN"); + assertEquals(251, updatedDepartment.getNumberOfMajors()); + } + + @Test + public void testAddMajorToNonExistentDept() { + ResponseEntity response = routeController.addMajorToDept("INVALID"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testRemoveMajorFromDept() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.addPersonToMajor(); // Make sure there's at least one major + ResponseEntity response = routeController.removeMajorFromDept("ELEN"); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Attribute was updated or is at minimum", response.getBody()); + + assertEquals(250, dept.getNumberOfMajors()); + } + + @Test + public void testRemoveMajorFromDeptNonExistent() { + ResponseEntity response = routeController.removeMajorFromDept("INVALID"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testChangeCourseLocationForElen() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.changeCourseLocation("ELEN", 3082, "Room 102"); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Attributed was updated successfully.", response.getBody()); + + Course updatedCourse = dept.getCourseSelection().get("3082"); + assertEquals("1205 MUDD", updatedCourse.getCourseLocation()); + } + + @Test + public void testChangeCourseLocationWithNullLocation() { + ResponseEntity response = routeController.changeCourseLocation("ELEN", 3082, null); + assertEquals(400, response.getStatusCodeValue()); + assertEquals("Invalid location", response.getBody()); + } + + @Test + public void testChangeCourseLocationWithInvalidCourse() { + ResponseEntity response = routeController.changeCourseLocation("ELEN", 999, "Room 102"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testChangeCourseLocationWithInvalidDepartment() { + ResponseEntity response = routeController.changeCourseLocation("INVALID", 3082, "Room 102"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testChangeCourseTeacherForElen() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.changeCourseTeacher("ELEN", 3082, "Keren Bergman"); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Attributed was updated successfully.", response.getBody()); + + Course updatedCourse = dept.getCourseSelection().get("3082"); + assertEquals("Keren Bergman", updatedCourse.getInstructorName()); + } + + @Test + public void testChangeCourseTeacherWithNullTeacher() { + ResponseEntity response = routeController.changeCourseTeacher("ELEN", 3082, null); + assertEquals(400, response.getStatusCodeValue()); + assertEquals("Invalid teacher", response.getBody()); + } + + @Test + public void testSetEnrollmentCountForElen() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.setEnrollmentCount("ELEN", 3082, 75); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Attributed was updated successfully.", response.getBody()); + + Course updatedCourse = dept.getCourseSelection().get("3082"); + assertEquals(75, updatedCourse.getEnrolledStudentCount()); + } + + @Test + public void testSetEnrollmentCountWithInvalidInput() { + ResponseEntity response = routeController.setEnrollmentCount("ELEN", 3082, -10); + assertEquals(400, response.getStatusCodeValue()); + assertEquals("Invalid enrollment count", response.getBody()); + } + + @Test + public void testIdentifyDeptChair() { + ResponseEntity response = routeController.identifyDeptChair("ELEN"); + assertEquals(200, response.getStatusCodeValue()); + assertTrue(response.getBody().toString().contains("this is the department chair.")); + } + + @Test + public void testIdentifyDeptChairNotFound() { + ResponseEntity response = routeController.identifyDeptChair("INVALID"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testGetMajorCtFromDept() { + ResponseEntity response = routeController.getMajorCtFromDept("ELEN"); + assertEquals(200, response.getStatusCodeValue()); + assertTrue(response.getBody().toString().contains("There are: 250 majors in the department")); + } + + @Test + public void testGetMajorCtFromDeptNotFound() { + ResponseEntity response = routeController.getMajorCtFromDept("INVALID"); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Department Not Found", response.getBody()); + } + + @Test + public void testFindCourseInstructor() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.findCourseInstructor("ELEN", 3082); + assertEquals(200, response.getStatusCodeValue()); + assertTrue(response.getBody().toString().contains("This is the instructor of course.")); + } + + @Test + public void testFindCourseInstructorNotFound() { + ResponseEntity response = routeController.findCourseInstructor("ELEN", 999); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testFindCourseLocation() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.findCourseLocation("ELEN", 3082); + assertEquals(200, response.getStatusCodeValue()); + assertTrue(response.getBody().toString().contains("1205 MUDD is where the course is located.")); + } + + @Test + public void testFindCourseLocationNotFound() { + ResponseEntity response = routeController.findCourseLocation("ELEN", 999); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testFindCourseTime() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.findCourseTime("ELEN", 3082); + assertEquals(200, response.getStatusCodeValue()); + assertTrue(response.getBody().toString().contains("The course meets at: 4:10-6:40")); + } + + @Test + public void testFindCourseTimeNotFound() { + ResponseEntity response = routeController.findCourseTime("ELEN", 999); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testChangeCourseTimeForElen() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + + ResponseEntity response = routeController.changeCourseTime("ELEN", 3082, "11:00-12:00"); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Attributed was updated successfully.", response.getBody()); + + Course updatedCourse = dept.getCourseSelection().get("3082"); + assertEquals("11:00-12:00", updatedCourse.getCourseTimeSlot()); + } + + @Test + public void testChangeCourseTimeWithNullTime() { + ResponseEntity response = routeController.changeCourseTime("ELEN", 3082, null); + assertEquals(400, response.getStatusCodeValue()); + assertEquals("Invalid time", response.getBody()); + } + + @Test + public void testDropStudentForElen() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + Course course = dept.getCourseSelection().get("3082"); + course.setEnrolledStudentCount(50); + + ResponseEntity response = routeController.dropStudent("ELEN", 3082); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("Student has been dropped.", response.getBody()); + assertEquals(49, course.getEnrolledStudentCount()); + } + + @Test + public void testDropStudentFromEmptyCourse() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 100); + Course course = dept.getCourseSelection().get("3082"); + course.setEnrolledStudentCount(0); + + ResponseEntity response = routeController.dropStudent("ELEN", 3082); + assertEquals(400, response.getStatusCodeValue()); + assertEquals("Student has not been dropped.", response.getBody()); + } + + @Test + public void testDropStudentNonExistentCourse() { + ResponseEntity response = routeController.dropStudent("ELEN", 999); + assertEquals(404, response.getStatusCodeValue()); + assertEquals("Course Not Found", response.getBody()); + } + + @Test + public void testIsCourseFull() { + Department dept = mockDatabase.getDepartmentMapping().get("ELEN"); + dept.createCourse("3082", "Kenneth Shepard", "1205 MUDD", "4:10-6:40", 250); + + Course course = dept.getCourseSelection().get("3082"); + course.setEnrolledStudentCount(250); // Full + + ResponseEntity response = routeController.isCourseFull("ELEN", 3082); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("The course is full.", response.getBody()); + + course.setEnrolledStudentCount(249); // Not full + response = routeController.isCourseFull("ELEN", 3082); + assertEquals(200, response.getStatusCodeValue()); + assertEquals("The course is not full.", response.getBody()); + } + +} diff --git a/IndividualProject/testDatabase.ser b/IndividualProject/testDatabase.ser new file mode 100644 index 00000000..e69de29b From b9cc3798d5e07c27b49e09f2a2c1ed4c5bcbd802 Mon Sep 17 00:00:00 2001 From: Jeremy Qu Date: Fri, 20 Sep 2024 17:10:33 -0400 Subject: [PATCH 2/2] I1-Jiaming Qu --- IndividualProject/honesty.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 IndividualProject/honesty.txt diff --git a/IndividualProject/honesty.txt b/IndividualProject/honesty.txt new file mode 100644 index 00000000..68041404 --- /dev/null +++ b/IndividualProject/honesty.txt @@ -0,0 +1,15 @@ +I, , have read and understood the following: + +1. CS department's Policies and Procedures on Academic Honesty +2. The Course Specific Academic Honesty Policies +3. The assignment specs outlining the consequences of not submitting this pledge and other aspects of the policy + +I affirm that I will abide by all the policies stated in the relevant materials from above. I understand that the relevant policies apply to: individual assignments, group projects, and individual examinations. + +I also affirm that I understand that all course materials, with the exception of the individual/group project, are subject to the appropriate copyrights and thus will not post them on any public forum or publicly hosted repository, this includes but is not limited to: GitHub, stackoverflow, chegg etc. + +I also affirm that I will be 100% honest when evaluating the performance of myself and my teammates when prompted by an assignment or member of the teaching staff. + +Finally I affirm that I will not attempt to find any loopholes in these policies for the benefit of myself or others enrolled in the course presently or possibly in the future. + +Signed: <09/20/2024> \ No newline at end of file