Advertisement
Guest User

GestionBiblio2

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.66 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Scanner;
  3.  
  4. import javax.persistence.EntityManager;
  5. import javax.persistence.EntityManagerFactory;
  6. import javax.persistence.Persistence;
  7.  
  8. import model.Adresse;
  9. import model.Article;
  10. import model.Bibliotheque;
  11.  
  12. public class Test {
  13.     static EntityManagerFactory emf = null;
  14.     static EntityManager em = null;
  15.     static Scanner sc = new Scanner(System.in);
  16.  
  17.     public static void main(String[] args) {
  18.         // Création du EntityManagerFactory
  19.         emf = Persistence.createEntityManagerFactory("GestionBiblio2");
  20.         // Création du EntityManagerFactory
  21.         em = emf.createEntityManager();
  22.         // Overture des transactions
  23.         em.getTransaction().begin();
  24.  
  25.         String continuer = "N";
  26.         int choix = 10;
  27.         do {
  28.             System.out.println("---------------------------------");
  29.             System.out.println("1  - Afficher");
  30.             System.out.println("\t--------");
  31.             System.out.println("2  - Ajouter");
  32.             System.out.println("\t--------");
  33.             System.out.println("3  - Modifier");
  34.             System.out.println("\t--------");
  35.             System.out.println("4  - Supprimer");
  36.             System.out.println("\t--------");
  37.             System.out.println("5 - Quitter");
  38.             System.out.println("---------------------------------");
  39.             System.out.print("Entrez votre choix: ");
  40.             choix = sc.nextInt();
  41.             switch (choix) {
  42.             case 1:
  43.                 System.out.println("\t--------");
  44.                 System.out.println("1  - Afficher les articles");
  45.                 System.out.println("2  - Afficher les bibliotheques");
  46.                 System.out.println("3  - Afficher les adresses");
  47.                 System.out.println("\t--------");
  48.                 System.out.println("4  - Retour");
  49.                 System.out.println("\t--------");
  50.                 do {
  51.                     System.out.print("Entrez votre choix: ");
  52.                     choix = sc.nextInt();
  53.                     if (choix > 4 || choix < 1)
  54.                         System.out.println("Choix invalide!");
  55.                 } while (choix > 4 || choix < 1);
  56.  
  57.                 switch (choix) {
  58.                 case 1:
  59.                     AfficherArticles();
  60.                     break;
  61.                 case 2:
  62.                     AfficherBibliotheques();
  63.                     break;
  64.                 case 3:
  65.                     AfficherAdresses();
  66.                     break;
  67.                 }
  68.                 break;
  69.             case 2:
  70.                 System.out.println("\t--------");
  71.                 System.out.println("1  - Ajouter une article");
  72.                 System.out.println("2  - Ajouter une bibliotheque");
  73.                 System.out.println("3  - Ajouter une adresse");
  74.                 System.out.println("\t--------");
  75.                 System.out.println("4  - Retour");
  76.                 System.out.println("\t--------");
  77.                 do {
  78.                     System.out.print("Entrez votre choix: ");
  79.                     choix = sc.nextInt();
  80.                     if (choix > 4 || choix < 1)
  81.                         System.out.println("Choix invalide!");
  82.                 } while (choix > 4 || choix < 1);
  83.                 switch (choix) {
  84.                 case 1:
  85.                     AjouterArticle();
  86.                     break;
  87.                 case 2:
  88.                     AjouterBiblio();
  89.                     break;
  90.                 case 3:
  91.                     AjouterAdresse();
  92.                     break;
  93.                 }
  94.                 break;
  95.             case 3:
  96.                 System.out.println("\t--------");
  97.                 System.out.println("1  - Modifier une article");
  98.                 System.out.println("2  - Modifier une bibliotheque");
  99.                 System.out.println("3  - Modifier une adresse");
  100.                 System.out.println("\t--------");
  101.                 System.out.println("4  - Retour");
  102.                 System.out.println("\t--------");
  103.                 do {
  104.                     System.out.print("Entrez votre choix: ");
  105.                     choix = sc.nextInt();
  106.                     if (choix > 4 || choix < 1)
  107.                         System.out.println("Choix invalide!");
  108.                 } while (choix > 4 || choix < 1);
  109.                 break;
  110.             case 4:
  111.                 System.out.println("\t--------");
  112.                 System.out.println("1  - Supprimer une article");
  113.                 System.out.println("2  - Supprimer une bibliotheque");
  114.                 System.out.println("3  - Supprimer une adresse");
  115.                 System.out.println("\t--------");
  116.                 System.out.println("4  - Retour");
  117.                 System.out.println("\t--------");
  118.                 do {
  119.                     System.out.print("Entrez votre choix: ");
  120.                     choix = sc.nextInt();
  121.                     if (choix > 4 || choix < 1)
  122.                         System.out.println("Choix invalide!");
  123.                 } while (choix > 4 || choix < 1);
  124.                 switch (choix) {
  125.                 case 1:
  126.                     List<Article> articles = em.createNamedQuery("Article.findAll", Article.class).getResultList();
  127.                     Article article = new Article();
  128.                     AfficherArticles2();
  129.                     do {
  130.                         System.out.print("Choisissez votre choix: ");
  131.                         choix = sc.nextInt();
  132.                     } while (choix < 1 || choix > articles.size());
  133.                     article = articles.get(choix-1);
  134.                     String code = article.getCode();
  135.                     em.remove(article);
  136.                     System.out.println("L'article " + code + " a été supprimer avec succée");
  137.                     break;
  138.                 case 2:
  139.                     List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class)
  140.                             .getResultList();
  141.                     Bibliotheque biblio = new Bibliotheque();
  142.                     AfficherBibliotheques2();
  143.                     do {
  144.                         System.out.print("Choisissez votre choix: ");
  145.                         choix = sc.nextInt();
  146.                     } while (choix < 1 || choix > biblios.size());
  147.                     biblio = biblios.get(choix-1);
  148.                     String nom = biblio.getNom();
  149.                     em.remove(biblio);
  150.                     System.out.println("La bibliotheque " + nom + " a été supprimer avec succée");
  151.                     break;
  152.                 case 3:
  153.                     List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
  154.                     Adresse adresse = new Adresse();
  155.                     AfficherAdresses2();
  156.                     do {
  157.                         System.out.print("Choisissez votre choix: ");
  158.                         choix = sc.nextInt();
  159.                     } while (choix < 1 || choix > adresses.size());
  160.                     adresse = adresses.get(choix-1);
  161.                     String codeAdr = adresse.getCode();
  162.                     em.remove(adresse);
  163.                     System.out.println("L'adresse " + codeAdr + " et tout les bibliotheques associé a cette adresse et les articles associé aux bibliotheques ont été supprimer avec succée");
  164.                     break;
  165.                 }
  166.             case 5:
  167.                 System.out.println("Fin du programme!");
  168.                 em.getTransaction().commit();
  169.                 System.exit(0);
  170.                 break;
  171.             }
  172.             System.out.print("Voulez-vous continuer ? [O/N] ");
  173.             continuer = sc.next();
  174.         } while (continuer.equals("O"));
  175.  
  176.         em.getTransaction().commit();
  177.         em.close();
  178.         emf.close();
  179.         System.out.println("Fin du programme!");
  180.         System.exit(0);
  181.     }
  182.  
  183.     private static void AjouterArticle() {
  184.         List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class).getResultList();
  185.         if (biblios.isEmpty()) {
  186.             System.out.println("Il n'existe aucune bibliotheque!");
  187.         } else {
  188.             int choix;
  189.             Bibliotheque biblio = new Bibliotheque();
  190.             System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  191.             System.out.print("Entrez le code : ");
  192.             String code = sc.next();
  193.             System.out.print("Entrez la description: ");
  194.             String description = sc.next();
  195.             AfficherBibliotheques2();
  196.             do {
  197.                 System.out.print("Choisissez votre choix: ");
  198.                 choix = sc.nextInt();
  199.             } while (choix < 1 || choix > biblios.size());
  200.             biblio = biblios.get(choix);
  201.             System.out.println("Vous avez choisi la bibliotheque: " + biblio.getNom());
  202.             System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  203.  
  204.             Article article = new Article(code, description, biblio);
  205.             biblio.addArticle(article);
  206.  
  207.             em.persist(article);
  208.             System.out.println("L' article " + article.getCode() + " a été ajouté avec succée");
  209.             System.out.println();
  210.         }
  211.     }
  212.  
  213.     private static void AjouterBiblio() {
  214.         String choix;
  215.         int choix2;
  216.         Adresse adresse = new Adresse();
  217.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  218.         System.out.print("Entrez le nom du bibliotheque: ");
  219.         String nom = sc.next();
  220.         do {
  221.             System.out.print("Voulez-vous choisir une adresse ? [O/N] ");
  222.             choix = sc.next();
  223.         } while (!choix.equals("O") || !choix.equals("N"));
  224.         if (choix.equals("O")) {
  225.             List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
  226.             AfficherAdresses2();
  227.             do {
  228.                 System.out.print("Choisissez votre choix: ");
  229.                 choix2 = sc.nextInt();
  230.             } while (choix2 < 1 || choix2 > adresses.size());
  231.             adresse = adresses.get(choix2);
  232.             System.out.println("Vous avez choisi l'adresse du code: " + adresse.getCode());
  233.         } else {
  234.             adresse = AjouterAdresse2();
  235.         }
  236.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  237.         Bibliotheque biblio = new Bibliotheque(nom, adresse);
  238.         em.persist(biblio);
  239.         System.out.println("La bibliotheque " + nom + " a été ajouté avec succée");
  240.         System.out.println();
  241.     }
  242.  
  243.     private static Adresse AjouterAdresse2() {
  244.         System.out.print("Entrez le code de l'adresse : ");
  245.         String code = sc.next();
  246.         System.out.print("Entrez la rue : ");
  247.         String rue = sc.next();
  248.         System.out.print("Entrez la ville : ");
  249.         String ville = sc.next();
  250.  
  251.         Adresse adresse = new Adresse(code, rue, ville);
  252.         em.persist(adresse);
  253.         System.out.println("L'adresse " + code + " a été ajouté avec succée");
  254.         return adresse;
  255.     }
  256.  
  257.     private static void AjouterAdresse() {
  258.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  259.         System.out.print("Entrez le code de l'adresse : ");
  260.         String code = sc.next();
  261.         System.out.print("Entrez la rue : ");
  262.         String rue = sc.next();
  263.         System.out.print("Entrez la ville : ");
  264.         String ville = sc.next();
  265.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  266.  
  267.         Adresse adresse = new Adresse(code, rue, ville);
  268.         em.persist(adresse);
  269.         System.out.println("L'adresse " + code + " a été ajouté avec succée");
  270.         System.out.println();
  271.     }
  272.  
  273.     private static void AfficherAdresses2() {
  274.         List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
  275.         System.out.println("*  - |\tCode\t|\tRue\t|\tVille\t|");
  276.         int i = 0;
  277.         for (Adresse a : adresses) {
  278.             System.out.println(++i + "  - |\t" + a.getCode() + "\t|\t" + a.getRue() + "\t|\t" + a.getVille() + "\t|");
  279.         }
  280.     }
  281.  
  282.     private static void AfficherAdresses() {
  283.         List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
  284.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  285.         System.out.println("|\tCode\t|\tRue\t|\tVille\t|");
  286.         for (Adresse a : adresses) {
  287.             System.out.println("|\t" + a.getCode() + "\t|\t" + a.getRue() + "\t|\t" + a.getVille() + "\t|");
  288.         }
  289.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  290.     }
  291.  
  292.     private static void AfficherBibliotheques2() {
  293.         List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class).getResultList();
  294.         System.out.println("*  - |\tNom\t|\tAdresse\t|");
  295.         int i = 0;
  296.         for (Bibliotheque b : biblios) {
  297.             System.out.println(++i + "  - |\t" + b.getNom() + "\t|\t" + b.getAdresse().getCode() + "\t|");
  298.         }
  299.     }
  300.  
  301.     private static void AfficherBibliotheques() {
  302.         List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class).getResultList();
  303.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  304.         System.out.println("|\tNom\t|\tAdresse\t|");
  305.         for (Bibliotheque b : biblios) {
  306.             System.out.println("|\t" + b.getNom() + "\t|\t" + b.getAdresse().getCode() + "\t|");
  307.         }
  308.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  309.     }
  310.  
  311.     private static void AfficherArticles2() {
  312.         List<Article> articles = em.createNamedQuery("Article.findAll", Article.class).getResultList();
  313.         System.out.println("*  - |\tCode\t|\tBiblioteque\t|\tDescription\t|");
  314.         int i = 0;
  315.         for (Article a : articles) {
  316.             System.out.println(++i + "  - |\t" + a.getCode() + "\t|\t" + a.getBibliotheque().getNom() + "\t|\t"
  317.                     + a.getDescription() + "\t|");
  318.         }
  319.     }
  320.  
  321.     private static void AfficherArticles() {
  322.         List<Article> articles = em.createNamedQuery("Article.findAll", Article.class).getResultList();
  323.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  324.         System.out.println("|\tCode\t|\tBiblioteque\t|\tDescription\t|");
  325.         for (Article a : articles) {
  326.             System.out.println("|\t" + a.getCode() + "\t|\t" + a.getBibliotheque().getNom() + "\t|\t"
  327.                     + a.getDescription() + "\t|");
  328.         }
  329.         System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement