From 6269d26692bf9b83d9596e657ffd2437bc38cfb3 Mon Sep 17 00:00:00 2001 From: issagodinez Date: Thu, 3 Sep 2020 12:38:30 -0400 Subject: [PATCH 1/8] Part 1 Complete --- src/main/java/com/github/perscholas/model/Course.java | 4 ++++ src/main/resources/courses.create-table.sql | 5 +++++ src/main/resources/students.create-table.sql | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/src/main/java/com/github/perscholas/model/Course.java b/src/main/java/com/github/perscholas/model/Course.java index 930aece..12b71f3 100644 --- a/src/main/java/com/github/perscholas/model/Course.java +++ b/src/main/java/com/github/perscholas/model/Course.java @@ -1,5 +1,9 @@ package com.github.perscholas.model; +import javax.persistence.*; +import java.util.*; // TODO - Annotate and Implement respective interface and define behaviors public class Course { + + } diff --git a/src/main/resources/courses.create-table.sql b/src/main/resources/courses.create-table.sql index e69de29..aae23b8 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/students.create-table.sql b/src/main/resources/students.create-table.sql index e69de29..91874d7 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 From 7d5feac672268be9737828bd10f479e861f0d10f Mon Sep 17 00:00:00 2001 From: issagodinez Date: Mon, 7 Sep 2020 21:49:12 -0400 Subject: [PATCH 2/8] changes added --- .../com/github/perscholas/model/Course.java | 75 ++++++++++++++++++- .../com/github/perscholas/model/Student.java | 64 +++++++++++++++- 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/perscholas/model/Course.java b/src/main/java/com/github/perscholas/model/Course.java index 12b71f3..4cb3367 100644 --- a/src/main/java/com/github/perscholas/model/Course.java +++ b/src/main/java/com/github/perscholas/model/Course.java @@ -3,7 +3,80 @@ import java.util.*; // TODO - Annotate and Implement respective interface and define behaviors -public class Course { +// Using each of the interfaces provided in the `model` package as a guide, create a model for each of the aforementioned tables. +// * Use the appropriate annotation to indicate +// * the models are to be used as an `Entity` +// * the name of the table each entity is based on +// * the variable that is used as a primary key +// * the relationships +// * the name of the column each variable is based on each entity. +// * Every Model class must contain the following general two requirements: +// * A nullary constructor +// * A non-nullary constructor which initializes every private member with a parameter provided to the constructor. +// * entirely private fields +// * public getters and setters + + +//Table 2 – Course +// * Column1 +// * Column Name: `id` +// * Column Data-Type: `int not null (PK)` +// * Column Description: Unique course identifier +// * Column2 +// * Column Name: `name` +// * Column Data-Type: `varchar(50) not null` +// * Column Description: provides the name of the course +// * Column3 +// * Column Name: `instructor` +// * Column Data-Type: `varchar(50) not null` +// * Column Description: provides the name of the instructor + +@Entity +@Table(name="course") +public class Course implements CourseInterface{ + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Integer id; + private String name; + private String instructor; + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public String getInstructor() { + return instructor; + } + + @Override + public void setInstructor(String instructor) { + this.instructor = instructor; + } + public Course (){ + + } + public Course(int id, String name, String instructor) { + this.id = id; + this.name = name; + 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..c0dbfff 100644 --- a/src/main/java/com/github/perscholas/model/Student.java +++ b/src/main/java/com/github/perscholas/model/Student.java @@ -1,5 +1,67 @@ package com.github.perscholas.model; +import com.sun.javafx.beans.IDProperty; + +import javax.persistence.*; +import java.util.*; + +//* Column1 +// * Column Name: `email` +// * Column Data-Type: `varchar(50) not null (PK)` +// * Column Description: Student's current school email, unique student identifier +// * Column2 +// * Column Name: `name` +// * Column Data-Type: `varchar(50) not null` +// * Column Description: The full name of the student +// * Column3 +// * Column Name: `password` +// * Column Data-Type: `varchar(50) not null` +// * Column Description: Student's password used to log in + // TODO - Annotate and Implement respective interface and define behaviors + + @Entity + @Table(name="Student") public class Student { -} + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private String email; + + private String name; + private String password; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Student(){ + + } + + public Student(String email, String name, String password) { + this.email = email; + this.name = name; + this.password = password; + } + + } From dae719896287162087debbb76ef7f86d67d857e3 Mon Sep 17 00:00:00 2001 From: issagodinez Date: Mon, 7 Sep 2020 23:26:52 -0400 Subject: [PATCH 3/8] added changes --- .../com/github/perscholas/model/Student.java | 2 +- .../perscholas/service/CourseService.java | 41 ++++++++++++++++++- .../perscholas/service/StudentService.java | 6 +++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/perscholas/model/Student.java b/src/main/java/com/github/perscholas/model/Student.java index c0dbfff..670e540 100644 --- a/src/main/java/com/github/perscholas/model/Student.java +++ b/src/main/java/com/github/perscholas/model/Student.java @@ -57,7 +57,7 @@ public void setPassword(String password) { public Student(){ } - + public Student(String email, String name, String password) { this.email = email; this.name = name; diff --git a/src/main/java/com/github/perscholas/service/CourseService.java b/src/main/java/com/github/perscholas/service/CourseService.java index 86e33e2..12ac4de 100644 --- a/src/main/java/com/github/perscholas/service/CourseService.java +++ b/src/main/java/com/github/perscholas/service/CourseService.java @@ -1,5 +1,44 @@ package com.github.perscholas.service; +import com.github.perscholas.dao.CourseDao; +import com.github.perscholas.model.Course; +import com.github.perscholas.model.CourseInterface; +import com.github.perscholas.DatabaseConnection; +import java.sql.*; +import java.util.*; + + // TODO - Implement respective DAO interface -public class CourseService { +public class CourseService implements CourseDao{ + + private final DatabaseConnection dbc; + + public CourseService(DatabaseConnection dbc) { + this.dbc = dbc; + } + + public CourseService() { + this(DatabaseConnection.MANAGEMENT_SYSTEM); + } + + @Override + public List getAllCourses() { + List coursesList = new ArrayList<>(); + try { + String query = "SELECT * FROM course"; + ResultSet resultSet = dbc.executeQuery(query); + + while(resultSet.next()) { + Course course = new Course(); + course.setId(resultSet.getInt("id")); + course.setName(resultSet.getString("name")); + course.setInstructor(resultSet.getString("instructor")); + coursesList.add(course); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return coursesList; + } } diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java index 0751a9c..af7daa9 100644 --- a/src/main/java/com/github/perscholas/service/StudentService.java +++ b/src/main/java/com/github/perscholas/service/StudentService.java @@ -8,6 +8,7 @@ import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.List; @@ -16,10 +17,12 @@ public class StudentService implements StudentDao { private final DatabaseConnection dbc; public StudentService(DatabaseConnection dbc) { + this.dbc = dbc; } public StudentService() { + this(DatabaseConnection.UAT); } @@ -35,11 +38,13 @@ public List getAllStudents() { @Override public StudentInterface getStudentByEmail(String studentEmail) { + return null; } @Override public Boolean validateStudent(String studentEmail, String password) { + return null; } @@ -50,6 +55,7 @@ public void registerStudentToCourse(String studentEmail, int courseId) { @Override public List getStudentCourses(String studentEmail) { + return null; } } From cb805343ee04a551cc7d8e9f04980f44301777a4 Mon Sep 17 00:00:00 2001 From: issagodinez Date: Tue, 8 Sep 2020 01:12:02 -0400 Subject: [PATCH 4/8] changes made --- .../github/perscholas/DatabaseConnection.java | 1 + .../perscholas/SchoolManagementSystem.java | 16 +++-- .../perscholas/service/StudentService.java | 67 +++++++++++++++++-- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/perscholas/DatabaseConnection.java b/src/main/java/com/github/perscholas/DatabaseConnection.java index 9e33f41..eeff8c5 100644 --- a/src/main/java/com/github/perscholas/DatabaseConnection.java +++ b/src/main/java/com/github/perscholas/DatabaseConnection.java @@ -73,6 +73,7 @@ public void executeStatement(String sqlStatement) { @Override public ResultSet executeQuery(String sqlQuery) { + return null; } } \ No newline at end of file diff --git a/src/main/java/com/github/perscholas/SchoolManagementSystem.java b/src/main/java/com/github/perscholas/SchoolManagementSystem.java index 79b55a3..8a80ad2 100644 --- a/src/main/java/com/github/perscholas/SchoolManagementSystem.java +++ b/src/main/java/com/github/perscholas/SchoolManagementSystem.java @@ -1,10 +1,13 @@ package com.github.perscholas; import com.github.perscholas.dao.StudentDao; +import com.github.perscholas.model.Course; 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.*; +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(); 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 = new StudentService().getStudentCourses(studentEmail); console.println(new StringBuilder() .append("[ %s ] is registered to the following courses:") .append("\n\t" + courses) @@ -64,7 +67,10 @@ private String getStudentDashboardInput() { private Integer getCourseRegistryInput() { - List listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds` + List listOfCoursesIds = new CourseService().getAllCourses() + .stream() + .map(course -> course.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/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java index af7daa9..f5a452a 100644 --- a/src/main/java/com/github/perscholas/service/StudentService.java +++ b/src/main/java/com/github/perscholas/service/StudentService.java @@ -7,10 +7,14 @@ import com.github.perscholas.model.StudentInterface; import java.sql.ResultSet; +import java.sql.PreparedStatement; + import java.sql.SQLException; import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.List; +import java.util.Optional; + // TODO - Implement respective DAO interface public class StudentService implements StudentDao { @@ -21,16 +25,26 @@ public StudentService(DatabaseConnection dbc) { this.dbc = dbc; } + + public StudentService() { - this(DatabaseConnection.UAT); + this(DatabaseConnection.MANAGEMENT_SYSTEM); } @Override public List getAllStudents() { ResultSet resultSet = dbc.executeQuery("SELECT * FROM students"); + ListstudentsList=null; try { - return null; // TODO - Parse `List` from `resultSet` + while(resultSet.next()) { + Student student = new Student(); + student.setEmail(resultSet.getString("email")); + student.setName(resultSet.getString("name")); + student.setPassword(resultSet.getString("password")); + studentsList.add((StudentInterface) student); + } + return studentsList; } catch(Exception e) { throw new Error(e); } @@ -38,24 +52,63 @@ public List getAllStudents() { @Override public StudentInterface getStudentByEmail(String studentEmail) { - - return null; + return getAllStudents().stream() + .filter(std -> std.getEmail().equals(studentEmail)) + .findFirst() + .get(); } @Override public Boolean validateStudent(String studentEmail, String password) { - return null; + return getAllStudents().stream() + .filter(std -> std.getEmail().equals(studentEmail) && std.getPassword().equals(password)) + .count() >= 1; } @Override public void registerStudentToCourse(String studentEmail, int courseId) { - + List studentCourses = getStudentCourses(studentEmail); + Optional course = studentCourses.stream() + .filter(crs -> crs.getId() == courseId) + .findFirst(); + if(course.isPresent()) { + course.ifPresent(crs -> System.out.println("Already registered to this Course!!")); + return; + } + String query = "INSERT INTO Student_Course values (?, ?)"; + try { + PreparedStatement preparedStatement = dbc.getDatabaseConnection().prepareStatement(query); + preparedStatement.setString(1, studentEmail); + preparedStatement.setInt(2, courseId); + preparedStatement.execute(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } @Override public List getStudentCourses(String studentEmail) { - return null; + List studentCourses = new ArrayList<>(); + String query = "SELECT * FROM Student_Course WHERE Student_email=?"; + try { + PreparedStatement preparedStatement = dbc.getDatabaseConnection().prepareStatement(query); + preparedStatement.setString(1, studentEmail); + ResultSet resultSet = preparedStatement.executeQuery(); + + while(resultSet.next()) { + int courseId = resultSet.getInt("Course_id"); + studentCourses.add(new CourseService().getAllCourses() + .stream() + .filter(crs -> crs.getId() == courseId) + .findFirst() + .get()); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return studentCourses; } } From bfa83d484adb299dcb450a7e72a2408d3f9e5f78 Mon Sep 17 00:00:00 2001 From: issagodinez Date: Tue, 8 Sep 2020 08:56:56 -0400 Subject: [PATCH 5/8] Some more changes --- .../github/perscholas/DatabaseConnection.java | 44 ++++++++++++++++--- .../github/perscholas/JdbcConfigurator.java | 7 ++- .../perscholas/service/StudentService.java | 9 +++- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/perscholas/DatabaseConnection.java b/src/main/java/com/github/perscholas/DatabaseConnection.java index eeff8c5..cc28441 100644 --- a/src/main/java/com/github/perscholas/DatabaseConnection.java +++ b/src/main/java/com/github/perscholas/DatabaseConnection.java @@ -2,7 +2,7 @@ import com.github.perscholas.utils.ConnectionBuilder; import com.github.perscholas.utils.IOConsole; - +import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; @@ -25,12 +25,13 @@ public enum DatabaseConnection implements DatabaseConnectionInterface { .setUser("root") .setPassword("") .setPort(3306) - .setDatabaseVendor("mariadb") + .setDatabaseVendor("mysql") .setHost("127.0.0.1")); } @Override public String getDatabaseName() { + return name().toLowerCase(); } @@ -43,15 +44,16 @@ public Connection getDatabaseConnection() { @Override public Connection getDatabaseEngineConnection() { + return connectionBuilder.build(); } @Override public void create() { - String sqlStatement = null; // TODO - define statement + String sqlStatement = "CREATE DATABASE " + name().toLowerCase(); String info; try { - // TODO - execute statement + executeStatement(sqlStatement); info = "Successfully executed statement `%s`."; } catch (Exception sqlException) { info = "Failed to executed statement `%s`."; @@ -61,19 +63,49 @@ public void create() { @Override public void drop() { + String sqlStatement = "DROP DATABASE IF EXISTS " + name().toLowerCase(); + String info; + try { + executeStatement(sqlStatement); + info = "Successfully dropped statement the database `%s`."; + } catch (Exception sqlException) { + info = "Failed to dropped statement the database `%s`."; + } + console.println(info, name().toLowerCase()); } @Override public void use() { + String sqlStatement = "USE " + name().toLowerCase(); + String info; + try { + executeStatement(sqlStatement); + info = "Successfully using statement the database `%s`."; + } catch (Exception sqlException) { + info = "Failed to used statement the database `%s`."; + } + console.println(info, name().toLowerCase()); } @Override public void executeStatement(String sqlStatement) { + try { + getDatabaseEngineConnection().createStatement().execute(sqlStatement); + } catch (SQLException e) { + try { + getDatabaseConnection().createStatement().execute(sqlStatement); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + } } @Override public ResultSet executeQuery(String sqlQuery) { - - return null; + try { + return getDatabaseConnection().createStatement().executeQuery(sqlQuery); + } 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..2eec0e0 100644 --- a/src/main/java/com/github/perscholas/JdbcConfigurator.java +++ b/src/main/java/com/github/perscholas/JdbcConfigurator.java @@ -2,19 +2,21 @@ import com.github.perscholas.utils.DirectoryReference; import com.github.perscholas.utils.FileReader; - +import com.mysql.cj.jdbc.Driver; +import java.sql.DriverManager; import java.io.File; public class JdbcConfigurator { static { try { - // TODO - Attempt to register JDBC Driver + DriverManager.registerDriver(Driver.class.newInstance()); } catch (Exception e) { throw new Error(e); } } private static final DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM; + private static final DatabaseConnection db_UAT = DatabaseConnection.UAT; public static void initialize() { dbc.drop(); @@ -24,6 +26,7 @@ public static void initialize() { executeSqlFile("courses.populate-table.sql"); executeSqlFile("students.create-table.sql"); executeSqlFile("students.populate-table.sql"); + executeSqlFile("studentcourse.create-table.sql"); } private static void executeSqlFile(String fileName) { diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java index f5a452a..6a64d29 100644 --- a/src/main/java/com/github/perscholas/service/StudentService.java +++ b/src/main/java/com/github/perscholas/service/StudentService.java @@ -44,10 +44,17 @@ public List getAllStudents() { student.setPassword(resultSet.getString("password")); studentsList.add((StudentInterface) student); } - return studentsList; } catch(Exception e) { + e.printStackTrace(); throw new Error(e); + } finally{ + try{ + resultSet.close(); + }catch(SQLException e){ + e.printStackTrace(); + } } + return studentsList; } @Override From eda67e8f72adc40af3d97a429bacabdf31921f2b Mon Sep 17 00:00:00 2001 From: issagodinez Date: Tue, 8 Sep 2020 22:06:10 -0400 Subject: [PATCH 6/8] Updated Changes --- pom.xml | 2 +- .../github/perscholas/DatabaseConnection.java | 9 ++++-- .../github/perscholas/JdbcConfigurator.java | 6 ++-- .../perscholas/SchoolManagementSystem.java | 32 ++++++++++++------- .../com/github/perscholas/model/Student.java | 7 ++-- .../perscholas/service/StudentService.java | 13 ++++---- .../perscholas/utils/ConnectionBuilder.java | 1 + src/main/resources/courses.create-table.sql | 6 ++-- .../resources/student-course.create-table.sql | 4 +++ .../student-course.populate-table.sql | 2 ++ src/main/resources/students.create-table.sql | 6 ++-- 11 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 src/main/resources/student-course.create-table.sql create mode 100644 src/main/resources/student-course.populate-table.sql diff --git a/pom.xml b/pom.xml index 31b4f4e..3f91b57 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ mysql mysql-connector-java - 8.0.18 + 8.0.21 diff --git a/src/main/java/com/github/perscholas/DatabaseConnection.java b/src/main/java/com/github/perscholas/DatabaseConnection.java index cc28441..a260fe4 100644 --- a/src/main/java/com/github/perscholas/DatabaseConnection.java +++ b/src/main/java/com/github/perscholas/DatabaseConnection.java @@ -2,9 +2,13 @@ import com.github.perscholas.utils.ConnectionBuilder; import com.github.perscholas.utils.IOConsole; +import com.mysql.jdbc.Driver; import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; +import java.sql.Statement; + + /** * Created by leon on 2/18/2020. @@ -16,14 +20,15 @@ public enum DatabaseConnection implements DatabaseConnectionInterface { private static final IOConsole console = new IOConsole(IOConsole.AnsiColor.CYAN); private final ConnectionBuilder connectionBuilder; - DatabaseConnection(ConnectionBuilder connectionBuilder) { + DatabaseConnection(ConnectionBuilder connectionBuilder) + { this.connectionBuilder = connectionBuilder; } DatabaseConnection() { this(new ConnectionBuilder() .setUser("root") - .setPassword("") + .setPassword("root") .setPort(3306) .setDatabaseVendor("mysql") .setHost("127.0.0.1")); diff --git a/src/main/java/com/github/perscholas/JdbcConfigurator.java b/src/main/java/com/github/perscholas/JdbcConfigurator.java index 2eec0e0..b0297ac 100644 --- a/src/main/java/com/github/perscholas/JdbcConfigurator.java +++ b/src/main/java/com/github/perscholas/JdbcConfigurator.java @@ -9,7 +9,8 @@ public class JdbcConfigurator { static { try { - DriverManager.registerDriver(Driver.class.newInstance()); + // DriverManager.registerDriver(Driver.class.newInstance()); + Class.forName(Driver.class.getName()); } catch (Exception e) { throw new Error(e); } @@ -26,7 +27,8 @@ 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("student-course.create-table.sql"); + executeSqlFile("student-course.populate-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 8a80ad2..70052ef 100644 --- a/src/main/java/com/github/perscholas/SchoolManagementSystem.java +++ b/src/main/java/com/github/perscholas/SchoolManagementSystem.java @@ -14,28 +14,40 @@ public class SchoolManagementSystem implements Runnable { @Override public void run() { + StudentDao studentService=new StudentService(); String smsDashboardInput; do { smsDashboardInput = getSchoolManagementSystemDashboardInput(); if ("login".equals(smsDashboardInput)) { - StudentDao studentService=new StudentService(); String studentEmail = console.getStringInput("Enter your email:"); String studentPassword = console.getStringInput("Enter your password:"); Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword); if (isValidLogin) { String studentDashboardInput = getStudentDashboardInput(); + + if("courses".equals(studentDashboardInput)){ + List courses = studentService.getStudentCourses(studentEmail); + console.println("Registered courses: "); + console.println("Id Course Name Instructor Name"); + for (CourseInterface course : courses) { + console.println(course.getId() + " " + course.getName() + " " + course.getInstructor()); + } + } + if ("register".equals(studentDashboardInput)) { Integer courseId = getCourseRegistryInput(); studentService.registerStudentToCourse(studentEmail, courseId); String studentCourseViewInput = getCourseViewInput(); if ("view".equals(studentCourseViewInput)) { - List courses = new StudentService().getStudentCourses(studentEmail); - console.println(new StringBuilder() - .append("[ %s ] is registered to the following courses:") - .append("\n\t" + courses) - .toString(), studentEmail); + List courses = studentService.getStudentCourses(studentEmail); + courses.forEach(course -> { + console.println(course.getId() + " " + course.getName() + " " + course.getInstructor()); + }); } } + }else{ + console.println("Invalid User Credentials."); + System.exit(0); } } } while (!"logout".equals(smsDashboardInput)); @@ -61,7 +73,7 @@ private String getStudentDashboardInput() { return console.getStringInput(new StringBuilder() .append("Welcome to the Course Registration Dashboard!") .append("\nFrom here, you can select any of the following options:") - .append("\n\t[ register ], [ logout]") + .append("\n\t['courses'], [ register ], [ logout]") .toString()); } @@ -74,11 +86,7 @@ private Integer getCourseRegistryInput() { return console.getIntegerInput(new StringBuilder() .append("Welcome to the Course Registration Dashboard!") .append("\nFrom here, you can select any of the following options:") - .append("\n\t" + listOfCoursesIds - .toString() - .replaceAll("\\[", "") - .replaceAll("\\]", "") - .replaceAll(", ", "\n\t")) + .append("\n\t['courses'], [ register ], [ logout]") .toString()); } } diff --git a/src/main/java/com/github/perscholas/model/Student.java b/src/main/java/com/github/perscholas/model/Student.java index 670e540..8a6c8f4 100644 --- a/src/main/java/com/github/perscholas/model/Student.java +++ b/src/main/java/com/github/perscholas/model/Student.java @@ -22,11 +22,10 @@ @Entity @Table(name="Student") -public class Student { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private String email; +public class Student implements StudentInterface{ + @Id + private String email; private String name; private String password; diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java index 6a64d29..d302c7a 100644 --- a/src/main/java/com/github/perscholas/service/StudentService.java +++ b/src/main/java/com/github/perscholas/service/StudentService.java @@ -34,15 +34,15 @@ public StudentService() { @Override public List getAllStudents() { - ResultSet resultSet = dbc.executeQuery("SELECT * FROM students"); - ListstudentsList=null; + ResultSet resultSet = dbc.executeQuery("SELECT * FROM student"); + ListstudentsList=new ArrayList<>(); try { while(resultSet.next()) { - Student student = new Student(); + StudentInterface student = new Student(); student.setEmail(resultSet.getString("email")); student.setName(resultSet.getString("name")); student.setPassword(resultSet.getString("password")); - studentsList.add((StudentInterface) student); + studentsList.add(student); } } catch(Exception e) { e.printStackTrace(); @@ -97,15 +97,16 @@ public void registerStudentToCourse(String studentEmail, int courseId) { @Override public List getStudentCourses(String studentEmail) { + List studentCourses = new ArrayList<>(); - String query = "SELECT * FROM Student_Course WHERE Student_email=?"; + String query = "SELECT c.* FROM student_course sc LEFT JOIN course c ON sc.course_id = c.id WHERE student_email=?"; try { PreparedStatement preparedStatement = dbc.getDatabaseConnection().prepareStatement(query); preparedStatement.setString(1, studentEmail); ResultSet resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { - int courseId = resultSet.getInt("Course_id"); + int courseId = resultSet.getInt("id"); studentCourses.add(new CourseService().getAllCourses() .stream() .filter(crs -> crs.getId() == courseId) diff --git a/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java b/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java index 745e5a9..7e88703 100644 --- a/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java +++ b/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java @@ -77,6 +77,7 @@ public String toString() { .append(hasPortBeenSet ? portNumber : "") .append("/") .append(databaseName != null ? databaseName : "") + .append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC") .toString(); return jdbcUrl; } diff --git a/src/main/resources/courses.create-table.sql b/src/main/resources/courses.create-table.sql index aae23b8..e4ab683 100644 --- a/src/main/resources/courses.create-table.sql +++ b/src/main/resources/courses.create-table.sql @@ -1,5 +1,5 @@ CREATE TABLE course( -id int not null primary key -name varchar(50) not null -instructor varchar(50) not null + 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/student-course.create-table.sql b/src/main/resources/student-course.create-table.sql new file mode 100644 index 0000000..504fb52 --- /dev/null +++ b/src/main/resources/student-course.create-table.sql @@ -0,0 +1,4 @@ +CREATE TABLE student_course( + course_id int not null, + student_email varchar(50) not null +); \ No newline at end of file diff --git a/src/main/resources/student-course.populate-table.sql b/src/main/resources/student-course.populate-table.sql new file mode 100644 index 0000000..1a75480 --- /dev/null +++ b/src/main/resources/student-course.populate-table.sql @@ -0,0 +1,2 @@ +insert into student_course (student_email, course_id) values ('hluckham0@google.ru', 1); +insert into student_course (student_email, course_id) values ('hluckham0@google.ru', 2); \ 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 91874d7..4024494 100644 --- a/src/main/resources/students.create-table.sql +++ b/src/main/resources/students.create-table.sql @@ -1,5 +1,5 @@ CREATE TABLE student ( -email varchar(50) not null primary key -name varchar(50) not null -password varchar(50) not null + email varchar(50) not null primary key, + name varchar(50) not null, + password varchar(50) not null ); \ No newline at end of file From dac3503fdf1d77ec21c80da2aa3200725c56779e Mon Sep 17 00:00:00 2001 From: issagodinez Date: Tue, 8 Sep 2020 23:35:37 -0400 Subject: [PATCH 7/8] complete part 5 :) --- .../perscholas/SchoolManagementSystem.java | 25 ++++++++++++------- .../perscholas/service/StudentService.java | 3 ++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/perscholas/SchoolManagementSystem.java b/src/main/java/com/github/perscholas/SchoolManagementSystem.java index 70052ef..4c9eb04 100644 --- a/src/main/java/com/github/perscholas/SchoolManagementSystem.java +++ b/src/main/java/com/github/perscholas/SchoolManagementSystem.java @@ -39,6 +39,8 @@ public void run() { studentService.registerStudentToCourse(studentEmail, courseId); String studentCourseViewInput = getCourseViewInput(); if ("view".equals(studentCourseViewInput)) { + console.println("Registered courses: "); + console.println("Id Course Name Instructor Name"); List courses = studentService.getStudentCourses(studentEmail); courses.forEach(course -> { console.println(course.getId() + " " + course.getName() + " " + course.getInstructor()); @@ -78,15 +80,20 @@ private String getStudentDashboardInput() { } - private Integer getCourseRegistryInput() { - List listOfCoursesIds = new CourseService().getAllCourses() - .stream() - .map(course -> course.getId().toString()) - .collect(Collectors.toList()); - return console.getIntegerInput(new StringBuilder() + private Integer getCourseRegistryInput(){ + List allCourses = new CourseService().getAllCourses(); + + StringBuilder baseMessage = new StringBuilder() .append("Welcome to the Course Registration Dashboard!") - .append("\nFrom here, you can select any of the following options:") - .append("\n\t['courses'], [ register ], [ logout]") - .toString()); + .append("\nFrom here, you can select any of the following options: \n"); + + for(int i = 0; i < allCourses.size(); i++){ + baseMessage.append( allCourses.get(i).getId() + ". " + allCourses.get(i).getName() + " " + allCourses.get(i).getInstructor() + " \n"); + } + + return console.getIntegerInput(baseMessage.toString()); + + + } } diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java index d302c7a..e57e0a2 100644 --- a/src/main/java/com/github/perscholas/service/StudentService.java +++ b/src/main/java/com/github/perscholas/service/StudentService.java @@ -83,8 +83,9 @@ public void registerStudentToCourse(String studentEmail, int courseId) { course.ifPresent(crs -> System.out.println("Already registered to this Course!!")); return; } - String query = "INSERT INTO Student_Course values (?, ?)"; + String query = "INSERT INTO student_course (student_email, course_id) values (?, ?)"; try { + PreparedStatement preparedStatement = dbc.getDatabaseConnection().prepareStatement(query); preparedStatement.setString(1, studentEmail); preparedStatement.setInt(2, courseId); From d39e2738c9879b115213662225c272b5e3d4ef24 Mon Sep 17 00:00:00 2001 From: issagodinez Date: Wed, 9 Sep 2020 00:03:18 -0400 Subject: [PATCH 8/8] some tests --- .../courseservice/GetAllCoursesTest.java | 35 +++++++-------- .../studentservice/GetAllStudentsTest.java | 28 ++++-------- .../studentservice/GetStudentByEmailTest.java | 43 ++++++++++--------- .../studentservice/GetStudentCoursesTest.java | 28 ++++++------ .../RegisterStudentToCourseTest.java | 22 +++++----- 5 files changed, 73 insertions(+), 83 deletions(-) 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..4a8846b 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,18 @@ 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.After; +import org.junit.Assert; import org.junit.Before; +import org.junit.Test; import java.io.File; +import java.util.ArrayList; +import java.util.List; /** * @author leonhunter @@ -13,28 +21,17 @@ public class GetAllCoursesTest { @Before // TODO (OPTIONAL) - Use files to execute SQL commands public void setup() { - DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; - File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); - File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); - File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); - File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); - File[] filesToExecute = new File[]{ - coursesSchemaFile, - studentsSchemaFile, - coursesPopulatorFile, - studentsPopulatorFile - }; + JdbcConfigurator.initialize(); } - // given - private void test() { - JdbcConfigurator.initialize(); + @Test + public void testGetAllStudentCourse(){ + CourseDao studentService = new CourseService(); - // when - // TODO - define `when` clause + List allCourses = studentService.getAllCourses(); + + Assert.assertNotNull(allCourses); + } - // then - // TODO - define `then` clause - } } 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..ecabad8 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,10 @@ import com.github.perscholas.model.StudentInterface; import com.github.perscholas.service.StudentService; import com.github.perscholas.utils.DirectoryReference; +import org.junit.After; +import org.junit.Assert; import org.junit.Before; +import org.junit.Test; import java.io.File; import java.util.List; @@ -17,29 +20,14 @@ public class GetAllStudentsTest { @Before // TODO (OPTIONAL) - Use files to execute SQL commands public void setup() { - DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; - File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); - File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); - File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); - File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); - File[] filesToExecute = new File[]{ - coursesSchemaFile, - studentsSchemaFile, - coursesPopulatorFile, - studentsPopulatorFile - }; + //JdbcConfigurator.initialize(); } - // given - // TODO - Add `@Test` annotation - public void test() { - JdbcConfigurator.initialize(); - StudentDao service = (StudentDao) new StudentService(); + @Test + public void testGetAllStudentTest() { - // when + StudentDao service = new StudentService(); List studentList = service.getAllStudents(); - - // then - // TODO - define _then_ clause + Assert.assertNotNull(studentList); } } 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..f8e37c0 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java @@ -1,8 +1,14 @@ 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.StudentService; import com.github.perscholas.utils.DirectoryReference; +import org.junit.After; +import org.junit.Assert; import org.junit.Before; +import org.junit.Test; import java.io.File; @@ -13,29 +19,26 @@ public class GetStudentByEmailTest { @Before // TODO (OPTIONAL) - Use files to execute SQL commands public void setup() { - DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; - File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); - File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); - File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); - File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); - File[] filesToExecute = new File[]{ - coursesSchemaFile, - studentsSchemaFile, - coursesPopulatorFile, - studentsPopulatorFile - }; +// DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; +// File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); +// File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); +// File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); +// File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); +// File[] filesToExecute = new File[]{ +// coursesSchemaFile, +// studentsSchemaFile, +// coursesPopulatorFile, +// studentsPopulatorFile +// }; +// JdbcConfigurator.initialize(); } - // given - // TODO - Add `@Test` annotation - public void test() { - JdbcConfigurator.initialize(); + @Test + public void testGetStudentByEmailPresent() { - // when - // TODO - define `when` clause + StudentDao service = (StudentDao) new StudentService(); - - // then - // TODO - define `then` clause + StudentInterface student = service.getStudentByEmail("hluckham0@google.ru"); + Assert.assertNotNull(student); } } 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..50ce392 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java @@ -2,7 +2,9 @@ import com.github.perscholas.JdbcConfigurator; import com.github.perscholas.utils.DirectoryReference; +import org.junit.After; import org.junit.Before; +import org.junit.Test; import java.io.File; @@ -13,21 +15,20 @@ public class GetStudentCoursesTest { @Before // TODO (OPTIONAL) - Use files to execute SQL commands public void setup() { - DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; - File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); - File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); - File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); - File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); - File[] filesToExecute = new File[]{ - coursesSchemaFile, - studentsSchemaFile, - coursesPopulatorFile, - studentsPopulatorFile - }; +// DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; +// File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); +// File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); +// File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); +// File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); +// File[] filesToExecute = new File[]{ +// coursesSchemaFile, +// studentsSchemaFile, +// coursesPopulatorFile, +// studentsPopulatorFile +// }; } - // given - // TODO - Add `@Test` annotation + @Test public void test() { JdbcConfigurator.initialize(); @@ -38,4 +39,5 @@ public void test() { // then // TODO - define `then` clause } + } diff --git a/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java b/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java index 559170e..844b070 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java @@ -13,17 +13,17 @@ public class RegisterStudentToCourseTest { @Before // TODO (OPTIONAL) - Use files to execute SQL commands public void setup() { - DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; - File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); - File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); - File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); - File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); - File[] filesToExecute = new File[]{ - coursesSchemaFile, - studentsSchemaFile, - coursesPopulatorFile, - studentsPopulatorFile - }; +// DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; +// File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); +// File studentsSchemaFile = directoryReference.getFileFromDirectory("students.create-table.sql"); +// File coursesPopulatorFile = directoryReference.getFileFromDirectory("courses.populate-table.sql"); +// File studentsPopulatorFile = directoryReference.getFileFromDirectory("students.populate-table.sql"); +// File[] filesToExecute = new File[]{ +// coursesSchemaFile, +// studentsSchemaFile, +// coursesPopulatorFile, +// studentsPopulatorFile +// }; } // given