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

FINAL SBA SUBMISSION #4

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
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.19</version>
</dependency>

<dependency>
Expand Down
66 changes: 55 additions & 11 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 @@ -20,59 +21,102 @@ public enum DatabaseConnection implements DatabaseConnectionInterface {
this.connectionBuilder = connectionBuilder;
}

//Added database details to connect with Database
DatabaseConnection() {
this(new ConnectionBuilder()
.setUser("root")
.setPassword("")
.setPassword("DakshaHello")
.setPort(3306)
.setDatabaseVendor("mariadb")
.setDatabaseVendor("mysql")
.setHost("127.0.0.1"));
}

@Override
public String getDatabaseName() {
public String getDatabaseName()
{
return name().toLowerCase();
}

@Override
public Connection getDatabaseConnection() {
public Connection getDatabaseConnection()
{
return connectionBuilder
.setDatabaseName(getDatabaseName())
.build();
}

@Override
public Connection getDatabaseEngineConnection() {
public Connection getDatabaseEngineConnection()
{
return connectionBuilder.build();
}

@Override
//creating Database
public void create() {
String sqlStatement = null; // TODO - define statement
String sqlStatement = "CREATE DATABASE " + name().toLowerCase();
String info;
try {
// TODO - execute statement
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to executed statement `%s`.";
executeStatement(sqlStatement);
info = "Successfully executed CREATE statement `%s`.";
}
catch (Exception sqlException) {
info = "Failed to execute CREATE statement `%s`.";
}
console.println(info, sqlStatement);
}

@Override
//Dropping Database if already exists
public void drop() {
System.out.println("Database Name : " + name().toLowerCase());
String sqlStatement = "DROP DATABASE IF EXISTS " +name().toLowerCase();
String info;
try {
executeStatement(sqlStatement);
info = "Successfully executed DROP statement '%s'.";
}
catch (Exception sqlException){
info = "Failed to execute DROP statement '%s'.";
}
console.println(info, sqlStatement);
}

@Override
//Use Main Schema
public void use() {
String sqlStatement = "USE " + name().toLowerCase();
String info;
try {
executeStatement(sqlStatement);
info = "Successfully executed USE statement '%s'.";
}
catch (Exception sqlException) {
info = "Failed to execute USE statement '%s'.";
}
console.println(info, sqlStatement);
}

@Override
public void executeStatement(String sqlStatement) {
//Executing SQL Statement
try {
//console.println( sqlStatement);
getDatabaseEngineConnection().createStatement().execute(sqlStatement);
}
catch (SQLException se) {
throw new RuntimeException(se);
}
}

@Override
public ResultSet executeQuery(String sqlQuery) {
return null;
//Executing SQL Query
try {
return getDatabaseConnection().createStatement().executeQuery(sqlQuery);
}
catch (SQLException se){
throw new RuntimeException(se);
}
}
}
16 changes: 11 additions & 5 deletions src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@

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;

public class JdbcConfigurator {
static {
try {
// TODO - Attempt to register JDBC Driver
DriverManager.registerDriver(Driver.class.newInstance());
} catch (Exception e) {
throw new Error(e);
}
}

private static final DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM;
private static final DatabaseConnection dc = DatabaseConnection.MANAGEMENT_SYSTEM;

public static void initialize() {
dbc.drop();
dbc.create();
dbc.use();
dc.drop();
dc.create();
dc.use();
executeSqlFile("courses.create-table.sql");
executeSqlFile("courses.populate-table.sql");
executeSqlFile("students.create-table.sql");
executeSqlFile("students.populate-table.sql");
//created Student course table and added data into the table
executeSqlFile("student_course.create-table.sql");
executeSqlFile("student_course.populate-table.sql");
}

private static void executeSqlFile(String fileName) {
Expand All @@ -32,7 +38,7 @@ private static void executeSqlFile(String fileName) {
String[] statements = fileReader.toString().split(";");
for (int i = 0; i < statements.length; i++) {
String statement = statements[i];
dbc.executeStatement(statement);
dc.executeStatement(statement);
}
}
}
52 changes: 42 additions & 10 deletions src/main/java/com/github/perscholas/SchoolManagementSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

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;

public class SchoolManagementSystem implements Runnable {
private static final IOConsole console = new IOConsole();
Expand All @@ -15,24 +18,36 @@ public void run() {
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
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);
//Handled the invalid case
if (isValidLogin) {
String studentDashboardInput = getStudentDashboardInput();
String studentDashboardInput = getStudentDashboardInput(studentEmail);
if ("register".equals(studentDashboardInput)) {
Integer courseId = getCourseRegistryInput();
studentService.registerStudentToCourse(studentEmail, courseId);
String studentCourseViewInput = getCourseViewInput();
//Formatted the output based on the example workflow
if ("view".equals(studentCourseViewInput)) {
List<CourseInterface> courses = null; // TODO - Instantiate and populate `courses`;
List<String> courses = new StudentService().getStudentCourses(studentEmail)
.stream()
.map(course -> String.format("%-5s %-15s %-10s", course.getId().toString() , course.getName() , course.getInstructor().toString()))
.collect(Collectors.toList());
console.println(new StringBuilder()
.append("[ %s ] is registered to the following courses:")
.append("\n\t" + courses)
.append( studentEmail +" is registered to the following courses:")
.append("\n\t" + String.format("%-5s %-15s %-10s", "ID", "Course Name", "Instructor Name"))
.append("\n\t" + courses
.toString()
.replaceAll("\\[", "")
.replaceAll("\\]", "")
.replaceAll(", ", "\n\t"))
.toString(), studentEmail);
}
}
} else {
console.println("Invalid Login Details, please try again: \n");
}
}
} while (!"logout".equals(smsDashboardInput));
Expand All @@ -54,20 +69,37 @@ private String getSchoolManagementSystemDashboardInput() {
.toString());
}

private String getStudentDashboardInput() {
//Formatted the output based on the example workflow
private String getStudentDashboardInput(String studentEmail) {
List<String> listOfStudentClass = new StudentService().getStudentCourses(studentEmail)
.stream()
.map(classes -> String.format("%-5s %-15s %-10s", classes.getId().toString() , classes.getName() , classes.getInstructor().toString()))
.collect(Collectors.toList());
return console.getStringInput(new StringBuilder()
.append("Welcome to the Course Registration Dashboard!")
.append("\nFrom here, you can select any of the following options:")
.append("My Classes: ")
.append("\n\t" + String.format("%-5s %-15s %-10s", "ID", "Course Name", "Instructor Name"))
.append("\n\t" + listOfStudentClass
.toString()
.replaceAll("\\[", "")
.replaceAll("\\]", "")
.replaceAll(", ", "\n\t"))
.append("\n\nWelcome to the Course Registration Dashboard!")
.append("\nFrom here, you can select any of the following options: ")
.append("\n\t[ register ], [ logout]")
.toString());
}


//Formatted the output based on the example workflow
private Integer getCourseRegistryInput() {
List<String> listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds`
List<String> listOfCoursesIds = new CourseService().getAllCourses()
.stream()
.map(course -> String.format("%-10s %-30s %-20s",course.getId().toString() , course.getName().toString() , course.getInstructor().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:")
.append("\nAll Courses: \n")
.append(String.format("%-5s %-15s %-10s", "ID", "Course Name", "Instructor Name"))
.append("\n\t" + listOfCoursesIds
.toString()
.replaceAll("\\[", "")
Expand Down
67 changes: 65 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,68 @@
package com.github.perscholas.model;

// TODO - Annotate and Implement respective interface and define behaviors
public class Course {
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;


@Entity
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;
}

@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);
}

}
Loading