Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.Scanner;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import model.Adresse;
- import model.Article;
- import model.Bibliotheque;
- public class Test {
- static EntityManagerFactory emf = null;
- static EntityManager em = null;
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- // Création du EntityManagerFactory
- emf = Persistence.createEntityManagerFactory("GestionBiblio2");
- // Création du EntityManagerFactory
- em = emf.createEntityManager();
- // Overture des transactions
- em.getTransaction().begin();
- String continuer = "N";
- int choix = 10;
- do {
- System.out.println("---------------------------------");
- System.out.println("1 - Afficher");
- System.out.println("\t--------");
- System.out.println("2 - Ajouter");
- System.out.println("\t--------");
- System.out.println("3 - Modifier");
- System.out.println("\t--------");
- System.out.println("4 - Supprimer");
- System.out.println("\t--------");
- System.out.println("5 - Quitter");
- System.out.println("---------------------------------");
- System.out.print("Entrez votre choix: ");
- choix = sc.nextInt();
- switch (choix) {
- case 1:
- System.out.println("\t--------");
- System.out.println("1 - Afficher les articles");
- System.out.println("2 - Afficher les bibliotheques");
- System.out.println("3 - Afficher les adresses");
- System.out.println("\t--------");
- System.out.println("4 - Retour");
- System.out.println("\t--------");
- do {
- System.out.print("Entrez votre choix: ");
- choix = sc.nextInt();
- if (choix > 4 || choix < 1)
- System.out.println("Choix invalide!");
- } while (choix > 4 || choix < 1);
- switch (choix) {
- case 1:
- AfficherArticles();
- break;
- case 2:
- AfficherBibliotheques();
- break;
- case 3:
- AfficherAdresses();
- break;
- }
- break;
- case 2:
- System.out.println("\t--------");
- System.out.println("1 - Ajouter une article");
- System.out.println("2 - Ajouter une bibliotheque");
- System.out.println("3 - Ajouter une adresse");
- System.out.println("\t--------");
- System.out.println("4 - Retour");
- System.out.println("\t--------");
- do {
- System.out.print("Entrez votre choix: ");
- choix = sc.nextInt();
- if (choix > 4 || choix < 1)
- System.out.println("Choix invalide!");
- } while (choix > 4 || choix < 1);
- switch (choix) {
- case 1:
- AjouterArticle();
- break;
- case 2:
- AjouterBiblio();
- break;
- case 3:
- AjouterAdresse();
- break;
- }
- break;
- case 3:
- System.out.println("\t--------");
- System.out.println("1 - Modifier une article");
- System.out.println("2 - Modifier une bibliotheque");
- System.out.println("3 - Modifier une adresse");
- System.out.println("\t--------");
- System.out.println("4 - Retour");
- System.out.println("\t--------");
- do {
- System.out.print("Entrez votre choix: ");
- choix = sc.nextInt();
- if (choix > 4 || choix < 1)
- System.out.println("Choix invalide!");
- } while (choix > 4 || choix < 1);
- break;
- case 4:
- System.out.println("\t--------");
- System.out.println("1 - Supprimer une article");
- System.out.println("2 - Supprimer une bibliotheque");
- System.out.println("3 - Supprimer une adresse");
- System.out.println("\t--------");
- System.out.println("4 - Retour");
- System.out.println("\t--------");
- do {
- System.out.print("Entrez votre choix: ");
- choix = sc.nextInt();
- if (choix > 4 || choix < 1)
- System.out.println("Choix invalide!");
- } while (choix > 4 || choix < 1);
- switch (choix) {
- case 1:
- List<Article> articles = em.createNamedQuery("Article.findAll", Article.class).getResultList();
- Article article = new Article();
- AfficherArticles2();
- do {
- System.out.print("Choisissez votre choix: ");
- choix = sc.nextInt();
- } while (choix < 1 || choix > articles.size());
- article = articles.get(choix-1);
- String code = article.getCode();
- em.remove(article);
- System.out.println("L'article " + code + " a été supprimer avec succée");
- break;
- case 2:
- List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class)
- .getResultList();
- Bibliotheque biblio = new Bibliotheque();
- AfficherBibliotheques2();
- do {
- System.out.print("Choisissez votre choix: ");
- choix = sc.nextInt();
- } while (choix < 1 || choix > biblios.size());
- biblio = biblios.get(choix-1);
- String nom = biblio.getNom();
- em.remove(biblio);
- System.out.println("La bibliotheque " + nom + " a été supprimer avec succée");
- break;
- case 3:
- List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
- Adresse adresse = new Adresse();
- AfficherAdresses2();
- do {
- System.out.print("Choisissez votre choix: ");
- choix = sc.nextInt();
- } while (choix < 1 || choix > adresses.size());
- adresse = adresses.get(choix-1);
- String codeAdr = adresse.getCode();
- em.remove(adresse);
- 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");
- break;
- }
- case 5:
- System.out.println("Fin du programme!");
- em.getTransaction().commit();
- System.exit(0);
- break;
- }
- System.out.print("Voulez-vous continuer ? [O/N] ");
- continuer = sc.next();
- } while (continuer.equals("O"));
- em.getTransaction().commit();
- em.close();
- emf.close();
- System.out.println("Fin du programme!");
- System.exit(0);
- }
- private static void AjouterArticle() {
- List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class).getResultList();
- if (biblios.isEmpty()) {
- System.out.println("Il n'existe aucune bibliotheque!");
- } else {
- int choix;
- Bibliotheque biblio = new Bibliotheque();
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- System.out.print("Entrez le code : ");
- String code = sc.next();
- System.out.print("Entrez la description: ");
- String description = sc.next();
- AfficherBibliotheques2();
- do {
- System.out.print("Choisissez votre choix: ");
- choix = sc.nextInt();
- } while (choix < 1 || choix > biblios.size());
- biblio = biblios.get(choix);
- System.out.println("Vous avez choisi la bibliotheque: " + biblio.getNom());
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- Article article = new Article(code, description, biblio);
- biblio.addArticle(article);
- em.persist(article);
- System.out.println("L' article " + article.getCode() + " a été ajouté avec succée");
- System.out.println();
- }
- }
- private static void AjouterBiblio() {
- String choix;
- int choix2;
- Adresse adresse = new Adresse();
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- System.out.print("Entrez le nom du bibliotheque: ");
- String nom = sc.next();
- do {
- System.out.print("Voulez-vous choisir une adresse ? [O/N] ");
- choix = sc.next();
- } while (!choix.equals("O") || !choix.equals("N"));
- if (choix.equals("O")) {
- List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
- AfficherAdresses2();
- do {
- System.out.print("Choisissez votre choix: ");
- choix2 = sc.nextInt();
- } while (choix2 < 1 || choix2 > adresses.size());
- adresse = adresses.get(choix2);
- System.out.println("Vous avez choisi l'adresse du code: " + adresse.getCode());
- } else {
- adresse = AjouterAdresse2();
- }
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- Bibliotheque biblio = new Bibliotheque(nom, adresse);
- em.persist(biblio);
- System.out.println("La bibliotheque " + nom + " a été ajouté avec succée");
- System.out.println();
- }
- private static Adresse AjouterAdresse2() {
- System.out.print("Entrez le code de l'adresse : ");
- String code = sc.next();
- System.out.print("Entrez la rue : ");
- String rue = sc.next();
- System.out.print("Entrez la ville : ");
- String ville = sc.next();
- Adresse adresse = new Adresse(code, rue, ville);
- em.persist(adresse);
- System.out.println("L'adresse " + code + " a été ajouté avec succée");
- return adresse;
- }
- private static void AjouterAdresse() {
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- System.out.print("Entrez le code de l'adresse : ");
- String code = sc.next();
- System.out.print("Entrez la rue : ");
- String rue = sc.next();
- System.out.print("Entrez la ville : ");
- String ville = sc.next();
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- Adresse adresse = new Adresse(code, rue, ville);
- em.persist(adresse);
- System.out.println("L'adresse " + code + " a été ajouté avec succée");
- System.out.println();
- }
- private static void AfficherAdresses2() {
- List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
- System.out.println("* - |\tCode\t|\tRue\t|\tVille\t|");
- int i = 0;
- for (Adresse a : adresses) {
- System.out.println(++i + " - |\t" + a.getCode() + "\t|\t" + a.getRue() + "\t|\t" + a.getVille() + "\t|");
- }
- }
- private static void AfficherAdresses() {
- List<Adresse> adresses = em.createNamedQuery("Adresse.findAll", Adresse.class).getResultList();
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- System.out.println("|\tCode\t|\tRue\t|\tVille\t|");
- for (Adresse a : adresses) {
- System.out.println("|\t" + a.getCode() + "\t|\t" + a.getRue() + "\t|\t" + a.getVille() + "\t|");
- }
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- }
- private static void AfficherBibliotheques2() {
- List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class).getResultList();
- System.out.println("* - |\tNom\t|\tAdresse\t|");
- int i = 0;
- for (Bibliotheque b : biblios) {
- System.out.println(++i + " - |\t" + b.getNom() + "\t|\t" + b.getAdresse().getCode() + "\t|");
- }
- }
- private static void AfficherBibliotheques() {
- List<Bibliotheque> biblios = em.createNamedQuery("Bibliotheque.findAll", Bibliotheque.class).getResultList();
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- System.out.println("|\tNom\t|\tAdresse\t|");
- for (Bibliotheque b : biblios) {
- System.out.println("|\t" + b.getNom() + "\t|\t" + b.getAdresse().getCode() + "\t|");
- }
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- }
- private static void AfficherArticles2() {
- List<Article> articles = em.createNamedQuery("Article.findAll", Article.class).getResultList();
- System.out.println("* - |\tCode\t|\tBiblioteque\t|\tDescription\t|");
- int i = 0;
- for (Article a : articles) {
- System.out.println(++i + " - |\t" + a.getCode() + "\t|\t" + a.getBibliotheque().getNom() + "\t|\t"
- + a.getDescription() + "\t|");
- }
- }
- private static void AfficherArticles() {
- List<Article> articles = em.createNamedQuery("Article.findAll", Article.class).getResultList();
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- System.out.println("|\tCode\t|\tBiblioteque\t|\tDescription\t|");
- for (Article a : articles) {
- System.out.println("|\t" + a.getCode() + "\t|\t" + a.getBibliotheque().getNom() + "\t|\t"
- + a.getDescription() + "\t|");
- }
- System.out.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement