Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SBA in progress - Yuru #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/main/java/com/github/perscholas/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,7 +27,7 @@ public enum DatabaseConnection implements DatabaseConnectionInterface {
.setUser("root")
.setPassword("")
.setPort(3306)
.setDatabaseVendor("mariadb")
.setDatabaseVendor("mysql")
.setHost("127.0.0.1"));
}

Expand All @@ -48,10 +50,10 @@ public Connection getDatabaseEngineConnection() {

@Override
public void create() {
String sqlStatement = null; // TODO - define statement
String sqlStatement = "CREATE DATABASE IF NOT EXISTS " + name().toLowerCase() + ";";
String info;
try {
// TODO - execute statement
getScrollableStatement().execute(sqlStatement);
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to executed statement `%s`.";
Expand All @@ -61,18 +63,68 @@ public void create() {

@Override
public void drop() {
String sqlStatement = "DROP DATABASE IF EXISTS " +name().toLowerCase();
String info;
try {
getScrollableStatement().execute(sqlStatement);
console.println("drop table");
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 " + name().toLowerCase() + ";";
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 void executeStatement(String sqlStatement) {
String info;
try {
getScrollableStatement().execute(sqlStatement.trim());
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;
ResultSet resultSet = null;
try {
resultSet = getScrollableStatement().executeQuery(sqlQuery);
info = "Successfully executed statement '%s'.";
}
catch (Exception sqlException) {
info = "Failed to execute statement '%s'.";
}
console.println(info, sqlQuery);
return resultSet;
}

private 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 RuntimeException(e);
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import com.github.perscholas.utils.FileReader;

import java.io.File;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLOutput;

public class JdbcConfigurator {
static {
try {
// TODO - Attempt to register JDBC Driver
Class.forName(Driver.class.getName());
} catch (Exception e) {
throw new Error(e);
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
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.sql.SQLOutput;
import java.util.List;

public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
CourseDao courseService = new CourseService();

@Override
public void run() {
String smsDashboardInput;
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
StudentDao studentService = new StudentService(); //Instantiate `StudentDao` child
String studentEmail = console.getStringInput("Enter your email:");
String studentPassword = console.getStringInput("Enter your password:");
Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword);
Expand All @@ -26,7 +31,7 @@ public void run() {
studentService.registerStudentToCourse(studentEmail, courseId);
String studentCourseViewInput = getCourseViewInput();
if ("view".equals(studentCourseViewInput)) {
List<CourseInterface> courses = null; // TODO - Instantiate and populate `courses`;
List<CourseInterface> courses = studentService.getStudentCourses(studentEmail); // Instantiate and populate `courses`;
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
Expand Down Expand Up @@ -64,7 +69,8 @@ private String getStudentDashboardInput() {


private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
List<String> listOfCoursesIds = null;
courseService.getAllCourses().forEach(e -> listOfCoursesIds.add(e.getId().toString())); // TODO - instantiate and populate `listOfCourseIds`
return console.getIntegerInput(new StringBuilder()
.append("Welcome to the Course Registration Dashboard!")
.append("\nFrom here, you can select any of the following options:")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/github/perscholas/dao/CourseDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
* @created 02/12/2020 - 5:56 PM
*/
public interface CourseDao {
/**
* reads the course table in database
* @return database data as a List<Course>
*/
List<CourseInterface> getAllCourses();
}
58 changes: 56 additions & 2 deletions src/main/java/com/github/perscholas/model/Course.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package com.github.perscholas.model;
import javax.persistence.*;

// TODO - Annotate and Implement respective interface and define behaviors
public class Course {
@Entity
@Table(name = "Course")
public class Course implements CourseInterface{

@Id
@Column(name = "id")
private int id;

@Basic
@Column(name = "name")
private String name;

@Basic
@Column(name = "id")
private String instructor;

public Course(){

}

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;
}
}
58 changes: 56 additions & 2 deletions src/main/java/com/github/perscholas/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package com.github.perscholas.model;
import javax.persistence.*;

// TODO - Annotate and Implement respective interface and define behaviors
public class Student {
@Entity
@Table(name = "Student")
public class Student implements StudentInterface{

@Id
@Column(name = "email")
private String email;

@Basic
@Column(name = "name")
private String name;

@Basic
@Column(name = "password")
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 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;
}
}
42 changes: 40 additions & 2 deletions src/main/java/com/github/perscholas/service/CourseService.java
Original file line number Diff line number Diff line change
@@ -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 com.github.perscholas.model.Student;
import com.github.perscholas.model.StudentInterface;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CourseService implements CourseDao {
private final DatabaseConnection dbc;

public CourseService(DatabaseConnection dbc) {
this.dbc = dbc;
}

public CourseService() {
this(DatabaseConnection.UAT);
}

@Override
public List<CourseInterface> getAllCourses() {
ResultSet resultSet = dbc.executeQuery("SELECT * FROM Course");
try {
List<CourseInterface> courseList = new ArrayList<>();
while(resultSet.next()){
Course course = new Course();
course.setId(Integer.parseInt(resultSet.getString(1)));
course.setName(resultSet.getString(2));
course.setInstructor(resultSet.getString(3));
courseList.add(course);
}
dbc.getDatabaseConnection().close();
return courseList;
} catch(Exception e) {
throw new Error(e);
}
}
}
Loading