Removido arquivos temporários
This commit is contained in:
BIN
src/.DS_Store
vendored
Normal file
BIN
src/.DS_Store
vendored
Normal file
Binary file not shown.
@@ -7,10 +7,10 @@ import interfaces.VotacaoInterface;
|
||||
|
||||
public class Cliente {
|
||||
|
||||
static int porta = 1099;
|
||||
static int porta = 8898;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String objName = "rmi://localhost:" + porta + "/server";
|
||||
String objName = "//localhost:" + porta + "/server";
|
||||
VotacaoInterface votacao = (VotacaoInterface) Naming.lookup(objName);
|
||||
|
||||
List<CandidatoImpl> candidatos = votacao.listarCandidatos();
|
||||
@@ -19,12 +19,12 @@ public class Cliente {
|
||||
System.out.println(x.getNome() + " " + x.getNumero());
|
||||
});
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
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();
|
||||
}
|
||||
}
|
||||
33
src/ServidorApuracao.java
Normal file
33
src/ServidorApuracao.java
Normal file
@@ -0,0 +1,33 @@
|
||||
import java.rmi.Naming;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.util.List;
|
||||
|
||||
import implementacoes.ApuracaoImpl;
|
||||
import implementacoes.CandidatoImpl;
|
||||
import implementacoes.VotacaoImpl;
|
||||
import interfaces.ApuracaoInterface;
|
||||
import interfaces.VotacaoInterface;
|
||||
|
||||
|
||||
public class ServidorApuracao {
|
||||
|
||||
static List<CandidatoImpl> candidatos;
|
||||
static ApuracaoInterface apuracao;
|
||||
static int porta = 8899;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.out.println("Registrando o objeto no RMIRegistry...");
|
||||
LocateRegistry.createRegistry(porta);
|
||||
|
||||
//votacaoImpl = new VotacaoImpl(candidatos);
|
||||
apuracao = new ApuracaoImpl();
|
||||
|
||||
Naming.rebind("//localhost:" + porta + "/server", apuracao);
|
||||
|
||||
System.out.println("Servidor de Apuração. Aguardando Clientes na porta " + porta + "!");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,30 @@
|
||||
import java.rmi.Naming;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import implementacoes.CandidatoImpl;
|
||||
import implementacoes.IsEvenImpl;
|
||||
import implementacoes.VotacaoImpl;
|
||||
import interfaces.ApuracaoInterface;
|
||||
import interfaces.VotacaoInterface;
|
||||
|
||||
public class Servidor {
|
||||
|
||||
public class ServidorVotacao {
|
||||
|
||||
static List<CandidatoImpl> candidatos;
|
||||
static VotacaoInterface votacaoImpl;
|
||||
static int porta = 1099;
|
||||
static int porta_servidor_votacao = 8898;
|
||||
static int porta_servidor_apuracao = 8899;
|
||||
static List<Integer> votos = new ArrayList<>();
|
||||
|
||||
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);
|
||||
|
||||
LocateRegistry.createRegistry(porta_servidor_votacao);
|
||||
|
||||
ApuracaoInterface apuracao = (ApuracaoInterface) Naming.lookup("//localhost:" + porta_servidor_apuracao + "/server");
|
||||
|
||||
// Lista de Candidatos
|
||||
candidatos = new ArrayList<>();
|
||||
@@ -35,10 +38,23 @@ public class Servidor {
|
||||
|
||||
votacaoImpl = new VotacaoImpl(candidatos);
|
||||
|
||||
Naming.rebind(objName, votacaoImpl);
|
||||
//Naming.rebind(objName, x);
|
||||
Naming.rebind("//localhost:" + porta_servidor_votacao + "/server", votacaoImpl);
|
||||
|
||||
System.out.println("Aguardando Clientes na porta " + porta + "!");
|
||||
System.out.println("Servidor de votação. Aguardando Clientes na porta " + porta_servidor_votacao + "!");
|
||||
|
||||
while (true) {
|
||||
System.out.println("Aperte Enter para ver a apuração");
|
||||
Scanner entrada = new Scanner(System.in);
|
||||
entrada.nextLine();
|
||||
|
||||
votos.clear();
|
||||
|
||||
votacaoImpl.getVotos().forEach(x -> {
|
||||
votos.add(x.getCandidato().getNumero());
|
||||
});
|
||||
|
||||
votacaoImpl.mostrar_apuracao(apuracao.calcularVotos(votos));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
30
src/implementacoes/ApuracaoImpl.java
Normal file
30
src/implementacoes/ApuracaoImpl.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package implementacoes;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import interfaces.ApuracaoInterface;
|
||||
|
||||
public class ApuracaoImpl extends UnicastRemoteObject implements ApuracaoInterface {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ApuracaoImpl() throws RemoteException {
|
||||
super();
|
||||
}
|
||||
|
||||
private Map<Integer, Integer> quantidade = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Map<Integer, Integer> calcularVotos(List<Integer> votos) {
|
||||
votos.forEach(voto -> {
|
||||
int count = (int) votos.stream().filter(p -> p.equals(voto)).count();
|
||||
quantidade.put(voto, ((100 * count) / votos.size()));
|
||||
});
|
||||
|
||||
return quantidade;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,11 @@ public class VotacaoImpl extends UnicastRemoteObject implements VotacaoInterface
|
||||
public List<CandidatoImpl> listarCandidatos() throws RemoteException {
|
||||
return this.candidatos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<VotoImpl> getVotos(){
|
||||
return this.votos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int salvarVoto(int posicao) throws RemoteException {
|
||||
@@ -48,6 +53,16 @@ public class VotacaoImpl extends UnicastRemoteObject implements VotacaoInterface
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mostrar_apuracao(Map<Integer, Integer> quantidade) {
|
||||
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++) {
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package implementacoes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
public class VotoImpl extends UnicastRemoteObject {
|
||||
import interfaces.VotoInterface;
|
||||
|
||||
public class VotoImpl extends UnicastRemoteObject implements Serializable, VotoInterface {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -16,10 +19,12 @@ public class VotoImpl extends UnicastRemoteObject {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CandidatoImpl getCandidato() {
|
||||
return candidato;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentificador() {
|
||||
return identificador;
|
||||
}
|
||||
|
||||
10
src/interfaces/ApuracaoInterface.java
Normal file
10
src/interfaces/ApuracaoInterface.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package interfaces;
|
||||
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ApuracaoInterface extends Remote {
|
||||
public Map<Integer, Integer> calcularVotos(List<Integer> votos) throws RemoteException;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package interfaces;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public interface IsEvenInterface extends Remote {
|
||||
public boolean is_even(int x) throws RemoteException;
|
||||
}
|
||||
@@ -3,12 +3,16 @@ package interfaces;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import implementacoes.CandidatoImpl;
|
||||
import implementacoes.VotoImpl;
|
||||
|
||||
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;
|
||||
public List<VotoImpl> getVotos() throws RemoteException;
|
||||
public void mostrar_apuracao(Map<Integer, Integer> apuracao) throws RemoteException;
|
||||
}
|
||||
|
||||
10
src/interfaces/VotoInterface.java
Normal file
10
src/interfaces/VotoInterface.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package interfaces;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
import implementacoes.CandidatoImpl;
|
||||
|
||||
public interface VotoInterface {
|
||||
public CandidatoImpl getCandidato() throws RemoteException;;
|
||||
public String getIdentificador() throws RemoteException;;
|
||||
}
|
||||
Reference in New Issue
Block a user