Skip to content

Instantly share code, notes, and snippets.

@washingtonsoares
Created October 4, 2017 23:28
Show Gist options
  • Save washingtonsoares/f1bd2db56f759cfd32a28ba8463c2e3b to your computer and use it in GitHub Desktop.
Save washingtonsoares/f1bd2db56f759cfd32a28ba8463c2e3b to your computer and use it in GitHub Desktop.
package chavevalor;
import java.util.ArrayList;
import java.util.List;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import chavevalor.Grafo;
import chavevalor.*;
import java.util.Scanner;
public class ChaveValorClient {
public static void main(String[] args) {
Integer porta = Integer.parseInt(args[0]);
String host = args[1];
TTransport transport = new TSocket(host, porta);
try {
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
ChaveValor.Client client = new ChaveValor.Client(protocol);
Scanner userInput = new Scanner(System.in);
String menuOptions = "=================================================\n" +
"Entre com uma opção: \n" +
"1) Cadastrar uma aresta \n" +
"2) Atualizar uma aresta \n" +
"3) Remover uma aresta \n" +
"4) Cadastrar um vertice \n" +
"5) Atualizar um vertice \n" +
"6) Remover um vertice \n" +
"7) Listar todos os vertices \n" +
"8) Listar todos as arestas \n" +
"9) Listar arestas de um vértice \n" +
"10) Listar vertices vizinhos de um vértice \n" +
"=================================================\n";
System.out.println(menuOptions);
while(true) {
Integer selectedOption = Integer.valueOf(userInput.nextLine());
switch (selectedOption) {
case 1: {
System.out.println("Digite o nome da vértice 1");
Integer nomeVertice1 = userInput.nextInt();
userInput.nextLine();
System.out.println("Digite o nome da vértice 2");
Integer nomeVertice2 = userInput.nextInt();
userInput.nextLine();
System.out.println("Digite o peso da aresta");
Double pesoAresta = userInput.nextDouble();
userInput.nextLine();
System.out.println("É aresta direcionada?");
Boolean ehDirecionada = userInput.nextBoolean();
System.out.println("Digite a descrição da aresta");
String descricaoAresta = userInput.nextLine();
System.out.println("Digite a descrição da aresta");
Integer nomeAresta = userInput.nextInt();
userInput.nextLine();
boolean result = client.setAresta(nomeVertice1, nomeVertice2, pesoAresta, ehDirecionada, descricaoAresta, nomeAresta);
if(result) {
System.out.println("Aresta cadastrada com sucesso");
} else {
System.out.println("Erro ao cadastrar Aresta");
}
break;
}
case 2: {
System.out.println("option 2");
break;
}
case 3: {
System.out.println("Qual aresta deseja remover?");
Integer aresta = userInput.nextInt();
userInput.nextLine();
client.delAresta(aresta);
break;
}
case 4: {
System.out.println("Digite o nome do vértice");
Integer nomeVertice = userInput.nextInt();
userInput.nextLine();
System.out.println("Digite a cor do vértice");
Integer corVertice = userInput.nextInt();
userInput.nextLine();
System.out.println("Digite a descrição do vértice");
String descricaoVertice = userInput.nextLine();
System.out.println("Digite o peso do vértice");
Integer pesoVertice = userInput.nextInt();
userInput.nextLine();
boolean result = client.setVertice(nomeVertice, corVertice, descricaoVertice, pesoVertice);
if(result) {
System.out.println("Vértice cadastrado com sucesso");
} else {
System.out.println("Erro ao cadastrar o vértice");
}
break;
}
case 5: {
System.out.println("option 5");
break;
}
case 6: {
System.out.println("Qual vértice deseja remover?");
Integer vertice = userInput.nextInt();
userInput.nextLine();
client.delVertice(vertice);
break;
}
case 7: {
System.out.println("Listando vértices do grafo");
for (Vertice ve : client.listaVerticesGrafo()) {
System.out.println("Nome " + ve.nome + " cor " + ve.cor);
}
break;
}
case 8: {
System.out.println("Listando arestas do grafo");
for (Aresta ar : client.listaArestasGrafo()) {
System.out.println("Aresta " + ar.nome + " vertice1 " + ar.nomeVertice1 + " vertice2 " + ar.nomeVertice2);
}
break;
}
case 9: {
System.out.println("Qual vértices deseja listar as arestas ?");
Integer vertice = userInput.nextInt();
userInput.nextLine();
System.out.println("Lista de arestas no vertice " + vertice);
for (Aresta ar : client.listaArestasVertice(vertice)) {
System.out.println("Aresta " + ar.nome + " vertice1 " + ar.nomeVertice1 + " vertice2 " + ar.nomeVertice2);
}
break;
}
case 10: {
System.out.println("Qual vértices deseja listar os vizinhos ?");
Integer vertice = userInput.nextInt();
userInput.nextLine();
System.out.println("Lista de vizinho do vertice " + vertice);
for (Vertice ve : client.listaVerticesVizinho(vertice)) {
System.out.println("Nome " + ve.nome + " cor " + ve.cor);
}
break;
}
default: {
break;
}
}
}
} catch (KeyNotFound k) {
System.out.println(k.chaveProcurada);
} catch (Exception k) {
System.out.println(k);
} finally {
transport.close();
}
}
public static void clearScreen() {
System.out.print ('\f');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment