|
6 | 6 |
|
7 | 7 | public class SortedArrayStorage extends AbstractArrayStorage { |
8 | 8 |
|
9 | | - @Override |
10 | | - public void update(Resume resume) { |
11 | | - int index = getIndex(resume.getUuid()); |
12 | | - |
13 | | - if (isExisting(index)) { |
14 | | - storage[index] = resume; |
15 | | - System.out.println("Резюме успешно обновлено ID:" + resume.getUuid()); |
16 | | - } else { |
17 | | - System.out.println("Такое резюме не найдено в массиве ID: " + resume.getUuid()); |
18 | | - } |
19 | | - } |
20 | | - |
21 | 9 | @Override |
22 | 10 | public void save(Resume resume) { |
23 | 11 | int index = getIndex(resume.getUuid()); |
24 | 12 |
|
25 | | - if (STORAGE_LIMIT <= size) { |
26 | | - System.out.println("Storage overflow!"); |
27 | | - } else if (isExisting(index)){ |
28 | | - System.out.println("Данный ID:" + resume.getUuid() + " уже существует!"); |
29 | | - } else if (size == 0){ |
30 | | - storage[0] = resume; |
31 | | - System.out.println("Резюме успешно сохранено в первую ячейку! ID:" + resume.getUuid()); |
| 13 | + if (overflowOrExist(resume, index)) { |
32 | 14 | return; |
33 | 15 | } |
34 | 16 |
|
35 | | - while (true) { |
36 | | - int lowerBound = 0; |
37 | | - int upperBound = size - 1; |
38 | | - int currentIndex = 0; |
39 | | - while (true) { |
40 | | - currentIndex = (upperBound + lowerBound) / 2; |
41 | | - if (storage[currentIndex].compareTo(resume) < 0) { |
42 | | - lowerBound = currentIndex + 1; // its in the upper |
43 | | - if (lowerBound > upperBound) |
44 | | - currentIndex += 1; |
45 | | - storage[currentIndex] = resume; |
46 | | - |
47 | | - } else { |
48 | | - upperBound = currentIndex - 1; // its in the lower |
49 | | - if (lowerBound > upperBound) |
50 | | - storage[currentIndex] = resume; |
51 | | - size++; |
52 | | - return; |
53 | | - } |
54 | | - } |
55 | | - } |
| 17 | + System.arraycopy(storage, -index - 1, storage, -index, size - index); |
| 18 | + storage[-index - 1] = resume; |
| 19 | + System.out.println("Резюме успешно добавлено в массив! ID:" + resume.getUuid()); |
| 20 | + size++; |
56 | 21 | } |
57 | 22 |
|
58 | 23 | @Override |
59 | 24 | public void delete(String uuid) { |
60 | 25 | int index = getIndex(uuid); |
61 | | - if(index < 0 ) { |
| 26 | + if (index < 0) { |
62 | 27 | System.out.println("Не найдено резюме ID:" + uuid); |
63 | | - return; |
64 | 28 | } else { |
65 | | - Resume[] tempArray = new Resume[size - index - 1]; |
66 | | - System.arraycopy(storage, index + 1, tempArray, 0, size - index - 1); |
67 | | - System.out.println(Arrays.toString(tempArray)); |
68 | | - System.arraycopy(tempArray, 0, storage, index, size - 1); |
| 29 | + System.arraycopy(storage, index + 1, storage, index, size - index - 1); |
69 | 30 | size--; |
70 | 31 | System.out.println("Резюме удалено из сортированного массива! ID:" + uuid); |
71 | 32 | } |
|
0 commit comments