-
Notifications
You must be signed in to change notification settings - Fork 0
/
Users.java
221 lines (192 loc) · 6.92 KB
/
Users.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import java.util.Set;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;
import java.util.Iterator;
import java.io.Serializable;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Comparator;
public class Users implements Serializable{
private Map<String, Info> users; // NIF, User
static final long serialVersionUID = 1L;
public Users() {
this.users = new HashMap<>();
}
public Users(Map<String, Info> users) {
this.users = users.values().stream().collect(Collectors.toMap((e) -> e.getNIF(),(e) -> e.clone()));
}
public Users(Users h) {
setUsers(h.getUsers());
}
public Map<String,Info> getUsers() {
return this.users.values().stream().collect(Collectors.toMap((e) -> e.getNIF(),(e) -> e.clone()));
}
public void setUsers(Map<String, Info> users) {
this.users = new HashMap<>();
for(Map.Entry<String, Info> e : users.entrySet()) {
this.users.put(e.getKey(), e.getValue());
}
}
public void apagaUser(Info u) {
this.users.remove(u);
}
public void validaFatura(Fatura f) {
String Nif_emitente = f.getNIF_emitente();
String Nif_cliente = f.getNIF_cliente();
List<Fatura> l = this.users.get(Nif_cliente).getValid();
List<Fatura> t = this.users.get(Nif_cliente).getNotValid();
int i = 0;
for (Fatura j : t) {
if (i == 0) l.add(f.clone());
else l.add(j.clone());
i++;
}
this.users.get(Nif_cliente).setFaturas(l);
Info empresa = this.users.get(Nif_emitente);
List<Fatura> paraEmp = new ArrayList<>();
for (Fatura r : empresa.getFaturas()) {
if (r.specialEquals(f))
paraEmp.add(f);
else paraEmp.add(r);
}
this.users.get(Nif_emitente).setFaturas(paraEmp);
}
public boolean existeUser (String cod) {
return this.users.containsKey(cod);
}
public int quantos() {
return this.users.size();
}
public Info getUser(String cod) {
if (this.users.containsKey(cod))
return this.users.get(cod).clone();
else return new Individual(); // tem que ser EXCEPTION
}
public void adiciona(Info h) throws UserExisteException{
if (this.users.containsKey(h.getNIF()))
throw new UserExisteException("Contribuinte " + h.getNIF() + " já existe.");
this.users.put(h.getNIF(), h.clone());
}
public List<Info> getUsers_List() {
return this.users.values().stream().collect(Collectors.toList());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
List<Info> l = this.getUsers_List();
for (Info h : l) {
sb.append(h.toString() + "\n");
}
return sb.toString();
}
public Users clone() {
return new Users(this);
}
// equals
public boolean equals(Object o){
if(o==this) return true;
if((o==null) || (o.getClass() != this.getClass()))
return false;
Users c = (Users) o;
return (this.getUsers().equals(c.getUsers()));
}
/**
* Guarda num ficheiro um objecto da classe Users
*/
public static void save(Users b)throws IOException{
String fileName= "Dados.ser";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(b);
oos.close();
}
/**
* Carrega de um ficheiro um objecto da classe Users
*/
public static Users load() throws IOException,ClassNotFoundException,FileNotFoundException{
String fileName= "Dados.ser";
FileInputStream fin = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fin);
Users b= (Users) ois.readObject();
ois.close();
return b;
}
public void adicionaFact(Fatura f) {
if (this.users.containsKey(f.getNIF_emitente())) this.users.get(f.getNIF_emitente()).adicionaF(f);
if (this.users.containsKey(f.getNIF_cliente())) this.users.get(f.getNIF_cliente()).adicionaF(f); // nao sei qual o metodo ainda
}
public double deducaoSolo(String NIF) {
double x = 0;
Info aux = this.getUser(NIF);
if (aux.getClass().equals(Individual.class)) {
Individual ind = (Individual) aux;
x += ind.valorDeduzido();
}
else x = 0;
return x;
}
public double deducaoAgregado(String NIF) {
double x = 0;
Info aux = this.getUser(NIF);
List<String> agregado = new ArrayList<>();
if (aux.getClass().equals(Individual.class)) {
Individual ind = (Individual) aux;
x += ind.valorDeduzido();
agregado = ind.getNIFAgregados();
for (String s : agregado) {
x += this.deducaoSolo(s);
}
}
else x = 0;
return x;
}
public List<Info> top10() {
List<Info> l = new ArrayList<>();
Map<String, Info> m = this.getUsers();
for (Map.Entry<String, Info> entrada : m.entrySet()) {
if (entrada.getValue().getNIF().equals("admin") == false) {
l.add(entrada.getValue());
l.sort(new Comparator<Info>() {
public int compare(Info m1, Info m2) {
if(m1.totalGasto() == m2.totalGasto()){
return 0;
}
else if (m1.totalGasto() > m2.totalGasto())
return -1;
else return 1;
}
});
}
}
return l.subList(0,10);
}
public List<Empresa> topX(int X) {
List<Empresa> l = new ArrayList<>();
Map<String, Info> m = this.getUsers();
for (Map.Entry<String, Info> entrada : m.entrySet()) {
if (entrada.getValue().getClass().equals(Empresa.class)) {
Empresa e = (Empresa) entrada.getValue();
l.add(e);
l.sort(new Comparator<Empresa>() {
public int compare(Empresa m1, Empresa m2) {
if(m1.totalFaturado() == m2.totalFaturado()){
return 0;
}
else if (m1.totalFaturado() > m2.totalFaturado())
return -1;
else return 1;
}
});
}
}
return l.subList(0,X);
}
}