Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- import java.util.List;
- import java.util.Scanner;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- public class Test {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("GestionVoiture");
- EntityManager em = emf.createEntityManager();
- em.getTransaction().begin();
- boolean continuer = true;
- int choix, choix2;
- String nom;
- do {
- System.out.println("1. Ajouter Garage");
- System.out.println("2. Ajouter Voiture");
- System.out.println("3. Afficher les Garages");
- System.out.println("4. Afficher les Voitures");
- System.out.println("5. Modifier un garage");
- System.out.println("6. Modifier une voiture");
- System.out.print("Entrez votre choix: ");
- choix = sc.nextInt();
- switch (choix) {
- case 1:
- System.out.print("Entrez le nom du garage: ");
- nom = sc.next();
- Garage g = new Garage(nom);
- em.persist(g);
- System.out.println("Garage ajouté avec succée!");
- em.getTransaction().commit();
- break;
- case 2:
- List<Garage> garages = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
- for (int i = 0; i < garages.size(); i++) {
- System.out.println(i + " - " + garages.get(i).getNom());
- }
- System.out.print("Choisir un garage: ");
- choix2 = sc.nextInt();
- Garage g3 = garages.get(choix2);
- System.out.print("Entrez le nom du voiture: ");
- nom = sc.next();
- Voiture v = new Voiture(nom, g3);
- g3.addVoiture(v);
- em.persist(v);
- System.out.println("Voiture ajouté avec succée!");
- em.getTransaction().commit();
- break;
- case 3:
- List<Garage> garages2 = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
- for (int i = 0; i < garages2.size(); i++) {
- System.out.println(garages2.get(i).getNom());
- }
- break;
- case 4:
- List<Voiture> voitures = em.createNamedQuery("Voiture.findAll", Voiture.class).getResultList();
- for (int i = 0; i < voitures.size(); i++) {
- System.out.println(voitures.get(i).getNom() + " - " + voitures.get(i).getGarage().getNom());
- }
- break;
- case 5:
- List<Garage> garages3 = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
- for (int i = 0; i < garages3.size(); i++) {
- System.out.println(i + " - " + garages3.get(i).getNom());
- }
- System.out.print("Choisir un garage: ");
- choix2 = sc.nextInt();
- System.out.print("Entrez le nouveau nom: ");
- String nvNom = sc.next();
- Garage ga = em.find(Garage.class, garages3.get(choix2).getId());
- ga.setNom(nvNom);
- em.persist(ga);
- em.getTransaction().commit();
- System.out.println("Modification faite avec succées");
- break;
- case 6:
- List<Voiture> voitures2 = em.createNamedQuery("Voiture.findAll", Voiture.class).getResultList();
- for (int i = 0; i < voitures2.size(); i++) {
- System.out.println(i + " - " + voitures2.get(i).getNom() + " | " + voitures2.get(i).getGarage().getNom());
- }
- System.out.print("Choisir un voiture: ");
- choix2 = sc.nextInt();
- System.out.print("Entrez le nouveau nom: ");
- nvNom = sc.next();
- garages3 = em.createNamedQuery("Garage.findAll", Garage.class).getResultList();
- for (int i = 0; i < garages3.size(); i++) {
- System.out.println(i + " - " + garages3.get(i).getNom());
- }
- System.out.print("Choisir un garage: ");
- int choix3 = sc.nextInt();
- ga = em.find(Garage.class, garages3.get(choix3).getId());
- Voiture vo = em.find(Voiture.class, voitures2.get(choix2).getId());
- vo.setNom(nvNom);
- vo.setGarage(ga);
- em.persist(ga);
- em.getTransaction().commit();
- System.out.println("Modification faite avec succées");
- break;
- default:
- continuer = false;
- break;
- }
- System.out.print("Vous voulez continuer ? [O/N]: ");
- String zitoun = sc.next();
- if (zitoun.equals("N"))
- continuer = false;
- } while (continuer);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement