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 School Management System #13

Open
wants to merge 7 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
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
<version>8.0.19</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
<!-- <dependency>-->
<!-- <groupId>org.mariadb.jdbc</groupId>-->
<!-- <artifactId>mariadb-java-client</artifactId>-->
<!-- <version>2.5.4</version>-->
<!-- </dependency>-->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
54 changes: 45 additions & 9 deletions src/main/java/com/github/perscholas/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* Created by leon on 2/18/2020.
Expand All @@ -23,9 +24,9 @@ public enum DatabaseConnection implements DatabaseConnectionInterface {
DatabaseConnection() {
this(new ConnectionBuilder()
.setUser("root")
.setPassword("")
.setPassword("dbpassword")
.setPort(3306)
.setDatabaseVendor("mariadb")
.setDatabaseVendor("mysql")
.setHost("127.0.0.1"));
}

Expand All @@ -48,31 +49,66 @@ public Connection getDatabaseEngineConnection() {

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

@Override
public void drop() {
String sqlStatement = "DROP DATABASE IF EXISTS " + name().toLowerCase() + ";";
String message;
try {
getDatabaseEngineConnection().prepareStatement(sqlStatement).execute();
message = "Successfully executed statement";
} catch (Exception sqlException) {
message = "Error executing statement";
}
console.println(message, sqlStatement);
}

@Override
public void use() {
try {
String sqlStatement = "USE " + name().toLowerCase() + ";";
getDatabaseEngineConnection()
.prepareStatement(sqlStatement)
.execute();
console.println("Successfully executed statement");

} catch (SQLException e) {
throw new Error(e);
}
}

@Override
public void executeStatement(String sqlStatement) {
try {
sqlStatement = sqlStatement.trim();
getDatabaseConnection().createStatement().execute(sqlStatement);
console.println("Successfully executed statement\n"+sqlStatement);
} catch (SQLException e) {
console.println("Error executing statement\n"+sqlStatement);
throw new Error(e);
}
}

@Override
public ResultSet executeQuery(String sqlQuery) {
return null;
try {
sqlQuery = sqlQuery.trim();
ResultSet result = getDatabaseConnection().createStatement().executeQuery(sqlQuery);
console.println("Successfully executed query\n"+sqlQuery);
return result;
} catch (SQLException e) {
console.println("Failed to execute query \n"+sqlQuery);
throw new Error(e);
}
}
}
5 changes: 5 additions & 0 deletions 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;

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 +27,8 @@ public static void initialize() {
executeSqlFile("courses.populate-table.sql");
executeSqlFile("students.create-table.sql");
executeSqlFile("students.populate-table.sql");
executeSqlFile("student-course.create-table.sql");

}

private static void executeSqlFile(String fileName) {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

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.Collector;
import java.util.stream.Collectors;

public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
Expand All @@ -15,7 +19,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,10 +30,10 @@ 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> courseList = studentService.getStudentCourses(studentEmail);
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
.append("\n\t" + courseList.toString())
.toString(), studentEmail);
}
}
Expand Down Expand Up @@ -64,7 +68,10 @@ private String getStudentDashboardInput() {


private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
List<CourseInterface> courseList=new CourseService().getAllCourses();
List<String> listOfCoursesIds = courseList.stream()
.map(CourseInterface::toString)
.collect(Collectors.toList()); // 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
78 changes: 77 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,81 @@
package com.github.perscholas.model;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

// TODO - Annotate and Implement respective interface and define behaviors
public class Course {
@Entity
@Table(name="course", schema = "management_system")
public class Course implements CourseInterface, Serializable {
@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;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getInstructor() {
return instructor;
}

@Override
public void setId(Integer id) {
this.id = id;
}

public void setInstructor(String instructor) {
this.instructor = instructor;
}

@Override
public String toString() {
return "Course{" +
"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);
}
}
99 changes: 98 additions & 1 deletion src/main/java/com/github/perscholas/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,102 @@
package com.github.perscholas.model;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

// TODO - Annotate and Implement respective interface and define behaviors
public class Student {
@Entity
@Table(name = "student", schema = "management_system")
public class Student implements StudentInterface, Serializable {
@Id
@Column(name = "email")
private String email;

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

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

//@OneToMany()
// @JoinTable(name = "studentcourse", joinColumns = {@JoinColumn(name = "email")},
// inverseJoinColumns = {@JoinColumn(name = "id")})
private List<CourseInterface> courses;

public void setCourses(List<CourseInterface> courses) {
this.courses = courses;
}

public List<CourseInterface> getCourses() {
if(courses==null){
courses=new ArrayList<>();
}
return courses;
}

public Student(String name, String email, String password) {
this.name = name;
this.email = email;
this.password = password;
}

public Student() {

}

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public String getEmail() {
return email;
}

@Override
public void setEmail(String email) {
this.email = email;
}

@Override
public String getPassword() {
return password;
}

@Override
public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "Student{" +
"email='" + email + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
", courses=" + courses +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return Objects.equals(email, student.email) &&
Objects.equals(name, student.name) &&
Objects.equals(password, student.password);
}


}
Loading