Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////// file bibliotecaPrincipale.java
- package biblioteca;
- import java.io.*;
- import java.util.Vector;
- /*
- * utilizzo la classe vector
- * ovvero un array dinamico riempibile com gli oggetti definiti in questo caso Libro
- * con la possibilità di eliminare gli elementi
- * non c'è un limite di oggetti
- */
- public class bibliotecaPrincipale {
- @SuppressWarnings("rawtypes")
- private static Vector lib = new Vector();
- private static Vector gen = new Vector();
- private static BufferedReader br = null;
- public static void main(String [] args){
- br = new BufferedReader( new InputStreamReader(System.in));
- String menu = "";
- do {
- System.out.print("\n\n");
- System.out.println("Inserisci Libro - 1 ");
- System.out.println("Visualizza Elenco Libri - 2");
- System.out.println("Visualizza Libro - 3");
- System.out.println("Cerca Libro - 4");
- System.out.println("Aggiungi Genere - 5");
- System.out.println("Visualizza Generi - 6");
- System.out.println("Cerca Genere - 7");
- System.out.println("Esci - 0");
- System.out.print("> ");
- try {
- menu = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(menu.equals("1")){
- inserisciLibro();
- } else if(menu.equals("2")){
- visualizzaElencoLibri();
- } else if(menu.equals("3")){
- try {
- visualizzaLibro();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if(menu.equals("4")){
- try {
- cercaLibro();
- } catch(IOException ex){
- ex.printStackTrace();
- }
- } else if(menu.equals("5")){
- try {
- aggiungiGenere();
- } catch(IOException ex){
- ex.printStackTrace();
- }
- } else if(menu.equals("6")){
- visualizzaGeneri();
- } else if(menu.equals("7")){
- try {
- cercaGenere();
- } catch(IOException ex){
- ex.printStackTrace();
- }
- }
- } while(menu.equals("0"));
- }
- private static void cercaGenere() throws IOException{
- br = new BufferedReader( new InputStreamReader(System.in));
- System.out.print("Chiave Ricerca: ");
- String chiave = br.readLine();
- Genere g = null;
- for(int i = 0; i < gen.size(); i++){
- g = (Genere) gen.elementAt(i);
- if(g.cercaGenere(chiave) == true){
- g.getGenere();
- break;
- }
- }
- System.out.print("\n\n");
- main(null);
- }
- private static void visualizzaGeneri() {
- for(int i = 0 ; i < gen.size() ; i++){
- System.out.print("\n");
- Genere g = (Genere) gen.elementAt(i);
- g.getGenere();
- }
- System.out.print("\n\n");
- main(null);
- }
- @SuppressWarnings("unchecked")
- private static void aggiungiGenere() throws IOException{
- br = new BufferedReader( new InputStreamReader(System.in));
- System.out.print("Nome Genere: ");
- String nome = br.readLine();
- Genere genere = new Genere(nome , (gen.size()));
- gen.addElement(genere);
- System.out.println("\n\nGenere Inserito Correttamente!\n");
- main(null);
- }
- private static void cercaLibro() throws IOException{
- br = new BufferedReader( new InputStreamReader(System.in));
- System.out.print("Chiave Ricerca: ");
- String chiave = br.readLine();
- Libro l = null;
- for(int i = 0; i < lib.size(); i++){
- l = (Libro) lib.elementAt(i);
- if(l.cerca(chiave) >= 0){
- l.getLibro();
- break;
- }
- }
- System.out.print("\n\n");
- main(null);
- }
- private static void visualizzaLibro() throws IOException {
- br = new BufferedReader( new InputStreamReader(System.in));
- System.out.print("Posizione Libro Da Visualizzare: ");
- String appoggio = br.readLine();
- System.out.print("\n\n");
- int posizione = Integer.parseInt(appoggio);
- if(posizione <= lib.size()){
- if(lib.size() == 1){
- Libro libro = (Libro) lib.elementAt(0);
- libro.getLibro();
- System.out.print("\n\n");
- main(null);
- }
- Libro libro = (Libro) lib.elementAt(posizione);
- libro.getLibro();
- } else {
- System.out.print("\n\n");
- visualizzaLibro();
- }
- System.out.print("\n\n");
- main(null);
- }
- private static void visualizzaElencoLibri() {
- for(int i = 0 ; i < lib.size() ; i++){
- System.out.print("\n");
- Libro l = (Libro) lib.elementAt(i);
- l.getLibro();
- }
- System.out.print("\n\n");
- main(null);
- }
- @SuppressWarnings("unchecked")
- private static void inserisciLibro() {
- br = new BufferedReader( new InputStreamReader(System.in));
- System.out.print("\nInserisci Libro\n");
- String[] valori = new String[5];
- try{
- do {
- System.out.print("\nTitolo: ");
- valori[0] = br.readLine();
- }while(valori[0].equals(""));
- do {
- System.out.print("\nAutore: ");
- valori[1] = br.readLine();
- }while(valori[1].equals(""));
- do {
- System.out.print("\nEditore: ");
- valori[2] = br.readLine();
- }while(valori[2].equals(""));
- do {
- System.out.print("\nAnno Pubblicazione: ");
- valori[3] = br.readLine();
- }while(valori[3].equals(""));
- do {
- System.out.print("\nGeneri: ");
- valori[4] = br.readLine();
- }while(valori[4].equals(""));
- Libro libro = new Libro(valori[0] , valori[1] , valori[2] , Integer.parseInt(valori[3]) , Integer.parseInt(valori[4]), (lib.size()));
- lib.addElement(libro);
- } catch(IOException ex){
- ex.printStackTrace();
- }
- System.out.println("\n\nLibro Inserito Correttamente!\n");
- main(null);
- }
- }
- ////// file Libro.java
- package biblioteca;
- public class Libro {
- public String titolo;
- public String autore;
- public String editore;
- public int annoPubblicazione;
- private int idGenere;
- private int posizione;
- public Libro(String titolo , String autore , String editore , int annoPubblicazione , int genere , int posizione){
- this.titolo = titolo;
- this.autore = autore;
- this.editore = editore;
- this.annoPubblicazione = annoPubblicazione;
- this.idGenere = genere;
- this.posizione = posizione;
- }
- public void getLibro(){
- System.out.println("Posizione: " + posizione);
- System.out.println("Titolo: " + titolo);
- System.out.println("Autore: " + autore);
- System.out.println("Editore: " + editore);
- System.out.println("Anno Pubblicazione: " + annoPubblicazione);
- }
- public int cerca(String chiave){
- if((chiave.equals(this.titolo)) || (chiave.equals(this.autore)) || (chiave.equals(this.editore)) || (chiave.equals(this.annoPubblicazione)) || (chiave.equals(this.idGenere))){
- return this.posizione;
- }
- return -1;
- }
- }
- ///// file Genere.java
- package biblioteca;
- public class Genere {
- private String nome;
- private int id;
- public Genere(String nome , int id){
- this.nome = nome;
- this.id = id;
- }
- public void getGenere() {
- System.out.println("ID: " + id);
- System.out.println("Nome: " + nome);
- }
- public boolean cercaGenere(String nome){
- if(nome.equals(this.nome)){
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement