Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- // En Java, on doit toujours se placer dans une classe
- public class ClientsCorrection {
- //Méthode permettant d'annuler une commande
- private static void annulerCommandeClient(Connection db, int idClient, int idCommande) throws Exception {
- PreparedStatement st = db.prepareStatement("DELETE FROM contenu_commandes AS cc, commandes AS c WHERE commande = ? AND client = ?";);
- st.setString(1, idClient);
- st.setString(2, idcommande);
- int result = st.executeUpdate();
- st.close;
- boolean ok = false;
- //On check si des lignes ont bien été supprimés de la base de données
- if(result != 0)
- ok = true;
- }else{
- stdout.println("Mauvais numéro de commande");
- }
- rs.close();
- }
- // Méthode affichant les données d'un client identifié par son id
- private static void afficherDonneesClient(Connection db, int numero) throws Exception {
- PreparedStatement st = db.prepareStatement("SELECT cl.nom, cl.adresse, con.commande, con.article, con.quantité FROM clients AS cl, commandes AS com, contenu_commandes AS con WHERE cl.id = ? AND cl.id = com.client AND com.id = con.commande;");
- st.setInt(1, numero);
- ResultSet rs = st.executeQuery();
- DBTablePrinter.printResultSet(rs);
- rs.close();
- st.close();
- }
- // Méthode permettant d'ajouter un nouveau client et renvoyant son id
- private static int ajouterClient(Connection db, String nom, String adresse) throws Exception {
- // Insertion du nouveau client
- PreparedStatement st = db.prepareStatement("INSERT INTO clients (nom, adresse) VALUES (?, ?);");
- st.setString(1, nom);
- st.setString(2, adresse);
- st.executeUpdate();
- st.close();
- // Récupération de son id
- st = db.prepareStatement("SELECT MAX(id) FROM clients WHERE nom = ? AND adresse = ?;");
- st.setString(1, nom);
- st.setString(2, adresse);
- ResultSet rs = st.executeQuery();
- rs.next();
- int idClient = rs.getInt(1);
- rs.close();
- st.close();
- // Renvoi de l'id
- return idClient;
- }
- // Méthode permettant de créer une commande et renvoyant son id
- private static int ajouterCommande(Connection db, int idClient) throws Exception {
- // Insertion de la nouvelle commande
- PreparedStatement st = db.prepareStatement("INSERT into commandes (client) VALUES (?);");
- st.setInt(1, idClient);
- st.executeUpdate();
- st.close();
- // Récupération de son id
- st = db.prepareStatement("SELECT MAX(id) FROM commandes WHERE client = ?;");
- st.setInt(1, idClient);
- ResultSet rs = st.executeQuery();
- rs.next();
- int idCommande = rs.getInt(1);
- rs.close();
- st.close();
- // Renvoi de l'id
- return idCommande;
- }
- // Méthode permettant d'ajouter un article dans une commande
- private static void ajouterArticle(Connection db, int idCommande, String article, int qte) throws Exception {
- // On cherche si l'article est déjà dans la commande
- int qteOrig = -1;
- PreparedStatement st = db.prepareStatement("SELECT quantité FROM contenu_commandes WHERE commande = ? AND article = ?;");
- st.setInt(1, idCommande);
- st.setString(2, article);
- ResultSet rs = st.executeQuery();
- if (rs.next()) {
- qteOrig = rs.getInt(1);
- }
- rs.close();
- st.close();
- // On modifie la quantité s'il apparaît déjà, sinon on l'ajoute
- if (qteOrig != -1) {
- st = db.prepareStatement("UPDATE contenu_commandes SET quantité = ? WHERE commande = ? AND article = ?;");
- st.setInt(1, qteOrig+qte);
- st.setInt(2, idCommande);
- st.setString(3, article);
- st.executeUpdate();
- st.close();
- } else {
- st = db.prepareStatement("INSERT INTO contenu_commandes VALUES (?, ?, ?);");
- st.setInt(1, idCommande);
- st.setString(2, article);
- st.setInt(3, qte);
- st.executeUpdate();
- st.close();
- }
- }
- private static void interfaceClient(Connection db) throws SQLException {
- // Entrée et sortie standard
- PrintWriter stdout = new PrintWriter(System.out, true);
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- // Message d'aide
- String help = "Liste des commandes :\n i: infos d'un client\n a: ajouter un client\n c: passer une commande\n q: quitter\n h: obtenir cette aide";
- // On attend en permanence des demandes de l'utilisateur,
- // jusqu'à ce qu'il quitte
- while (true) {
- try {
- // On demande la commande
- stdout.println("Commande ? [iacqh]");
- // On attend la réponse
- String commande = stdin.readLine();
- // On réagit en fonction de cette réponse
- if (commande.equals("i")) {
- System.out.println("Numéro du client ?");
- String num = stdin.readLine();
- int idClient = 1;
- boolean ok = false;
- while (!ok) {
- try {
- idClient = Integer.parseInt(num);
- ok = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- num = stdin.readLine();
- }
- }
- afficherDonneesClient(db, idClient);
- } else if (commande.equals("a")) {
- System.out.println("Nom du client ?");
- String nom = stdin.readLine();
- System.out.println("Adresse du client ?");
- String adresse = stdin.readLine();
- int id = ajouterClient(db, nom, adresse);
- System.out.println("L'identifiant de ce client est " + id);
- } else if (commande.equals("c")) {
- System.out.println("Numéro du client ?");
- String num = stdin.readLine();
- int idClient = 1;
- boolean ok = false;
- while (!ok) {
- try {
- idClient = Integer.parseInt(num);
- ok = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- num = stdin.readLine();
- }
- }
- int idCommande = ajouterCommande(db, idClient);
- boolean continuer = true;
- while (continuer) {
- System.out.println("Désignation de l'article ?");
- String article = stdin.readLine();
- System.out.println("Quantité ?");
- String quantite = stdin.readLine();
- int qte = 1;
- boolean ok2 = false;
- while (!ok2) {
- try {
- qte = Integer.parseInt(quantite);
- ok2 = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- quantite = stdin.readLine();
- }
- }
- ajouterArticle(db, idCommande, article, qte);
- System.out.println("Voulez-vous ajouter d'autres articles ? [o/n]");
- String choix = stdin.readLine();
- if (!choix.equals("o")) {
- continuer = false;
- }
- }
- System.out.println("Récapitulatif de vos commandes:");
- afficherDonneesClient(db, idClient);
- } else if (commande.equals("d")) {
- //Annulation de la commande
- System.out.println("Numéro du client ?");
- String num = stdin.readLine();
- int idClient = 1;
- boolean ok = false;
- while (!ok) {
- try {
- idClient = Integer.parseInt(num);
- ok = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- num = stdin.readLine();
- }
- }
- System.out.println("Numéro du client ?");
- String num = stdin.readLine();
- int idCommande = 1;
- boolean ok = false;
- while (!ok) {
- try {
- idCommande = Integer.parseInt(num);
- ok = true;
- } catch (IllegalArgumentException e) {
- stdout.println("Numéro illisible, veuillez recommencer");
- num = stdin.readLine();
- }
- }
- annulerCommandeClient(db, idClient, idCommande);
- }else if (commande.equals("q")) {
- // On quitte
- break;
- } else {
- // Si l'utilisateur entre 'h' ou une commande
- // inconnue, on lui affiche l'aide
- stdout.println(help);
- }
- } catch (Exception e) {
- // En cas d'erreur, le programme s'arrête et on affiche le
- // message d'erreur
- e.printStackTrace(System.out);
- }
- }
- }
- // Point d'entrée du programme
- public static void main(String[] args) {
- try {
- // On récupère le port et le nom de la base ; par défaut, le
- // port est 5433 et la base s'appelle TD2
- int port = 5433;
- String dbname = "TD2";
- if (args.length >= 1) {
- try {
- port = Integer.parseInt(args[0]);
- } catch (NumberFormatException e) {
- System.out.println("Invalid port number " + args[0] + "; use " + port + " instead");
- }
- }
- if (args.length >= 2) {
- dbname = args[1];
- }
- // On tente de se connecter à la base de données
- Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + dbname);
- // On lance l'interface de dialogue avec l'utilisateur
- interfaceClient(db);
- } catch (SQLException e) {
- e.printStackTrace(System.out);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement