From eb5e01e21b06d47c716b66ef34681a5ed0aca412 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 18 Dec 2020 13:35:01 +0100 Subject: [PATCH] Add creation constructor on DB class --- src/main/java/DB/Competition.java | 12 ++++++++++ src/main/java/DB/DB.java | 26 +++++++++++++++++++++- src/main/java/DB/Movie.java | 12 ++++++++++ src/main/java/DB/Projection.java | 13 ++++++++++- src/main/java/DB/Room.java | 10 +++++++++ src/main/java/DB/Slot.java | 11 ++++++++- src/main/java/DB/Table.java | 16 +++++++++++++ src/main/java/DB/User.java | 12 ++++++++++ src/main/java/DB/UserType.java | 8 +++++++ src/main/java/ProjectionPlanning/Main.java | 21 +++-------------- 10 files changed, 120 insertions(+), 21 deletions(-) diff --git a/src/main/java/DB/Competition.java b/src/main/java/DB/Competition.java index 2cc58fe..adfb604 100644 --- a/src/main/java/DB/Competition.java +++ b/src/main/java/DB/Competition.java @@ -1,5 +1,6 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; @@ -7,6 +8,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Map; /** * # Competition @@ -35,6 +37,16 @@ public class Competition extends Table { this.name = name; } + public Competition(String name, int days, int movies, int movieMax) throws AlreadyOnTable { + super("Competition", "name", name, Map.ofEntries( + Map.entry("name", name), + Map.entry("days", Integer.toString(days)), + Map.entry("movies", Integer.toString(movies)), + Map.entry("movieMax", Integer.toString(movieMax)) + )); + this.name = name; + } + public Integer getDays() { return get(Integer.class, "days"); } diff --git a/src/main/java/DB/DB.java b/src/main/java/DB/DB.java index 4ba0f85..24e6627 100644 --- a/src/main/java/DB/DB.java +++ b/src/main/java/DB/DB.java @@ -167,12 +167,36 @@ public class DB { if (r > 0) return cl.getConstructor(String.class).newInstance(values.get(id)); } catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { - System.out.println(e); return null; } return null; } + static public String create(Class cl, String table, Map values) { + try { + String query = String.format( + "INSERT INTO "+table+" (%s) VALUES (%s)", + String.join(",", values.keySet()), + String.join(",", Collections.nCopies(values.values().size(), "?")) + ); + PreparedStatement ps; + ps = DB.connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + int i = 1; + for (String value : values.values()) { + ps.setString(i, value); + i++; + } + ps.execute(); + ResultSet gk = ps.getGeneratedKeys(); + gk.next(); + String rs = gk.getObject(1).toString(); + ps.close(); + return rs; + } catch (SQLException e) { + return null; + } + } + static public boolean delete(String table, String check, String checkValue) { try { PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+check+" = ?"); diff --git a/src/main/java/DB/Movie.java b/src/main/java/DB/Movie.java index f9df91d..b16064c 100644 --- a/src/main/java/DB/Movie.java +++ b/src/main/java/DB/Movie.java @@ -1,5 +1,6 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; @@ -8,6 +9,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Time; import java.util.ArrayList; +import java.util.Map; public class Movie extends Table { @Getter final private String name; @@ -17,6 +19,16 @@ public class Movie extends Table { this.name = name; } + public Movie(String name, String director, Time duration, Competition competition) throws AlreadyOnTable { + super("Movie", "name", name, Map.ofEntries( + Map.entry("name", name), + Map.entry("director", director), + Map.entry("duration", duration.toString()), + Map.entry("CompetitionName", competition.getName()) + )); + this.name = name; + } + public String getDirector() { return get(String.class, "director"); } diff --git a/src/main/java/DB/Projection.java b/src/main/java/DB/Projection.java index 57c722a..776ee49 100644 --- a/src/main/java/DB/Projection.java +++ b/src/main/java/DB/Projection.java @@ -3,11 +3,12 @@ package DB; import Exceptions.NotFoundInTable; import lombok.Getter; +import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Date; +import java.util.Map; public class Projection extends Table { @Getter final private int id; @@ -17,6 +18,16 @@ public class Projection extends Table { this.id = id; } + public Projection(Date startDate, Competition competition, Room room, Movie movie) { + super("Projection", "id", Map.ofEntries( + Map.entry("startDate", startDate.toString()), + Map.entry("CompetitionName", competition.getName()), + Map.entry("RoomName", room.getName()), + Map.entry("MovieName", movie.getName()) + )); + this.id = Integer.parseInt(super.checkValue); + } + public Date getStartDate() { return get(Date.class, "startDate"); } diff --git a/src/main/java/DB/Room.java b/src/main/java/DB/Room.java index 3a8effa..7ae4706 100644 --- a/src/main/java/DB/Room.java +++ b/src/main/java/DB/Room.java @@ -1,5 +1,6 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; @@ -7,6 +8,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Map; public class Room extends Table { @Getter final private String name; @@ -16,6 +18,14 @@ public class Room extends Table { this.name = name; } + public Room(String name, int places) throws AlreadyOnTable { + super("Room", "name", name, Map.ofEntries( + Map.entry("name", name), + Map.entry("places", Integer.toString(places)) + )); + this.name = name; + } + public Integer getPlaces() { return get(Integer.class, "places"); } diff --git a/src/main/java/DB/Slot.java b/src/main/java/DB/Slot.java index d871e00..982f105 100644 --- a/src/main/java/DB/Slot.java +++ b/src/main/java/DB/Slot.java @@ -3,11 +3,12 @@ package DB; import Exceptions.NotFoundInTable; import lombok.Getter; +import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Date; +import java.util.Map; public class Slot extends Table { @Getter final private int id; @@ -17,6 +18,14 @@ public class Slot extends Table { this.id = id; } + public Slot(Date startTime, Competition competition) { + super("Slot", "id", Map.ofEntries( + Map.entry("startTime", startTime.toString()), + Map.entry("CompetitionName", competition.getName()) + )); + this.id = Integer.parseInt(super.checkValue); + } + public Date getStartTime() { return get(Date.class, "startTime"); } diff --git a/src/main/java/DB/Table.java b/src/main/java/DB/Table.java index 58e4892..2d18774 100644 --- a/src/main/java/DB/Table.java +++ b/src/main/java/DB/Table.java @@ -1,7 +1,10 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; +import java.util.Map; + public abstract class Table { final protected String tableName; final protected String check; @@ -15,6 +18,19 @@ public abstract class Table { throw new NotFoundInTable(this.tableName); } + protected Table(String tableName, String check, String checkValue, Map values) throws AlreadyOnTable { + this.tableName = tableName; + this.check = check; + this.checkValue = checkValue; + DB.create(this.getClass(), this.tableName, this.check, values); + } + + protected Table(String tableName, String check, Map values) { + this.tableName = tableName; + this.check = check; + this.checkValue = DB.create(this.getClass(), this.tableName, values); + } + protected T get(Class cl, String value) { return DB.get(cl, tableName, value, check, checkValue); } diff --git a/src/main/java/DB/User.java b/src/main/java/DB/User.java index 2f5e1da..046e8eb 100644 --- a/src/main/java/DB/User.java +++ b/src/main/java/DB/User.java @@ -1,5 +1,6 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; @@ -7,6 +8,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Map; public class User extends Table { @Getter final private String email; @@ -16,6 +18,16 @@ public class User extends Table { this.email = email; } + public User(String email, String firstName, String lastName, String phoneNumber) throws AlreadyOnTable { + super("User", "email", email, Map.ofEntries( + Map.entry("email", email), + Map.entry("firstName", firstName), + Map.entry("lastName", lastName), + Map.entry("phoneNumber", phoneNumber) + )); + this.email = email; + } + public String getFirstName() { return get(String.class, "firstName"); } diff --git a/src/main/java/DB/UserType.java b/src/main/java/DB/UserType.java index e557448..e551dbe 100644 --- a/src/main/java/DB/UserType.java +++ b/src/main/java/DB/UserType.java @@ -1,5 +1,6 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Getter; @@ -7,6 +8,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Map; public class UserType extends Table { @Getter final private String name; @@ -16,6 +18,12 @@ public class UserType extends Table { this.name = name; } + static public UserType create(String name) throws AlreadyOnTable { + return DB.create(UserType.class, "UserType", "name", Map.ofEntries( + Map.entry("name", name) + )); + } + static public ArrayList getAll() { ArrayList list = new ArrayList<>(); try { diff --git a/src/main/java/ProjectionPlanning/Main.java b/src/main/java/ProjectionPlanning/Main.java index e3bda66..f61cc63 100644 --- a/src/main/java/ProjectionPlanning/Main.java +++ b/src/main/java/ProjectionPlanning/Main.java @@ -1,27 +1,12 @@ package ProjectionPlanning; import DB.*; -import Exceptions.AlreadyOnTable; -import Exceptions.NotFoundInTable; - -import java.util.Collections; -import java.util.Map; +import Exceptions.*; +import java.sql.*; public class Main { - public static void main(String[] args) throws AlreadyOnTable { + public static void main(String[] args) throws AlreadyOnTable, NotFoundInTable { //new GUI(); DB.connect(); - User u = DB.create(User.class, - "User", - "email", - Map.ofEntries( - Map.entry("email", "test2@test.com"), - Map.entry("firstName", "Test2"), - Map.entry("lastName", "Test2"), - Map.entry("phoneNumber", "0000000000") - ) - ); - System.out.println(u); - System.out.println(u.getFirstName()); } }