Advertisement
filipao223

ex3_ficha7_POO_feito

Oct 22nd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.82 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ficha7_3;
  7.  
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author Filipe
  13.  */
  14.  
  15. class Biblioteca{
  16.     ArrayList<Livro> todosLivros = new ArrayList<>();
  17.     public Biblioteca(){}
  18. }
  19.  
  20. class Area extends Biblioteca{
  21.  
  22.     ArrayList<Livro> listaLivros = new ArrayList<>();
  23.    
  24.     protected String nomeArea;
  25.    
  26.     public Area(String nome){
  27.    
  28.         this.nomeArea = nome;
  29.     }
  30. }
  31.  
  32. class Livro{
  33.    
  34.     protected String nome, autor;
  35.     protected int cota;
  36.    
  37.     private int numLeitorReq = -1;
  38.    
  39.     public Livro(String nome, String autor, int cota){
  40.    
  41.         this.nome = nome;
  42.         this.autor = autor;
  43.         this.cota = cota;
  44.     }
  45.    
  46.     public int getNumLeitorReq(){
  47.         return numLeitorReq;
  48.     }
  49.    
  50.     public void setNumLeitorReq(int novo){
  51.         this.numLeitorReq = novo;
  52.     }
  53.    
  54.     @Override
  55.     public String toString(){
  56.    
  57.         return this.nome + "|" + this.autor + "|" + this.cota;
  58.     }
  59. }
  60.  
  61. class Leitor{
  62.  
  63.     protected String nome, morada, email, tipoLeitor;
  64.     protected int idade, telefone, numLeitor;
  65.    
  66.     ArrayList<String> infoLivroReq = new ArrayList<>();
  67.    
  68.     public Leitor(int numLeitor, String tipoLeitor, String nome, String morada, String email){
  69.         this.email = email;
  70.         this.morada = morada;
  71.         this.nome = nome;
  72.         this.tipoLeitor = tipoLeitor;
  73.         this.numLeitor = numLeitor;
  74.     }
  75.    
  76.     public Leitor(int numLeitor, String tipoLeitor){
  77.         this(numLeitor, tipoLeitor, "Vazio", "Vazio", "Vazio");
  78.     }
  79.    
  80.     protected int reqLivroMain(int cota, Biblioteca b, String data, int numLeitor){
  81.         for(Livro livro: b.todosLivros){
  82.             //Para o caso de querer fazer mais operaçoes com Livro livro
  83.             if(livro.cota == cota){
  84.                 if(livro.getNumLeitorReq()!= -1) return -1;
  85.                 infoLivroReq.add(Integer.toString(cota) + "_" + data);
  86.                 livro.setNumLeitorReq(numLeitor);
  87.                 return 1;
  88.             }
  89.         }
  90.         return 0;
  91.     }
  92.    
  93.     protected int entLivroMain(int cota, Biblioteca b){
  94.        
  95.         //Procura o livro em infoLivroReq
  96.         for(String info: infoLivroReq){
  97.             String tokens[] = info.split("_");
  98.  
  99.             if (Objects.equals(tokens[0], Integer.toString(cota))){
  100.                 //È o livro
  101.                 infoLivroReq.remove(info);
  102.                 for(Livro livro:b.todosLivros){
  103.                     if(livro.cota==cota) livro.setNumLeitorReq(-1);
  104.                 }
  105.                 return 1;
  106.             }
  107.         }
  108.        
  109.         //Altera os dados de quem tem o livro em Livro
  110.         for(Livro livro: b.todosLivros){
  111.        
  112.             if(livro.cota == cota) livro.setNumLeitorReq(-1);
  113.         }
  114.        
  115.         return 0;
  116.     }
  117.    
  118.     public ArrayList<Livro> checkData(Biblioteca b, String data){
  119.        
  120.         ArrayList<Livro> livrosPorEnt = new ArrayList<>();
  121.         String dataAtual[] = data.split("/");
  122.        
  123.        
  124.         long dataAtualDias = (Integer.parseInt(dataAtual[2])*365) + (Integer.parseInt(dataAtual[1])*30) + Integer.parseInt(dataAtual[0]);
  125.         //Procura livro
  126.         for(String info: this.infoLivroReq){
  127.            
  128.             //Obtem todas as cotas
  129.            
  130.             String tokens[] = info.split("_");
  131.             int cota=0;
  132.            
  133.             for(Livro livro:b.todosLivros){
  134.                 if(Integer.parseInt(tokens[0]) == livro.cota){
  135.                     cota = livro.cota;
  136.                     break;
  137.                 }
  138.             }
  139.             if(Integer.parseInt(tokens[0]) == cota){
  140.                
  141.                 //Converte em milissegundos a data de requisiçao
  142.                 String dataReq[] = tokens[1].split("/");
  143.  
  144.                 long dataReqDias = (Integer.parseInt(dataReq[2])*365) + (Integer.parseInt(dataReq[1])*30) + Integer.parseInt(dataReq[0]);
  145.  
  146.                 //Faz a diferença entre as datas, se for maior que msEm60Dias,já passou a data de entrega
  147.                 if((dataAtualDias-dataReqDias)>60){
  148.                    
  149.                     for(Livro livro:b.todosLivros){
  150.                        
  151.                         if(livro.cota==cota) livrosPorEnt.add(livro);
  152.                     }
  153.                 }
  154.             }
  155.            
  156.         }
  157.        
  158.         return livrosPorEnt;
  159.     }
  160. }
  161.  
  162. class Aluno extends Leitor{
  163.  
  164.     protected static int numMaxReq = 3;
  165.     protected int contReq = 0;
  166.    
  167.     public Aluno(int numLeitor, String tipoLeitor, String nome, String morada, String email){
  168.         super(numLeitor, tipoLeitor, nome, morada, email);
  169.     }
  170.    
  171.     public Aluno(int numLeitor, String tipoLeitor){
  172.         super(numLeitor, tipoLeitor);
  173.     }
  174.    
  175.     public void reqLivro(int cota, Biblioteca b, String data){
  176.         if(contReq>=numMaxReq) System.out.println("Numero maximo de livros requisitados.");
  177.        
  178.         else{
  179.             int retValue = reqLivroMain(cota, b, data, this.numLeitor);
  180.            
  181.             if(retValue == -1) System.out.println("Este livro já se encontra requisitado.");
  182.             else if(retValue == 0) System.out.println("Livro não encontrado.");
  183.             else contReq++;
  184.         }
  185.     }
  186.    
  187.     public void entLivro(int cota, Biblioteca b){
  188.         if(entLivroMain(cota,b) == 0) System.out.println("Livro não encontrado.");
  189.        
  190.         else{
  191.             contReq--;
  192.         }
  193.     }
  194. }
  195.  
  196. class Prof extends Leitor{
  197.  
  198.     protected static int numMaxReq = 10;
  199.     protected int contReq = 0;
  200.    
  201.     public Prof(int numLeitor, String tipoLeitor, String nome, String morada, String email){
  202.         super(numLeitor, tipoLeitor, nome, morada, email);
  203.     }
  204.    
  205.     public Prof(int numLeitor, String tipoLeitor){
  206.         super(numLeitor, tipoLeitor);
  207.     }
  208.    
  209.     public void reqLivro(int cota, Biblioteca b, String data){
  210.         if(contReq>=numMaxReq) System.out.println("Numero maximo de livros requisitados.");
  211.        
  212.         else{
  213.             int retValue = reqLivroMain(cota, b, data, this.numLeitor);
  214.            
  215.             if(retValue == -1) System.out.println("Este livro já se encontra requisitado.");
  216.             else if(retValue == 0) System.out.println("Livro não encontrado.");
  217.             else contReq++;
  218.         }
  219.     }
  220.    
  221.     public void entLivro(int cota, Biblioteca b){
  222.         if(entLivroMain(cota,b) == 0) System.out.println("Livro não encontrado.");
  223.        
  224.         else{
  225.             contReq--;
  226.         }
  227.     }
  228. }
  229.  
  230. public class Ficha7_3 {
  231.  
  232.     public static void main(String[] args) {
  233.        
  234.         //Cria uma biblioteca
  235.         Biblioteca biblioteca = new Biblioteca();
  236.        
  237.         //Cria duas areas
  238.         Area informatica = new Area("informatica");
  239.         Area historia = new Area("historia");
  240.        
  241.         //Coloca as areas num ArrayList
  242.         ArrayList<Area> areas = new ArrayList<>();
  243.        
  244.         areas.addAll(Arrays.asList(informatica, historia));
  245.        
  246.         //Cria 5 livros
  247.         Livro livro1 = new Livro("Nome1", "Autor1", 1);
  248.         Livro livro2 = new Livro("Nome2", "Autor1", 2);
  249.         Livro livro3 = new Livro("Nome3", "Autor2", 3);
  250.         Livro livro4 = new Livro("Nome4", "Autor3", 4);
  251.         Livro livro5 = new Livro("Nome5", "Autor4", 5);
  252.        
  253.         //Coloca todos na biblioteca
  254.         biblioteca.todosLivros.addAll(Arrays.asList(livro1, livro2, livro3, livro4, livro5));
  255.        
  256.         //Coloca 3 na primeira area e dois na segunda
  257.         informatica.listaLivros.addAll(Arrays.asList(livro1, livro2, livro3));
  258.         historia.listaLivros.addAll(Arrays.asList(livro4, livro5));
  259.        
  260.         //Cria dois leitores e adiciona num arraylist
  261.         Aluno aluno1 = new Aluno(1234, "Aluno", "João", "Coimbra", "mail");
  262.         Prof prof1 = new Prof(4321, "Prof");
  263.        
  264.         ArrayList<Aluno> listaAlunos = new ArrayList<>();
  265.         ArrayList<Prof> listaProf = new ArrayList<>();
  266.        
  267.         listaAlunos.add(aluno1);
  268.         listaProf.add(prof1);
  269.        
  270.         Scanner sc = new Scanner(System.in);
  271.        
  272.         //Ciclo while principal
  273.         while(true){
  274.        
  275.             System.out.println("OPCOES:\n\t1.Consultar livros\n\t2.Requisitar livro\n\t3.Entregar livro\n\t4.Verificar data entrega\n\t5.Sair");
  276.             int esc = sc.nextInt();
  277.            
  278.             switch(esc){
  279.            
  280.                 case 1:
  281.                     ArrayList<Livro> listaLivros = new ArrayList<>();
  282.                    
  283.                     System.out.println("OPCOES:\n\t1.Por area\n\t2.Por autor");
  284.                     int esc2 = sc.nextInt();
  285.                     sc.nextLine();
  286.                    
  287.                     System.out.print("Nome d" + (esc2==1?"a area?: ":"o autor?: "));
  288.                     if(esc2==1){
  289.                    
  290.                         String nomeArea = sc.nextLine();
  291.                        
  292.                         listaLivros.addAll(search(nomeArea, biblioteca, areas));
  293.                     }
  294.                     else{
  295.                    
  296.                         String nomeAutor = sc.nextLine();
  297.                        
  298.                         listaLivros.addAll(search(nomeAutor, biblioteca, areas));
  299.                     }
  300.                    
  301.                     if(listaLivros.isEmpty()) System.out.println((esc2==1?"Area":"Autor") + " inexistente");
  302.                    
  303.                     System.out.println(listaLivros);
  304.                    
  305.                     break;
  306.                 case 2:
  307.                     System.out.print("Numero de leitor?: ");
  308.                     int numLeitor = sc.nextInt();
  309.                     int cotaLivro;
  310.                     String data;
  311.                    
  312.                     for(Aluno aluno: listaAlunos){
  313.                         if(aluno.numLeitor == numLeitor){
  314.                        
  315.                             System.out.print("Cota do livro?: ");
  316.                             cotaLivro = sc.nextInt();
  317.                             sc.nextLine();
  318.                             System.out.print("Data?(formato dia/mes/ano): ");
  319.                             data=sc.nextLine();
  320.                            
  321.                             aluno.reqLivro(cotaLivro, biblioteca, data);
  322.                         }
  323.                     }
  324.                     for(Prof prof: listaProf){
  325.                    
  326.                         if(prof.numLeitor == numLeitor){
  327.                        
  328.                             System.out.print("Cota do livro?: ");
  329.                             cotaLivro = sc.nextInt();
  330.                             sc.nextLine();
  331.                             System.out.print("Data?(formato dia/mes/ano): ");
  332.                             data=sc.nextLine();
  333.                            
  334.                             prof.reqLivro(cotaLivro, biblioteca, data);
  335.                         }
  336.                     }
  337.                     break;
  338.                 case 3:
  339.                     System.out.print("Numero de leitor?: ");
  340.                     numLeitor = sc.nextInt();
  341.                     sc.nextLine();
  342.                     int check=0;
  343.                    
  344.                     for(Aluno aluno: listaAlunos){
  345.                         if(aluno.numLeitor == numLeitor){
  346.                        
  347.                             System.out.print("Cota do livro?: ");
  348.                             cotaLivro = sc.nextInt();
  349.                             sc.nextLine();
  350.                            
  351.                             aluno.entLivro(cotaLivro, biblioteca);
  352.                             check=1;
  353.                             System.out.println("Livro entregue.");
  354.                             break;
  355.                         }
  356.                     }
  357.                     for(Prof prof: listaProf){
  358.                    
  359.                         if(prof.numLeitor == numLeitor){
  360.                        
  361.                             System.out.print("Cota do livro?: ");
  362.                             cotaLivro = sc.nextInt();
  363.                             sc.nextLine();
  364.                            
  365.                             prof.entLivro(cotaLivro, biblioteca);
  366.                             check=1;
  367.                             System.out.println("Livro entregue.");
  368.                             break;
  369.                         }
  370.                     }
  371.                     if(check==0) System.out.println("Leitor não encontrado.");
  372.                     break;
  373.                 case 4:
  374.                     ArrayList<Livro> livrosPorEnt = new ArrayList<>();
  375.                     System.out.print("Data com a qual comparar?(formato dia/mes/ano): ");
  376.                     sc.nextLine();
  377.                     data = sc.nextLine();
  378.                    
  379.                     System.out.print("Numero de leitor?: ");
  380.                     numLeitor = sc.nextInt();
  381.                    
  382.                     for(Aluno aluno: listaAlunos){
  383.                         if(aluno.numLeitor == numLeitor){
  384.                        
  385.                             livrosPorEnt.addAll(aluno.checkData(biblioteca, data));
  386.                             System.out.println("Livros por entregar do aluno com n eleitor " + numLeitor + ": " + livrosPorEnt);
  387.                             break;
  388.                         }
  389.                     }
  390.                     livrosPorEnt = new ArrayList<>();
  391.                     for(Prof prof: listaProf){
  392.                    
  393.                         if(prof.numLeitor == numLeitor){
  394.                        
  395.                             livrosPorEnt.addAll(prof.checkData(biblioteca, data));
  396.                             System.out.println("Livros por entregar do prof com n eleitor " + numLeitor + ": " + livrosPorEnt);
  397.                             break;
  398.                         }
  399.                     }
  400.                     break;
  401.                 case 5:
  402.                     return;
  403.             }
  404.         }
  405.     }
  406.    
  407.     /**
  408.      *
  409.      * @param param
  410.      * @param b
  411.      * @param areas
  412.      * @return
  413.      */
  414.     public static ArrayList<Livro> search(String param, Biblioteca b, ArrayList<Area> areas){
  415.    
  416.         ArrayList<Livro> listaLivros = new ArrayList<>();
  417.         int check=0;
  418.        
  419.         //Procura por area
  420.         for(Area area: areas){
  421.        
  422.             if(Objects.equals(param, area.nomeArea)){
  423.            
  424.                 listaLivros.addAll(area.listaLivros);
  425.                 check=1;
  426.             }
  427.         }
  428.        
  429.         //Procura por autor
  430.         if(check==0){
  431.        
  432.             //Não encontrou area, procura por autor
  433.             for(Livro livro: b.todosLivros){
  434.            
  435.                 if(Objects.equals(param, livro.autor)) listaLivros.add(livro);
  436.             }
  437.         }
  438.        
  439.         return listaLivros;
  440.     }
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement