Skip to content

Commit 42fa14d

Browse files
authored
Исправлены ошибки
- добавлено поле size для хранения количества резюме в базе - в методе getAll возвращаемый массив переименован в resumes - в методе delete удаляется последнее добавленное резюме, исправлена ошибка выхода за границы массива - в методе save добавлена проверка максимальной длины массива, если база переполнена то новое резюме не сохраняется
1 parent 17dc102 commit 42fa14d

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

src/ArrayStorage.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
* Array based storage for Resumes
33
*/
44
public class ArrayStorage {
5-
Resume[] storage = new Resume[10000];
5+
Resume[] storage = new Resume[3];
6+
private int size = size();
67

78
void clear() {
8-
int size = this.size();
99
for (int i = 0; i < size; i++) {
1010
storage[i] = null;
1111
}
12+
size = 0;
1213
}
1314

1415
void save(Resume r) {
15-
int size = this.size();
16-
if (r.uuid != null) {
16+
if (r.uuid != null & size < storage.length) {
1717
storage[size] = r;
18+
size++;
1819
}
1920
}
2021

2122
Resume get(String uuid) {
22-
int size = this.size();
2323
if (uuid != null) {
2424
for (int i = 0; i < size; i++) {
2525
if (storage[i].uuid.equals(uuid)) {
@@ -31,13 +31,13 @@ Resume get(String uuid) {
3131
}
3232

3333
void delete(String uuid) {
34-
int size = this.size();
3534
if (uuid != null) {
36-
for (int i = 0; i < size; i++) {
35+
for (int i = size - 1; i >= 0; i--) {
3736
if (storage[i].uuid.equals(uuid)) {
38-
for (int k = i; k < size; k++) {
37+
for (int k = i; k < size - 1; k++) {
3938
storage[k] = storage[k+1];
4039
}
40+
size--;
4141
break;
4242
}
4343
}
@@ -48,12 +48,11 @@ void delete(String uuid) {
4848
* @return array, contains only Resumes in storage (without null)
4949
*/
5050
Resume[] getAll() {
51-
int size = this.size();
52-
Resume[] storageAll = new Resume[size];
51+
Resume[] resumes = new Resume[size];
5352
for (int i = 0; i < size; i++) {
54-
storageAll[i] = storage[i];
53+
resumes[i] = storage[i];
5554
}
56-
return storageAll;
55+
return resumes;
5756
}
5857

5958
int size() {

0 commit comments

Comments
 (0)