diff --git a/DEV/DEV3.1/TP01/Exercise1/Database.java b/DEV/DEV3.1/TP01/Exercise1/Database.java index d20035b..ba75b3c 100644 --- a/DEV/DEV3.1/TP01/Exercise1/Database.java +++ b/DEV/DEV3.1/TP01/Exercise1/Database.java @@ -3,17 +3,9 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; public class Database { - - //Countries - public static String COUNTRY_ID = "id"; - public static String COUNTRY_NAME = "name"; - - //Votes table - public static String VOTES_COMPETITOR_ID = "competitor_id"; - public static String VOTES_VOTER_ID = "voter_id"; - public static String VOTES_POINTS = "points"; private Connection connection; @@ -33,13 +25,20 @@ public class Database { } } - public ResultSet getVotesFromCompetitor(String country) { + public Votes getVotesFromCompetitor(String country) { try { PreparedStatement query = this.connection.prepareStatement( - "SELECT Countries.name, Countries.points FROM Votes INNER JOIN Countries ON Votes.competitor_id = Countries.id WHERE Votes.competitor_id = Countries.id" + "SELECT Countries.name Votes.points" + + "FROM Votes JOIN Countries ON Votes.competitor_id = Countries.id" + + "WHERE Countries.name = " + country ); ResultSet result = query.executeQuery(); + ArrayList voteList = new ArrayList<>(); + + while(result.next()) { + voteList.add(new Vote(result.getString("name"), result.getInt("points"))); + } try { query.close(); @@ -47,7 +46,7 @@ public class Database { System.err.println("Error while trying to close the query."); } - return result; + return new Votes(country, voteList); } catch(SQLException exception) { System.err.println("Error while trying to prepare or execute the query."); } diff --git a/DEV/DEV3.1/TP01/Exercise1/Main.java b/DEV/DEV3.1/TP01/Exercise1/Main.java index 5b54983..938cc46 100644 --- a/DEV/DEV3.1/TP01/Exercise1/Main.java +++ b/DEV/DEV3.1/TP01/Exercise1/Main.java @@ -1,26 +1,16 @@ -import java.sql.ResultSet; -import java.sql.SQLException; - public class Main { public static void main(String[] args) { Database database = new Database(); - String country = "France"; - ResultSet result = database.getVotesFromCompetitor(country); + Votes votesForItaly = database.getVotesFromCompetitor("Italie"); - try { - while(result.next()) { - System.out.println(country + " -> " + result.getString("name") + " : " + result.getInt("points")); - } - } catch(SQLException exception) { - System.err.println("Error while read the data from the result."); + System.out.println("Vote " + votesForItaly.getCompetitorName() + " :"); + for(Vote vote : votesForItaly.getVoters()) { + System.out.println(vote.getVoterName() + " " + vote.getPoints()); } - try { - result.close(); - } catch(SQLException exception) { - System.err.println("Error while trying to close the result."); - } + System.out.println("-------------------"); + System.out.println("Total " + votesForItaly.getTotalPoints()); } } diff --git a/DEV/DEV3.1/TP01/Exercise1/Vote.java b/DEV/DEV3.1/TP01/Exercise1/Vote.java new file mode 100644 index 0000000..cfa10e7 --- /dev/null +++ b/DEV/DEV3.1/TP01/Exercise1/Vote.java @@ -0,0 +1,18 @@ +public class Vote { + + private String voter; + private int points; + + public Vote(String voter, int points) { + this.voter = voter; + this.points = points; + } + + public String getVoterName() { + return this.voter; + } + + public int getPoints() { + return this.points; + } +} diff --git a/DEV/DEV3.1/TP01/Exercise1/Votes.java b/DEV/DEV3.1/TP01/Exercise1/Votes.java new file mode 100644 index 0000000..ce33509 --- /dev/null +++ b/DEV/DEV3.1/TP01/Exercise1/Votes.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; + +public class Votes { + + private String competitor; + private ArrayList voters; + + public Votes(String competitor, ArrayList voters) { + this.competitor = competitor; + this.voters = voters; + } + + public String getCompetitorName() { + return this.competitor; + } + + public ArrayList getVoters() { + return this.voters; + } + + public int getTotalPoints() { + int sum = 0; + + for(Vote vote : this.voters) { + sum += vote.getPoints(); + } + + return sum; + } +}