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 #25

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

<dependency>
Expand Down
52 changes: 45 additions & 7 deletions src/main/java/com/github/perscholas/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -16,21 +20,23 @@ 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("mariadb")
.setDatabaseVendor("mysql")
.setHost("127.0.0.1"));
}

@Override
public String getDatabaseName() {

return name().toLowerCase();
}

Expand All @@ -43,15 +49,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`.";
Expand All @@ -61,18 +68,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);
}
}
}
9 changes: 7 additions & 2 deletions src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

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());
Class.forName(Driver.class.getName());
} 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();
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");
executeSqlFile("student-course.populate-table.sql");
}

private static void executeSqlFile(String fileName) {
Expand Down
59 changes: 40 additions & 19 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
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();

@Override
public void run() {
StudentDao studentService=new StudentService();
String smsDashboardInput;
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
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<CourseInterface> 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<CourseInterface> courses = null; // TODO - Instantiate and populate `courses`;
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
.toString(), studentEmail);
console.println("Registered courses: ");
console.println("Id Course Name Instructor Name");
List<CourseInterface> 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));
Expand All @@ -58,21 +75,25 @@ 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());
}


private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
return console.getIntegerInput(new StringBuilder()
private Integer getCourseRegistryInput(){
List<CourseInterface> 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" + listOfCoursesIds
.toString()
.replaceAll("\\[", "")
.replaceAll("\\]", "")
.replaceAll(", ", "\n\t"))
.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());



}
}
79 changes: 78 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,82 @@
package com.github.perscholas.model;
import javax.persistence.*;
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;
}


}
Loading