Advertisement
Guest User

GestionVoiture

a guest
Nov 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package model;
  2.  
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.EntityManagerFactory;
  8. import javax.persistence.Persistence;
  9.  
  10. public class Test {
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner sc = new Scanner(System.in);
  14.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("GestionVoiture");
  15.         EntityManager em = emf.createEntityManager();
  16.         em.getTransaction().begin();
  17.  
  18.         boolean continuer = true;
  19.         int choix, choix2;
  20.         String nom;
  21.         do {
  22.             System.out.println("1. Ajouter Garage");
  23.             System.out.println("2. Ajouter Voiture");
  24.             System.out.println("3. Afficher les Garages");
  25.             System.out.println("4. Afficher les Voitures");
  26.             System.out.println("5. Modifier un garage");
  27.             System.out.println("6. Modifier une voiture");
  28.             System.out.print("Entrez votre choix: ");
  29.             choix = sc.nextInt();
  30.             switch (choix) {
  31.             case 1:
  32.                 System.out.print("Entrez le nom du garage: ");
  33.                 nom = sc.next();
  34.                 Garage g = new Garage(nom);
  35.                 em.persist(g);
  36.                 System.out.println("Garage ajouté avec succée!");
  37.                 em.getTransaction().commit();
  38.                 break;
  39.             case 2:
  40.                 List<Garage> garages = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
  41.                 for (int i = 0; i < garages.size(); i++) {
  42.                     System.out.println(i + " - " + garages.get(i).getNom());
  43.                 }
  44.                 System.out.print("Choisir un garage: ");
  45.                 choix2 = sc.nextInt();
  46.  
  47.                 Garage g3 = garages.get(choix2);
  48.  
  49.                 System.out.print("Entrez le nom du voiture: ");
  50.                 nom = sc.next();
  51.                 Voiture v = new Voiture(nom, g3);
  52.                 g3.addVoiture(v);
  53.                 em.persist(v);
  54.                 System.out.println("Voiture ajouté avec succée!");
  55.                 em.getTransaction().commit();
  56.                 break;
  57.             case 3:
  58.                 List<Garage> garages2 = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
  59.                 for (int i = 0; i < garages2.size(); i++) {
  60.                     System.out.println(garages2.get(i).getNom());
  61.                 }
  62.                 break;
  63.             case 4:
  64.                 List<Voiture> voitures = em.createNamedQuery("Voiture.findAll", Voiture.class).getResultList();
  65.                 for (int i = 0; i < voitures.size(); i++) {
  66.                     System.out.println(voitures.get(i).getNom() + " - " + voitures.get(i).getGarage().getNom());
  67.                 }
  68.                 break;
  69.             case 5:
  70.                 List<Garage> garages3 = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
  71.                 for (int i = 0; i < garages3.size(); i++) {
  72.                     System.out.println(i + " - " + garages3.get(i).getNom());
  73.                 }
  74.                 System.out.print("Choisir un garage: ");
  75.                 choix2 = sc.nextInt();
  76.                
  77.                 System.out.print("Entrez le nouveau nom: ");
  78.                 String nvNom = sc.next();
  79.                
  80.                 Garage ga = em.find(Garage.class, garages3.get(choix2).getId());
  81.                 ga.setNom(nvNom);
  82.                
  83.                 em.persist(ga);
  84.                 em.getTransaction().commit();
  85.                 System.out.println("Modification faite avec succées");
  86.                 break;
  87.             case 6:
  88.                 List<Voiture> voitures2 = em.createNamedQuery("Voiture.findAll", Voiture.class).getResultList();
  89.                 for (int i = 0; i < voitures2.size(); i++) {
  90.                     System.out.println(i + " - " + voitures2.get(i).getNom() + " | " + voitures2.get(i).getGarage().getNom());
  91.                 }
  92.                 System.out.print("Choisir un voiture: ");
  93.                 choix2 = sc.nextInt();
  94.                
  95.                 System.out.print("Entrez le nouveau nom: ");
  96.                 nvNom = sc.next();
  97.                 garages3 = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
  98.                 for (int i = 0; i < garages3.size(); i++) {
  99.                     System.out.println(i + " - " + garages3.get(i).getNom());
  100.                 }
  101.                 System.out.print("Choisir un garage: ");
  102.                 int choix3 = sc.nextInt();
  103.                 ga = em.find(Garage.class, garages3.get(choix3).getId());
  104.                
  105.                 Voiture vo = em.find(Voiture.class, voitures2.get(choix2).getId());
  106.                 vo.setNom(nvNom);
  107.                 vo.setGarage(ga);
  108.                
  109.                 em.persist(ga);
  110.                 em.getTransaction().commit();
  111.                 System.out.println("Modification faite avec succées");
  112.                
  113.                 break;
  114.             default:
  115.                 continuer = false;
  116.                 break;
  117.             }
  118.             System.out.print("Vous voulez continuer ? [O/N]: ");
  119.             String zitoun = sc.next();
  120.             if (zitoun.equals("N"))
  121.                 continuer = false;
  122.         } while (continuer);
  123.         System.exit(0);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement