diff --git a/pom.xml b/pom.xml
index 31b4f4e..0936a54 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,7 +26,7 @@
mysql
mysql-connector-java
- 8.0.18
+ 8.0.19
diff --git a/src/main/java/com/github/perscholas/DatabaseConnection.java b/src/main/java/com/github/perscholas/DatabaseConnection.java
index 9e33f41..b922291 100644
--- a/src/main/java/com/github/perscholas/DatabaseConnection.java
+++ b/src/main/java/com/github/perscholas/DatabaseConnection.java
@@ -5,6 +5,8 @@
import java.sql.Connection;
import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
/**
* Created by leon on 2/18/2020.
@@ -23,9 +25,9 @@ public enum DatabaseConnection implements DatabaseConnectionInterface {
DatabaseConnection() {
this(new ConnectionBuilder()
.setUser("root")
- .setPassword("")
+ .setPassword("Password")
.setPort(3306)
- .setDatabaseVendor("mariadb")
+ .setDatabaseVendor("mysql")
.setHost("127.0.0.1"));
}
@@ -48,10 +50,10 @@ public Connection getDatabaseEngineConnection() {
@Override
public void create() {
- String sqlStatement = null; // TODO - define statement
+ String sqlStatement = "CREATE DATABASE IF NOT EXISTS " + getDatabaseName() + ";";
String info;
try {
- // TODO - execute statement
+ executeStatement(sqlStatement);
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to executed statement `%s`.";
@@ -61,18 +63,44 @@ public void create() {
@Override
public void drop() {
+ String sqlStatement = "DROP DATABASE IF EXISTS " + getDatabaseName() + ";";
+ executeStatement(sqlStatement);
}
@Override
public void use() {
+ String sqlStatement = "USE " + getDatabaseName() + ";";
+ executeStatement(sqlStatement);
}
@Override
public void executeStatement(String sqlStatement) {
+ try {
+ Statement statement = getScrollableStatement(getDatabaseConnection());
+ statement.execute(sqlStatement);
+ statement.close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
}
@Override
public ResultSet executeQuery(String sqlQuery) {
- return null;
+ try {
+ Statement statement = getScrollableStatement(getDatabaseConnection());
+ return statement.executeQuery(sqlQuery);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Statement getScrollableStatement(Connection connection) {
+ int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
+ int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+ try {
+ return connection.createStatement(resultSetType, resultSetConcurrency);
+ } catch (SQLException e) {
+ throw new RuntimeException(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..11defe3 100644
--- a/src/main/java/com/github/perscholas/JdbcConfigurator.java
+++ b/src/main/java/com/github/perscholas/JdbcConfigurator.java
@@ -4,11 +4,13 @@
import com.github.perscholas.utils.FileReader;
import java.io.File;
+import java.sql.Driver;
+import java.sql.DriverManager;
public class JdbcConfigurator {
static {
try {
- // TODO - Attempt to register JDBC Driver
+ Class.forName(Driver.class.getName());
} catch (Exception e) {
throw new Error(e);
}
@@ -24,6 +26,9 @@ public static void initialize() {
executeSqlFile("courses.populate-table.sql");
executeSqlFile("students.create-table.sql");
executeSqlFile("students.populate-table.sql");
+ executeSqlFile("studentcourse.create-table.sql");
+ executeSqlFile("studentcourse.populate-table.sql");
+ System.out.println("Initialized successfully");
}
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..201c3b9 100644
--- a/src/main/java/com/github/perscholas/SchoolManagementSystem.java
+++ b/src/main/java/com/github/perscholas/SchoolManagementSystem.java
@@ -2,9 +2,12 @@
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;
public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
@@ -15,7 +18,7 @@ public void run() {
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
- StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
+ StudentDao studentService = new StudentService(DatabaseConnection.MANAGEMENT_SYSTEM);
String studentEmail = console.getStringInput("Enter your email:");
String studentPassword = console.getStringInput("Enter your password:");
Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword);
@@ -26,7 +29,7 @@ public void run() {
studentService.registerStudentToCourse(studentEmail, courseId);
String studentCourseViewInput = getCourseViewInput();
if ("view".equals(studentCourseViewInput)) {
- List courses = null; // 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)
@@ -64,7 +67,11 @@ private String getStudentDashboardInput() {
private Integer getCourseRegistryInput() {
- List listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
+ List courseInterfaces = new CourseService().getAllCourses();
+ List listOfCoursesIds = courseInterfaces
+ .stream()
+ .map(courseInterface -> courseInterface.getId().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/model/Course.java b/src/main/java/com/github/perscholas/model/Course.java
index 930aece..a4a3ec8 100644
--- a/src/main/java/com/github/perscholas/model/Course.java
+++ b/src/main/java/com/github/perscholas/model/Course.java
@@ -1,5 +1,58 @@
package com.github.perscholas.model;
-// TODO - Annotate and Implement respective interface and define behaviors
-public class Course {
+import javax.persistence.*;
+
+@Entity
+@Table(name = "course")
+public class Course implements CourseInterface{
+
+ @Id
+ @Column(name = "id", nullable = false)
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private int id;
+
+ @Column(name = "name", nullable = false, length = 50)
+ private String name;
+
+ @Column(name = "instructor", nullable = false, length = 50)
+ private String instructor;
+
+ public Course() {
+ }
+
+ public Course(int 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;
+ }
}
diff --git a/src/main/java/com/github/perscholas/model/Student.java b/src/main/java/com/github/perscholas/model/Student.java
index 94db67f..ee4d1ff 100644
--- a/src/main/java/com/github/perscholas/model/Student.java
+++ b/src/main/java/com/github/perscholas/model/Student.java
@@ -1,5 +1,58 @@
package com.github.perscholas.model;
-// TODO - Annotate and Implement respective interface and define behaviors
-public class Student {
+import javax.persistence.*;
+
+@Entity
+@Table(name = "student")
+public class Student implements StudentInterface{
+
+ @Id
+ @Column(name = "email", nullable = false, length = 50)
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private String email;
+
+ @Column(name = "name", nullable = false, length = 50)
+ private String name;
+
+ @Column(name = "password", nullable = false, length = 50)
+ private String password;
+
+ public Student() {
+ }
+
+ public Student(String email, String name, String password) {
+ this.email = email;
+ this.name = name;
+ this.password = password;
+ }
+
+ @Override
+ public String getEmail() {
+ return email;
+ }
+
+ @Override
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public void setPassword(String password) {
+ this.password = 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..c95b83b 100644
--- a/src/main/java/com/github/perscholas/service/CourseService.java
+++ b/src/main/java/com/github/perscholas/service/CourseService.java
@@ -1,5 +1,43 @@
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 java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+public class CourseService implements CourseDao {
+
+ private final DatabaseConnection dbc;
+
+ public CourseService(DatabaseConnection dbc) {
+ this.dbc = dbc;
+ }
+
+ public CourseService() {
+ this(DatabaseConnection.UAT);
+ }
+
+ @Override
+ public List getAllCourses() {
+ List courseList = new ArrayList<>();
+ ResultSet resultSet = dbc.executeQuery("SELECT * FROM Course;");
+
+ try {
+ do {
+ courseList.add(new Course(resultSet.getInt("id"),
+ resultSet.getString("name"),
+ resultSet.getString("instructor")));
+
+ } while (Objects.requireNonNull(resultSet).next());
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return courseList;
+ }
}
diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java
index 0751a9c..edf0c87 100644
--- a/src/main/java/com/github/perscholas/service/StudentService.java
+++ b/src/main/java/com/github/perscholas/service/StudentService.java
@@ -2,16 +2,16 @@
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.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
-// TODO - Implement respective DAO interface
public class StudentService implements StudentDao {
private final DatabaseConnection dbc;
@@ -25,31 +25,56 @@ public StudentService() {
@Override
public List getAllStudents() {
- ResultSet resultSet = dbc.executeQuery("SELECT * FROM students");
+ List studentList = new ArrayList<>();
+ ResultSet resultSet = dbc.executeQuery("SELECT * FROM Student;");
+
try {
- return null; // TODO - Parse `List` from `resultSet`
- } catch(Exception e) {
- throw new Error(e);
+ do {
+ studentList.add(new Student(resultSet.getString("email"),
+ resultSet.getString("name"),
+ resultSet.getString("password")));
+
+ } while (Objects.requireNonNull(resultSet).next());
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
}
+ return studentList;
}
@Override
public StudentInterface getStudentByEmail(String studentEmail) {
- return null;
+ return getAllStudents().stream()
+ .filter(studentInterface -> studentInterface.getEmail().equals(studentEmail))
+ .findAny()
+ .get();
}
@Override
public Boolean validateStudent(String studentEmail, String password) {
- return null;
+ return password.equals(Objects.requireNonNull(getStudentByEmail(studentEmail)).getPassword());
}
@Override
public void registerStudentToCourse(String studentEmail, int courseId) {
-
+ dbc.executeStatement("insert into StudentCourse(studentEmail, courseId) values (" + studentEmail + ", " + courseId + ");");
}
@Override
public List getStudentCourses(String studentEmail) {
- return null;
+ List list = new ArrayList<>();
+ ResultSet resultSet = dbc.executeQuery("SELECT c.id, c.name, c.instructor FROM course c, intermediate i WHERE i.student_email = '" + studentEmail + "' AND i.course_id = c.id;");
+
+ try {
+ do {
+ list.add(new Course(resultSet.getInt("id"),
+ resultSet.getString("name"),
+ resultSet.getString("instructor")) {
+ });
+
+ } while (Objects.requireNonNull(resultSet).next());
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return list;
}
}
diff --git a/src/main/resources/courses.create-table.sql b/src/main/resources/courses.create-table.sql
index e69de29..158e053 100644
--- a/src/main/resources/courses.create-table.sql
+++ b/src/main/resources/courses.create-table.sql
@@ -0,0 +1,5 @@
+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/studentcourse.create-table.sql b/src/main/resources/studentcourse.create-table.sql
new file mode 100644
index 0000000..879c619
--- /dev/null
+++ b/src/main/resources/studentcourse.create-table.sql
@@ -0,0 +1,6 @@
+CREATE table StudentCourse(
+ studentEmail varchar(50) not null,
+ foreign key (studentEmail) references Student(email),
+ courseId int not null,
+ foreign key (courseId) references Course(id)
+)
\ No newline at end of file
diff --git a/src/main/resources/studentcourse.populate-table.sql b/src/main/resources/studentcourse.populate-table.sql
new file mode 100644
index 0000000..6238874
--- /dev/null
+++ b/src/main/resources/studentcourse.populate-table.sql
@@ -0,0 +1,6 @@
+insert into StudentCourse (studentEmail, courseId) values ('davidkburchett@gmail.com', 1);
+insert into StudentCourse (studentEmail, courseId) values ('davidkburchett@gmail.com', 2);
+insert into StudentCourse (studentEmail, courseId) values ('davidkburchett@gmail.com', 3);
+insert into StudentCourse (studentEmail, courseId) values ('davidkburchett@gmail.com', 4);
+insert into StudentCourse (studentEmail, courseId) values ('cjaulme9@bing.com', 2);
+insert into StudentCourse (studentEmail, courseId) values ('cjaulme9@bing.com', 8);
diff --git a/src/main/resources/students.create-table.sql b/src/main/resources/students.create-table.sql
index e69de29..e5bb8e0 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
+)
\ No newline at end of file
diff --git a/src/main/resources/students.populate-table.sql b/src/main/resources/students.populate-table.sql
index e352727..36384da 100644
--- a/src/main/resources/students.populate-table.sql
+++ b/src/main/resources/students.populate-table.sql
@@ -7,4 +7,5 @@ insert into Student (email, name, password) values ('hguerre5@deviantart.com', '
insert into Student (email, name, password) values ('htaffley6@columbia.edu', 'Holmes Taffley', 'xowtOQ');
insert into Student (email, name, password) values ('aiannitti7@is.gd', 'Alexandra Iannitti', 'TWP4hf5j');
insert into Student (email, name, password) values ('ljiroudek8@sitemeter.com', 'Laryssa Jiroudek', 'bXRoLUP');
-insert into Student (email, name, password) values ('cjaulme9@bing.com', 'Cahra Jaulme', 'FnVklVgC6r6');
\ No newline at end of file
+insert into Student (email, name, password) values ('cjaulme9@bing.com', 'Cahra Jaulme', 'FnVklVgC6r6');
+insert into Student (email, name, password) values ('davidkburchett@gmail.com', 'Kyle Burchett', 'password');
\ No newline at end of file
diff --git a/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java b/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java
index fdf4686..ae69749 100644
--- a/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java
+++ b/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java
@@ -1,10 +1,16 @@
package com.github.perscholas.service.courseservice;
import com.github.perscholas.JdbcConfigurator;
+import com.github.perscholas.dao.CourseDao;
+import com.github.perscholas.model.CourseInterface;
+import com.github.perscholas.service.CourseService;
import com.github.perscholas.utils.DirectoryReference;
+import org.junit.Assert;
import org.junit.Before;
+import org.junit.Test;
import java.io.File;
+import java.util.List;
/**
* @author leonhunter
@@ -27,14 +33,14 @@ public void setup() {
}
// given
- private void test() {
- JdbcConfigurator.initialize();
+ @Test
+ public void numberOfCoursesTest() {
+ CourseDao service = new CourseService();
// when
- // TODO - define `when` clause
-
+ List courselist = service.getAllCourses();
// then
- // TODO - define `then` clause
+ Assert.assertEquals(courselist.size(), 10);
}
-}
+}
\ No newline at end of file
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..282aa9a 100644
--- a/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java
+++ b/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java
@@ -5,7 +5,9 @@
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.util.List;
@@ -31,15 +33,15 @@ public void setup() {
}
// given
- // TODO - Add `@Test` annotation
- public void test() {
+ @Test
+ public void numberOfStudentsTest() {
JdbcConfigurator.initialize();
- StudentDao service = (StudentDao) new StudentService();
+ StudentDao service = new StudentService();
// when
List studentList = service.getAllStudents();
// then
- // TODO - define _then_ clause
+ Assert.assertEquals(studentList.size(), 10);
}
}
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..e20348e 100644
--- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java
+++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java
@@ -1,10 +1,17 @@
package com.github.perscholas.service.studentservice;
import com.github.perscholas.JdbcConfigurator;
+import com.github.perscholas.dao.StudentDao;
+import com.github.perscholas.model.StudentInterface;
+import com.github.perscholas.service.CourseService;
+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.util.List;
/**
* @author leonhunter
@@ -27,15 +34,17 @@ public void setup() {
}
// given
- // TODO - Add `@Test` annotation
- public void test() {
+ @Test
+ public void getStudentByEmailTest() {
JdbcConfigurator.initialize();
+ StudentDao service = new StudentService();
// when
- // TODO - define `when` clause
-
+ StudentInterface student = service.getStudentByEmail("davidkburchett@gmail.com");
// then
- // TODO - define `then` clause
+ Assert.assertEquals(student.getEmail(), "davidkburchett@gmail.com");
+ Assert.assertEquals(student.getName(), "Kyle Burchett");
+ Assert.assertEquals(student.getPassword(), "Password");
}
}
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..204b0ec 100644
--- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java
+++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java
@@ -1,10 +1,17 @@
package com.github.perscholas.service.studentservice;
import com.github.perscholas.JdbcConfigurator;
+import com.github.perscholas.dao.StudentDao;
+import com.github.perscholas.model.CourseInterface;
+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.util.List;
/**
* @author leonhunter
@@ -27,15 +34,18 @@ public void setup() {
}
// given
- // TODO - Add `@Test` annotation
- public void test() {
+ @Test
+ public void getStudentCoursesTest() {
JdbcConfigurator.initialize();
+ StudentDao service = new StudentService();
// when
- // TODO - define `when` clause
-
+ List courses = service.getStudentCourses("davidkburchett@gmail.com");
// then
- // TODO - define `then` clause
+ Assert.assertEquals(courses.get(0).getName(), "English");
+ Assert.assertEquals(courses.get(1).getName(), "Mathematics");
+ Assert.assertEquals(courses.get(2).getName(), "Anatomy");
+ Assert.assertEquals(courses.get(3).getName(), "Organic Chemistry");
}
}