Skip to content

Commit c4e3506

Browse files
committed
Second lesson
1 parent ac1cf55 commit c4e3506

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

src/ArrayStorage.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,76 @@
66
public class ArrayStorage {
77
Resume[] storage = new Resume[10000];
88
private int lastRecord = 0;
9+
private static final String ERROR_MSG = "ERROR: record doesn't exists!";
910

10-
void clear() {
11+
public void clear() {
1112
for (int i = 0; i < lastRecord; i++) {
1213
storage[i] = null;
1314
}
1415
lastRecord = 0;
1516
}
1617

17-
void save(Resume r) {
18+
public void save(Resume r) {
19+
if (get(r) != null) {
20+
System.out.println(ERROR_MSG);
21+
return;
22+
}
1823
storage[lastRecord++] = r;
1924
}
2025

21-
Resume get(String uuid) {
22-
for (int i = 0; i < lastRecord; i++) {
23-
if (storage[i].uuid.equals(uuid)) {
24-
return storage[i];
25-
}
26+
public Resume get(String uuid) {
27+
if (getNumber(uuid) >= 0) {
28+
return storage[getNumber(uuid)];
2629
}
2730
return null;
2831
}
2932

30-
void delete(String uuid) {
33+
private Resume get(Resume resume) {
34+
return get(resume.uuid);
35+
}
36+
37+
private int getNumber(String uuid) {
3138
for (int i = 0; i < lastRecord; i++) {
3239
if (storage[i].uuid.equals(uuid)) {
33-
storage[i] = storage[lastRecord - 1];
34-
storage[lastRecord - 1] = null;
35-
lastRecord--;
36-
break;
40+
return i;
3741
}
3842
}
43+
return -1;
3944
}
4045

46+
47+
public void delete(String uuid) {
48+
if (getNumber(uuid) >= 0) {
49+
storage[getNumber(uuid)] = storage[lastRecord - 1];
50+
storage[lastRecord - 1] = null;
51+
lastRecord--;
52+
} else {
53+
System.out.println(ERROR_MSG);
54+
}
55+
}
56+
57+
4158
/**
4259
* @return array, contains only Resumes in storage (without null)
4360
*/
44-
Resume[] getAll() {
61+
62+
63+
public Resume[] getAll() {
4564
return Arrays.copyOf(storage, lastRecord);
4665
}
4766

48-
int size() {
67+
public int size() {
4968
return lastRecord;
5069
}
70+
71+
72+
public boolean update(Resume resume) {
73+
int pos = getNumber(resume.uuid);
74+
if (pos == -1) {
75+
System.out.println(ERROR_MSG);
76+
return false;
77+
}
78+
get(storage[pos]).uuid = resume.uuid;
79+
return true;
80+
}
5181
}

0 commit comments

Comments
 (0)