-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoja2.java
More file actions
115 lines (77 loc) · 2.38 KB
/
Copy pathLoja2.java
File metadata and controls
115 lines (77 loc) · 2.38 KB
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
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Stack;
public class Loja2 extends UnicastRemoteObject implements LojaRemota {
private Produto[] produtos;
int proxima;
public Loja2(int tamanho) throws RemoteException {
super();
this.produtos = new Produto[tamanho];
this.proxima = 0;
}
public synchronized void inserirProduto(String nome, int preco) throws RemoteException {
if (this.proxima == this.produtos.length) {
proxima = 0;
}
produtos[proxima] = new Produto(nome,preco);
proxima++;
}
public synchronized int procurarPosicao (String nome) {
int i = 0;
while (i < this.produtos.length){
if (this.produtos[i] != null && produtos[i].getNome().equalsIgnoreCase(nome)) {
return i;
}
i++;
}
return -1;
}
public synchronized Stack<String> getAllProducts () {
Stack<String> products = new Stack<String>();
int i= 0;
while (i < this.produtos.length){
if (this.produtos[i] != null) {
products.push(this.produtos[i].getNome());
}
i++;
}
return products;
}
public synchronized void removerProduto(String nome) throws RemoteException, Excecao {
int i = this.procurarPosicao(nome);
if (i != -1) {
this.produtos[i] = this.produtos[proxima-1];
this.produtos[proxima-1] = null;
proxima = proxima -1;
} else {
throw new Excecao("Produto n�o encontrado");
}
}
public synchronized Produto procurarProduto(String nome) throws RemoteException, Excecao {
int i = this.procurarPosicao(nome);
if (i != -1) {
return produtos[i];
} else throw new Excecao("Produto n�o encontrado");
}
public synchronized void alterarPreco(String nome, int valor) throws RemoteException {
int i = this.procurarPosicao(nome);
this.produtos[i].setPreco(valor);
}
public static void main (String args[]) {
try {
Loja2 loja2 = new Loja2(10);
Registry r = LocateRegistry.createRegistry(2127);
Naming.rebind("rmi://localhost:2127/loja", loja2);
System.out.println("Servidor da loja 2 ar!");
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}