-
Notifications
You must be signed in to change notification settings - Fork 1
/
MagazineShelf.java
executable file
·293 lines (253 loc) · 8.16 KB
/
MagazineShelf.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Librarian Assistant Pro - Version 1.7
* Class: MagazineShelf
* Duties:
* The instance of this class represents a shef of magazines
* magazines in this shelf are all checked in
* the class gives us access to magazines and helps us
* to manipulate and use them
* Creation Date: December 2008
*/
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
class MagazineShelf
{
private DoublyLinkedList magazineShelf = new DoublyLinkedList();
public int size()
{
return magazineShelf.size();
}
public void addToShelf(Magazine newMagazine)
{
magazineShelf.add(newMagazine);
}
public Magazine getMagazine(int n)
throws NullPointerException
{
try
{
return (Magazine)magazineShelf.get(n+1).getValue();
}
catch (NullPointerException ex)
{
System.out.println("Oops, the index is out of bound!");
}
return null;
}
public void removeMagazine(Magazine magazine)
{
magazineShelf.removeObj(magazine);
}
public DoublyLinkedList getAllMagazines()
{
return magazineShelf;
}
public void setMagazineShelf(DoublyLinkedList newMagazineShelf)
{
magazineShelf = newMagazineShelf;
}
public ArrayList<String> magazineShelfToString()
{
ArrayList<String> strArr = new ArrayList<String>();
int counter = 0;
while(counter < magazineShelf.size())
{
Magazine magazine = (Magazine)magazineShelf.get(counter+1).getValue();
strArr.add(magazine.toString());
counter++;
}
return strArr;
}
//Creates a Magazine type object using the given string
public Magazine stringToMagazine(String strBlock)
{
String titleStr = strBlock.substring(0,strBlock.indexOf("|"));
strBlock = strBlock.substring(strBlock.indexOf("|") + 1);
Title title = new Title(titleStr, null, 0, null, 0);
String trackingCode = strBlock.substring(0,strBlock.indexOf("|"));
strBlock = strBlock.substring(strBlock.indexOf("|") + 1);
String arrivalDateStr = strBlock.substring(0);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
Date arrivalDate = new Date();
try
{
arrivalDate = dateFormat.parse(arrivalDateStr);
}
catch (ParseException e)
{
System.out.println("Invalid Date Parser Exception.");
e.printStackTrace();
}
Magazine myMagazine = new Magazine(title, trackingCode, arrivalDate);
return myMagazine;
}
public void addAllMagazines(ArrayList<String> strArr)
{
int i = 0;
while(i < strArr.size())
{
Magazine myMagazine = stringToMagazine(strArr.get(i));
magazineShelf.add(myMagazine);
i++;
}
}
public void printMagazineShelfReport()
{
int i = 0;
System.out.println("---------------------------Report: Checke in Magazines---------------------------");
System.out.println("Title" + "\t\t\t\t\t\t\t" + "Tracking Code" + "\t\t\t\t\t\t" + "Arrival Date");
while(i<magazineShelf.size())
{
Magazine myMagazine = (Magazine)magazineShelf.get(i+1).getValue();
String title = myMagazine.getTitle();
String trackingCode = myMagazine.getTrackingCode();
Date arrivalDate = myMagazine.getArrivalDate();
DateFunctions formatter = new DateFunctions();
String arrivalDateStr = formatter.dateToString(arrivalDate);
System.out.println(title + "\t\t\t\t\t\t\t" + trackingCode + "\t\t\t\t\t\t" + arrivalDateStr);
i++;
}
System.out.println("");
}
//returns true If magazine exists in magazineShelf
public boolean magazineExist(Title title, Date arrivalDate)
{
Magazine myMagazine;
for (int i=0; i < magazineShelf.size(); i++)
{
myMagazine = (Magazine)magazineShelf.get(i+1).getValue();
if(myMagazine.getTitle().equals(title) && myMagazine.getArrivalDate().equals(arrivalDate))
{
return true;
}
}
return false;
}
//Looks for a specific tracking code in magazineShelf
//If tracking code exists returns true
public boolean trackingCodeExist(String trackingCode)
{
Magazine myMagazine;
for (int i=0; i < magazineShelf.size(); i++)
{
myMagazine = (Magazine)magazineShelf.get(i+1).getValue();
if(myMagazine.getTrackingCode().equals(trackingCode))
{
return true;
}
}
return false;
}
// This method arranges all the issues of "title" based on their arrival date and returns an
// arraylist of Magazines which contains all the issues of "title"
public ArrayList<Magazine> sortIssues(Title title)
{
ArrayList<Magazine> magArr = new ArrayList<Magazine>();
Magazine myMagazine;
for (int i=0; i < magazineShelf.size(); i++)
{
myMagazine = (Magazine)magazineShelf.get(i+1).getValue();
if(myMagazine.getTitle().equals(title.getTitle()))
{
magArr.add(myMagazine);
}
}
if (!magArr.isEmpty())
{
quickSort(magArr, 0, magArr.size()-1);
return magArr;
}
return null;
}
//Returns the latest issue of a specific title
//available in magazine shelf
public Magazine latestIssue(Title title)
{
if (sortIssues(title) != null)
{
ArrayList<Magazine> magArr = sortIssues(title);
return magArr.get(magArr.size()-1); //returning the last magaizne in the array list
//which would be the latest issue
}
else
return null;
}
//returns an arraylist of strings which contains
//the string representation of all the magazines
//which were to be arrived but are delayed
public ArrayList<String> delayedMagazines(Titles titleList)
{
ArrayList<String> arrTitles = new ArrayList<String>();
Date today = new Date(); //today's date
for(int i = 0; i < titleList.size(); i++)
{
Title myTitle = titleList.getTitle(i);
int period = myTitle.getPeriod();
Magazine lastIssue = latestIssue(myTitle);
if (lastIssue != null)
{
Date lastArrivalDate = lastIssue.getArrivalDate();
//adding period days to last arrival date
DateFunctions dateTool = new DateFunctions();
Date nextArrivalDate = dateTool.addDaysToDate(lastArrivalDate, period);
//checking whether the magazine issue is delayed or not
if(nextArrivalDate.before(today))
arrTitles.add(myTitle.getTitle() + "|" + dateTool.dateToString(nextArrivalDate));
}
}
return arrTitles;
}
public void printDelayedMagazines(Titles titleList)
{
int i = 0;
ArrayList<String> arrTitles = delayedMagazines(titleList);
System.out.println("---------------------------Report: Delayed Magazines---------------------------");
System.out.println("Title" + "\t\t\t\t\t\t\t\t\t\t" + "Should have been arrived at");
while(i<magazineShelf.size()-1)
{
String strBlock = arrTitles.get(i);
String title = strBlock.substring(0, strBlock.indexOf("|"));
strBlock = strBlock.substring(strBlock.indexOf("|")+1);
String passedDate = strBlock.substring(0);
System.out.println(title + "\t\t\t\t\t\t\t\t" + passedDate);
i++;
}
System.out.println("");
}
// This sorting method is desigend to sort the magazines by their arrival dates
public void quickSort (ArrayList<Magazine> magArr, int from, int to)
{
// from is the lower index, to is the upper index
// of the region of the arraylist that is to be sorted
if(to != 0)
{
int i = from;
int j = to;
Date temp;
Date mid = magArr.get((from + to)/2).getArrivalDate();
// partition
do
{
while (magArr.get(i).getArrivalDate().before(mid)) //magArr.get(from).date < mid
i++;
while (magArr.get(j).getArrivalDate().after(mid)) //magArr.get(to).date > mid
j--;
if (i <= j)
{
temp = magArr.get(i).getArrivalDate();
magArr.get(i).setArrivalDate(magArr.get(j).getArrivalDate());
magArr.get(j).setArrivalDate(temp);
i++;
j--;
}
}while (i <= j);
// Using recursion to sort all the arraylist:
if (from < j) //Basecase
quickSort(magArr, from, j);
if (i < to) //Basecase
quickSort(magArr, i, to);
}
}
}