Skip to content

Commit

Permalink
Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagogomes187 committed Aug 27, 2023
1 parent 0742fe2 commit d181e4f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Optional/Optional.iml
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>
114 changes: 114 additions & 0 deletions Optional/src/OptionalExemplo.java
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() ));

}

}

0 comments on commit d181e4f

Please sign in to comment.