Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3.  
  4.  
  5. // En Java, on doit toujours se placer dans une classe
  6. public class ClientsCorrection {
  7.  
  8. //Méthode permettant d'annuler une commande
  9. private static void annulerCommandeClient(Connection db, int idClient, int idCommande) throws Exception {
  10. PreparedStatement st = db.prepareStatement("DELETE FROM contenu_commandes AS cc, commandes AS c WHERE commande = ? AND client = ?";);
  11. st.setString(1, idClient);
  12. st.setString(2, idcommande);
  13. int result = st.executeUpdate();
  14. st.close;
  15.  
  16. boolean ok = false;
  17. //On check si des lignes ont bien été supprimés de la base de données
  18. if(result != 0)
  19. ok = true;
  20. }else{
  21. stdout.println("Mauvais numéro de commande");
  22. }
  23.  
  24. rs.close();
  25. }
  26.  
  27. // Méthode affichant les données d'un client identifié par son id
  28. private static void afficherDonneesClient(Connection db, int numero) throws Exception {
  29. 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;");
  30. st.setInt(1, numero);
  31. ResultSet rs = st.executeQuery();
  32. DBTablePrinter.printResultSet(rs);
  33. rs.close();
  34. st.close();
  35. }
  36.  
  37.  
  38. // Méthode permettant d'ajouter un nouveau client et renvoyant son id
  39. private static int ajouterClient(Connection db, String nom, String adresse) throws Exception {
  40. // Insertion du nouveau client
  41. PreparedStatement st = db.prepareStatement("INSERT INTO clients (nom, adresse) VALUES (?, ?);");
  42. st.setString(1, nom);
  43. st.setString(2, adresse);
  44. st.executeUpdate();
  45. st.close();
  46.  
  47. // Récupération de son id
  48. st = db.prepareStatement("SELECT MAX(id) FROM clients WHERE nom = ? AND adresse = ?;");
  49. st.setString(1, nom);
  50. st.setString(2, adresse);
  51. ResultSet rs = st.executeQuery();
  52. rs.next();
  53. int idClient = rs.getInt(1);
  54. rs.close();
  55. st.close();
  56.  
  57. // Renvoi de l'id
  58. return idClient;
  59. }
  60.  
  61.  
  62. // Méthode permettant de créer une commande et renvoyant son id
  63. private static int ajouterCommande(Connection db, int idClient) throws Exception {
  64. // Insertion de la nouvelle commande
  65. PreparedStatement st = db.prepareStatement("INSERT into commandes (client) VALUES (?);");
  66. st.setInt(1, idClient);
  67. st.executeUpdate();
  68. st.close();
  69.  
  70. // Récupération de son id
  71. st = db.prepareStatement("SELECT MAX(id) FROM commandes WHERE client = ?;");
  72. st.setInt(1, idClient);
  73. ResultSet rs = st.executeQuery();
  74. rs.next();
  75. int idCommande = rs.getInt(1);
  76. rs.close();
  77. st.close();
  78.  
  79. // Renvoi de l'id
  80. return idCommande;
  81. }
  82.  
  83. // Méthode permettant d'ajouter un article dans une commande
  84. private static void ajouterArticle(Connection db, int idCommande, String article, int qte) throws Exception {
  85.  
  86. // On cherche si l'article est déjà dans la commande
  87. int qteOrig = -1;
  88. PreparedStatement st = db.prepareStatement("SELECT quantité FROM contenu_commandes WHERE commande = ? AND article = ?;");
  89. st.setInt(1, idCommande);
  90. st.setString(2, article);
  91. ResultSet rs = st.executeQuery();
  92. if (rs.next()) {
  93. qteOrig = rs.getInt(1);
  94. }
  95. rs.close();
  96. st.close();
  97.  
  98. // On modifie la quantité s'il apparaît déjà, sinon on l'ajoute
  99. if (qteOrig != -1) {
  100. st = db.prepareStatement("UPDATE contenu_commandes SET quantité = ? WHERE commande = ? AND article = ?;");
  101. st.setInt(1, qteOrig+qte);
  102. st.setInt(2, idCommande);
  103. st.setString(3, article);
  104. st.executeUpdate();
  105. st.close();
  106. } else {
  107. st = db.prepareStatement("INSERT INTO contenu_commandes VALUES (?, ?, ?);");
  108. st.setInt(1, idCommande);
  109. st.setString(2, article);
  110. st.setInt(3, qte);
  111. st.executeUpdate();
  112. st.close();
  113. }
  114. }
  115.  
  116. private static void interfaceClient(Connection db) throws SQLException {
  117. // Entrée et sortie standard
  118. PrintWriter stdout = new PrintWriter(System.out, true);
  119. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  120.  
  121. // Message d'aide
  122. 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";
  123.  
  124. // On attend en permanence des demandes de l'utilisateur,
  125. // jusqu'à ce qu'il quitte
  126. while (true) {
  127. try {
  128. // On demande la commande
  129. stdout.println("Commande ? [iacqh]");
  130. // On attend la réponse
  131. String commande = stdin.readLine();
  132.  
  133. // On réagit en fonction de cette réponse
  134. if (commande.equals("i")) {
  135. System.out.println("Numéro du client ?");
  136. String num = stdin.readLine();
  137. int idClient = 1;
  138. boolean ok = false;
  139. while (!ok) {
  140. try {
  141. idClient = Integer.parseInt(num);
  142. ok = true;
  143. } catch (IllegalArgumentException e) {
  144. stdout.println("Numéro illisible, veuillez recommencer");
  145. num = stdin.readLine();
  146. }
  147. }
  148. afficherDonneesClient(db, idClient);
  149.  
  150. } else if (commande.equals("a")) {
  151. System.out.println("Nom du client ?");
  152. String nom = stdin.readLine();
  153. System.out.println("Adresse du client ?");
  154. String adresse = stdin.readLine();
  155. int id = ajouterClient(db, nom, adresse);
  156. System.out.println("L'identifiant de ce client est " + id);
  157.  
  158. } else if (commande.equals("c")) {
  159. System.out.println("Numéro du client ?");
  160. String num = stdin.readLine();
  161. int idClient = 1;
  162. boolean ok = false;
  163. while (!ok) {
  164. try {
  165. idClient = Integer.parseInt(num);
  166. ok = true;
  167. } catch (IllegalArgumentException e) {
  168. stdout.println("Numéro illisible, veuillez recommencer");
  169. num = stdin.readLine();
  170. }
  171. }
  172. int idCommande = ajouterCommande(db, idClient);
  173. boolean continuer = true;
  174. while (continuer) {
  175. System.out.println("Désignation de l'article ?");
  176. String article = stdin.readLine();
  177. System.out.println("Quantité ?");
  178. String quantite = stdin.readLine();
  179. int qte = 1;
  180. boolean ok2 = false;
  181. while (!ok2) {
  182. try {
  183. qte = Integer.parseInt(quantite);
  184. ok2 = true;
  185. } catch (IllegalArgumentException e) {
  186. stdout.println("Numéro illisible, veuillez recommencer");
  187. quantite = stdin.readLine();
  188. }
  189. }
  190. ajouterArticle(db, idCommande, article, qte);
  191. System.out.println("Voulez-vous ajouter d'autres articles ? [o/n]");
  192. String choix = stdin.readLine();
  193. if (!choix.equals("o")) {
  194. continuer = false;
  195. }
  196. }
  197. System.out.println("Récapitulatif de vos commandes:");
  198. afficherDonneesClient(db, idClient);
  199.  
  200. } else if (commande.equals("d")) {
  201. //Annulation de la commande
  202. System.out.println("Numéro du client ?");
  203. String num = stdin.readLine();
  204. int idClient = 1;
  205. boolean ok = false;
  206. while (!ok) {
  207. try {
  208. idClient = Integer.parseInt(num);
  209. ok = true;
  210. } catch (IllegalArgumentException e) {
  211. stdout.println("Numéro illisible, veuillez recommencer");
  212. num = stdin.readLine();
  213. }
  214. }
  215. System.out.println("Numéro du client ?");
  216. String num = stdin.readLine();
  217. int idCommande = 1;
  218. boolean ok = false;
  219. while (!ok) {
  220. try {
  221. idCommande = Integer.parseInt(num);
  222. ok = true;
  223. } catch (IllegalArgumentException e) {
  224. stdout.println("Numéro illisible, veuillez recommencer");
  225. num = stdin.readLine();
  226. }
  227. }
  228. annulerCommandeClient(db, idClient, idCommande);
  229.  
  230. }else if (commande.equals("q")) {
  231. // On quitte
  232. break;
  233.  
  234. } else {
  235. // Si l'utilisateur entre 'h' ou une commande
  236. // inconnue, on lui affiche l'aide
  237. stdout.println(help);
  238. }
  239.  
  240. } catch (Exception e) {
  241. // En cas d'erreur, le programme s'arrête et on affiche le
  242. // message d'erreur
  243. e.printStackTrace(System.out);
  244. }
  245. }
  246. }
  247.  
  248.  
  249. // Point d'entrée du programme
  250. public static void main(String[] args) {
  251. try {
  252. // On récupère le port et le nom de la base ; par défaut, le
  253. // port est 5433 et la base s'appelle TD2
  254. int port = 5433;
  255. String dbname = "TD2";
  256.  
  257. if (args.length >= 1) {
  258. try {
  259. port = Integer.parseInt(args[0]);
  260. } catch (NumberFormatException e) {
  261. System.out.println("Invalid port number " + args[0] + "; use " + port + " instead");
  262. }
  263. }
  264.  
  265. if (args.length >= 2) {
  266. dbname = args[1];
  267. }
  268.  
  269. // On tente de se connecter à la base de données
  270. Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + dbname);
  271.  
  272. // On lance l'interface de dialogue avec l'utilisateur
  273. interfaceClient(db);
  274.  
  275. } catch (SQLException e) {
  276. e.printStackTrace(System.out);
  277. }
  278. }
  279.  
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement