From 2bd9311e29ebb5831de1c04ae112d34e83c8121d Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 17 Dec 2020 11:01:45 +0100 Subject: [PATCH] Add getter and setter connected to DB --- src/main/java/DB/Competition.java | 116 ++++++++++++++++++--- src/main/java/DB/DB.java | 3 + src/main/java/DB/Movie.java | 70 ++++++++++--- src/main/java/DB/Projection.java | 64 +++++++++--- src/main/java/DB/Room.java | 61 +++++++++-- src/main/java/DB/Slot.java | 50 ++++++--- src/main/java/DB/Table.java | 20 +++- src/main/java/DB/User.java | 77 +++++++++++--- src/main/java/DB/UserType.java | 37 +++++-- src/main/java/ProjectionPlanning/Main.java | 7 +- 10 files changed, 414 insertions(+), 91 deletions(-) diff --git a/src/main/java/DB/Competition.java b/src/main/java/DB/Competition.java index 43ba3bd..33c606d 100644 --- a/src/main/java/DB/Competition.java +++ b/src/main/java/DB/Competition.java @@ -1,8 +1,10 @@ package DB; import lombok.Getter; -import lombok.Setter; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; /** @@ -24,41 +26,121 @@ import java.util.ArrayList; *

* The maximum number of different movies projected */ -public class Competition implements Table { - @Getter @Setter private String name; - @Getter @Setter private int days; - @Getter @Setter private int movies; - @Getter @Setter private int movieMax; +public class Competition extends Table { + @Getter final private String name; - public Competition() { + public Competition(String name) { + this.name = name; + tableName = "Competition"; + check = "name"; + this.checkValue = this.name; + } + + public Integer getDays() { + return get(Integer.class, "days"); + } + + public Integer getMovies() { + return get(Integer.class, "movies"); + } + + public Integer getMovieMax() { + return get(Integer.class, "movieMax"); + } + + public boolean setDays(int days) { + return set("days", Integer.toString(days)); + } + + public boolean setMovies(int movies) { + return set("movies", Integer.toString(movies)); + } + + public boolean setMovieMax(int movieMax) { + return set("movieMax", Integer.toString(movieMax)); } static public ArrayList getAll() { - return null; - } - - static public ArrayList getAvailable() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT name FROM Competition"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Competition(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getUsers() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT email FROM User WHERE CompetitionName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new User(rs.getString("email"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getSlots() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Slot WHERE CompetitionName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Slot(rs.getInt("idk"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getRooms() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE CompetitionName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Room(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getProjections() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection WHERE CompetitionName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Projection(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getTableMovies() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT name FROM Movie WHERE CompetitionName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Movie(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/DB/DB.java b/src/main/java/DB/DB.java index 801739b..3340d8a 100644 --- a/src/main/java/DB/DB.java +++ b/src/main/java/DB/DB.java @@ -1,5 +1,6 @@ package DB; +import lombok.Data; import lombok.Getter; import java.sql.*; @@ -59,6 +60,8 @@ public class DB { returnValue = rs.getInt(value); else if (cl == Time.class) returnValue = rs.getTime(value); + else if (cl == Data.class) + returnValue = rs.getDate(value); else returnValue = rs.getObject(value); ps.close(); diff --git a/src/main/java/DB/Movie.java b/src/main/java/DB/Movie.java index 16b030a..f1b83a4 100644 --- a/src/main/java/DB/Movie.java +++ b/src/main/java/DB/Movie.java @@ -1,30 +1,72 @@ package DB; import lombok.Getter; -import lombok.Setter; -import java.time.Duration; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Time; import java.util.ArrayList; -public class Movie implements Table { - @Getter @Setter private String name; - @Getter @Setter private User director; - @Getter @Setter private Duration duration; - @Getter @Setter private Competition competition; +public class Movie extends Table { + @Getter final private String name; - public Movie() { + public Movie(String name) { + this.name = name; + tableName = "Movie"; + check = "name"; + this.checkValue = this.name; + } + + public String getDirector() { + return get(String.class, "director"); + } + + public Time getDuration() { + return get(Time.class, "duration"); + } + + public Competition getCompetition() { + return new Competition(get(String.class, "CompetitionName")); + } + + public boolean setDirector(String director) { + return set("director", director); + } + + public boolean setDuration(Time duration) { + return set("duration", duration.toString()); + } + + public boolean setCompetition(Competition competition) { + return set("CompetitionName", competition.getName()); } static public ArrayList getAll() { - return null; - } - - static public ArrayList getAvailable(Competition competition) { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT name FROM Movie"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Movie(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getProjections() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection WHERE MovieName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Projection(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/DB/Projection.java b/src/main/java/DB/Projection.java index a41cc62..0d94513 100644 --- a/src/main/java/DB/Projection.java +++ b/src/main/java/DB/Projection.java @@ -1,26 +1,66 @@ package DB; import lombok.Getter; -import lombok.Setter; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; -public class Projection implements Table { - @Getter @Setter private Date startDate; - @Getter @Setter private Competition competition; - @Getter @Setter private Room room; - @Setter @Getter private Movie movie; +public class Projection extends Table { + @Getter final private int id; - public Projection() { + public Projection(int id) { + this.id = id; + tableName = "Projection"; + check = "id"; + this.checkValue = Integer.toString(this.id); + } + + public Date getStartDate() { + return get(Date.class, "startDate"); + } + + public Competition getCompetition() { + return new Competition(get(String.class, "CompetitionName")); + } + + public Room getRoom() { + return new Room(get(String.class, "RoomName")); + } + + public Movie getMovie() { + return new Movie(get(String.class, "MovieName")); + } + + public boolean setStartDate(Date startDate) { + return set("startDate", startDate.toString()); + } + + public boolean setCompetition(Competition competition) { + return set("CompetitionName", competition.getName()); + } + + public boolean setRoom(Room room) { + return set("RoomName", room.getName()); + } + + public boolean setMovie(Movie movie) { + return set("MovieName", movie.getName()); } static public ArrayList getAll() { - return null; - } - - static public ArrayList getAvailable(Competition competition) { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Projection(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/DB/Room.java b/src/main/java/DB/Room.java index 3f4d0db..602fc93 100644 --- a/src/main/java/DB/Room.java +++ b/src/main/java/DB/Room.java @@ -1,30 +1,69 @@ package DB; import lombok.Getter; -import lombok.Setter; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; -public class Room implements Table { - @Getter @Setter private int places; +public class Room extends Table { + @Getter final private String name; - public Room() { + public Room(String name) { + this.name = name; + tableName = "Room"; + check = "name"; + this.checkValue = this.name; + } + + public Integer getPlaces() { + return get(Integer.class, "places"); + } + + public boolean setPlaces(int places) { + return set("places", Integer.toString(places)); } static public ArrayList getAll() { - return null; - } - - static public ArrayList getAvailable(Movie movie, Slot slot) { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT name FROM Room"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Room(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getCompetitions() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE RoomName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Competition(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getProjections() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection WHERE RoomName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Projection(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/DB/Slot.java b/src/main/java/DB/Slot.java index 0e26caa..16c2926 100644 --- a/src/main/java/DB/Slot.java +++ b/src/main/java/DB/Slot.java @@ -1,28 +1,50 @@ package DB; import lombok.Getter; -import lombok.Setter; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; -public class Slot implements Table { - @Getter @Setter private Date startTime; - @Getter @Setter private Competition competition; +public class Slot extends Table { + @Getter final private int id; - public Slot() { + public Slot(int id) { + this.id = id; + tableName = "Slot"; + check = "id"; + this.checkValue = Integer.toString(this.id); + } + + public Date getStartTime() { + return get(Date.class, "startTime"); + } + + public Competition getCompetition() { + return new Competition(get(String.class, "CompetitionName")); + } + + public boolean setCompetition(Competition competition) { + return set("CompetitionName", competition.getName()); + } + + public boolean setStartTime(Date startTime) { + return set("startTime", startTime.toString()); } static public ArrayList getAll() { - return null; - } - - static public ArrayList getAvailable(Movie movie) { - return null; - } - - static public ArrayList getAvailable(Competition competition) { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Slot"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Slot(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/DB/Table.java b/src/main/java/DB/Table.java index 250ba7f..0de08bd 100644 --- a/src/main/java/DB/Table.java +++ b/src/main/java/DB/Table.java @@ -1,7 +1,19 @@ package DB; -public interface Table { - public boolean save(); - public boolean update(); - public boolean delete(); + +public abstract class Table { + static protected String tableName; + static protected String check; + protected String checkValue; + abstract public boolean save(); + abstract public boolean update(); + abstract public boolean delete(); + + protected T get(Class cl, String value) { + return DB.get(cl, tableName, value, check, checkValue); + } + + protected boolean set(String value, String newValue) { + return DB.set(tableName, value, newValue, check, checkValue); + } } diff --git a/src/main/java/DB/User.java b/src/main/java/DB/User.java index 83b6f77..6282de9 100644 --- a/src/main/java/DB/User.java +++ b/src/main/java/DB/User.java @@ -1,23 +1,67 @@ package DB; import lombok.Getter; -import lombok.Setter; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; -public class User implements Table { - @Getter @Setter private String firstName; - @Getter @Setter private String lastName; - @Getter @Setter private String phoneNumber; - @Getter @Setter private String email; - @Getter @Setter private UserType type; - @Getter @Setter private Competition competition; - private String passwordHash; +public class User extends Table { + @Getter final private String email; - public User() { + public User(String email) { + this.email = email; + tableName = "User"; + check = "email"; + this.checkValue = this.email; } - public String getPassword() { + public String getFirstName() { + return get(String.class, "firstName"); + } + + public String getLastName() { + return get(String.class, "lastName"); + } + + public String getPhoneNumber() { + return get(String.class, "phoneNumber"); + } + + public UserType getType() { + return new UserType(get(String.class, "UserTypeName")); + } + + public Competition getCompetition() { + return new Competition(get(String.class, "competitionName")); + } + + private String getPasswordHash() { + return get(String.class, "passwordHash"); + } + + public boolean setFirstName(String firstName) { + return set("firstName", firstName); + } + + public boolean setLastName(String lastName) { + return set("lastName", lastName); + } + + public boolean setPhoneNumber(String phoneNumber) { + return set("phoneNumber", phoneNumber); + } + + public boolean setType(UserType userType) { + return set("UserTypeName", userType.getName()); + } + + public boolean setCompetition(Competition competition) { + return set("CompetitionName", competition.getName()); + } + + public String checkPassword() { return null; } @@ -26,7 +70,16 @@ public class User implements Table { } static public ArrayList getAll() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT email FROM User"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new User(rs.getString("email"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/DB/UserType.java b/src/main/java/DB/UserType.java index 6c68035..3c55ac3 100644 --- a/src/main/java/DB/UserType.java +++ b/src/main/java/DB/UserType.java @@ -1,22 +1,47 @@ package DB; import lombok.Getter; -import lombok.Setter; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; -public class UserType implements Table { - @Getter @Setter private String name; +public class UserType extends Table { + @Getter final private String name; - public UserType() { + public UserType(String name) { + this.name = name; + tableName = "UserType"; + check = "name"; + this.checkValue = this.name; } static public ArrayList getAll() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT name FROM UserType"); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new UserType(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } public ArrayList getUsers() { - return null; + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT email FROM User WHERE UserTypeName = ?"); + ps.setString(1, name); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new User(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException e) { + return null; + } + return list; } @Override diff --git a/src/main/java/ProjectionPlanning/Main.java b/src/main/java/ProjectionPlanning/Main.java index a895fcc..b77eb4e 100644 --- a/src/main/java/ProjectionPlanning/Main.java +++ b/src/main/java/ProjectionPlanning/Main.java @@ -1,9 +1,14 @@ package ProjectionPlanning; +import DB.*; import GUI.GUI; public class Main { public static void main(String[] args) { - new GUI(); + //new GUI(); + DB.connect(); + Competition u = new Competition("Court Métrage"); + System.out.println(Competition.getAll()); + System.out.println(u.getUsers()); } }