Advertisement
spiny94

Untitled

Sep 8th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package Library;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Queue;
  10. import java.util.TreeMap;
  11.  
  12. public class Biblioteca {
  13.     private String nome;
  14.     private Map<String,Libro> dotazione;
  15.     private Map<Integer,Utente> utenti;
  16.     private Map<Libro,Utente> prestiti;
  17.     private Map<Libro,Queue<Utente>> richieste;
  18.  
  19.     public Biblioteca (String n) {
  20.         nome = n;
  21.         dotazione = new HashMap<>();
  22.         utenti = new TreeMap<>();
  23.         prestiti = new TreeMap<>();
  24.         richieste = new TreeMap<>();
  25.     }
  26.    
  27.     public void addLibro(Libro lib) throws InvalidIsbn {
  28.         String i = lib.getIsbn();
  29.         Libro l = dotazione.get(i);
  30.         if(l == null)
  31.             dotazione.put(i, lib);
  32.         else
  33.             throw new InvalidIsbn();
  34.     }
  35.    
  36.     public Libro getLibro(String isbn){
  37.         return dotazione.get(isbn);
  38.     }
  39.  
  40.    
  41.     public List<Libro> libriPerAutore(){
  42.         List<Libro> elenco = new ArrayList<Libro>(dotazione.values());
  43.         Collections.sort(elenco);
  44.         return elenco;
  45.     }
  46.    
  47.     public void addUtente(Utente ut) throws InvalidCode{
  48.         Integer c = ut.getCodice();
  49.         Utente u = utenti.get(c);
  50.         if(u == null)
  51.             utenti.put(c, ut);
  52.         else
  53.             throw new InvalidCode();
  54.     }
  55.    
  56.     public List<Utente> utenti(){
  57.         return new ArrayList<Utente>(utenti.values());
  58.     }
  59.    
  60.     public Libro prestito(int cu, String is) throws InvalidCode, InvalidIsbn {
  61.         Utente u = utenti.get(cu);
  62.         if(u == null)
  63.             throw new InvalidCode();
  64.         Libro l = dotazione.get(is);
  65.         if(l == null)
  66.             throw new InvalidIsbn();
  67.         Utente up = prestiti.get(l);
  68.         if(up == null) {
  69.             prestiti.put(l, u);
  70.             u.addLibro(l);
  71.             return l;
  72.         }
  73.         else if(u != up) {
  74.                 Queue<Utente> q = richieste.get(l);
  75.                 if(q == null)
  76.                     q = new LinkedList<>();
  77.                     q.add(u);
  78.                     richieste.put(l, q);
  79.                     return null;
  80.             }
  81.             else
  82.                 return null;
  83.     }
  84.    
  85.     public Libro restituzione(int cu, String is) throws InvalidCode, InvalidIsbn {
  86.         Utente u = utenti.get(cu);
  87.         if(u == null)
  88.             throw new InvalidCode();
  89.         Libro l = dotazione.get(is);
  90.         if(l == null)
  91.             throw new InvalidIsbn();
  92.         Utente up = prestiti.get(l);
  93.         if(up == u){
  94.             prestiti.remove(l);
  95.             u.retLibro(l);
  96.             Queue<Utente> q = richieste.get(l);
  97.             if(q != null && !q.isEmpty()){
  98.                 Utente ur = q.remove();
  99.                 prestiti.put(l, ur);
  100.                 ur.addLibro(l);
  101.             }
  102.             return l;
  103.         }
  104.         else
  105.             return null;
  106.     }
  107.    
  108.     public Queue<Utente> getRichieste(Libro l) {
  109.         return richieste.get(l);
  110.     }
  111.    
  112.     public List<Libro> elencoPrestiti() {
  113.         return new ArrayList<>(prestiti.keySet());
  114.     }
  115.    
  116.     public List<Libro> elencoRichieste() {
  117.         ArrayList<Libro> keys = new ArrayList<>(richieste.keySet());
  118.         for(int j=0 ; j<keys.size() ; j++)
  119.             if(richieste.get(keys.get(j)).isEmpty())
  120.                 keys.remove(j);
  121.         return keys;
  122.     }
  123. }
  124. package Library;
  125.  
  126. public class Libro implements Comparable<Libro>{
  127.     private String autori;
  128.     private String titolo;
  129.     private String editore;
  130.     private String isbn;
  131.  
  132.     public Libro (String a, String t, String e, String i) {
  133.         autori = a;
  134.         titolo = t;
  135.         editore = e;
  136.         isbn = i;
  137.     }
  138.    
  139.     public String getIsbn () {
  140.         return isbn;
  141.     }
  142.    
  143.     public String getAutori () {
  144.         return autori;
  145.     }
  146.    
  147.     public String toString(){
  148.         return autori + ";" + titolo + ";"+ editore + ";"+ isbn;
  149.     }
  150.    
  151.     public int compareTo(Libro l) {
  152.         return autori.compareTo(l.getAutori());
  153.     }
  154. }
  155. package Library;
  156.  
  157. import java.util.Collections;
  158. import java.util.LinkedList;
  159. import java.util.List;
  160.  
  161. public class Utente {
  162.     int codice;
  163.     String nome;
  164.     String cognome;
  165.     List<Libro> libri;
  166.  
  167.     public Utente (int cod, String n , String c ){
  168.         codice = cod;
  169.         nome = n;
  170.         cognome = c;
  171.         libri = new LinkedList<>();
  172.     }
  173.    
  174.     public Integer getCodice() {
  175.         return codice;
  176.     }
  177.    
  178.     public String toString(){
  179.         return codice + "- " + nome + " "+ cognome;
  180.     }
  181.    
  182.     public void addLibro(Libro l) {
  183.         libri.add(l);
  184.     }
  185.    
  186.     public void retLibro(Libro l) {
  187.         libri.remove(l);
  188.     }
  189.    
  190.     public List<Libro> prestiti() {
  191.         Collections.sort(libri);
  192.         return libri;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement