diff --git a/pom.xml b/pom.xml
index 31b4f4e..70c37c7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -50,6 +50,11 @@
hibernate-jpa-2.0-api
1.0.1.Final
+
+ org.mariadb.jdbc
+ mariadb-java-client
+ 2.5.4
+
diff --git a/src/main/java/com/github/perscholas/DatabaseConnection.java b/src/main/java/com/github/perscholas/DatabaseConnection.java
index 9e33f41..a57d979 100644
--- a/src/main/java/com/github/perscholas/DatabaseConnection.java
+++ b/src/main/java/com/github/perscholas/DatabaseConnection.java
@@ -5,11 +5,30 @@
import java.sql.Connection;
import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
/**
* Created by leon on 2/18/2020.
*/
+
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/6/2020
+ */
public enum DatabaseConnection implements DatabaseConnectionInterface {
+ /*MANAGEMENT_SYSTEM(new ConnectionBuilder()
+ .setUser("root")
+ .setPassword("5000")
+ .setPort(3300)
+ .setDatabaseVendor("mariadb")
+ .setHost("127.0.0.1")),
+ UAT(new ConnectionBuilder()
+ .setUser("root")
+ .setPassword("5000")
+ .setPort(3300)
+ .setDatabaseVendor("mariadb")
+ .setHost("127.0.0.1"));*/
MANAGEMENT_SYSTEM,
UAT;
@@ -21,12 +40,18 @@ public enum DatabaseConnection implements DatabaseConnectionInterface {
}
DatabaseConnection() {
- this(new ConnectionBuilder()
+ this(new ConnectionBuilder()
.setUser("root")
.setPassword("")
.setPort(3306)
.setDatabaseVendor("mariadb")
.setHost("127.0.0.1"));
+ /*this(new ConnectionBuilder()
+ .setUser("root")
+ .setPassword("5000")
+ .setPort(3300)
+ .setDatabaseVendor("mariadb")
+ .setHost("127.0.0.1"));*/
}
@Override
@@ -48,31 +73,88 @@ public Connection getDatabaseEngineConnection() {
@Override
public void create() {
- String sqlStatement = null; // TODO - define statement
+ //String sqlStatement = null; // TODO - define statement
+ String sqlStatement = "CREATE DATABASE IF NOT EXISTS " + getDatabaseName() + ";" ;
String info;
try {
// TODO - execute statement
+ getDatabaseEngineConnection()
+ .prepareStatement(sqlStatement)
+ .execute();
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
- info = "Failed to executed statement `%s`.";
+ info = "Failed to execute statement `%s`.";
}
console.println(info, sqlStatement);
}
@Override
public void drop() {
+ String sqlStatement = "DROP DATABASE IF EXISTS " + getDatabaseName() + ";" ;
+ String info;
+ try {
+ //execute statement
+ getDatabaseEngineConnection()
+ .prepareStatement(sqlStatement)
+ .execute();
+ info = "Successfully executed statement `%s`.";
+ } catch (Exception sqlException) {
+ info = "Failed to execute statement `%s`.";
+ }
+ console.println(info, sqlStatement);
}
@Override
public void use() {
+ String sqlStatement = "USE " + getDatabaseName() + ";" ;
+ String info;
+ try {
+ //execute statement
+ getDatabaseEngineConnection()
+ .prepareStatement(sqlStatement)
+ .execute();
+ info = "Successfully executed statement `%s`.";
+ } catch (Exception sqlException) {
+ info = "Failed to execute statement `%s`.";
+ }
+ console.println(info, sqlStatement);
}
@Override
public void executeStatement(String sqlStatement) {
+ sqlStatement = sqlStatement.trim(); //remove leading and trailing spaces
+ String info;
+ try {
+ getScrollableStatement().execute(sqlStatement);
+ info = "Successfully executed statement `%s`.";
+ } catch (Exception sqlException) {
+ info = "Failed to execute statement `%s`.";
+ }
+ console.println(info, sqlStatement);
}
@Override
public ResultSet executeQuery(String sqlQuery) {
- return null;
+ String info;
+ try{
+ sqlQuery = sqlQuery.trim();
+ ResultSet resultSet = getScrollableStatement().executeQuery(sqlQuery);
+ info = "Successfully executed query '%s'.";
+ console.println(info, sqlQuery);
+ return resultSet;
+ }catch (SQLException e){
+ info = String.format("Failed to execute query '%s'.", sqlQuery);
+ throw new Error(info, e);
+ }
+ }
+
+ public Statement getScrollableStatement() {
+ int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
+ int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+ try {
+ return getDatabaseConnection().createStatement(resultSetType, resultSetConcurrency);
+ } catch (SQLException e) {
+ throw new Error(e);
+ }
}
}
\ No newline at end of file
diff --git a/src/main/java/com/github/perscholas/JdbcConfigurator.java b/src/main/java/com/github/perscholas/JdbcConfigurator.java
index cdee602..611cffd 100644
--- a/src/main/java/com/github/perscholas/JdbcConfigurator.java
+++ b/src/main/java/com/github/perscholas/JdbcConfigurator.java
@@ -1,14 +1,23 @@
package com.github.perscholas;
+import com.github.perscholas.DatabaseConnectionInterface;
+import org.mariadb.jdbc.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
import com.github.perscholas.utils.DirectoryReference;
import com.github.perscholas.utils.FileReader;
-
+//import com.mysql.cj.jdbc.Driver;
import java.io.File;
-
+import java.sql.DriverManager;
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/6/2020
+ */
public class JdbcConfigurator {
static {
try {
// TODO - Attempt to register JDBC Driver
+ DriverManager.registerDriver(Driver.class.newInstance());
} catch (Exception e) {
throw new Error(e);
}
@@ -24,6 +33,7 @@ public static void initialize() {
executeSqlFile("courses.populate-table.sql");
executeSqlFile("students.create-table.sql");
executeSqlFile("students.populate-table.sql");
+ executeSqlFile("studentCourses.create-table.sql");
}
private static void executeSqlFile(String fileName) {
diff --git a/src/main/java/com/github/perscholas/SchoolManagementSystem.java b/src/main/java/com/github/perscholas/SchoolManagementSystem.java
index 79b55a3..d7ae86d 100644
--- a/src/main/java/com/github/perscholas/SchoolManagementSystem.java
+++ b/src/main/java/com/github/perscholas/SchoolManagementSystem.java
@@ -1,11 +1,19 @@
package com.github.perscholas;
+import com.github.perscholas.dao.CourseDao;
import com.github.perscholas.dao.StudentDao;
import com.github.perscholas.model.CourseInterface;
+import com.github.perscholas.service.CourseService;
+import com.github.perscholas.service.StudentService;
import com.github.perscholas.utils.IOConsole;
import java.util.List;
+import java.util.stream.Collectors;
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/6/2020
+ */
public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
@@ -15,7 +23,8 @@ public void run() {
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
- StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
+ //StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
+ StudentDao studentService = new StudentService();
String studentEmail = console.getStringInput("Enter your email:");
String studentPassword = console.getStringInput("Enter your password:");
Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword);
@@ -26,7 +35,8 @@ public void run() {
studentService.registerStudentToCourse(studentEmail, courseId);
String studentCourseViewInput = getCourseViewInput();
if ("view".equals(studentCourseViewInput)) {
- List courses = null; // TODO - Instantiate and populate `courses`;
+ // TODO - Instantiate and populate `courses`;
+ List courses = studentService.getStudentCourses(studentEmail);
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
@@ -65,6 +75,12 @@ private String getStudentDashboardInput() {
private Integer getCourseRegistryInput() {
List listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
+ CourseDao courseService = new CourseService();
+ List courses = courseService.getAllCourses();
+ listOfCoursesIds = courses.stream()
+ .map(CourseInterface::toString)
+ .collect(Collectors.toList());
+
return console.getIntegerInput(new StringBuilder()
.append("Welcome to the Course Registration Dashboard!")
.append("\nFrom here, you can select any of the following options:")
diff --git a/src/main/java/com/github/perscholas/dao/StudentDao.java b/src/main/java/com/github/perscholas/dao/StudentDao.java
index ed96d90..3307fe5 100644
--- a/src/main/java/com/github/perscholas/dao/StudentDao.java
+++ b/src/main/java/com/github/perscholas/dao/StudentDao.java
@@ -3,6 +3,7 @@
import com.github.perscholas.model.CourseInterface;
import com.github.perscholas.model.StudentInterface;
+import java.sql.SQLException;
import java.util.List;
/**
diff --git a/src/main/java/com/github/perscholas/model/Course.java b/src/main/java/com/github/perscholas/model/Course.java
index 930aece..10f0af7 100644
--- a/src/main/java/com/github/perscholas/model/Course.java
+++ b/src/main/java/com/github/perscholas/model/Course.java
@@ -1,5 +1,85 @@
package com.github.perscholas.model;
// TODO - Annotate and Implement respective interface and define behaviors
-public class Course {
+
+import javax.persistence.*;
+import java.util.Objects;
+
+/**
+ * Implemented by Monica Deshmukh
+ * 9/6/2020
+ */
+@Entity
+@Table(name="Course")
+public class Course implements CourseInterface {
+ @Id
+ private Integer id;
+
+ @Column
+ @Basic
+ private String name;
+
+ @Column
+ @Basic
+ private String instructor;
+
+ /*@ManyToMany(mappedBy = "sCourses") //maps to the list object in the Student class
+ List studentCoursesList;*/
+
+ //nullary constructor
+ public Course(){
+ this(null,null,null);
+ }
+
+ //non-nullary constructor
+ public Course(Integer id, String name, String instructor){
+ this.id = id;
+ this.name = name;
+ this.instructor = instructor;
+ }
+
+ @Override
+ public Integer getId() {
+ return id;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String getInstructor() {
+ return instructor;
+ }
+
+ @Override
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public void setInstructor(String instructor) {
+ this.instructor = instructor;
+ }
+
+ @Override
+ public String toString() {
+ return "[Id: " + id + " Name: " + name + " Instructor: " + instructor + "]";
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Course)) return false;
+ Course course = (Course) o;
+ return Objects.equals(id, course.id) &&
+ Objects.equals(name, course.name) &&
+ Objects.equals(instructor, course.instructor);
+ }
}
diff --git a/src/main/java/com/github/perscholas/model/Student.java b/src/main/java/com/github/perscholas/model/Student.java
index 94db67f..f1186cb 100644
--- a/src/main/java/com/github/perscholas/model/Student.java
+++ b/src/main/java/com/github/perscholas/model/Student.java
@@ -1,5 +1,101 @@
package com.github.perscholas.model;
// TODO - Annotate and Implement respective interface and define behaviors
-public class Student {
+
+import javax.persistence.*;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Implemnted by Monica Deshmukh
+ * 9/6/2020
+ */
+@Entity
+@Table(name="Student")
+public class Student implements StudentInterface{
+ @Id
+ private String email;
+
+ @Column
+ @Basic
+ private String name;
+
+ @Column
+ @Basic
+ private String password;
+
+ /*@ManyToMany
+ @JoinTable(
+ name = "StudentCourses",
+ joinColumns = @JoinColumn(name = "email"),
+ inverseJoinColumns = @JoinColumn(name = "id"))*/
+ private List sCourses;
+
+ //nullary constructor
+ public Student(){
+ this(null,null,null);
+ }
+
+ //non-nullary constructors
+ public Student(String email, String name, String password){
+ this(email, name, password,null);
+ }
+
+ public Student(String email, String name, String password, List sCourses){
+ this.email = email;
+ this.name = name;
+ this.password = password;
+ this.sCourses = sCourses;
+ }
+
+
+ @Override
+ public String getEmail() {
+ return email;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public void setCourses(List sCourses) {this.sCourses = sCourses;}
+
+ public List getCourses() {return this.sCourses;}
+
+ @Override
+ public String toString() {
+ return "[ Email: " + email + " Name: " + name + " Password: " + password + "]";
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Student)) return false;
+ Student student = (Student) o;
+ return email.equals(student.email) &&
+ Objects.equals(name, student.name) &&
+ Objects.equals(password, student.password);
+ }
+
}
diff --git a/src/main/java/com/github/perscholas/service/CourseService.java b/src/main/java/com/github/perscholas/service/CourseService.java
index 86e33e2..c4d1c56 100644
--- a/src/main/java/com/github/perscholas/service/CourseService.java
+++ b/src/main/java/com/github/perscholas/service/CourseService.java
@@ -1,5 +1,50 @@
package com.github.perscholas.service;
// TODO - Implement respective DAO interface
-public class CourseService {
+
+import com.github.perscholas.DatabaseConnection;
+import com.github.perscholas.dao.CourseDao;
+import com.github.perscholas.model.Course;
+import com.github.perscholas.model.CourseInterface;
+import com.github.perscholas.model.Student;
+import com.github.perscholas.model.StudentInterface;
+
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * TODO implemneted by Monica Deshmukh
+ * 9/6/2020
+ */
+
+public class CourseService implements CourseDao {
+ private final DatabaseConnection dbc;
+
+ public CourseService(DatabaseConnection dbc) {
+ this.dbc = dbc;
+ }
+
+ //changed for testing purpose..revert the comment beofore submitting************************************
+ /* public StudentService() {
+ this(DatabaseConnection.UAT);
+ }*/
+ public CourseService() {
+ this(DatabaseConnection.MANAGEMENT_SYSTEM);
+ }
+ @Override
+ public List getAllCourses() {
+ ResultSet resultSet = dbc.executeQuery("SELECT * FROM Course");
+ try {
+ List courses = new ArrayList<>();
+ while (resultSet.next()) {
+ courses.add(new Course(resultSet.getInt("id"),
+ resultSet.getString("name"),
+ resultSet.getString("instructor")));
+ }
+ return courses;
+ } catch(Exception e) {
+ throw new Error(e);
+ }
+ }
}
diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java
index 0751a9c..12c44be 100644
--- a/src/main/java/com/github/perscholas/service/StudentService.java
+++ b/src/main/java/com/github/perscholas/service/StudentService.java
@@ -2,16 +2,29 @@
import com.github.perscholas.DatabaseConnection;
import com.github.perscholas.dao.StudentDao;
+import com.github.perscholas.model.Course;
import com.github.perscholas.model.CourseInterface;
import com.github.perscholas.model.Student;
import com.github.perscholas.model.StudentInterface;
+import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+
// TODO - Implement respective DAO interface
+
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/6/2020
+ */
+
+
public class StudentService implements StudentDao {
private final DatabaseConnection dbc;
@@ -19,15 +32,30 @@ public StudentService(DatabaseConnection dbc) {
this.dbc = dbc;
}
- public StudentService() {
+ //changed for testing purpose..revert the comment beofore submitting************************************
+ /* public StudentService() {
this(DatabaseConnection.UAT);
+ }*/
+ public StudentService() {
+ this(DatabaseConnection.MANAGEMENT_SYSTEM);
}
+ /**
+ * reads the student table in database
+ * @return database data as a List
+ */
@Override
public List getAllStudents() {
- ResultSet resultSet = dbc.executeQuery("SELECT * FROM students");
+ ResultSet resultSet = dbc.executeQuery("SELECT * FROM Student");
try {
- return null; // TODO - Parse `List` from `resultSet`
+ // TODO - Parse `List` from `resultSet`
+ List students = new ArrayList<>();
+ while (resultSet.next()) {
+ students.add(new Student(resultSet.getString("email"),
+ resultSet.getString("name"),
+ resultSet.getString("password")));
+ }
+ return students;
} catch(Exception e) {
throw new Error(e);
}
@@ -35,21 +63,93 @@ public List getAllStudents() {
@Override
public StudentInterface getStudentByEmail(String studentEmail) {
- return null;
+ try{
+ return getAllStudents()
+ .stream()
+ .filter(student -> student.getEmail().equals(studentEmail))
+ .findFirst()
+ .get();
+ }catch(NullPointerException| NoSuchElementException e){
+ System.out.println("Email address does not exist. Please enter valid email.");
+ return null;
+ }
}
@Override
public Boolean validateStudent(String studentEmail, String password) {
- return null;
+ /*Boolean studentExists = getAllStudents()
+ .stream()
+ .anyMatch(student -> student.getEmail().equals(studentEmail) && student.getPassword().equals(password));
+ if (!studentExists)
+ System.out.println("No entry found for this student. Please enter valid email and password.");
+ return studentExists;*/
+ //Leon's approach
+ try {
+ Boolean studentExists = password.equals(Objects.requireNonNull(getStudentByEmail(studentEmail)).getPassword());
+ if (!studentExists)
+ System.out.println("No entry found for this student. Please enter valid email and password.");
+ return studentExists;
+ }catch (NullPointerException e){
+ System.out.println("No entry found for this student. Please enter valid email and password.");
+ }
+ return false;
}
@Override
public void registerStudentToCourse(String studentEmail, int courseId) {
+ List registeredCourses = getStudentCourses(studentEmail);
+
+ //If the user selects an invalid course ID give error message
+ CourseService courseService = new CourseService();
+ List courses = courseService.getAllCourses();
+ if (!courses.stream().anyMatch(course -> course.getId().equals(courseId)))
+ System.out.println("This is not a valid courseId. Please choose a valid course from the given list.");
+ else {
+ if (registeredCourses != null && registeredCourses.stream().anyMatch(thisCourse -> thisCourse.getId().equals(courseId))) {
+ System.out.println("The Student is already registered for this course.");
+ }
+ else {
+ registerForCourse(studentEmail, courseId);
+ //display registered courses after successfully registering to the course
+ registeredCourses = getStudentCourses(studentEmail);
+ System.out.println("[ " + studentEmail + " ] is registered for the following courses: " + registeredCourses.toString());
+ }
+ }
}
@Override
public List getStudentCourses(String studentEmail) {
- return null;
+ List courses = new ArrayList<>();
+
+ try{
+ String statement = "SELECT c.id, c.name, c.instructor " +
+ "FROM Course c, StudentCourses sc " +
+ "WHERE sc.studentEmail = '" + studentEmail +"' " +
+ "AND sc.courseId = c.id";
+ ResultSet resultSet = dbc.executeQuery(statement);
+ while (resultSet.next()) {
+ courses.add(new Course(resultSet.getInt("c.id"),
+ resultSet.getString("c.name"),
+ resultSet.getString("c.instructor")));
+ }
+ return courses;
+ } catch(Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException();
+ }
+ }
+
+ private void registerForCourse(String studentEmail, int courseId) {
+ try{
+ String registerCourse = "INSERT INTO StudentCourses VALUES (?, ?)";
+ PreparedStatement insertStatement = dbc.getDatabaseConnection().prepareStatement(registerCourse);
+ insertStatement.setString(1, studentEmail);
+ insertStatement.setInt(2, courseId);
+ insertStatement.execute();
+ }catch(SQLException e){
+ System.out.println("Unable to register for this course.");
+ e.printStackTrace();
+ }
}
}
diff --git a/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java b/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java
index 745e5a9..19b528e 100644
--- a/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java
+++ b/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java
@@ -7,6 +7,11 @@
/**
* Created by leon on 3/13/18.
*/
+
+/**
+ * toString method updated. hasPortbeenSet statement corrected.
+ * Monica Deshmukh 9/7/2020
+ */
public class ConnectionBuilder {
private Integer portNumber;
private String userName;
@@ -65,7 +70,11 @@ public Connection build() {
public String toString() {
Boolean isHostNull = this.hostName == null;
Boolean isPortNull = portNumber == null;
- Boolean hasPortBeenSet = !isPortNull && !Integer.valueOf(3306).equals(portNumber);
+ // Boolean hasPortBeenSet = !isPortNull && !Integer.valueOf(3306).equals(portNumber);
+
+ //using port no.3300 for my database. Also the second value in the && operation should check if portNumber==3300
+ //Boolean hasPortBeenSet = !isPortNull && Integer.valueOf(3300).equals(portNumber);
+ Boolean hasPortBeenSet = !isPortNull && Integer.valueOf(3306).equals(portNumber);
String jdbcUrl = new StringBuilder()
.append("jdbc:")
@@ -78,6 +87,7 @@ public String toString() {
.append("/")
.append(databaseName != null ? databaseName : "")
.toString();
+ //System.out.println("jdbcUrl: " + jdbcUrl);
return jdbcUrl;
}
}
\ No newline at end of file
diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml
index f83b594..257ee34 100644
--- a/src/main/resources/META-INF/persistence.xml
+++ b/src/main/resources/META-INF/persistence.xml
@@ -6,9 +6,37 @@ http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
com.github.perscholas.model.Student
com.github.perscholas.model.Course
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/courses.create-table.sql b/src/main/resources/courses.create-table.sql
index e69de29..4c7dabd 100644
--- a/src/main/resources/courses.create-table.sql
+++ b/src/main/resources/courses.create-table.sql
@@ -0,0 +1,6 @@
+/*Course Table*/
+CREATE TABLE Course(
+id int NOT NULL PRIMARY KEY,
+name varchar(50) NOT NULL,
+instructor varchar(50) NOT NULL
+);
\ No newline at end of file
diff --git a/src/main/resources/studentCourses.create-table.sql b/src/main/resources/studentCourses.create-table.sql
new file mode 100644
index 0000000..260c83a
--- /dev/null
+++ b/src/main/resources/studentCourses.create-table.sql
@@ -0,0 +1,7 @@
+CREATE TABLE StudentCourses(
+studentEmail varchar(50) NOT NULL,
+courseId int NOT NULL,
+FOREIGN KEY(studentEmail) REFERENCES Student(email),
+FOREIGN KEY(courseId) REFERENCES Course(id),
+UNIQUE(studentEmail, courseId)
+);
\ No newline at end of file
diff --git a/src/main/resources/students.create-table.sql b/src/main/resources/students.create-table.sql
index e69de29..67a6b12 100644
--- a/src/main/resources/students.create-table.sql
+++ b/src/main/resources/students.create-table.sql
@@ -0,0 +1,5 @@
+CREATE TABLE Student(
+email varchar(50) NOT NULL PRIMARY KEY,
+name varchar(50) NOT NULL,
+password VARCHAR(50) NOT NULL);
+
diff --git a/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java b/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java
index f7e80b4..fc414a2 100644
--- a/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java
+++ b/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java
@@ -1,19 +1,30 @@
package com.github.perscholas.service.studentservice;
+import com.github.perscholas.DatabaseConnection;
import com.github.perscholas.JdbcConfigurator;
import com.github.perscholas.dao.StudentDao;
+import com.github.perscholas.model.Student;
import com.github.perscholas.model.StudentInterface;
import com.github.perscholas.service.StudentService;
import com.github.perscholas.utils.DirectoryReference;
+import org.junit.Assert;
import org.junit.Before;
+import org.junit.Test;
import java.io.File;
+import java.sql.ResultSet;
+import java.util.ArrayList;
import java.util.List;
/**
* @author leonhunter
* @created 02/12/2020 - 8:22 PM
*/
+
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/7/2020
+ */
public class GetAllStudentsTest {
@Before // TODO (OPTIONAL) - Use files to execute SQL commands
public void setup() {
@@ -32,14 +43,31 @@ public void setup() {
// given
// TODO - Add `@Test` annotation
+ @Test
public void test() {
JdbcConfigurator.initialize();
StudentDao service = (StudentDao) new StudentService();
+ DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM;
// when
List studentList = service.getAllStudents();
// then
// TODO - define _then_ clause
+ ResultSet resultSet = dbc.executeQuery("SELECT * FROM Student");
+ try {
+ // TODO - Parse `List` from `resultSet`
+ List studentsInDatabase = new ArrayList<>();
+ while (resultSet.next()) {
+ studentsInDatabase.add(new Student(resultSet.getString("email"),
+ resultSet.getString("name"),
+ resultSet.getString("password")));
+ }
+ //since we are comparing two Lists, we use assertArrayEquals, so that it compares each element in the two lists
+ Assert.assertArrayEquals(studentList.toArray(), studentsInDatabase.toArray());
+ } catch(Exception e) {
+ e.printStackTrace();
+ throw new Error(e);
+ }
}
}
diff --git a/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java b/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java
index 3f0e7e4..2bbfc16 100644
--- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java
+++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java
@@ -1,15 +1,30 @@
package com.github.perscholas.service.studentservice;
+import com.github.perscholas.DatabaseConnection;
import com.github.perscholas.JdbcConfigurator;
+import com.github.perscholas.dao.StudentDao;
+import com.github.perscholas.model.Student;
+import com.github.perscholas.model.StudentInterface;
+import com.github.perscholas.service.StudentService;
import com.github.perscholas.utils.DirectoryReference;
+import org.junit.Assert;
import org.junit.Before;
+import org.junit.Test;
import java.io.File;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.List;
/**
* @author leonhunter
* @created 02/12/2020 - 8:23 PM
*/
+
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/7/2020
+ */
public class GetStudentByEmailTest {
@Before // TODO (OPTIONAL) - Use files to execute SQL commands
public void setup() {
@@ -28,14 +43,32 @@ public void setup() {
// given
// TODO - Add `@Test` annotation
+ @Test
public void test() {
JdbcConfigurator.initialize();
+ StudentDao service = (StudentDao) new StudentService();
+ DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM;
// when
// TODO - define `when` clause
-
+ List studentList = service.getAllStudents();
+ String email = studentList.get(0).getEmail();
// then
// TODO - define `then` clause
+ ResultSet resultSet = dbc.executeQuery("SELECT * FROM Student WHERE email = '" + email +"'");
+ try {
+ // TODO - Parse `List` from `resultSet`
+ List studentInDatabase = new ArrayList<>();
+ //supposed to return one record
+ while (resultSet.next()) {
+ studentInDatabase.add(new Student(resultSet.getString("email"),
+ resultSet.getString("name"),
+ resultSet.getString("password")));
+ }
+ Assert.assertTrue(studentList.get(0).equals(studentInDatabase.get(0)));
+ } catch(Exception e) {
+ throw new Error(e);
+ }
}
}
diff --git a/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java b/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java
index a037077..9528039 100644
--- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java
+++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java
@@ -1,15 +1,31 @@
package com.github.perscholas.service.studentservice;
+import com.github.perscholas.DatabaseConnection;
import com.github.perscholas.JdbcConfigurator;
+import com.github.perscholas.dao.StudentDao;
+import com.github.perscholas.model.Course;
+import com.github.perscholas.model.CourseInterface;
+import com.github.perscholas.model.Student;
+import com.github.perscholas.model.StudentInterface;
+import com.github.perscholas.service.StudentService;
import com.github.perscholas.utils.DirectoryReference;
+import org.junit.Assert;
import org.junit.Before;
+import org.junit.Test;
import java.io.File;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.List;
/**
* @author leonhunter
* @created 02/12/2020 - 8:25 PM
*/
+/**
+ * TODO implemented by Monica Deshmukh
+ * 9/7/2020
+ */
public class GetStudentCoursesTest {
@Before // TODO (OPTIONAL) - Use files to execute SQL commands
public void setup() {
@@ -28,14 +44,36 @@ public void setup() {
// given
// TODO - Add `@Test` annotation
+ @Test
public void test() {
JdbcConfigurator.initialize();
+ StudentDao service = (StudentDao) new StudentService();
+ DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM;
// when
// TODO - define `when` clause
-
+ List studentList = service.getAllStudents();
+ String email = studentList.get(0).getEmail();
+ List registeredCourses = service.getStudentCourses(email);
// then
// TODO - define `then` clause
+ try{
+ List expectedCourses = new ArrayList<>();
+ String statement = "SELECT c.id, c.name, c.instructor " +
+ "FROM Course c, StudentCourses sc " +
+ "WHERE sc.studentEmail = '" + email +"' " +
+ "AND sc.courseId = c.id";
+ ResultSet resultSet = dbc.executeQuery(statement);
+ while (resultSet.next()) {
+ expectedCourses.add(new Course(resultSet.getInt("c.id"),
+ resultSet.getString("c.name"),
+ resultSet.getString("c.instructor")));
+ }
+ Assert.assertArrayEquals(registeredCourses.toArray(),expectedCourses.toArray());
+ } catch(Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException();
+ }
}
}