DEV TP01
This commit is contained in:
@@ -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<Vote> 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.");
|
||||
}
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
18
DEV/DEV3.1/TP01/Exercise1/Vote.java
Normal file
18
DEV/DEV3.1/TP01/Exercise1/Vote.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
30
DEV/DEV3.1/TP01/Exercise1/Votes.java
Normal file
30
DEV/DEV3.1/TP01/Exercise1/Votes.java
Normal file
@@ -0,0 +1,30 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Votes {
|
||||
|
||||
private String competitor;
|
||||
private ArrayList<Vote> voters;
|
||||
|
||||
public Votes(String competitor, ArrayList<Vote> voters) {
|
||||
this.competitor = competitor;
|
||||
this.voters = voters;
|
||||
}
|
||||
|
||||
public String getCompetitorName() {
|
||||
return this.competitor;
|
||||
}
|
||||
|
||||
public ArrayList<Vote> getVoters() {
|
||||
return this.voters;
|
||||
}
|
||||
|
||||
public int getTotalPoints() {
|
||||
int sum = 0;
|
||||
|
||||
for(Vote vote : this.voters) {
|
||||
sum += vote.getPoints();
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user