Created
June 13, 2020 13:08
-
-
Save Rastrian/f27e5ca63ad1d633977ae8949ef3c7eb to your computer and use it in GitHub Desktop.
Prova Modular - Questão Clube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.Calendar; | |
import java.util.stream.Stream; | |
public class Clube { | |
private ArrayList<Membros> listaAcesso; | |
private static Calendar dataAcesso; | |
private static Calendar dataToday; | |
public Clube() { | |
this.listaAcesso = new ArrayList<Membros>(); | |
} | |
public void cadastraAcesso(Membros acesso, Date today) { | |
if (acesso instanceof Visitante) | |
dataAcesso = Calendar.getInstance(); | |
dataToday = Calendar.getInstance(); | |
dataAcesso.setTime(acesso.retornaDiaAcesso()); | |
dataToday.setTime(today); | |
boolean sameDay = dataAcesso.get(Calendar.YEAR) == dataToday.get(Calendar.YEAR) && | |
dataAcesso.get(Calendar.DAY_OF_YEAR) == dataToday.get(Calendar.DAY_OF_YEAR); | |
if (!sameDay){ | |
System.out.println("Você não pode entrar hoje no clube, pois sua entrada de visitante não é valida para hoje."); | |
return; | |
} | |
acesso.setDiaAcesso(today); | |
this.listaAcesso.add(acesso); | |
System.out.println("Acesso cadastrado."); | |
} | |
public Stream<Membros> retornaListaVisitantes(Date dia) { | |
return this.listaAcesso.stream() | |
.filter(acesso -> acesso instanceof Visitante) | |
.filter(acesso -> acesso.retornaDiaAcesso().getTime() == dia.getTime()); | |
} | |
public Stream<Membros> retornaTotalAcessos(Date dataInicio, Date dataFinal) { | |
return this.listaAcesso.stream().filter(acesso -> | |
acesso.retornaDiaAcesso().getTime() >= dataInicio.getTime() && | |
acesso.retornaDiaAcesso().getTime() <= dataFinal.getTime() | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
public class Main { | |
static ArrayList<Visitante> visitantes; | |
static ArrayList<Socio> socios; | |
static Clube clube; | |
static Integer numero; | |
public static void main(String[] args) throws NumberFormatException, IOException, ParseException { | |
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); | |
Date today = new Date(System.currentTimeMillis()); | |
clube = new Clube(); | |
visitantes = new ArrayList<Visitante>(); | |
socios = new ArrayList<Socio>(); | |
boolean close = false; | |
while (!close) { | |
System.out.println("\nOpções:\n\n0 → Sair\n1 → Cadastrar Visitante\n2 → Cadastrar Socio\n3 → Acessar Clube"+ | |
"\n4 → Listar visitantes de um determinado dia.\n5 → Listar totais de acessos de um periodo.\n\nInsira a opção desejada:"); | |
Integer output = null; | |
while (output == null) { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
try { | |
output = Integer.parseInt(br.readLine()); | |
} catch (NumberFormatException | IOException e) { | |
System.out.println("Insira um formato valido"); | |
return; | |
} | |
} | |
if (output == 0) | |
close = true; | |
if (output == 1) { | |
Date acesso = null; | |
System.out.println("→ Insira o numero de convite:"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
numero = Integer.parseInt(br.readLine()); | |
} catch (NumberFormatException | IOException e) { | |
System.out.println("Insira um formato valido"); | |
return; | |
} | |
System.out.println("→ Insira a data de acesso do visitante (dd/MM/yyyy):"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
acesso = dateFormat.parse(br.readLine()); | |
} catch (ParseException | IOException e) { | |
System.out.println("Insira um formato valido (dd/MM/yyyy)"); | |
return; | |
} | |
visitantes.add(new Visitante(numero, acesso)); | |
System.out.println("Visitante adicionado."); | |
} | |
if (output == 2) { | |
System.out.println("→ Insira o numero de socio:"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
numero = Integer.parseInt(br.readLine()); | |
} catch (NumberFormatException | IOException e) { | |
System.out.println("Insira um formato valido"); | |
return; | |
} | |
socios.add(new Socio(numero)); | |
System.out.println("Socio adicionado."); | |
} | |
if (output == 3) { | |
String option = null; | |
System.out.println("→ Insira o tipo de acesso (V - Visitante / S - Socio):"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
option = br.readLine(); | |
if (option == "V" || option == "S"){ | |
System.out.println("Opções invalidas, insira V para Visitante ou S para Socio"); | |
return; | |
} | |
} catch (NumberFormatException | IOException e) { | |
System.out.println("Insira um formato valido"); | |
return; | |
} | |
System.out.println("→ Insira o numero de convite:"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
numero = Integer.parseInt(br.readLine()); | |
} catch (NumberFormatException | IOException e) { | |
System.out.println("Insira um formato valido"); | |
return; | |
} | |
switch(option){ | |
case "V": | |
visitantes.forEach(v -> { | |
if (v.retornaNumero() == numero){ | |
clube.cadastraAcesso(v, today); | |
return; | |
} | |
}); | |
break; | |
case "S": | |
socios.forEach(s -> { | |
if (s.retornaNumero() == numero){ | |
clube.cadastraAcesso(s, today); | |
return; | |
} | |
}); | |
break; | |
default: | |
System.out.println("Erro na leitura de opção, tente novamente."); | |
} | |
} | |
if (output == 4){ | |
Date dia = null; | |
System.out.println("→ Insira a data desejada (dd/MM/yyyy):"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
dia = dateFormat.parse(br.readLine()); | |
} catch (ParseException | IOException e) { | |
System.out.println("Insira um formato valido (dd/MM/yyyy)"); | |
return; | |
} | |
System.out.println("→ Lista de visitantes:"); | |
clube.retornaListaVisitantes(dia).forEach(visi -> { | |
System.out.println("Numero:"+ visi.retornaNumero()); | |
}); | |
} | |
if (output == 5){ | |
Date dataInicio = null; | |
Date dataFinal = null; | |
System.out.println("→ Insira a data inicial desejada (dd/MM/yyyy):"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
dataInicio = dateFormat.parse(br.readLine()); | |
} catch (ParseException | IOException e) { | |
System.out.println("Insira um formato valido (dd/MM/yyyy)"); | |
return; | |
} | |
System.out.println("→ Insira a data final desejada (dd/MM/yyyy):"); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
dataFinal = dateFormat.parse(br.readLine()); | |
} catch (ParseException | IOException e) { | |
System.out.println("Insira um formato valido (dd/MM/yyyy)"); | |
return; | |
} | |
System.out.println("→ Lista de acessos:"); | |
clube.retornaTotalAcessos(dataInicio, dataFinal).forEach(a -> { | |
if (a instanceof Visitante){ | |
System.out.println("Visitante - Numero:"+ a.retornaNumero()); | |
}else if (a instanceof Socio){ | |
System.out.println("Socio - Numero:"+ a.retornaNumero()); | |
} | |
}); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
public interface Membros { | |
public int retornaNumero(); | |
public Date retornaDiaAcesso(); | |
public void setDiaAcesso(Date newDate); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
public class Socio implements Membros { | |
private int numberSocio; | |
private Date date; | |
public Socio(int numero) { | |
this.numberSocio = numero; | |
this.date = new Date(System.currentTimeMillis()); | |
} | |
public int retornaNumero() { | |
return this.numberSocio; | |
} | |
public Date retornaDiaAcesso() { | |
return this.date; | |
} | |
public void setDiaAcesso(Date newDate) { | |
this.date = newDate; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
public class Visitante implements Membros { | |
private int numberInvite; | |
private Date date; | |
public Visitante(int numero, Date date) { | |
this.numberInvite = numero; | |
this.date = date; | |
} | |
public int retornaNumero() { | |
return this.numberInvite; | |
} | |
public Date retornaDiaAcesso() { | |
return this.date; | |
} | |
@Override | |
public void setDiaAcesso(Date newDate) { | |
this.date = newDate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment