Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Admin {
- public static java.util.Scanner scanner = new java.util.Scanner(System.in);
- public static void main(String[] args) {
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Driver PostgreSQL manquant !");
- System.exit(1);
- }
- String url = "jdbc:postgresql://localhost:5432/postgres"; // LOCAL
- //String url="jdbc:postgresql://172.24.2.6:5432/dbnlorphe16"; // ECOLE
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(url, "nlorphe16", "AyQ&B6E");
- } catch (SQLException e) {
- System.out.println("Impossible de joindre le serveur");
- System.exit(1);
- }
- System.err.println("Veuillez eviter les fautes d'input s'il vous plait, merci");
- System.err.println("Une facturation supplementaire vous sera demandee en cas d'erreur!");
- System.err.println("Le service IT !");
- //err ou out ? question avec Jeje
- scanner.nextLine();
- /* Begin of the menu's phase for the adminisatrator */
- System.out.println("Bienvenue cher administrateur,");
- int choix;
- do{
- System.out.println("Voici votre menu de navigation:");
- System.out.println("1 -> Voir des statistiques");
- System.out.println("2 -> Voir les comptes suspendus");
- System.out.println("3 -> Desuspendre un compte");
- System.out.println("4 -> Supprimer un compte");
- System.out.println("5 -> Faire une recherche approfondie");
- System.out.println("6 -> Se deconnecter");
- choix=scanner.nextInt();
- switch (choix){
- case 1:
- stats(conn); // /!\ gerer les differentes recherches qu'ils veut effectuer....
- break;
- case 2:
- comptesSuspendus(conn);
- break;
- case 3:
- if(desuspendreCompte(conn))
- System.out.println("Compte remis a la noral");
- break;
- case 4:
- if(supprimerCompte(conn))
- System.out.println("Compte bien supprime");
- break;
- case 5:
- faireRechercheApprofondie(conn);
- break;
- }
- }while(choix != 6);
- System.out.println("Merci de votre visite, a la prochaine o/");
- System.exit(1);
- /* End of the menu's phase for the administrator */
- }
- /* Admin method */
- public static void stats(Connection connection){
- System.out.println("1 -> Classement des utilisateurs en fonction des moyennes des évaluations");
- System.out.println("2 -> Classement des utilisateurs en fonction du nombre d’objets vendus");
- System.out.println("3 -> Retour menu principal");
- int choix=scanner.nextInt();
- if (choix==1)
- {
- try{
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT login, avg_rating FROM projet.users GROUP BY login ORDER BY avg_rating DESC;");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println(" Le compte avec comme login "+rs.getString(1)+ " avec une moyenne de "+rs.getDouble(2)+"/5");
- }
- }
- catch(SQLException e)
- {
- System.out.println("Error, "+ e.getMessage());
- }
- return;
- }
- else if(choix==2)
- {
- try{
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT login, total_sales FROM projet.users GROUP BY login ORDER BY total_sales DESC;");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println(" Le compte avec comme login "+rs.getString(1)+ " a vendu "+rs.getInt(2)+" objet(s)");
- }
- }
- catch(SQLException e)
- {
- System.out.println("Error, " +e.getMessage());
- }
- return;
- }
- else
- return;
- }
- /**
- * montre tout les comptes suspendu et permet de desuspendre ou supprimer un compte
- */
- public static void comptesSuspendus(Connection connection)
- {
- try {
- Statement statement = connection.createStatement();
- try (ResultSet rs = statement.executeQuery("SELECT * FROM projet.getUser WHERE \"Etat du compte\"="+1+";")){//get all the users with the login whitch is equals to login
- while (rs.next()){
- System.out.println(rs.getString(1));
- }
- }
- }catch (SQLException se) {
- se.printStackTrace();
- System.exit(1);
- }
- }
- /**
- * permet de desuspendre un compte
- */
- public static boolean desuspendreCompte(Connection connection){
- System.out.println("Quel compte voulez vous desuspendre:");
- String compte= scanner.next();
- try{
- Statement statement= connection.createStatement();
- statement.execute("UPDATE projet.users SET suspended=0 WHERE login="+"'"+compte+"';");
- return true;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- System.exit(1);
- }
- return false;
- }
- /**
- * permet de supprimer un compte
- */
- public static boolean supprimerCompte(Connection connection){
- System.out.println("Quel compte voulez vous supprimez:");
- String compte= scanner.next();
- try{
- Statement statement= connection.createStatement();
- statement.execute("UPDATE projet.users SET suspended=2 WHERE login="+"'"+compte+"';");
- return true;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- System.exit(1);
- }
- return false;
- }
- /**
- * permet de faire une recherche plus approfondie -> celle la va etre bagdad
- */
- public static void faireRechercheApprofondie(Connection connection){
- String login="";
- String debut="";
- String fin="";
- System.out.println("1 -> Historique sur des encheres effectuees");
- System.out.println("2 -> Historique sur des ventes effectuees");
- System.out.println("3 -> Retour menu principal");
- int choix=scanner.nextInt();
- scanner.nextLine();
- if (choix==1)
- {
- try{
- System.out.println("Sur quel utilisateur voulez vous effectuez cette recherche:");
- login ="'"+scanner.next()+"'";
- System.out.println("Donner la date de debut de la recherche(YYY-MM-DD)");
- debut="'"+scanner.next()+"'";
- System.out.println("Donner la date de fin de la recherche(YYY-MM-DD)");
- fin="'"+scanner.next()+"'";
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT * from projet.getOwnerBids WHERE \"Encherisseur\" ="+login+" AND \"Date\" BETWEEN "+debut+" AND "+fin+";");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("Enchere sur l'objet ayant comme id "+rs.getInt(1)+" avec comme montant "+rs.getDouble(2)+"$");
- }
- }
- catch(SQLException e)
- {
- System.out.println("Error, "+ e.getMessage());
- System.exit(1);
- }
- return;
- }
- else if(choix==2)
- {
- try{
- System.out.println("Sur quel utilisateur voulez vous effectuez cette recherche:");
- login = "'"+scanner.next()+"'";
- System.out.println("Donner la date de debut de la recherche(YYY-MM-DD)");
- debut="'"+scanner.next()+"'";
- System.out.println("Donner la date de fin de la recherche(YYY-MM-DD)");
- fin="'"+scanner.next()+"'";
- PreparedStatement ps;
- //ps = connection.prepareStatement("SELECT * from projet.getOwnerSales WHERE \"Proprietaire\" ="+login+" AND \"Date d'ajout\" BETWEEN "+debut+" AND "+fin+";");
- ps = connection.prepareStatement("SELECT * from projet.getOwnerTransactions WHERE (\"Acheteur\" ="+login+" OR \"Vendeur\"="+login+") AND \"Date\" BETWEEN "+debut+" AND "+fin+";");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- //System.out.println(" Objet ayant comme id "+rs.getInt(1)+ " avec comme prix de départ "+rs.getDouble(2)+"$ a comme etat "+ rs.getString(8));
- System.out.println(" Objet ayant comme id "+rs.getInt(1)+ " a ete achete par "+rs.getString(3)+" a "+ rs.getString(2)+", a la date du "+rs.getString(4));
- }
- }
- catch(SQLException e)
- {
- System.out.println("Error, " +e.getMessage());
- System.exit(1);
- }
- return;
- }
- else
- return;
- }
- }
Add Comment
Please, Sign In to add comment