-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtherPerson.java
More file actions
135 lines (112 loc) · 3.24 KB
/
OtherPerson.java
File metadata and controls
135 lines (112 loc) · 3.24 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
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
package newLessons.lessonTen;
public class OtherPerson {
private String name;
private boolean gender;
private Month month;
private int day;
private int year;
private State state;
private OtherPerson[] friends;
private OtherPerson[] enemies;
public OtherPerson(String personsName, boolean personsGender, Month month, int day, int year, State state) {
this.name = personsName;
this.gender = personsGender;
this.month = month;
this.day = this.month.fixDay(day);
this.year = year;
this.state = state;
this.friends = new OtherPerson[2];
this.enemies = new OtherPerson[2];
}
public void addFriend(OtherPerson friend) {
boolean added = false;
for (int count = 0; count < this.friends.length; count++) {
if (!added) {
if (this.friends[count] == null) {
this.friends[count] = friend;
added = true;
}
}
}
if (!added) {
OtherPerson[] tempArray = this.friends;
this.friends = new OtherPerson[this.friends.length * 2];
for (int count = 0; count < tempArray.length; count++) {
this.friends[count] = tempArray[count];
}
this.friends[tempArray.length] = friend;
}
for (int count = 0; count < this.enemies.length; count++) {
if (this.enemies[count] != null && this.enemies[count].equals(friend)) {
this.enemies[count] = null;
}
}
}
public void addEnemy(OtherPerson enemy) {
boolean added = false;
for (int count = 0; count < this.enemies.length; count++) {
if (!added) {
if (this.enemies[count] == null) {
this.enemies[count] = enemy;
added = true;
}
}
}
if (!added) {
OtherPerson[] tempArray = this.enemies;
this.enemies = new OtherPerson[this.enemies.length * 2];
for (int count = 0; count < tempArray.length; count++) {
this.enemies[count] = tempArray[count];
}
this.enemies[tempArray.length] = enemy;
}
for (int count = 0; count < this.friends.length; count++) {
if (this.friends[count] != null && this.friends[count].equals(enemy)) {
this.friends[count] = null;
}
}
}
public void speak() {
System.out.println(this.toString());
}
public void speakTo(OtherPerson person) {
System.out.print("Hello " + person.name + "! ");
this.speak();
for (int count = 0; count < this.friends.length; count++) {
if (person.equals(this.friends[count])) {
System.out.println("You are my friend!");
}
}
for (int count = 0; count < this.enemies.length; count++) {
if (person.equals(this.enemies[count])) {
System.out.println("You are my enemy!");
}
}
}
@Override
public String toString() {
String info = "My name is " + this.name + " and I am a ";
info += (2014 - this.year) + " year old ";
if (gender) {
info += "boy!";
} else {
info += "girl!";
}
return info;
}
@Override
public boolean equals(Object o) {
if (o instanceof OtherPerson) {
OtherPerson person = (OtherPerson)o;
boolean name = this.name.equals(person.name);
boolean gender = this.gender == person.gender;
boolean month = this.month == person.month;
boolean day = this.day == person.day;
boolean year = this.year == person.year;
boolean state = this.state == person.state;
return name && gender && month && day && year && state;
} else {
return false;
}
}
}