Advertisement
worlok110

esercitazione biblioteca 4ia

Oct 20th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. ////// file bibliotecaPrincipale.java
  2. package biblioteca;
  3.  
  4. import java.io.*;
  5. import java.util.Vector;
  6.  
  7. /*
  8.  * utilizzo la classe vector
  9.  * ovvero un array dinamico riempibile com gli oggetti definiti in questo caso Libro
  10.  * con la possibilità di eliminare gli elementi
  11.  * non c'è un limite di oggetti
  12.  */
  13.  
  14. public class bibliotecaPrincipale {
  15.  
  16.    
  17.    
  18.     @SuppressWarnings("rawtypes")
  19.     private static Vector lib = new Vector();
  20.     private static Vector gen = new Vector();
  21.     private static BufferedReader br = null;
  22.    
  23.     public static void main(String [] args){
  24.         br = new BufferedReader( new InputStreamReader(System.in));
  25.         String menu = "";
  26.         do {
  27.             System.out.print("\n\n");
  28.             System.out.println("Inserisci Libro - 1 ");
  29.             System.out.println("Visualizza Elenco Libri - 2");
  30.             System.out.println("Visualizza Libro - 3");
  31.             System.out.println("Cerca Libro - 4");
  32.             System.out.println("Aggiungi Genere - 5");
  33.             System.out.println("Visualizza Generi - 6");
  34.             System.out.println("Cerca Genere - 7");
  35.             System.out.println("Esci - 0");
  36.             System.out.print("> ");
  37.            
  38.             try {
  39.                 menu = br.readLine();
  40.             } catch (IOException e) {
  41.                 e.printStackTrace();
  42.             }
  43.            
  44.             if(menu.equals("1")){
  45.                 inserisciLibro();
  46.             } else if(menu.equals("2")){
  47.                 visualizzaElencoLibri();
  48.             } else if(menu.equals("3")){
  49.                 try {
  50.                     visualizzaLibro();
  51.                 } catch (IOException e) {
  52.                     e.printStackTrace();
  53.                 }
  54.             } else if(menu.equals("4")){
  55.                 try {
  56.                     cercaLibro();
  57.                 } catch(IOException ex){
  58.                     ex.printStackTrace();
  59.                 }
  60.             } else if(menu.equals("5")){
  61.                 try {
  62.                     aggiungiGenere();
  63.                 } catch(IOException ex){
  64.                     ex.printStackTrace();
  65.                 }
  66.             } else if(menu.equals("6")){
  67.                 visualizzaGeneri();
  68.             } else if(menu.equals("7")){
  69.                 try {
  70.                     cercaGenere();
  71.                 } catch(IOException ex){
  72.                     ex.printStackTrace();
  73.                 }
  74.             }
  75.         } while(menu.equals("0"));
  76.        
  77.     }
  78.  
  79.     private static void cercaGenere() throws IOException{
  80.         br = new BufferedReader( new InputStreamReader(System.in));
  81.         System.out.print("Chiave Ricerca: ");
  82.         String chiave = br.readLine();
  83.         Genere g = null;
  84.         for(int i = 0; i < gen.size(); i++){
  85.             g = (Genere) gen.elementAt(i);
  86.             if(g.cercaGenere(chiave) == true){
  87.                 g.getGenere();
  88.                 break;
  89.             }
  90.         }
  91.         System.out.print("\n\n");
  92.         main(null);    
  93.     }
  94.  
  95.     private static void visualizzaGeneri() {
  96.         for(int i = 0 ; i < gen.size() ; i++){
  97.             System.out.print("\n");
  98.             Genere g = (Genere) gen.elementAt(i);
  99.             g.getGenere();
  100.         }
  101.         System.out.print("\n\n");
  102.         main(null);
  103.        
  104.     }
  105.  
  106.     @SuppressWarnings("unchecked")
  107.     private static void aggiungiGenere() throws IOException{
  108.         br = new BufferedReader( new InputStreamReader(System.in));
  109.         System.out.print("Nome Genere: ");
  110.         String nome = br.readLine();
  111.         Genere genere = new Genere(nome , (gen.size()));
  112.         gen.addElement(genere);
  113.         System.out.println("\n\nGenere Inserito Correttamente!\n");
  114.         main(null);
  115.     }
  116.  
  117.     private static void cercaLibro() throws IOException{
  118.         br = new BufferedReader( new InputStreamReader(System.in));
  119.         System.out.print("Chiave Ricerca: ");
  120.         String chiave = br.readLine();
  121.         Libro l = null;
  122.         for(int i = 0; i < lib.size(); i++){
  123.             l = (Libro) lib.elementAt(i);
  124.             if(l.cerca(chiave) >= 0){
  125.                 l.getLibro();
  126.                 break;
  127.             }
  128.         }
  129.         System.out.print("\n\n");
  130.         main(null);
  131.     }
  132.  
  133.     private static void visualizzaLibro() throws IOException {
  134.         br = new BufferedReader( new InputStreamReader(System.in));
  135.         System.out.print("Posizione Libro Da Visualizzare: ");
  136.         String appoggio = br.readLine();
  137.         System.out.print("\n\n");
  138.         int posizione = Integer.parseInt(appoggio);
  139.         if(posizione <= lib.size()){
  140.             if(lib.size() == 1){
  141.                 Libro libro = (Libro) lib.elementAt(0);
  142.                 libro.getLibro();
  143.                 System.out.print("\n\n");
  144.                 main(null);
  145.             }
  146.             Libro libro = (Libro) lib.elementAt(posizione);
  147.             libro.getLibro();
  148.         } else {
  149.             System.out.print("\n\n");
  150.             visualizzaLibro();
  151.         }
  152.         System.out.print("\n\n");
  153.         main(null);
  154.     }
  155.  
  156.     private static void visualizzaElencoLibri() {
  157.         for(int i = 0 ; i < lib.size() ; i++){
  158.             System.out.print("\n");
  159.             Libro l = (Libro) lib.elementAt(i);
  160.             l.getLibro();
  161.         }
  162.         System.out.print("\n\n");
  163.         main(null);
  164.     }
  165.  
  166.     @SuppressWarnings("unchecked")
  167.     private static void inserisciLibro() {
  168.         br = new BufferedReader( new InputStreamReader(System.in));
  169.         System.out.print("\nInserisci Libro\n");
  170.         String[] valori = new String[5];
  171.         try{
  172.             do {
  173.                 System.out.print("\nTitolo: ");
  174.                 valori[0] = br.readLine();
  175.             }while(valori[0].equals(""));
  176.            
  177.             do {
  178.                 System.out.print("\nAutore: ");
  179.                 valori[1] = br.readLine();
  180.             }while(valori[1].equals(""));
  181.            
  182.             do {
  183.                 System.out.print("\nEditore: ");
  184.                 valori[2] = br.readLine();
  185.             }while(valori[2].equals(""));
  186.            
  187.             do {
  188.                 System.out.print("\nAnno Pubblicazione: ");
  189.                 valori[3] = br.readLine();
  190.             }while(valori[3].equals(""));
  191.            
  192.             do {
  193.                
  194.                 System.out.print("\nGeneri: ");
  195.                 valori[4] = br.readLine();
  196.             }while(valori[4].equals(""));
  197.            
  198.             Libro libro = new Libro(valori[0] , valori[1] , valori[2] , Integer.parseInt(valori[3]) , Integer.parseInt(valori[4]), (lib.size()));
  199.             lib.addElement(libro);
  200.         } catch(IOException ex){
  201.             ex.printStackTrace();
  202.         }
  203.         System.out.println("\n\nLibro Inserito Correttamente!\n");
  204.         main(null);
  205.     }
  206.    
  207.    
  208.  
  209.    
  210. }
  211.  
  212.  
  213. ////// file Libro.java
  214.  
  215. package biblioteca;
  216.  
  217.  
  218.  
  219. public class Libro {
  220.  
  221.     public String titolo;
  222.     public String autore;
  223.     public String editore;
  224.     public int annoPubblicazione;
  225.     private int idGenere;
  226.     private int posizione;
  227.  
  228.    
  229.     public Libro(String titolo , String autore , String editore , int annoPubblicazione , int genere , int posizione){
  230.         this.titolo = titolo;
  231.         this.autore = autore;
  232.         this.editore = editore;
  233.         this.annoPubblicazione = annoPubblicazione;
  234.         this.idGenere = genere;
  235.         this.posizione = posizione;
  236.        
  237.     }
  238.    
  239.     public void getLibro(){
  240.         System.out.println("Posizione: " + posizione);
  241.         System.out.println("Titolo: "  + titolo);
  242.         System.out.println("Autore: " + autore);
  243.         System.out.println("Editore: " + editore);
  244.         System.out.println("Anno Pubblicazione: " + annoPubblicazione);
  245.        
  246.     }
  247.    
  248.    
  249.     public int cerca(String chiave){
  250.         if((chiave.equals(this.titolo)) || (chiave.equals(this.autore)) || (chiave.equals(this.editore)) || (chiave.equals(this.annoPubblicazione)) || (chiave.equals(this.idGenere))){
  251.             return this.posizione;
  252.            
  253.         }
  254.         return -1;
  255.     }
  256.    
  257. }
  258.  
  259.  
  260.  
  261. ///// file Genere.java
  262.  
  263. package biblioteca;
  264.  
  265.  
  266. public class Genere {
  267.    
  268.     private String nome;
  269.     private int id;
  270.     public Genere(String nome , int id){
  271.         this.nome = nome;
  272.         this.id = id;
  273.     }
  274.    
  275.     public void getGenere() {
  276.         System.out.println("ID: " + id);
  277.         System.out.println("Nome: " + nome);
  278.        
  279.     }
  280.  
  281.     public boolean cercaGenere(String nome){
  282.         if(nome.equals(this.nome)){
  283.             return true;
  284.         }
  285.        
  286.         return false;
  287.        
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement