forked from hamzaouaddafe/AnnonceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnnonceDaoDb.java
86 lines (69 loc) · 2.25 KB
/
AnnonceDaoDb.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
import java.util.ArrayList;
import java.util.List;
public class AnnonceDaoDb implements AnnonceDao {
private List<Annonce> annonces = new ArrayList<Annonce>();
private UserDao userDao;
public AnnonceDaoDb(UserDao userDao) {
this.userDao = userDao;
annonces.add(
new Annonce(
1, new Domain(1, "Telephone"), this.userDao.select(1), 1000, "Iphone X"));
annonces.add(new Annonce(2, new Domain(2, "Voiture"), this.userDao.select(2), 4500, "BMW"));
annonces.add(
new Annonce(
3, new Domain(3, "Telephone"), this.userDao.select(3), 700, "Iphone 8"));
annonces.add(
new Annonce(
4, new Domain(4, "Telephone"), this.userDao.select(2), 150, "Samsung s6"));
annonces.add(
new Annonce(
5, new Domain(5, "Telephone"), this.userDao.select(4), 900, "Iphone X"));
}
public List<Annonce> select() {
return annonces;
}
public Annonce select(int id) {
for (Annonce annonce : annonces) {
if (annonce.getId() == id) {
return annonce;
}
}
return null;
}
public int insert(Annonce a) {
annonces.add(a);
return 1;
}
public int delete(Annonce a) {
annonces.remove(a);
return 1;
}
public int update(Annonce a) {
if (annonces.contains(a)) {
Annonce ann = select(a.getId());
Annonce tmp = ann;
ann = a;
annonces.remove(tmp);
annonces.add(ann);
}
return 1;
}
public List<Annonce> selectByUser(User user) {
List<Annonce> list = new ArrayList<Annonce>();
for (Annonce annonce : annonces) {
if (annonce.getUser().getId() == user.getId()) {
list.add(annonce);
}
}
return list;
}
public List<Annonce> selectByUser(String username) {
List<Annonce> list = new ArrayList<Annonce>();
for (Annonce annonce : annonces) {
if (annonce.getUser().getUsername().equals(username)) {
list.add(annonce);
}
}
return list;
}
}