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 completed. #12

Open
wants to merge 4 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.5.4</version>
</dependency>

</dependencies>

Expand Down
90 changes: 86 additions & 4 deletions src/main/java/com/github/perscholas/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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);
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -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) {
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
Expand All @@ -26,7 +35,8 @@ public void run() {
studentService.registerStudentToCourse(studentEmail, courseId);
String studentCourseViewInput = getCourseViewInput();
if ("view".equals(studentCourseViewInput)) {
List<CourseInterface> courses = null; // TODO - Instantiate and populate `courses`;
// TODO - Instantiate and populate `courses`;
List<CourseInterface> courses = studentService.getStudentCourses(studentEmail);
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
Expand Down Expand Up @@ -65,6 +75,12 @@ private String getStudentDashboardInput() {

private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
CourseDao courseService = new CourseService();
List<CourseInterface> 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:")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/perscholas/dao/StudentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.perscholas.model.CourseInterface;
import com.github.perscholas.model.StudentInterface;

import java.sql.SQLException;
import java.util.List;

/**
Expand Down
82 changes: 81 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,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<StudentInterface> 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);
}
}
Loading