-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostMan.java
More file actions
77 lines (67 loc) · 1.82 KB
/
PostMan.java
File metadata and controls
77 lines (67 loc) · 1.82 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
package com.wonderfulbytes.tests.stacksAndQueues.familymail;
import java.util.ArrayList;
public class PostMan extends Person {
// Mail he receives from MailService
ArrayList<Letter> mail = new ArrayList<>();
// MailBoxes assigned to
ArrayList<MailBox> mailBoxes = new ArrayList<>();
/**
*
*/
public PostMan() {
super(Gender.MALE);
}
public void addMailBox(MailBox mailBox) {
mailBoxes.add(mailBox);
}
public ArrayList<MailBox> getMailBoxes() {
return mailBoxes;
}
public void collectMail(ArrayList<Letter> letters) {
// Collects only the mail belonging
// to the mailboxes he's assigned to
for (Letter letter : letters) {
for (MailBox mailBox : mailBoxes) {
if (letter.getRecipient().getResidence().getMailBox() == mailBox) {
mail.add(letter);
letters.remove(letter);
}
}
}
}
public void collectLetter(Letter letter) {
mail.add(letter);
}
public void deliverMail() {
// Go through all the letters
// and put right letters in the mailBox
ArrayList<Letter> lettersToRemove =
new ArrayList<>();
for (MailBox mailBox : mailBoxes) {
for (Letter letter : mail) {
if (letter.getRecipient()
.getResidence().getMailBox()
== mailBox) {
mailBox.put(letter);
lettersToRemove.add(letter);
}
}
for (Letter letterToRemove : lettersToRemove) {
mail.remove(letterToRemove);
}
}
}
public void displayMail() {
for (Letter letter : mail) {
System.out.println("\t\tLetter for "
+ letter.getRecipient().getName() );
}
}
public String toString() {
return "\tHi, I'm " + this.getName() + ", a postman.\n"+
"\tI've been assigned to " +
mailBoxes.size() + " mail box(es)" +
"\n\tI have " + mail.size() +
" letter(s) pending.";
}
}