// File: Biblioteca.java
// Time-stamp: "2003-04-10 12:19:36 calvanes"
// Scopo: esame del 08/04/2003 - compito A - domanda 1

class Libro {
  String titolo;
  boolean prestato;
  String inPrestitoA;
  Libro next;
}

public class Biblioteca {

  // rappresentazione degli oggetti
  private String nome;
  private Libro libri;
  
  // metodi pubblici

  public Biblioteca(String b) {   // realizza la funzionalita' crea
    nome = b;
    libri = null;
  }
  
  public String nome() {
    return nome;
  }
  
  public boolean dellaBiblioteca(String t) {
    Libro temp = libri;
    while (temp != null) {
      if (temp.titolo.equals(t))
        return true;
      temp = temp.next;
    }
    return false;
  }

  public String inPrestito(String t) throws EccezioneBiblioteca {
    Libro temp = libri;
    while(temp != null) {
      if (temp.titolo.equals(t))
        if (temp.prestato)
          return temp.inPrestitoA;
        else
          return null;
      temp = temp.next;
    }
    throw new EccezioneBiblioteca("Libro inesistente");
  }
  
  public void aggiungi(String t) throws EccezioneBiblioteca {
    if (dellaBiblioteca(t))
      throw new EccezioneBiblioteca("Libro gia' presente");
    else {
      //inserimento in testa
      Libro temp = new Libro();
      temp.titolo = t;
      temp.prestato = false;
      temp.inPrestitoA = null;
      temp.next = libri;
      libri = temp;
    }
  }
  
  public void elimina (String t) throws EccezioneBiblioteca {
    Libro temp = new Libro();    // creazione nodo generatore
    temp.next = libri;
    libri = temp;

    boolean trovato = false;
    while (temp.next != null && !trovato)
      if (temp.next.titolo.equals(t))
        if (temp.next.prestato) {
          libri = libri.next;      // elimina il nodo generatore
          throw new EccezioneBiblioteca("Libro in prestito");
        } else {
          temp.next = temp.next.next;
          trovato = true;
        }
      else
        temp = temp.next;

    libri = libri.next;  // elimina il nodo generatore
  }
 
  public void presta(String t, String p) throws EccezioneBiblioteca {
    Libro temp = libri;
    while (temp != null)
      if (temp.titolo.equals(t))
        if (temp.prestato)
          throw new EccezioneBiblioteca("Libro gia' prestato");
        else {
          temp.prestato = true;
          temp.inPrestitoA = p;
          return;
        }
      else
        temp = temp.next;

    throw new EccezioneBiblioteca("Libro inesistente");
  }
  
  public int numLibriPrestati(String p) {
    Libro temp = libri;
    int num = 0;
    while (temp != null) {
      if (temp.prestato && temp.inPrestitoA.equals(p))
        num++;
      temp = temp.next;
    }
    return num;
  }
  
  public String[] libriPrestati(String p) {
    String[] titoli = new String[numLibriPrestati(p)];
    Libro temp = libri;
    int i = 0;
    while (temp != null) {
      if (temp.prestato && temp.inPrestitoA.equals(p)) {
        titoli[i] = temp.titolo;
        i++;
      }
      temp = temp.next;
    }
    return titoli;
  }
  
}
