Commit inicial

This commit is contained in:
2022-09-18 14:09:37 -04:00
commit 88c9d4a901
18 changed files with 258 additions and 0 deletions

30
src/Cliente.java Normal file
View File

@@ -0,0 +1,30 @@
import java.rmi.Naming;
import java.util.List;
import java.util.Scanner;
import implementacoes.CandidatoImpl;
import interfaces.VotacaoInterface;
public class Cliente {
static int porta = 1099;
public static void main(String[] args) throws Exception {
String objName = "rmi://localhost:" + porta + "/server";
VotacaoInterface votacao = (VotacaoInterface) Naming.lookup(objName);
List<CandidatoImpl> candidatos = votacao.listarCandidatos();
candidatos.forEach(x -> {
System.out.println(x.getNome() + " " + x.getNumero());
});
Scanner entrada = new Scanner(System.in);
System.out.println("Digite seu voto");
String numero_candidato = entrada.nextLine();
int posicao = votacao.buscarCandidato(numero_candidato);
System.out.println("Seu voto foi: " + candidatos.get(posicao).getNome());
votacao.apuracao();
}
}

46
src/Servidor.java Normal file
View File

@@ -0,0 +1,46 @@
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.List;
import implementacoes.CandidatoImpl;
import implementacoes.IsEvenImpl;
import implementacoes.VotacaoImpl;
import interfaces.VotacaoInterface;
public class Servidor {
static List<CandidatoImpl> candidatos;
static VotacaoInterface votacaoImpl;
static int porta = 1099;
public static void main(String[] args) {
try {
//IsEvenImpl x = new IsEvenImpl();
String objName = "rmi://localhost:" + porta + "/server";
System.out.println("Registrando o objeto no RMIRegistry...");
LocateRegistry.createRegistry(porta);
// Lista de Candidatos
candidatos = new ArrayList<>();
candidatos.add(new CandidatoImpl(1, "1 Luiz Picolo"));
candidatos.add(new CandidatoImpl(2, "2 Luiz Picolo"));
candidatos.add(new CandidatoImpl(3, "3 Luiz Picolo"));
candidatos.add(new CandidatoImpl(4, "4 Luiz Picolo"));
candidatos.add(new CandidatoImpl(5, "5 Luiz Picolo"));
candidatos.add(new CandidatoImpl(6, "6 Luiz Picolo"));
candidatos.add(new CandidatoImpl(7, "7 Luiz Picolo"));
votacaoImpl = new VotacaoImpl(candidatos);
Naming.rebind(objName, votacaoImpl);
//Naming.rebind(objName, x);
System.out.println("Aguardando Clientes na porta " + porta + "!");
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,35 @@
package implementacoes;
import java.io.Serializable;
import java.rmi.RemoteException;
import interfaces.CandidatoInterface;
public class CandidatoImpl implements CandidatoInterface, Serializable {
private static final long serialVersionUID = 6717433396141724642L;
private String nome;
private int numero;
public CandidatoImpl(int numero, String nome) throws RemoteException {
super();
this.nome = nome;
this.numero = numero;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
}

View File

@@ -0,0 +1,24 @@
package implementacoes;
import java.rmi.server.UnicastRemoteObject;
import interfaces.IsEvenInterface;
import java.rmi.RemoteException;
public class IsEvenImpl extends UnicastRemoteObject implements IsEvenInterface {
public IsEvenImpl() throws RemoteException {
super();
}
private static final long serialVersionUID = 1L;
@Override
public boolean is_even(int x) throws RemoteException {
System.out.println("Conectado. Parâmetro enviado é " + x);
if((x & 1) == 0) {
return true;
} else {
return false;
}
}
}

View File

@@ -0,0 +1,61 @@
package implementacoes;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import interfaces.VotacaoInterface;
public class VotacaoImpl extends UnicastRemoteObject implements VotacaoInterface {
private static final long serialVersionUID = 1L;
private List<CandidatoImpl> candidatos;
private List<VotoImpl> votos = new ArrayList<>();
private Map<Integer, Integer> quantidade = new HashMap<>();
public VotacaoImpl(List<CandidatoImpl> candidatos) throws RemoteException {
super();
this.candidatos = candidatos;
}
@Override
public List<CandidatoImpl> listarCandidatos() throws RemoteException {
return this.candidatos;
}
@Override
public int salvarVoto(int posicao) throws RemoteException {
this.votos.add(new VotoImpl("123", this.candidatos.get(posicao)));
return posicao;
}
@Override
public void apuracao() {
this.votos.forEach(voto -> {
int count = (int) votos.stream().filter(p -> p.getCandidato().equals(voto.getCandidato())).count();
quantidade.put(voto.getCandidato().getNumero(), count);
});
final String format = "O candidato %d possui %d votos";
final Set<Integer> chaves = quantidade.keySet();
System.out.println("Apuração dos votos");
for (final Integer chave : chaves) {
System.out.println(String.format(format, chave, quantidade.get(chave)));
}
}
@Override
public int buscarCandidato(String numero) throws RemoteException {
for(int i = 0; i < this.candidatos.size(); i++) {
if(this.candidatos.get(i).getNumero() == Integer.parseInt(numero)) {
return this.salvarVoto(i);
}
}
return -1;
}
}

View File

@@ -0,0 +1,26 @@
package implementacoes;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class VotoImpl extends UnicastRemoteObject {
private static final long serialVersionUID = 1L;
private CandidatoImpl candidato;
private String identificador;
public VotoImpl(String identificador, CandidatoImpl candidato) throws RemoteException {
super();
this.candidato = candidato;
this.identificador = identificador;
}
public CandidatoImpl getCandidato() {
return candidato;
}
public String getIdentificador() {
return identificador;
}
}

View File

@@ -0,0 +1,11 @@
package interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface CandidatoInterface extends Remote {
public String getNome() throws RemoteException;
public void setNome(String nome) throws RemoteException;
public int getNumero() throws RemoteException;
public void setNumero(int numero) throws RemoteException;
}

View File

@@ -0,0 +1,7 @@
package interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IsEvenInterface extends Remote {
public boolean is_even(int x) throws RemoteException;
}

View File

@@ -0,0 +1,14 @@
package interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import implementacoes.CandidatoImpl;
public interface VotacaoInterface extends Remote {
public List<CandidatoImpl> listarCandidatos() throws RemoteException;
public int salvarVoto(int posicao) throws RemoteException;
public int buscarCandidato(String numero) throws RemoteException;
public void apuracao() throws RemoteException;
}