-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilmDatabase.java
205 lines (202 loc) · 6.8 KB
/
FilmDatabase.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
package com.accenture.itfactory.base.FilmApplication;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class FilmDatabase {
List<Film> filmList;
Film selectedFilm;
List<Review> reviewList;
XmlDocClass xmlDoc;
public FilmDatabase(){
filmList = new ArrayList<Film>();
selectedFilm = new Film();
reviewList = new ArrayList<Review>();
xmlDoc = new XmlDocClass();
}
public void addFilm(String fName, String fId, String fType, String fGenre, Date fDate,
double fRating, String fDescription){
filmList.add(new Film());
filmList.get(filmList.size()-1).addNewFilm(fName,fId,fType,fGenre,fDate,fRating,fDescription);
}
public void addReview(String fName,String newReview,String reviewer) {
selectedFilm = searchByName(fName);
String selectedId = selectedFilm.imdbId;
for (Review r : reviewList) {
if (r.filmId.equals(selectedId)) {
if (r.review.isEmpty()) {
r.review = newReview;
r.createdBy = reviewer;
r.createdOn = Calendar.getInstance();
}
else {
editReview(fName, newReview, reviewer);
}
}
}
}
public void editReview(String fName,String newReview,String reviewer){
selectedFilm = searchByName(fName);
String selectedId = selectedFilm.imdbId;
for(Review r : reviewList){
if(r.filmId.equals(selectedId)){
if(r.review.isEmpty())
addReview(fName,newReview,reviewer);
else{
if(r.review.equals(newReview)) return;
else
r.review = newReview;
}
}
}
}
public void deleteReview(String fName){
selectedFilm = searchByName(fName);
String selectedId = selectedFilm.imdbId;
int selectedIndex = 0;
for(int i = 0; i< reviewList.size(); i++){
if(reviewList.get(i).filmId.equals(selectedId)) {
selectedIndex = i;
break;
}
}
reviewList.remove(selectedIndex);
}
public void loadData(String path){
File film_file = new File(path);
if(!film_file.isFile()) return;
filmList.clear();
filmList = xmlDoc.startSession(path);
}
public void saveData(String path){
xmlDoc.saveSession(path, filmList);
}
public void removeByName(String fName){
selectedFilm = searchByName(fName);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public void removeById(String fId){
selectedFilm = searchById(fId);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public void removeByType(String fType){
selectedFilm = searchByType(fType);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public void removeByGenre(String fGenre){
selectedFilm = searchByGenre(fGenre);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public void removeByReleaseDate(Date fDate){
selectedFilm = searchByReleaseDate(fDate);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public void removeByRating(double fRating){
selectedFilm = searchByRating(fRating);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public void removeByDescription(String fDescription){
selectedFilm = searchByDescription(fDescription);
int index = filmList.indexOf(selectedFilm);
filmList.remove(index);
}
public Film search(String stringParam){
boolean findFilm = false;
for(int i =0;i<filmList.size();i++){
findFilm = filmList.get(i).search(stringParam);
if(findFilm)
return filmList.get(i);
}
return null;
}
public Film searchByName(String fName){
for(Film f: filmList)
if (f.name.equals(fName))
return f;
return null;
}
public Film searchById(String fId){
for(Film f: filmList)
if (f.imdbId.equals(fId))
return f;
return null;
}
public Film searchByType(String fType){
for(Film f: filmList)
if (f.filmType.equals(fType))
return f;
return null;
}
public Film searchByGenre(String fGenre){
for(Film f: filmList)
if (f.genre.equals(fGenre))
return f;
return null;
}
public Film searchByReleaseDate(Date fDate){
for(Film f: filmList)
if (f.releaseDate.equals(fDate))
return f;
return null;
}
public Film searchByRating(double fRating){
for(Film f: filmList)
if (f.rating == fRating)
return f;
return null;
}
public Film searchByDescription(String fDescription){
for(Film f: filmList)
if (f.description.equals(fDescription))
return f;
return null;
}
public Review searchForReview(String fId){
for(Review r : reviewList)
if (r.filmId.equals(fId))
return r;
return null;
}
public List<Film> viewFilms(){
int counter = 0;
List<Film> viewList = new ArrayList<Film>();
for(int i = 0; i< filmList.size(); i++){
while(counter < 30){
viewList.add(filmList.get(i));
}
counter++;
}
return viewList;
}
public List<Film> viewFilms(int count){
List<Film> viewList = new ArrayList<Film>(count);
for(int i = 0; i< count;i++){
viewList.add(filmList.get(i));
}
return viewList;
}
public List<Film> viewFilms(int lowerBound,int higherBound){
if(higherBound > filmList.size()) return null;
List<Film> viewList = new ArrayList<Film>();
for(int i = lowerBound; i< higherBound;i++){
viewList.add(filmList.get(i));
}
return viewList;
}
public List<Film> searchDateRange(Date lowerDate,Date higherDate){
List<Film> dateList = new ArrayList<Film>();
for(Film f: filmList){
if(f.releaseDate.after(lowerDate) && f.releaseDate.before(higherDate)){
dateList.add(f);
}
}
return dateList;
}
}