-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClientSubs.java
179 lines (141 loc) · 4.94 KB
/
ClientSubs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
public class ClientSubs {
private ClientSubs() {}
public static void cadastrarSubscriber(Oliver stub)
{
try
{
Scanner s = new Scanner(System.in);
System.out.println("\nDigite seu email: ");
String email = s.nextLine();
System.out.println("Digite seu id: ");
int id = s.nextInt();
System.out.println("\n" + stub.cadastrarSubscriber(id, email));
}
catch(Exception e)
{
System.out.println("Cadastrar subscriber exception: " + e.toString());
e.printStackTrace();
}
}
public static void adicionarCurso(Oliver stub, String host)
{
try
{
Scanner s = new Scanner(System.in);
System.out.println("Digite o seu id:\n");
int id_subscriber = s.nextInt();
Thread t1 = new Thread(new ClientThread(host, "Cursos"));
t1.start();
System.out.println("Digite o id do curso a ser adicionado:\n");
int id_curso = s.nextInt();
System.out.println(stub.adicionarCurso(id_subscriber, id_curso) + "\n");
}
catch(Exception e)
{
System.out.println("Adicionar curso exception: " + e.toString());
e.printStackTrace();
}
}
public static void removerCurso(Oliver stub, String host)
{
try
{
Scanner s = new Scanner(System.in);
System.out.println("\nDigite o seu id:");
int id_subscriber = s.nextInt();
Thread t1 = new Thread(new ClientThread(host, "Cursos"));
t1.start();
System.out.println("\nDigite o id do curso a ser removido:");
int id_curso = s.nextInt();
String response = stub.removerCurso(id_subscriber, id_curso);
System.out.println("\nresponse: " + response);
}
catch(Exception e)
{
System.out.println("Remover curso exception: " + e.toString());
e.printStackTrace();
}
}
public static void imprimirMeusCursos(Oliver stub)
{
try
{
Scanner s = new Scanner(System.in);
System.out.println("\nDigite seu id: ");
int id = s.nextInt();
String response = stub.listarCursosPorId(id);
System.out.println("\nresponse: " + response);
}
catch(Exception e)
{
System.out.println("Listar cursos por id exception: " + e.toString());
e.printStackTrace();
}
}
public static void listarMensagens(Oliver stub)
{
try
{
Scanner s = new Scanner(System.in);
System.out.println("\nDigite seu id: ");
int id = s.nextInt();
System.out.println(stub.listarMensagens(id));
}
catch (Exception e)
{
System.out.println("Listar mensagens exception: " + e.toString());
e.printStackTrace();
}
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try
{
Registry registry = LocateRegistry.getRegistry(host);
Oliver stub = (Oliver) registry.lookup("Oliver");
Thread mensagem = new Thread(new ClientThread(host, "Mensagem"));
mensagem.start();
int op = 0;
do
{
System.out.println("\nDigite a opcao desejada:\n");
System.out.println("1 - Cadastrar um novo subscriber");
System.out.println("2 - Se inscrever em um curso");
System.out.println("3 - Remover um curso");
System.out.println("4 - Listar todos os meus cursos");
System.out.println("5 - Visualizar caixa de entrada");
System.out.println("6 - Sair");
Scanner s = new Scanner(System.in);
op = s.nextInt();
switch(op)
{
case 1:
cadastrarSubscriber(stub);
break;
case 2:
adicionarCurso(stub, host);
break;
case 3:
removerCurso(stub, host);
break;
case 4:
imprimirMeusCursos(stub);
break;
case 5:
listarMensagens(stub);
break;
default:
break;
}
} while (op != 6);
}
catch (Exception e)
{
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}