Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ficha7_3;
- import java.util.*;
- /**
- *
- * @author Filipe
- */
- class Biblioteca{
- ArrayList<Livro> todosLivros = new ArrayList<>();
- public Biblioteca(){}
- }
- class Area extends Biblioteca{
- ArrayList<Livro> listaLivros = new ArrayList<>();
- protected String nomeArea;
- public Area(String nome){
- this.nomeArea = nome;
- }
- }
- class Livro{
- protected String nome, autor;
- protected int cota;
- private int numLeitorReq = -1;
- public Livro(String nome, String autor, int cota){
- this.nome = nome;
- this.autor = autor;
- this.cota = cota;
- }
- public int getNumLeitorReq(){
- return numLeitorReq;
- }
- public void setNumLeitorReq(int novo){
- this.numLeitorReq = novo;
- }
- @Override
- public String toString(){
- return this.nome + "|" + this.autor + "|" + this.cota;
- }
- }
- class Leitor{
- protected String nome, morada, email, tipoLeitor;
- protected int idade, telefone, numLeitor;
- ArrayList<String> infoLivroReq = new ArrayList<>();
- public Leitor(int numLeitor, String tipoLeitor, String nome, String morada, String email){
- this.email = email;
- this.morada = morada;
- this.nome = nome;
- this.tipoLeitor = tipoLeitor;
- this.numLeitor = numLeitor;
- }
- public Leitor(int numLeitor, String tipoLeitor){
- this(numLeitor, tipoLeitor, "Vazio", "Vazio", "Vazio");
- }
- protected int reqLivroMain(int cota, Biblioteca b, String data, int numLeitor){
- for(Livro livro: b.todosLivros){
- //Para o caso de querer fazer mais operaçoes com Livro livro
- if(livro.cota == cota){
- if(livro.getNumLeitorReq()!= -1) return -1;
- infoLivroReq.add(Integer.toString(cota) + "_" + data);
- livro.setNumLeitorReq(numLeitor);
- return 1;
- }
- }
- return 0;
- }
- protected int entLivroMain(int cota, Biblioteca b){
- //Procura o livro em infoLivroReq
- for(String info: infoLivroReq){
- String tokens[] = info.split("_");
- if (Objects.equals(tokens[0], Integer.toString(cota))){
- //È o livro
- infoLivroReq.remove(info);
- for(Livro livro:b.todosLivros){
- if(livro.cota==cota) livro.setNumLeitorReq(-1);
- }
- return 1;
- }
- }
- //Altera os dados de quem tem o livro em Livro
- for(Livro livro: b.todosLivros){
- if(livro.cota == cota) livro.setNumLeitorReq(-1);
- }
- return 0;
- }
- public ArrayList<Livro> checkData(Biblioteca b, String data){
- ArrayList<Livro> livrosPorEnt = new ArrayList<>();
- String dataAtual[] = data.split("/");
- long dataAtualDias = (Integer.parseInt(dataAtual[2])*365) + (Integer.parseInt(dataAtual[1])*30) + Integer.parseInt(dataAtual[0]);
- //Procura livro
- for(String info: this.infoLivroReq){
- //Obtem todas as cotas
- String tokens[] = info.split("_");
- int cota=0;
- for(Livro livro:b.todosLivros){
- if(Integer.parseInt(tokens[0]) == livro.cota){
- cota = livro.cota;
- break;
- }
- }
- if(Integer.parseInt(tokens[0]) == cota){
- //Converte em milissegundos a data de requisiçao
- String dataReq[] = tokens[1].split("/");
- long dataReqDias = (Integer.parseInt(dataReq[2])*365) + (Integer.parseInt(dataReq[1])*30) + Integer.parseInt(dataReq[0]);
- //Faz a diferença entre as datas, se for maior que msEm60Dias,já passou a data de entrega
- if((dataAtualDias-dataReqDias)>60){
- for(Livro livro:b.todosLivros){
- if(livro.cota==cota) livrosPorEnt.add(livro);
- }
- }
- }
- }
- return livrosPorEnt;
- }
- }
- class Aluno extends Leitor{
- protected static int numMaxReq = 3;
- protected int contReq = 0;
- public Aluno(int numLeitor, String tipoLeitor, String nome, String morada, String email){
- super(numLeitor, tipoLeitor, nome, morada, email);
- }
- public Aluno(int numLeitor, String tipoLeitor){
- super(numLeitor, tipoLeitor);
- }
- public void reqLivro(int cota, Biblioteca b, String data){
- if(contReq>=numMaxReq) System.out.println("Numero maximo de livros requisitados.");
- else{
- int retValue = reqLivroMain(cota, b, data, this.numLeitor);
- if(retValue == -1) System.out.println("Este livro já se encontra requisitado.");
- else if(retValue == 0) System.out.println("Livro não encontrado.");
- else contReq++;
- }
- }
- public void entLivro(int cota, Biblioteca b){
- if(entLivroMain(cota,b) == 0) System.out.println("Livro não encontrado.");
- else{
- contReq--;
- }
- }
- }
- class Prof extends Leitor{
- protected static int numMaxReq = 10;
- protected int contReq = 0;
- public Prof(int numLeitor, String tipoLeitor, String nome, String morada, String email){
- super(numLeitor, tipoLeitor, nome, morada, email);
- }
- public Prof(int numLeitor, String tipoLeitor){
- super(numLeitor, tipoLeitor);
- }
- public void reqLivro(int cota, Biblioteca b, String data){
- if(contReq>=numMaxReq) System.out.println("Numero maximo de livros requisitados.");
- else{
- int retValue = reqLivroMain(cota, b, data, this.numLeitor);
- if(retValue == -1) System.out.println("Este livro já se encontra requisitado.");
- else if(retValue == 0) System.out.println("Livro não encontrado.");
- else contReq++;
- }
- }
- public void entLivro(int cota, Biblioteca b){
- if(entLivroMain(cota,b) == 0) System.out.println("Livro não encontrado.");
- else{
- contReq--;
- }
- }
- }
- public class Ficha7_3 {
- public static void main(String[] args) {
- //Cria uma biblioteca
- Biblioteca biblioteca = new Biblioteca();
- //Cria duas areas
- Area informatica = new Area("informatica");
- Area historia = new Area("historia");
- //Coloca as areas num ArrayList
- ArrayList<Area> areas = new ArrayList<>();
- areas.addAll(Arrays.asList(informatica, historia));
- //Cria 5 livros
- Livro livro1 = new Livro("Nome1", "Autor1", 1);
- Livro livro2 = new Livro("Nome2", "Autor1", 2);
- Livro livro3 = new Livro("Nome3", "Autor2", 3);
- Livro livro4 = new Livro("Nome4", "Autor3", 4);
- Livro livro5 = new Livro("Nome5", "Autor4", 5);
- //Coloca todos na biblioteca
- biblioteca.todosLivros.addAll(Arrays.asList(livro1, livro2, livro3, livro4, livro5));
- //Coloca 3 na primeira area e dois na segunda
- informatica.listaLivros.addAll(Arrays.asList(livro1, livro2, livro3));
- historia.listaLivros.addAll(Arrays.asList(livro4, livro5));
- //Cria dois leitores e adiciona num arraylist
- Aluno aluno1 = new Aluno(1234, "Aluno", "João", "Coimbra", "mail");
- Prof prof1 = new Prof(4321, "Prof");
- ArrayList<Aluno> listaAlunos = new ArrayList<>();
- ArrayList<Prof> listaProf = new ArrayList<>();
- listaAlunos.add(aluno1);
- listaProf.add(prof1);
- Scanner sc = new Scanner(System.in);
- //Ciclo while principal
- while(true){
- System.out.println("OPCOES:\n\t1.Consultar livros\n\t2.Requisitar livro\n\t3.Entregar livro\n\t4.Verificar data entrega\n\t5.Sair");
- int esc = sc.nextInt();
- switch(esc){
- case 1:
- ArrayList<Livro> listaLivros = new ArrayList<>();
- System.out.println("OPCOES:\n\t1.Por area\n\t2.Por autor");
- int esc2 = sc.nextInt();
- sc.nextLine();
- System.out.print("Nome d" + (esc2==1?"a area?: ":"o autor?: "));
- if(esc2==1){
- String nomeArea = sc.nextLine();
- listaLivros.addAll(search(nomeArea, biblioteca, areas));
- }
- else{
- String nomeAutor = sc.nextLine();
- listaLivros.addAll(search(nomeAutor, biblioteca, areas));
- }
- if(listaLivros.isEmpty()) System.out.println((esc2==1?"Area":"Autor") + " inexistente");
- System.out.println(listaLivros);
- break;
- case 2:
- System.out.print("Numero de leitor?: ");
- int numLeitor = sc.nextInt();
- int cotaLivro;
- String data;
- for(Aluno aluno: listaAlunos){
- if(aluno.numLeitor == numLeitor){
- System.out.print("Cota do livro?: ");
- cotaLivro = sc.nextInt();
- sc.nextLine();
- System.out.print("Data?(formato dia/mes/ano): ");
- data=sc.nextLine();
- aluno.reqLivro(cotaLivro, biblioteca, data);
- }
- }
- for(Prof prof: listaProf){
- if(prof.numLeitor == numLeitor){
- System.out.print("Cota do livro?: ");
- cotaLivro = sc.nextInt();
- sc.nextLine();
- System.out.print("Data?(formato dia/mes/ano): ");
- data=sc.nextLine();
- prof.reqLivro(cotaLivro, biblioteca, data);
- }
- }
- break;
- case 3:
- System.out.print("Numero de leitor?: ");
- numLeitor = sc.nextInt();
- sc.nextLine();
- int check=0;
- for(Aluno aluno: listaAlunos){
- if(aluno.numLeitor == numLeitor){
- System.out.print("Cota do livro?: ");
- cotaLivro = sc.nextInt();
- sc.nextLine();
- aluno.entLivro(cotaLivro, biblioteca);
- check=1;
- System.out.println("Livro entregue.");
- break;
- }
- }
- for(Prof prof: listaProf){
- if(prof.numLeitor == numLeitor){
- System.out.print("Cota do livro?: ");
- cotaLivro = sc.nextInt();
- sc.nextLine();
- prof.entLivro(cotaLivro, biblioteca);
- check=1;
- System.out.println("Livro entregue.");
- break;
- }
- }
- if(check==0) System.out.println("Leitor não encontrado.");
- break;
- case 4:
- ArrayList<Livro> livrosPorEnt = new ArrayList<>();
- System.out.print("Data com a qual comparar?(formato dia/mes/ano): ");
- sc.nextLine();
- data = sc.nextLine();
- System.out.print("Numero de leitor?: ");
- numLeitor = sc.nextInt();
- for(Aluno aluno: listaAlunos){
- if(aluno.numLeitor == numLeitor){
- livrosPorEnt.addAll(aluno.checkData(biblioteca, data));
- System.out.println("Livros por entregar do aluno com n eleitor " + numLeitor + ": " + livrosPorEnt);
- break;
- }
- }
- livrosPorEnt = new ArrayList<>();
- for(Prof prof: listaProf){
- if(prof.numLeitor == numLeitor){
- livrosPorEnt.addAll(prof.checkData(biblioteca, data));
- System.out.println("Livros por entregar do prof com n eleitor " + numLeitor + ": " + livrosPorEnt);
- break;
- }
- }
- break;
- case 5:
- return;
- }
- }
- }
- /**
- *
- * @param param
- * @param b
- * @param areas
- * @return
- */
- public static ArrayList<Livro> search(String param, Biblioteca b, ArrayList<Area> areas){
- ArrayList<Livro> listaLivros = new ArrayList<>();
- int check=0;
- //Procura por area
- for(Area area: areas){
- if(Objects.equals(param, area.nomeArea)){
- listaLivros.addAll(area.listaLivros);
- check=1;
- }
- }
- //Procura por autor
- if(check==0){
- //Não encontrou area, procura por autor
- for(Livro livro: b.todosLivros){
- if(Objects.equals(param, livro.autor)) listaLivros.add(livro);
- }
- }
- return listaLivros;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement