-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonDataManager.java
228 lines (180 loc) · 5.96 KB
/
PersonDataManager.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
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
public class PersonDataManager {
private static HW1_Person[] peopleArray = new HW1_Person[0];
private static int counter = 0;
public PersonDataManager() { // no - arg constructor
}
public PersonDataManager(HW1_Person[] p) {
this.peopleArray = p;
}
public static PersonDataManager buildFromFile(String location) throws IllegalArgumentException {
try {
File f = new File(location);
Scanner read = new Scanner(f);
int count = 0;
while (read.hasNextLine()) {
read.nextLine();
count++;
}
peopleArray = new HW1_Person[count - 1];
read = new Scanner(f);
// skip line 1
read.nextLine();
int index = 0;
while (read.hasNextLine()) {
String temp = read.nextLine(); // reading the data from file
String[] vals = temp.split(",");
if (vals.length != 5)
continue;
String name = vals[0].replace('\"', ' ').trim();
String gender = vals[1].replace('\"', ' ').trim();
if (!gender.toUpperCase().equals("M") && !gender.toUpperCase().equals("F")) {
throw new IllegalArgumentException();
}
if (name.matches("-?\\d+")) {
throw new IllegalArgumentException();
}
int age = Integer.parseInt(vals[2].trim());
double height = Double.parseDouble(vals[3].trim());
double weight = Double.parseDouble(vals[4].trim());
HW1_Person tempo = new HW1_Person(name, age, gender, height, weight);
for (int i = 0; i <= index; i++) {
if (contains(tempo)) {
throw new IllegalArgumentException();
}
}
peopleArray[index] = tempo; // storing the data in peopleArray
index++;
}
read.close();
PersonDataManager pm = new PersonDataManager(peopleArray);
counter = peopleArray.length;
return pm;
} catch (IOException io) {
System.out.print("File not found !");
} catch (IllegalArgumentException ex) {
System.out.print(" Input is in the wrong format . Please check the format !");
throw new IllegalArgumentException();
}
throw new IllegalArgumentException();
}
public static boolean contains(HW1_Person person2) { // checks if the person with same data exists
for (int i = 0; i < peopleArray.length; i++) {
if (peopleArray[i] != null && peopleArray[i].equals(person2)) {
return true;
}
}
return false;
}
public void addPerson(HW1_Person newPerson) throws PersonAlreadyExistsException {
HW1_Person[] newPeopleArray = new HW1_Person[peopleArray.length + 1];
for (int i = 0; i < peopleArray.length; i++) {
if (contains(newPerson)) {
throw new PersonAlreadyExistsException();
} else {
System.arraycopy(peopleArray, 0, newPeopleArray, 0, peopleArray.length);
newPeopleArray[peopleArray.length] = newPerson;
break;
}
}
sort(newPeopleArray);
peopleArray = newPeopleArray;
counter++;
}
public void printTable() {
System.out.println(
" Name Age Gender Height Weight ");
for (int i = 0; i < peopleArray.length - 1; i++) {
if (peopleArray[i] != null) {
System.out.println(peopleArray[i].toString());
}
}
}
public void getPerson(String name) throws PersonDoesNotExistsException {
int count = 0;
int temp = 0;
for (int i = 0; i < peopleArray.length - 1; i++) {
if (peopleArray[i] != null) {
if ((peopleArray[i].getName().toUpperCase()).equals(name.toUpperCase())) {
if (peopleArray[i].getGender().toUpperCase().equals("M")) {
count = i;
temp = 1;
break;
} else {
count = i;
temp = 2;
break;
}
}
}
}
if (temp == 1) {
System.out.println(name + " is a " + peopleArray[count].getAge() + " years old male who is "
+ peopleArray[count].getHeight() + " inches tall and weighs " + peopleArray[count].getWeight()
+ " pounds");
return;
} else if (temp == 2) {
System.out.println(name + " is a " + peopleArray[count].getAge() + " years old female who is "
+ peopleArray[count].getHeight() + " inches tall and weighs " + peopleArray[count].getWeight()
+ " pounds");
return;
}
throw new PersonDoesNotExistsException();
}
public void removePerson(String name) throws PersonDoesNotExistsException {
int count = -1;
for (int i = 0; i < peopleArray.length - 1; i++) {
if (peopleArray[i] != null) {
if ((peopleArray[i].getName().toUpperCase()).equals(name.toUpperCase())) {
count = i;
}
}
}
if (count == -1) {
throw new PersonDoesNotExistsException();
}
HW1_Person[] copyPeopleArray = new HW1_Person[peopleArray.length - 1];
for (int i = 0, j = 0; i < peopleArray.length; i++) {
if (i == count) {
continue;
}
copyPeopleArray[j++] = peopleArray[i];
}
peopleArray = copyPeopleArray;
}
public void SaveToFile(String name) throws IOException {
FileWriter fw = new FileWriter(name);
PrintWriter out = new PrintWriter(fw);
out.println(
" Name Age Gender Height Weight ");
for (int i = 0; i < peopleArray.length - 1; i++) {
if (peopleArray[i] != null) {
out.println(peopleArray[i].toString());
}
}
out.close();
}
public static void sort(HW1_Person[] array) {
HW1_Person temp;
for (int a = 0; a < array.length; a++) {
if (array[a] != null) {
for (int b = 0; b < array.length - 1; b++) {
if (array[b] == null && array[b + 1] != null) {
array[b] = array[b + 1];
array[b + 1] = null;
} else if (array[b] != null && array[b + 1] != null) {
if (array[b].getName().toUpperCase().compareTo(array[b + 1].getName().toUpperCase()) > 0) {
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
}
}
}
}