commit 88c9d4a901601adef5b29fdc6d543ac7d833f840 Author: Luiz F. Picolo Date: Sun Sep 18 14:09:37 2022 -0400 Commit inicial diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..a358587 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a17f1cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/*.class +.classpath +.project +.settings \ No newline at end of file diff --git a/bin/implementacoes/CandidatoImpl.class b/bin/implementacoes/CandidatoImpl.class new file mode 100644 index 0000000..58d933d Binary files /dev/null and b/bin/implementacoes/CandidatoImpl.class differ diff --git a/bin/implementacoes/IsEvenImpl.class b/bin/implementacoes/IsEvenImpl.class new file mode 100644 index 0000000..d771697 Binary files /dev/null and b/bin/implementacoes/IsEvenImpl.class differ diff --git a/bin/implementacoes/VotacaoImpl.class b/bin/implementacoes/VotacaoImpl.class new file mode 100644 index 0000000..914d019 Binary files /dev/null and b/bin/implementacoes/VotacaoImpl.class differ diff --git a/bin/implementacoes/VotoImpl.class b/bin/implementacoes/VotoImpl.class new file mode 100644 index 0000000..08a86e8 Binary files /dev/null and b/bin/implementacoes/VotoImpl.class differ diff --git a/bin/interfaces/CandidatoInterface.class b/bin/interfaces/CandidatoInterface.class new file mode 100644 index 0000000..eabe2db Binary files /dev/null and b/bin/interfaces/CandidatoInterface.class differ diff --git a/bin/interfaces/IsEvenInterface.class b/bin/interfaces/IsEvenInterface.class new file mode 100644 index 0000000..54046b0 Binary files /dev/null and b/bin/interfaces/IsEvenInterface.class differ diff --git a/bin/interfaces/VotacaoInterface.class b/bin/interfaces/VotacaoInterface.class new file mode 100644 index 0000000..f17d39d Binary files /dev/null and b/bin/interfaces/VotacaoInterface.class differ diff --git a/src/Cliente.java b/src/Cliente.java new file mode 100644 index 0000000..450772b --- /dev/null +++ b/src/Cliente.java @@ -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 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(); + } +} \ No newline at end of file diff --git a/src/Servidor.java b/src/Servidor.java new file mode 100644 index 0000000..3a31120 --- /dev/null +++ b/src/Servidor.java @@ -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 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(); + } + } +} \ No newline at end of file diff --git a/src/implementacoes/CandidatoImpl.java b/src/implementacoes/CandidatoImpl.java new file mode 100644 index 0000000..98ac03b --- /dev/null +++ b/src/implementacoes/CandidatoImpl.java @@ -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; + } +} \ No newline at end of file diff --git a/src/implementacoes/IsEvenImpl.java b/src/implementacoes/IsEvenImpl.java new file mode 100644 index 0000000..ce4f19e --- /dev/null +++ b/src/implementacoes/IsEvenImpl.java @@ -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; + } + } +} \ No newline at end of file diff --git a/src/implementacoes/VotacaoImpl.java b/src/implementacoes/VotacaoImpl.java new file mode 100644 index 0000000..b9f513c --- /dev/null +++ b/src/implementacoes/VotacaoImpl.java @@ -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 candidatos; + private List votos = new ArrayList<>(); + private Map quantidade = new HashMap<>(); + + public VotacaoImpl(List candidatos) throws RemoteException { + super(); + this.candidatos = candidatos; + } + + @Override + public List 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 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; + } +} diff --git a/src/implementacoes/VotoImpl.java b/src/implementacoes/VotoImpl.java new file mode 100644 index 0000000..d50024c --- /dev/null +++ b/src/implementacoes/VotoImpl.java @@ -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; + } +} diff --git a/src/interfaces/CandidatoInterface.java b/src/interfaces/CandidatoInterface.java new file mode 100644 index 0000000..5af2274 --- /dev/null +++ b/src/interfaces/CandidatoInterface.java @@ -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; +} diff --git a/src/interfaces/IsEvenInterface.java b/src/interfaces/IsEvenInterface.java new file mode 100644 index 0000000..3dca6d5 --- /dev/null +++ b/src/interfaces/IsEvenInterface.java @@ -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; +} \ No newline at end of file diff --git a/src/interfaces/VotacaoInterface.java b/src/interfaces/VotacaoInterface.java new file mode 100644 index 0000000..56495f6 --- /dev/null +++ b/src/interfaces/VotacaoInterface.java @@ -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 listarCandidatos() throws RemoteException; + public int salvarVoto(int posicao) throws RemoteException; + public int buscarCandidato(String numero) throws RemoteException; + public void apuracao() throws RemoteException; +}