Advertisement
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 Utilisateur {
- 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, "jdebors16", "(xrNeE9");
- } 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 !");
- scanner.nextLine();
- System.out.println("1 -> S'inscrire");
- System.out.println("2 -> Se loger");
- int curse = scanner.nextInt();
- /* Inscription phase */
- if(curse==1){
- try {
- System.out.println("Veuillez trouver un login");
- String lg = scanner.next();
- System.out.println("Veuillez entrer votre nom,");
- String name = scanner.next();
- System.out.println("votre prenom,");
- String fname = scanner.next();
- System.out.println("votre email,");
- String email = scanner.next();
- System.out.println("et enfin votre mot de passe");
- String pass = scanner.next();
- PreparedStatement ps;
- ps = conn.prepareStatement("SELECT projet.addUser(?,?,?,?,?);");
- ps.setString(1,lg);
- ps.setString(2,name);
- ps.setString(3,fname);
- ps.setString(4,email);
- ps.setString(5,pass);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("Inscription bien effectuée");
- }
- }catch (SQLException se){
- System.out.println("Error, "+se.getMessage());
- System.exit(1);
- }
- }
- /* End of Inscription */
- /* Begin of the login's phase */
- System.out.println("Bonjour, veuillez-vous identifier svp:");
- System.out.println("Login:");
- String s = scanner.next();
- System.out.println("Mot de Passe:");
- String s2 = scanner.next();
- if (!authentifacte(conn, s, s2)){
- System.out.println("Votre login/password est incorrect");
- System.exit(1);
- }
- /* End of login's phase */
- /* Begin of the menu's phase for the user */
- System.out.println("Authentifie, bienvenue cher(e) client(e),");
- int choix;
- do{
- System.out.println("Voici votre menu de navigation:");
- System.out.println("1 -> Faire une recherche");
- System.out.println("2 -> Mettre un objet en vente");
- System.out.println("3 -> Modifier ton objet en vente");
- System.out.println("4 -> Terminer une vente");
- System.out.println("5 -> Encherir");
- System.out.println("6 -> Soumettre une evaluation");
- System.out.println("7 -> Se deconnecter");
- choix = scanner.nextInt();
- switch (choix){
- case 1:
- visited(conn,s);
- break;
- case 2:
- int value = mettreObjetEnVente(s,conn);
- if(value!=-1)
- System.out.println("Objet bien mis en vente, le numero de votre objet est le suivant: "+value);
- break;
- case 3:
- if(modifierObjet(s,conn))
- System.out.println("Objet bien modifie");
- break;
- case 4:
- if(accepter(s,conn))
- System.out.println("Enchere bien acceptee vous recevrez le montant d'ici peu");
- break;
- case 5:
- int bidid= faireEnchere(s,conn);
- if(bidid!=-1)
- System.out.println("Enchere effectue votre enchere a l'id suivant "+bidid);
- break;
- case 6:
- int evalid = evaluate(s,conn);
- if(evalid!=-1)
- System.out.println("Evaluation bien effectuee son id est le suivant "+evalid);
- break;
- }
- }while(choix != 7);
- System.out.println("Merci de votre visite, a la prochaine o/");
- System.exit(1);
- /* End of the menu's phase for the user */
- }
- /* login method */
- /**
- * @POST: return a boolean depending on the authentification's success
- * @param connection
- * @param login
- * @param mdp
- */
- public static boolean authentifacte(Connection connection, String login, String mdp){
- try {
- Statement statement = connection.createStatement();
- try (ResultSet rs = statement.executeQuery("SELECT * FROM projet.getUser WHERE \"Login\"=" + "'" + login + "' AND \"Etat du compte\" !=2;")){//get all the users with the login whitch is equals to login
- while (rs.next()) {
- try (ResultSet rt = statement.executeQuery("SELECT * FROM projet.users WHERE login=" + "'" + login + "';")) {//get the tuple of the specified user with the login
- while (rt.next()){
- if (rt.getString(5).equals(mdp))
- return true;
- }
- }
- }
- }
- } catch (SQLException se) {
- return false;
- }
- return false;
- }
- /* User method */
- /**
- * permet de visiter un profil?
- * @param connection
- */
- public static void visited(Connection connection, String login){
- System.out.println("Quelle type de recherche voulez vous effectuer?");
- int option;
- System.out.println("1 -> Visiter un profil");
- System.out.println("2 -> Voir tous les objets en vente");
- System.out.println("3 -> Voir tous ses objets en vente");
- System.out.println("4 -> Voir ses encheres");
- System.out.println("5 -> Voir ses evaluations");
- System.out.println("6 -> Voir ses transactions");
- System.out.println("7 -> Retour au menu principal");
- option=scanner.nextInt();
- switch (option){
- case 1:
- visiterProfil(connection);
- break;
- case 2:
- visiterTousObjets(connection,login);
- break;
- case 3:
- visiterTousSesObjets(connection,login);
- break;
- case 4:
- visiterSesEnchere(connection, login);
- break;
- case 5:
- visiterEvaluation(connection, login);
- break;
- case 6:
- visiterTransaction(connection, login);
- break;
- case 7:
- return;
- }
- }
- public static void visiterProfil(Connection connection){
- try {
- System.out.println("Quel profil voulez vous visiter (entrer son login)");
- String profil="'"+scanner.next()+"'";
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT * FROM projet.getUser WHERE \"Login\" = "+profil+" AND suspended != 2;");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("Le compte de " +rs.getString(2)+" "+rs.getString(3)+", connu(e) sous le login de "+rs.getString(1)+", a une moyenne de "+rs.getInt(5)+"/5 et a vendu "+rs.getInt(6)+" objet(s) actuellement et possede "+rs.getInt(7)+ " objet(s) en vente actuellement");
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- }
- public static void visiterTousObjets(Connection connection, String login){
- try {
- String owner ="'"+login+"'";
- PreparedStatement ps;
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- ps = connection.prepareStatement("SELECT * FROM projet.getOwnerSales WHERE \"Etat\"=\'pending\' AND \"Proprietaire\"!="+owner+";");
- ResultSet rs = ps.executeQuery();
- String bafouille="";
- while(rs.next()){
- if(rs.getInt(5)==0){
- bafouille="ne possede pas encore de meilleur enchere";
- }
- else{
- PreparedStatement pt;
- pt = connection.prepareStatement("SELECT price FROM projet.bids WHERE bid_id ="+rs.getInt(5)+";");
- ResultSet rt =pt.executeQuery();
- while(rt.next())
- bafouille="a comme meilleur enchere l'enchere ayant l'id "+rs.getInt(5)+ " dont le montant est de "+rt.getDouble(1)+"$";
- }
- System.out.println(rs.getString(4)+" ayant l'id " +rs.getInt(1)+", possede un prix de depart de "+rs.getDouble(2)+"$, le proprietaire est "+rs.getString(3)+", "+bafouille+", expire dans "+rs.getString(6)+" ");
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- }
- private static void visiterTousSesObjets(Connection connection, String login){
- try {
- String owner ="'"+login+"'";
- PreparedStatement ps;
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- ps = connection.prepareStatement("SELECT * FROM projet.getOwnerSalesAndBids WHERE \"Proprietaire\"="+owner+";");
- ResultSet rs = ps.executeQuery();
- String bafouille="";
- while(rs.next()){
- if(rs.getInt(5)==0){
- bafouille="ne possede pas encore de meilleur enchere";
- }
- else{
- bafouille="a comme meilleur enchere, l'enchere ayant l'id "+rs.getInt(5)+" dont le montant est de "+rs.getDouble(6)+"$";
- }
- System.out.println(rs.getString(4)+" ayant l'id " +rs.getInt(1)+", possede un prix de depart de "+rs.getDouble(2)+"$,"+bafouille);
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- }
- /**
- * Afficher, pour chaque objet sur lequel Damas a enchéri dans les 15 derniers jours, la meilleure enchère de l’utilisateur, sa date ainsi que son statut. -> une enchère remportée, une enchère annulée.
- * @param connection
- * @param login
- */
- private static void visiterSesEnchere(Connection connection, String login){
- String bidder="'"+login+"'";
- try {
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT * FROM projet.getOwnerBids WHERE \"Encherisseur\"="+bidder+";");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("L'enchere ayant comme valeur "+ rs.getDouble(2)+ "$, dont la date est "+ rs.getDate(4) + " a comme statut "+rs.getString(5));
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- }
- private static void visiterEvaluation(Connection connection, String login){
- String bidder="'"+login+"'";
- try {
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT * FROM projet.getOwnerReviews WHERE \"Evalue\"="+bidder+";");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("L'evaluation a porte sur la transaction ayant l'id "+rs.getInt(1)+ "a ete donnee par "+rs.getString(3)+" avec la note de "+ rs.getInt(4)+ " avec en commentaire: "+ rs.getString(5));
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- }
- private static void visiterTransaction(Connection connection, String login){
- String bidder="'"+login+"'";
- try {
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT * FROM projet.getOwnerTransaction WHERE \"Acheteur\"="+bidder+";");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("La transaction, a porte sur l'objet ayant l'id "+rs.getInt(1)+ " vendu par "+rs.getString(2)+", et a ete effectuee a la date "+ rs.getDate(4));
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- }
- /**
- * Allow the owner to put a sale
- * @param owner
- * @param connection
- * @return the id of the sale
- */
- public static int mettreObjetEnVente(String owner,Connection connection){
- try {
- System.out.println("Veuillez inserer les informations concernant la vente comme suit:");
- System.out.println("Le prix de base pour la vente:");
- double prix = scanner.nextDouble();
- System.out.println("La description de votre objet (separer votre description avec des \" _ \")");
- String description = scanner.next();
- System.out.println("La date d'expiration de votre enchere, son type (month,days, minutes ou aucune si pas de date voulue):");
- String date = scanner.next();
- System.out.println("Le nombre (de mois,jours ou minutes)");
- int nbr = scanner.nextInt();
- String datecoller = "'"+nbr+" "+date+"'";
- String o= "'"+owner+"'";
- PreparedStatement ps;
- if(date.equals("aucune"))
- ps = connection.prepareStatement("SELECT projet.addSale(?,?,"+o+");");
- else
- ps = connection.prepareStatement("SELECT projet.addSale(?,?,"+o+",(NOW()+interval "+datecoller+")::timestamp);");
- ps.setDouble(1, prix);
- ps.setString(2, description);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- return rs.getInt(1);
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- return -1;
- }
- /**
- * Allow the owner to modify his sale only if there isn't bid on it
- * @param owner
- * @param connection
- * @return boolean if the modification succeed
- */
- public static boolean modifierObjet(String owner,Connection connection){
- try {
- System.out.println("Voici la liste de tous vos objets:");
- visiterTousSesObjets(connection, owner);
- System.out.println("Quel objet voulez vous modifier? (inserer son ID)");
- int obj = scanner.nextInt();
- System.out.println("Veuillez inserer les nouvelles informations concernant la vente comme suit:");
- System.out.println("Le nouveau prix de base pour la vente:");
- double prix = scanner.nextDouble();
- System.out.println("La nouvelle description de votre objet:");
- String description = scanner.next();
- System.out.println("La nouvelle date d'expiration de votre enchere, son type (month,days, minutes ou aucune si pas de date voulue):");
- String date = scanner.next();
- String o= "'"+owner+"'";
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- PreparedStatement ps;
- if (date.equals("aucune")) {
- ps = connection.prepareStatement("SELECT projet.modifySale(?,?,?,"+o+", NULL);");
- } else {
- System.out.println("Le nombre (de mois,jours ou minutes)");
- int nbr = scanner.nextInt();
- String datecoller = "'"+nbr+" "+date+"'";
- ps = connection.prepareStatement("SELECT projet.modifySale(?,?,?,"+o+",(NOW()+interval ?)::timestamp);");
- ps.setString(4, datecoller);
- }
- ps.setInt(1, obj);
- ps.setDouble(2, prix);
- ps.setString(3, description);
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- return true;
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- se.printStackTrace();
- return false;
- }
- return false;
- }
- /**
- * Allow the owner to accept the best bid on his sale
- * @param owner
- * @param connection
- * @return boolean if the deal has been accepted
- */
- public static boolean accepter(String owner,Connection connection){
- try {
- System.out.println("Veuillez inserer l'id de l'objet pour lequel vous voulez accepter la meilleure enchere");
- int obj= scanner.nextInt();
- String o= "'"+owner+"'";
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT projet.finalizeSale(?,"+o+");");
- ps.setInt(1, obj);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- return true;
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- return false;
- }
- /**
- * Allow to make a bid
- * @param bidder
- * @param connection
- * @return the id of the bid done
- */
- public static int faireEnchere(String bidder,Connection connection){
- try {
- System.out.println("Voici la liste de tous les objets en vente actuellement:");
- visiterTousObjets(connection, bidder);
- System.out.println("Veuillez inserer les informations concernant la vente comme suit:");
- System.out.println("Le prix de votre enchere pour la vente:");
- double prix = scanner.nextDouble();
- System.out.println("Surquel objet voulez vous encherir (son id):");
- int obj=scanner.nextInt();
- String o= "'"+bidder+"'";
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT projet.addBid(?,?, "+o+");");
- ps.setInt(1, obj);
- ps.setDouble(2, prix);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- return rs.getInt(1);
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+ se.getMessage());
- }
- return -1;
- }
- /**
- * Allow the user to give an evaluation
- * @param giver
- * @param connection
- * @return the id of the evaluation
- */
- public static int evaluate(String giver,Connection connection){
- try {
- System.out.println("Quelle transaction voulez vous evaluee (son id):");
- int eval=scanner.nextInt();
- System.out.println("Quelle est la personne que vous voulez evaluee (son login):");
- String receiver=scanner.next();
- String o= "'"+giver+"'";
- System.out.println("Quel note vous lui attribuer: (entre 1 et 5) ");
- int note = scanner.nextInt();
- System.out.println("Desirer vous mettre un commentaire dessus ?");
- scanner.nextLine();
- String description = scanner.nextLine();
- Statement s = connection.createStatement();
- s.execute("SELECT update_sales();");
- PreparedStatement ps;
- ps = connection.prepareStatement("SELECT projet.addReview(?,?,"+o+",?,?);");
- ps.setInt(1, eval);
- ps.setString(2, receiver);
- ps.setInt(3,note);
- ps.setString(4, description);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- return rs.getInt(1);
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+ se.getMessage());
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement