-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0742fe2
commit d181e4f
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
class Curso { | ||
private String nome; | ||
|
||
private List<Aluno> listaAlunos; | ||
|
||
public Curso(String nome) { | ||
this.nome = nome; | ||
this.listaAlunos = new ArrayList<>(); | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
|
||
public List<Aluno> getListaAlunos() { | ||
return listaAlunos; | ||
} | ||
|
||
public void setListaAlunos(List<Aluno> listaAlunos) { | ||
this.listaAlunos = listaAlunos; | ||
} | ||
} | ||
|
||
class Aluno { | ||
private Optional<Matricula> matricula; | ||
private String nome; | ||
|
||
public Aluno(String nome) { | ||
this.nome = nome; | ||
//Inicializa Optional como um container vazio | ||
this.matricula = Optional.empty(); | ||
} | ||
|
||
public Optional<Matricula> getMatricula() { | ||
return matricula; | ||
} | ||
|
||
public void setMatricula(Matricula matricula) { | ||
this.matricula = Optional.of(matricula); | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
} | ||
|
||
class Matricula { | ||
private String numero; | ||
|
||
public Matricula(String numero){ | ||
this.numero = numero; | ||
} | ||
|
||
public String getNumero() { | ||
return numero; | ||
} | ||
|
||
public void setNumero(String numero) { | ||
this.numero = numero; | ||
} | ||
} | ||
|
||
public class OptionalExemplo { | ||
|
||
public static void main(String[] args) { | ||
|
||
Curso cursoAdm = new Curso("Administração"); | ||
|
||
Aluno jose = new Aluno("José"); | ||
jose.setMatricula(new Matricula("11100")); | ||
//Adiciona aluno ao curso | ||
cursoAdm.getListaAlunos().add(jose); | ||
|
||
Aluno maria = new Aluno("Maria"); | ||
maria.setMatricula(new Matricula("12010")); | ||
//Adiciona aluno ao curso | ||
cursoAdm.getListaAlunos().add(maria); | ||
|
||
//SEM MATRÍCULA !!! | ||
Aluno ana = new Aluno("Ana"); | ||
//Adiciona aluno ao curso | ||
cursoAdm.getListaAlunos().add(ana); | ||
|
||
Aluno paulo = new Aluno("Paulo"); | ||
paulo.setMatricula(new Matricula("14010")); | ||
//Adiciona aluno ao curso | ||
cursoAdm.getListaAlunos().add(paulo); | ||
|
||
Aluno tiago = new Aluno("Tiago"); | ||
tiago.setMatricula(new Matricula("58534")); | ||
cursoAdm.getListaAlunos().add(tiago); | ||
|
||
//Exibe o nome do aluno e o número de sua matrícula no curso | ||
cursoAdm.getListaAlunos().stream() | ||
|
||
.filter( a -> a.getMatricula().isPresent() ) | ||
|
||
.forEach( a -> System.out.println(a.getNome() + " - " + a.getMatricula().get().getNumero() )); | ||
|
||
} | ||
|
||
} |