Guest User

Untitled

a guest
Dec 7th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class Admin {
  10. public static java.util.Scanner scanner = new java.util.Scanner(System.in);
  11.  
  12. public static void main(String[] args) {
  13. try {
  14. Class.forName("org.postgresql.Driver");
  15.  
  16. } catch (ClassNotFoundException e) {
  17. System.out.println("Driver PostgreSQL manquant !");
  18. System.exit(1);
  19. }
  20.  
  21. String url = "jdbc:postgresql://localhost:5432/postgres"; // LOCAL
  22. //String url="jdbc:postgresql://172.24.2.6:5432/dbnlorphe16"; // ECOLE
  23. Connection conn = null;
  24. try {
  25.  
  26. conn = DriverManager.getConnection(url, "nlorphe16", "AyQ&B6E");
  27.  
  28. } catch (SQLException e) {
  29. System.out.println("Impossible de joindre le serveur");
  30. System.exit(1);
  31. }
  32.  
  33. System.err.println("Veuillez eviter les fautes d'input s'il vous plait, merci");
  34. System.err.println("Une facturation supplementaire vous sera demandee en cas d'erreur!");
  35. System.err.println("Le service IT !");
  36. //err ou out ? question avec Jeje
  37.  
  38. scanner.nextLine();
  39.  
  40.  
  41. /* Begin of the menu's phase for the adminisatrator */
  42. System.out.println("Bienvenue cher administrateur,");
  43. int choix;
  44. do{
  45.  
  46. System.out.println("Voici votre menu de navigation:");
  47. System.out.println("1 -> Voir des statistiques");
  48. System.out.println("2 -> Voir les comptes suspendus");
  49. System.out.println("3 -> Desuspendre un compte");
  50. System.out.println("4 -> Supprimer un compte");
  51. System.out.println("5 -> Faire une recherche approfondie");
  52. System.out.println("6 -> Se deconnecter");
  53. choix=scanner.nextInt();
  54. switch (choix){
  55. case 1:
  56. stats(conn); // /!\ gerer les differentes recherches qu'ils veut effectuer....
  57. break;
  58. case 2:
  59. comptesSuspendus(conn);
  60. break;
  61. case 3:
  62. if(desuspendreCompte(conn))
  63. System.out.println("Compte remis a la noral");
  64. break;
  65. case 4:
  66. if(supprimerCompte(conn))
  67. System.out.println("Compte bien supprime");
  68. break;
  69. case 5:
  70. faireRechercheApprofondie(conn);
  71. break;
  72. }
  73. }while(choix != 6);
  74. System.out.println("Merci de votre visite, a la prochaine o/");
  75. System.exit(1);
  76. /* End of the menu's phase for the administrator */
  77. }
  78.  
  79. /* Admin method */
  80.  
  81. public static void stats(Connection connection){
  82.  
  83. System.out.println("1 -> Classement des utilisateurs en fonction des moyennes des évaluations");
  84. System.out.println("2 -> Classement des utilisateurs en fonction du nombre d’objets vendus");
  85. System.out.println("3 -> Retour menu principal");
  86. int choix=scanner.nextInt();
  87. if (choix==1)
  88. {
  89. try{
  90. PreparedStatement ps;
  91. ps = connection.prepareStatement("SELECT login, avg_rating FROM projet.users GROUP BY login ORDER BY avg_rating DESC;");
  92. ResultSet rs = ps.executeQuery();
  93. while(rs.next()){
  94. System.out.println(" Le compte avec comme login "+rs.getString(1)+ " avec une moyenne de "+rs.getDouble(2)+"/5");
  95. }
  96. }
  97. catch(SQLException e)
  98. {
  99. System.out.println("Error, "+ e.getMessage());
  100. }
  101. return;
  102. }
  103. else if(choix==2)
  104. {
  105. try{
  106. PreparedStatement ps;
  107. ps = connection.prepareStatement("SELECT login, total_sales FROM projet.users GROUP BY login ORDER BY total_sales DESC;");
  108. ResultSet rs = ps.executeQuery();
  109. while(rs.next()){
  110. System.out.println(" Le compte avec comme login "+rs.getString(1)+ " a vendu "+rs.getInt(2)+" objet(s)");
  111. }
  112. }
  113. catch(SQLException e)
  114. {
  115. System.out.println("Error, " +e.getMessage());
  116. }
  117. return;
  118. }
  119. else
  120. return;
  121. }
  122.  
  123. /**
  124. * montre tout les comptes suspendu et permet de desuspendre ou supprimer un compte
  125. */
  126. public static void comptesSuspendus(Connection connection)
  127. {
  128. try {
  129. Statement statement = connection.createStatement();
  130. 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
  131. while (rs.next()){
  132. System.out.println(rs.getString(1));
  133. }
  134. }
  135. }catch (SQLException se) {
  136. se.printStackTrace();
  137. System.exit(1);
  138. }
  139. }
  140.  
  141. /**
  142. * permet de desuspendre un compte
  143. */
  144. public static boolean desuspendreCompte(Connection connection){
  145. System.out.println("Quel compte voulez vous desuspendre:");
  146. String compte= scanner.next();
  147. try{
  148. Statement statement= connection.createStatement();
  149. statement.execute("UPDATE projet.users SET suspended=0 WHERE login="+"'"+compte+"';");
  150. return true;
  151. }
  152. catch(SQLException e)
  153. {
  154. e.printStackTrace();
  155. System.exit(1);
  156. }
  157. return false;
  158. }
  159.  
  160. /**
  161. * permet de supprimer un compte
  162. */
  163. public static boolean supprimerCompte(Connection connection){
  164. System.out.println("Quel compte voulez vous supprimez:");
  165. String compte= scanner.next();
  166. try{
  167. Statement statement= connection.createStatement();
  168. statement.execute("UPDATE projet.users SET suspended=2 WHERE login="+"'"+compte+"';");
  169. return true;
  170. }
  171. catch(SQLException e)
  172. {
  173. e.printStackTrace();
  174. System.exit(1);
  175. }
  176. return false;
  177. }
  178.  
  179. /**
  180. * permet de faire une recherche plus approfondie -> celle la va etre bagdad
  181. */
  182. public static void faireRechercheApprofondie(Connection connection){
  183. String login="";
  184. String debut="";
  185. String fin="";
  186. System.out.println("1 -> Historique sur des encheres effectuees");
  187. System.out.println("2 -> Historique sur des ventes effectuees");
  188. System.out.println("3 -> Retour menu principal");
  189. int choix=scanner.nextInt();
  190. scanner.nextLine();
  191. if (choix==1)
  192. {
  193. try{
  194. System.out.println("Sur quel utilisateur voulez vous effectuez cette recherche:");
  195. login ="'"+scanner.next()+"'";
  196. System.out.println("Donner la date de debut de la recherche(YYY-MM-DD)");
  197. debut="'"+scanner.next()+"'";
  198. System.out.println("Donner la date de fin de la recherche(YYY-MM-DD)");
  199. fin="'"+scanner.next()+"'";
  200.  
  201. PreparedStatement ps;
  202. ps = connection.prepareStatement("SELECT * from projet.getOwnerBids WHERE \"Encherisseur\" ="+login+" AND \"Date\" BETWEEN "+debut+" AND "+fin+";");
  203.  
  204. ResultSet rs = ps.executeQuery();
  205. while(rs.next()){
  206. System.out.println("Enchere sur l'objet ayant comme id "+rs.getInt(1)+" avec comme montant "+rs.getDouble(2)+"$");
  207. }
  208. }
  209. catch(SQLException e)
  210. {
  211. System.out.println("Error, "+ e.getMessage());
  212. System.exit(1);
  213. }
  214. return;
  215. }
  216. else if(choix==2)
  217. {
  218. try{
  219. System.out.println("Sur quel utilisateur voulez vous effectuez cette recherche:");
  220. login = "'"+scanner.next()+"'";
  221. System.out.println("Donner la date de debut de la recherche(YYY-MM-DD)");
  222. debut="'"+scanner.next()+"'";
  223. System.out.println("Donner la date de fin de la recherche(YYY-MM-DD)");
  224. fin="'"+scanner.next()+"'";
  225.  
  226. PreparedStatement ps;
  227. //ps = connection.prepareStatement("SELECT * from projet.getOwnerSales WHERE \"Proprietaire\" ="+login+" AND \"Date d'ajout\" BETWEEN "+debut+" AND "+fin+";");
  228. ps = connection.prepareStatement("SELECT * from projet.getOwnerTransactions WHERE (\"Acheteur\" ="+login+" OR \"Vendeur\"="+login+") AND \"Date\" BETWEEN "+debut+" AND "+fin+";");
  229. ResultSet rs = ps.executeQuery();
  230. while(rs.next()){
  231. //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));
  232. 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));
  233. }
  234. }
  235. catch(SQLException e)
  236. {
  237. System.out.println("Error, " +e.getMessage());
  238. System.exit(1);
  239. }
  240. return;
  241. }
  242. else
  243. return;
  244. }
  245.  
  246. }
Add Comment
Please, Sign In to add comment