import java.util.*;

public class Ludoteca {

  // Rappresentazione degli oggetti Ludoteca
  
  private String nome;
  private Map map; //uso l'intefaccia map
  
  // Costruttore
  public Ludoteca(String n) {
    nome = n;
    map = new HashMap();  //uso una classe concreta che implementa Map
  }

  // funzioni pubbliche della classe
  public String nome() {
    return nome;
  }

  public boolean presente(Gioco g) {
    return map.containsKey(g);
  }

  public Persona chiHaIlGioco(Gioco g) {
    return (Persona)map.get(g);
  }

  public int numeroGiochi() {
    return map.size();
  }

  public int numeroGiochiPrestati() {
    int cont = 0;
    Iterator it = map.entrySet().iterator();
    while(it.hasNext()) {
      Map.Entry e = (Map.Entry)it.next();
      if (e.getValue() != null) cont++;
    }
    return cont;
  }

  public void aggiungiGioco(Gioco g) {
    map.put(g,null);
  }

  public void eliminaGioco(Gioco g) {
    map.remove(g);
  }

  public void prestaGioco(Gioco g, Persona p) {
    if (!map.containsKey(g))
      throw new RuntimeException(
        "Errore, prestaGioco: il gioco non e' presente");
    else if (map.get(g) != null)
      throw new RuntimeException(
        "Errore, prestaGioco: il gioco e' gia' in prestito");
    else map.put(g,p);
  }

  public void restituisciGioco(Gioco g) {
    if (!map.containsKey(g))
      throw new RuntimeException(
        "Errore, restituisciGioco: il gioco non e' presente");
    else if (map.get(g) == null)
          throw new RuntimeException(
            "Errore, restituisciGioco: il gioco non e' in prestito");
    else map.put(g,null);
  }

  public Gioco[] tuttiIGiochi() {
    Gioco[] ris = new Gioco[numeroGiochi()];
    Object[] ao = map.keySet().toArray();
    for(int i = 0; i < ao.length; i++)
      ris[i] = (Gioco)ao[i];
    return ris;
  }

}
