Skip to content

Commit 4e06aa3

Browse files
committed
patch
1 parent 95877a3 commit 4e06aa3

1 file changed

Lines changed: 38 additions & 17 deletions

File tree

src/ru/javawebinar/basejava/storage/ArrayStorage.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,43 @@ public void clear() {
2020

2121
}
2222

23-
public void save(Resume r) {
24-
//TODO check if resume present & check storage overflow
25-
storage[size] = r;
26-
size++;
23+
public void update(Resume r) {
24+
int index = getIndex(r.getUuid());
25+
if (index == -1) {
26+
System.out.println("Resume " + r.getUuid() + " not exist");
27+
} else {
28+
storage[index] = r;
29+
}
30+
}
2731

32+
public void save(Resume r) {
33+
if (getIndex(r.getUuid()) != -1) {
34+
System.out.println("Resume " + r.getUuid() + " already exist");
35+
} else if (size >= storage.length) {
36+
System.out.println("Storage overflow");
37+
} else {
38+
storage[size] = r;
39+
size++;
40+
}
2841
}
2942

3043
public Resume get(String uuid) {
31-
for (int i = 0; i < size; i++) {
32-
if (uuid.equals(storage[i].getUuid())) {
33-
return storage[i];
34-
}
44+
int index = getIndex(uuid);
45+
if (index == -1) {
46+
System.out.println("Resume " + uuid + " not exist");
47+
return null;
3548
}
36-
return null;
37-
49+
return storage[index];
3850
}
3951

4052
public void delete(String uuid) {
41-
// TODO check if resume present
42-
for (int i = 0; i < size; i++) {
43-
if (uuid.equals(storage[i].getUuid())) {
44-
storage[i] = storage[size - 1];
45-
storage[size - 1] = null;
46-
size--;
47-
}
53+
int index = getIndex(uuid);
54+
if (index == -1) {
55+
System.out.println("Resume " + uuid + " not exist");
56+
} else {
57+
storage[index] = storage[size - 1];
58+
storage[size - 1] = null;
59+
size--;
4860
}
4961
}
5062

@@ -62,4 +74,13 @@ public int size() {
6274
return size;
6375
}
6476

77+
private int getIndex(String uuid) {
78+
for (int i = 0; i < size; i++) {
79+
if (uuid.equals(storage[i].getUuid())) {
80+
return i;
81+
}
82+
}
83+
return -1;
84+
}
85+
6586
}

0 commit comments

Comments
 (0)