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

some methods are still incomplete and tests need to be added #17

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
61 changes: 57 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,11 +50,14 @@ public Connection getDatabaseEngineConnection() {

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

@Override
public void drop() {
String dropQuery="DROP DATABASE IF EXISTS "+name().toLowerCase()+";";
try {
getDatabaseEngineConnection().
prepareStatement(dropQuery)
.execute();
String successMessage = String.format("Successfully executed statement \n\t`%s`", dropQuery);
console.println(successMessage);
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
public void use() {
String useQuery= "USE "+name().toLowerCase()+";";
try {
getDatabaseEngineConnection()
.prepareStatement(useQuery)
.execute();
String successMessage=String.format("Successfully executed statement \n\t`%s`", useQuery);
console.println(successMessage);
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
public void executeStatement(String sqlStatement) {
try {
sqlStatement = sqlStatement.trim();
getScrollableStatement().execute(sqlStatement);
String successMessage = String.format("Successfully executed statement \n\t`%s`", sqlStatement);
console.println(successMessage);
} catch (SQLException e) {
String errorMessage = String.format("Failed to execute statement \n\t`%s`", sqlStatement);
throw new Error(errorMessage, e);
}
}

@Override
public ResultSet executeQuery(String sqlQuery) {
return null;
try {
sqlQuery = sqlQuery.trim();
ResultSet result = getScrollableStatement().executeQuery(sqlQuery);
String successMessage = String.format("Successfully executed query \n\t`%s`", sqlQuery);
console.println(successMessage);
return result;
} catch (SQLException e) {
String errorMessage = String.format("Failed to execute query \n\t`%s`", sqlQuery);
throw new Error(errorMessage, 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);
}
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.github.perscholas.utils.FileReader;

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

public class JdbcConfigurator {
static {
public JdbcConfigurator() {
try {
// TODO - Attempt to register JDBC Driver
} catch (Exception e) {
DriverManager.registerDriver(Driver.class.newInstance());
} catch (InstantiationException | IllegalAccessException | SQLException e) {
throw new Error(e);
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
Expand All @@ -15,7 +20,7 @@ public void run() {
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
StudentDao studentService = new StudentService(); // TODO - 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); // TODO - Instantiate and populate `courses`;
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
Expand All @@ -35,6 +40,7 @@ public void run() {
}
}
}

} while (!"logout".equals(smsDashboardInput));
}

Expand Down Expand Up @@ -64,7 +70,10 @@ private String getStudentDashboardInput() {


private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
List<String> listOfCoursesIds =new ArrayList<>();// TODO - instantiate and populate `listOfCourseIds`
// listOfCoursesIds.stream().forEach(new CourseService().getAllCourses().get());
List<CourseInterface>listOfCoursesIds2=new CourseService().getAllCourses();
listOfCoursesIds2.stream().forEach(x -> listOfCoursesIds.add(x.toString()));
return console.getIntegerInput(new StringBuilder()
.append("Welcome to the Course Registration Dashboard!")
.append("\nFrom here, you can select any of the following options:")
Expand All @@ -75,4 +84,4 @@ private Integer getCourseRegistryInput() {
.replaceAll(", ", "\n\t"))
.toString());
}
}
}
72 changes: 71 additions & 1 deletion src/main/java/com/github/perscholas/model/Course.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
package com.github.perscholas.model;

import javax.persistence.*;
// TODO - Annotate and Implement respective interface and define behaviors
public class Course {
@Entity
@Table
public class Course implements CourseInterface {

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

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


@Basic
@Column(name = "instructor")
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;
}

@Override
public String toString() {
// String lineOne=String.format("%-10d%s%-30s\n",id,name,instructor);
String CourseList="\\["+id+" "+name+" "+instructor+"\\]"+", ";
// return "Course{" +
// "id=" + id +
// ", name='" + name + '\'' +
// ", instructor='" + instructor + '\'' +
// '}';
return CourseList;
}
}
65 changes: 63 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,66 @@
package com.github.perscholas.model;

// TODO - Annotate and Implement respective interface and define behaviors
public class Student {
}

import javax.persistence.*;
import java.util.List;

@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;
}
}
51 changes: 49 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,52 @@
package com.github.perscholas.service;

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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 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<CourseInterface> getAllCourses() {
List<CourseInterface> courseInterfaceList= new ArrayList<>();
String CourseQuery= "SELECT * FROM course";

ResultSet resultSet= dbc.executeQuery(CourseQuery);
try {
while (resultSet.next())
{
Course course= new Course();
course.setId(resultSet.getInt("id"));
course.setName(resultSet.getString("name"));
course.setInstructor(resultSet.getString("Instructor"));
courseInterfaceList.add(course);
}
} catch (SQLException e) {
e.printStackTrace();
}
return courseInterfaceList;

}
}
Loading