From 54cb720c1b58852529b7644095b4593593c1578e Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 17 Oct 2024 12:08:42 +0500 Subject: [PATCH 01/39] HW1 --- src/ArrayStorage.java | 38 +++++++++++++++++++++++++++++++++++--- src/Resume.java | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/ArrayStorage.java b/src/ArrayStorage.java index 7aff0388..bfac4bff 100644 --- a/src/ArrayStorage.java +++ b/src/ArrayStorage.java @@ -1,30 +1,62 @@ +import java.util.Arrays; + /** * Array based storage for Resumes */ public class ArrayStorage { Resume[] storage = new Resume[10000]; + int size = 0; void clear() { + for (int i = 0; i < size; i++) { + storage[i] = null; + } + System.out.println("\nThe array was successfully cleared"); } void save(Resume r) { + if (size < storage.length) { + storage[size] = r; + size++; + } + System.out.println("Element " + r + " successfully saved to array"); } Resume get(String uuid) { + for (int i = 0; i < size; i++) { + if (storage[i].getUuid().equals(uuid)) { + return storage[i]; + } + } return null; } void delete(String uuid) { + for (int i = 0; i < size; i++) { + if (storage[i].getUuid().equals(uuid) && storage[i] != null) { + for (int j = i; j < size - 1; j++) { + storage[j] = storage[j + 1]; + } + storage[size - 1] = null; + size--; + System.out.println("\nElement " + uuid + " successfully deleted"); + return; + } + } + System.out.println("\nElement " + uuid + " not found"); } - /** * @return array, contains only Resumes in storage (without null) */ Resume[] getAll() { - return new Resume[0]; + Resume[] storageAll = new Resume[size]; + for (int i = 0; i < size; i++) { + storageAll[i] = storage[i]; + } + return storageAll; } int size() { - return 0; + return size; } } diff --git a/src/Resume.java b/src/Resume.java index 8de4e4b8..b6a930ec 100644 --- a/src/Resume.java +++ b/src/Resume.java @@ -6,6 +6,10 @@ public class Resume { // Unique identifier String uuid; + public String getUuid() { + return uuid; + } + @Override public String toString() { return uuid; From 1aba60b74ebaf2ddb4d58c53782fc02ca0cab00c Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 17 Oct 2024 12:17:48 +0500 Subject: [PATCH 02/39] HW1 --- src/ArrayStorage.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ArrayStorage.java b/src/ArrayStorage.java index bfac4bff..ab8b2263 100644 --- a/src/ArrayStorage.java +++ b/src/ArrayStorage.java @@ -1,5 +1,3 @@ -import java.util.Arrays; - /** * Array based storage for Resumes */ @@ -11,6 +9,7 @@ void clear() { for (int i = 0; i < size; i++) { storage[i] = null; } + size = 0; System.out.println("\nThe array was successfully cleared"); } @@ -24,7 +23,7 @@ void save(Resume r) { Resume get(String uuid) { for (int i = 0; i < size; i++) { - if (storage[i].getUuid().equals(uuid)) { + if (storage[i] != null && storage[i].getUuid().equals(uuid)) { return storage[i]; } } @@ -33,7 +32,7 @@ Resume get(String uuid) { void delete(String uuid) { for (int i = 0; i < size; i++) { - if (storage[i].getUuid().equals(uuid) && storage[i] != null) { + if (storage[i] != null && storage[i].getUuid().equals(uuid)) { for (int j = i; j < size - 1; j++) { storage[j] = storage[j + 1]; } From 4e444d67c8e0ec9a9d5b5d49c6eadb20ac5c01e4 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 22 Oct 2024 12:58:35 +0500 Subject: [PATCH 03/39] HW1:fix delete null, rename storageAll->resumes, corrected delete() --- src/ArrayStorage.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/ArrayStorage.java b/src/ArrayStorage.java index ab8b2263..aeb9c210 100644 --- a/src/ArrayStorage.java +++ b/src/ArrayStorage.java @@ -23,7 +23,7 @@ void save(Resume r) { Resume get(String uuid) { for (int i = 0; i < size; i++) { - if (storage[i] != null && storage[i].getUuid().equals(uuid)) { + if (storage[i].getUuid().equals(uuid)) { return storage[i]; } } @@ -32,11 +32,8 @@ Resume get(String uuid) { void delete(String uuid) { for (int i = 0; i < size; i++) { - if (storage[i] != null && storage[i].getUuid().equals(uuid)) { - for (int j = i; j < size - 1; j++) { - storage[j] = storage[j + 1]; - } - storage[size - 1] = null; + if (storage[i].getUuid().equals(uuid)) { + storage[i] = storage[size - 1]; size--; System.out.println("\nElement " + uuid + " successfully deleted"); return; @@ -44,15 +41,16 @@ void delete(String uuid) { } System.out.println("\nElement " + uuid + " not found"); } + /** * @return array, contains only Resumes in storage (without null) */ Resume[] getAll() { - Resume[] storageAll = new Resume[size]; + Resume[] resumes = new Resume[size]; for (int i = 0; i < size; i++) { - storageAll[i] = storage[i]; + resumes[i] = storage[i]; } - return storageAll; + return resumes; } int size() { From 09b08b7b4cdec30de30b03b4671331daee310a13 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 22 Nov 2024 16:20:54 +0500 Subject: [PATCH 04/39] HW2: sorted by packages --- lesson/lesson1.md | 12 +++++------ src/{ => com/urise/webapp}/MainArray.java | 9 +++++++-- src/{ => com/urise/webapp/model}/Resume.java | 8 +++++++- .../urise/webapp/storage}/ArrayStorage.java | 20 +++++++++++-------- .../webapp/storage}/MainTestArrayStorage.java | 16 +++++++++------ 5 files changed, 42 insertions(+), 23 deletions(-) rename src/{ => com/urise/webapp}/MainArray.java (91%) rename src/{ => com/urise/webapp/model}/Resume.java (62%) rename src/{ => com/urise/webapp/storage}/ArrayStorage.java (78%) rename src/{ => com/urise/webapp/storage}/MainTestArrayStorage.java (70%) diff --git a/lesson/lesson1.md b/lesson/lesson1.md index 6c2f6331..1ebfc3fb 100644 --- a/lesson/lesson1.md +++ b/lesson/lesson1.md @@ -67,7 +67,7 @@ ![Screenshot_5](https://user-images.githubusercontent.com/29703461/199550057-fce7cf3c-7040-422f-b490-7b85b47ae952.png) -- Реализуйте методы `save, get, delete, clear, getAll, size` в классе `ArrayStorage`, организовав хранение резюме в массиве +- Реализуйте методы `save, get, delete, clear, getAll, size` в классе `com.urise.webapp.storage.ArrayStorage`, организовав хранение резюме в массиве - Храните все резюме в начале `storage` (без пустот в виде `null`), чтобы не перебирать каждый раз все 10_000 элементов - При реализации метода `delete` учитывайте, что после удаления резюме между оставшимися резюме не должно быть пустых ячеек, заполненных null ``` @@ -77,7 +77,7 @@ r1, r2, r3,..., rn, null, null,..., null <----- size -----> <------- storage.length (10000) -------> ``` -- Проверьте вашу реализацию с помощью классов `MainArray.main()` и `MainTestArrayStorage.main()` +- Проверьте вашу реализацию с помощью классов `com.urise.webapp.MainArray.main()` и `com.urise.webapp.storage.MainTestArrayStorage.main()` - Изучите дополнительные материалы по IntelliJ IDEA: - [Idea Wiki](https://github.com/JavaOPs/topjava/wiki/IDEA) - [Отладка Java кода в IDEA. Основные возможности отладчика](https://youtu.be/Z1BQsf0A4xY) @@ -96,11 +96,11 @@ r1, r2, r3,..., rn, null, null,..., null 1. Перед каждым коммитом не забывайте пользоваться сочетанием клавиш `Ctrl + Alt + L` (автоматическое форматирование кода) 1. Удаляйте в классах неиспользуемые импорты (`Ctrl + Alt + O`) 1. Не игнорируй подсказки IDEA (подсвечивает) -1. В методе `clear()` обнуление массива предполагает обнуление (null) ячеек, где хранятся Resume, а не создание нового или присваивание ему null +1. В методе `clear()` обнуление массива предполагает обнуление (null) ячеек, где хранятся com.urise.webapp.model.Resume, а не создание нового или присваивание ему null 1. При реализации методов не используйте коллекции -1. Не меняйте сигнатуры методов в `ArrayStorage` -1. Не добавляйте в `Resume` новые поля -1. Resume r — давайте переменным осмысленные имена, например resume. r допустимо в коротких циклах и лямбда-выражениях +1. Не меняйте сигнатуры методов в `com.urise.webapp.storage.ArrayStorage` +1. Не добавляйте в `com.urise.webapp.model.Resume` новые поля +1. com.urise.webapp.model.Resume r — давайте переменным осмысленные имена, например resume. r допустимо в коротких циклах и лямбда-выражениях ## ![video](https://cloud.githubusercontent.com/assets/13649199/13672715/06dbc6ce-e6e7-11e5-81a9-04fbddb9e488.png) 5. [Вебинар "Быть программистом: от детства к зрелости"](https://www.youtube.com/watch?v=D5Hej0TyLaU) - [Слайды вебинара](https://docs.google.com/presentation/d/1YwtCCZsaGMdl-V15kTDHiJxiS52IAl-qqheNPpiNr54/) diff --git a/src/MainArray.java b/src/com/urise/webapp/MainArray.java similarity index 91% rename from src/MainArray.java rename to src/com/urise/webapp/MainArray.java index 063364db..99ca1df9 100644 --- a/src/MainArray.java +++ b/src/com/urise/webapp/MainArray.java @@ -1,9 +1,14 @@ +package com.urise.webapp; + +import com.urise.webapp.model.Resume; +import com.urise.webapp.storage.ArrayStorage; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** - * Interactive test for ArrayStorage implementation + * Interactive test for com.urise.webapp.storage.ArrayStorage implementation * (just run, no need to understand) */ public class MainArray { @@ -32,7 +37,7 @@ public static void main(String[] args) throws IOException { break; case "save": r = new Resume(); - r.uuid = uuid; + r.setUuid(uuid); ARRAY_STORAGE.save(r); printAll(); break; diff --git a/src/Resume.java b/src/com/urise/webapp/model/Resume.java similarity index 62% rename from src/Resume.java rename to src/com/urise/webapp/model/Resume.java index b6a930ec..93db9f2c 100644 --- a/src/Resume.java +++ b/src/com/urise/webapp/model/Resume.java @@ -1,15 +1,21 @@ +package com.urise.webapp.model; + /** * Initial resume class */ public class Resume { // Unique identifier - String uuid; + private String uuid; public String getUuid() { return uuid; } + public void setUuid(String uuid) { + this.uuid = uuid; + } + @Override public String toString() { return uuid; diff --git a/src/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java similarity index 78% rename from src/ArrayStorage.java rename to src/com/urise/webapp/storage/ArrayStorage.java index aeb9c210..6d3f26f4 100644 --- a/src/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -1,11 +1,15 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + /** * Array based storage for Resumes */ public class ArrayStorage { - Resume[] storage = new Resume[10000]; - int size = 0; + private Resume[] storage = new Resume[10000]; + private int size = 0; - void clear() { + public void clear() { for (int i = 0; i < size; i++) { storage[i] = null; } @@ -13,7 +17,7 @@ void clear() { System.out.println("\nThe array was successfully cleared"); } - void save(Resume r) { + public void save(Resume r) { if (size < storage.length) { storage[size] = r; size++; @@ -21,7 +25,7 @@ void save(Resume r) { System.out.println("Element " + r + " successfully saved to array"); } - Resume get(String uuid) { + public Resume get(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { return storage[i]; @@ -30,7 +34,7 @@ Resume get(String uuid) { return null; } - void delete(String uuid) { + public void delete(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { storage[i] = storage[size - 1]; @@ -45,7 +49,7 @@ void delete(String uuid) { /** * @return array, contains only Resumes in storage (without null) */ - Resume[] getAll() { + public Resume[] getAll() { Resume[] resumes = new Resume[size]; for (int i = 0; i < size; i++) { resumes[i] = storage[i]; @@ -53,7 +57,7 @@ Resume[] getAll() { return resumes; } - int size() { + public int size() { return size; } } diff --git a/src/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java similarity index 70% rename from src/MainTestArrayStorage.java rename to src/com/urise/webapp/storage/MainTestArrayStorage.java index b15b81e2..81b57ce1 100644 --- a/src/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -1,28 +1,32 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + /** - * Test for your ArrayStorage implementation + * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { static final ArrayStorage ARRAY_STORAGE = new ArrayStorage(); public static void main(String[] args) { Resume r1 = new Resume(); - r1.uuid = "uuid1"; + r1.setUuid("uuid1"); Resume r2 = new Resume(); - r2.uuid = "uuid2"; + r2.setUuid("uuid2"); Resume r3 = new Resume(); - r3.uuid = "uuid3"; + r3.setUuid("uuid3"); ARRAY_STORAGE.save(r1); ARRAY_STORAGE.save(r2); ARRAY_STORAGE.save(r3); - System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.uuid)); + System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid())); System.out.println("Size: " + ARRAY_STORAGE.size()); System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); printAll(); - ARRAY_STORAGE.delete(r1.uuid); + ARRAY_STORAGE.delete(r1.getUuid()); printAll(); ARRAY_STORAGE.clear(); printAll(); From de0768143a0ac0ef47da7022f3ebfdffa57f64bf Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 26 Nov 2024 14:23:10 +0500 Subject: [PATCH 05/39] HW2: update --- src/com/urise/webapp/MainArray.java | 8 +++++++- src/com/urise/webapp/model/Resume.java | 15 +++++++++++++++ src/com/urise/webapp/storage/ArrayStorage.java | 13 ++++++++++++- .../webapp/storage/MainTestArrayStorage.java | 4 ++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/com/urise/webapp/MainArray.java b/src/com/urise/webapp/MainArray.java index 99ca1df9..59f08bd3 100644 --- a/src/com/urise/webapp/MainArray.java +++ b/src/com/urise/webapp/MainArray.java @@ -18,7 +18,7 @@ public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Resume r; while (true) { - System.out.print("Введите одну из команд - (list | size | save uuid | delete uuid | get uuid | clear | exit): "); + System.out.print("Введите одну из команд - (list | size | save uuid | delete uuid | get uuid | clear | update | exit): "); String[] params = reader.readLine().trim().toLowerCase().split(" "); if (params.length < 1 || params.length > 2) { System.out.println("Неверная команда."); @@ -52,8 +52,14 @@ public static void main(String[] args) throws IOException { ARRAY_STORAGE.clear(); printAll(); break; + case "update": + r = new Resume(); + r.setUuid(uuid); + ARRAY_STORAGE.update(r); + break; case "exit": return; + default: System.out.println("Неверная команда."); break; diff --git a/src/com/urise/webapp/model/Resume.java b/src/com/urise/webapp/model/Resume.java index 93db9f2c..d6b45cb6 100644 --- a/src/com/urise/webapp/model/Resume.java +++ b/src/com/urise/webapp/model/Resume.java @@ -1,5 +1,7 @@ package com.urise.webapp.model; +import java.util.Objects; + /** * Initial resume class */ @@ -20,4 +22,17 @@ public void setUuid(String uuid) { public String toString() { return uuid; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Resume resume = (Resume) o; + return Objects.equals(uuid, resume.uuid); + } + + @Override + public int hashCode() { + return Objects.hash(uuid); + } } diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 6d3f26f4..fc36e1e1 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -17,6 +17,17 @@ public void clear() { System.out.println("\nThe array was successfully cleared"); } + public void update(Resume r) { + for (int i = 0; i < size; i++) { + if (storage[i].equals(r)) { + storage[i].setUuid(r.getUuid()); + System.out.println("\nElement " + r + " successfully update"); + } else { + System.out.println("\nERROR: Element " + r + " not found"); + } + } + } + public void save(Resume r) { if (size < storage.length) { storage[size] = r; @@ -25,7 +36,7 @@ public void save(Resume r) { System.out.println("Element " + r + " successfully saved to array"); } - public Resume get(String uuid) { + public Resume get(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { return storage[i]; diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java index 81b57ce1..1f0aa359 100644 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -25,6 +25,10 @@ public static void main(String[] args) { System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); + Resume r = new Resume(); + r.setUuid("uuid3"); + ARRAY_STORAGE.update(r); + printAll(); ARRAY_STORAGE.delete(r1.getUuid()); printAll(); From 5592ef7aa8dc8242a3b747479764836004fe3787 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 26 Nov 2024 22:07:31 +0500 Subject: [PATCH 06/39] HW2: clear, save, get, delete, getAll,desiredIndex --- .../urise/webapp/storage/ArrayStorage.java | 63 ++++++++++--------- .../webapp/storage/MainTestArrayStorage.java | 3 +- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index fc36e1e1..77957095 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -2,57 +2,57 @@ import com.urise.webapp.model.Resume; +import java.util.Arrays; + /** * Array based storage for Resumes */ public class ArrayStorage { - private Resume[] storage = new Resume[10000]; + private final Resume[] storage = new Resume[10000]; private int size = 0; public void clear() { - for (int i = 0; i < size; i++) { - storage[i] = null; - } + Arrays.fill(storage, 0, size, null); size = 0; System.out.println("\nThe array was successfully cleared"); } public void update(Resume r) { - for (int i = 0; i < size; i++) { - if (storage[i].equals(r)) { - storage[i].setUuid(r.getUuid()); - System.out.println("\nElement " + r + " successfully update"); - } else { - System.out.println("\nERROR: Element " + r + " not found"); - } + int index = desiredIndex(r.getUuid()); + if (index >= 0) { + storage[index].setUuid(r.getUuid()); + System.out.println("\nElement " + r + " successfully update"); + } else { + System.out.println("\nERROR: Element " + r + " not found"); } } public void save(Resume r) { - if (size < storage.length) { + int index = desiredIndex(r.getUuid()); + if (index < 0 && size < storage.length) { storage[size] = r; size++; + System.out.println("Element " + r + " successfully saved to array"); + } else { + System.out.println("ERROR: array overflow! Element " + r + " not saved to array."); } - System.out.println("Element " + r + " successfully saved to array"); } public Resume get(String uuid) { - for (int i = 0; i < size; i++) { - if (storage[i].getUuid().equals(uuid)) { - return storage[i]; - } + int index = desiredIndex(uuid); + if (index >= 0) { + return storage[index]; } return null; } public void delete(String uuid) { - for (int i = 0; i < size; i++) { - if (storage[i].getUuid().equals(uuid)) { - storage[i] = storage[size - 1]; - size--; - System.out.println("\nElement " + uuid + " successfully deleted"); - return; - } + int index = desiredIndex(uuid); + if (index >= 0) { + storage[index] = storage[size - 1]; + size--; + System.out.println("\nElement " + uuid + " successfully deleted"); + return; } System.out.println("\nElement " + uuid + " not found"); } @@ -61,14 +61,19 @@ public void delete(String uuid) { * @return array, contains only Resumes in storage (without null) */ public Resume[] getAll() { - Resume[] resumes = new Resume[size]; - for (int i = 0; i < size; i++) { - resumes[i] = storage[i]; - } - return resumes; + return Arrays.copyOf(storage, size); } public int size() { return size; } + + public int desiredIndex(String uuid) { + for (int i = 0; i < size; i++) { + if (storage[i].getUuid().equals(uuid)) { + return i; + } + } + return -1; + } } diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java index 1f0aa359..37e5224c 100644 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -26,8 +26,9 @@ public static void main(String[] args) { System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); Resume r = new Resume(); - r.setUuid("uuid3"); + r.setUuid("uuid4"); ARRAY_STORAGE.update(r); + ARRAY_STORAGE.save(r); printAll(); ARRAY_STORAGE.delete(r1.getUuid()); From d416a6bc1bf20e25624252d2ff41aee3a368e11e Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 29 Nov 2024 12:00:44 +0500 Subject: [PATCH 07/39] HW2: finally --- .../urise/webapp/storage/ArrayStorage.java | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 77957095..a48512f2 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -8,19 +8,19 @@ * Array based storage for Resumes */ public class ArrayStorage { - private final Resume[] storage = new Resume[10000]; + private final Resume[] STORAGE_LIMIT = new Resume[10000]; private int size = 0; public void clear() { - Arrays.fill(storage, 0, size, null); + Arrays.fill(STORAGE_LIMIT, 0, size, null); size = 0; System.out.println("\nThe array was successfully cleared"); } public void update(Resume r) { - int index = desiredIndex(r.getUuid()); + int index = findIndex(r.getUuid()); if (index >= 0) { - storage[index].setUuid(r.getUuid()); + STORAGE_LIMIT[index].setUuid(r.getUuid()); System.out.println("\nElement " + r + " successfully update"); } else { System.out.println("\nERROR: Element " + r + " not found"); @@ -28,9 +28,9 @@ public void update(Resume r) { } public void save(Resume r) { - int index = desiredIndex(r.getUuid()); - if (index < 0 && size < storage.length) { - storage[size] = r; + int index = findIndex(r.getUuid()); + if (index < 0 && size < STORAGE_LIMIT.length) { + STORAGE_LIMIT[size] = r; size++; System.out.println("Element " + r + " successfully saved to array"); } else { @@ -39,17 +39,18 @@ public void save(Resume r) { } public Resume get(String uuid) { - int index = desiredIndex(uuid); + int index = findIndex(uuid); if (index >= 0) { - return storage[index]; + return STORAGE_LIMIT[index]; } + System.out.println("\nElement " + uuid + " not found"); return null; } public void delete(String uuid) { - int index = desiredIndex(uuid); + int index = findIndex(uuid); if (index >= 0) { - storage[index] = storage[size - 1]; + STORAGE_LIMIT[index] = STORAGE_LIMIT[size - 1]; size--; System.out.println("\nElement " + uuid + " successfully deleted"); return; @@ -61,19 +62,20 @@ public void delete(String uuid) { * @return array, contains only Resumes in storage (without null) */ public Resume[] getAll() { - return Arrays.copyOf(storage, size); + return Arrays.copyOf(STORAGE_LIMIT, size); } public int size() { return size; } - public int desiredIndex(String uuid) { + public int findIndex(String uuid) { for (int i = 0; i < size; i++) { - if (storage[i].getUuid().equals(uuid)) { + if (STORAGE_LIMIT[i].getUuid().equals(uuid)) { return i; } } return -1; } } + From b092f4a2168a5220ccfcca14ca6d8f909f7c0a8e Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 3 Dec 2024 13:48:40 +0500 Subject: [PATCH 08/39] HW2: finally with static class members --- .../urise/webapp/storage/ArrayStorage.java | 20 ++++++++++--------- .../webapp/storage/MainTestArrayStorage.java | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index a48512f2..fb1fcb2c 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -8,11 +8,13 @@ * Array based storage for Resumes */ public class ArrayStorage { - private final Resume[] STORAGE_LIMIT = new Resume[10000]; + private static final int STORAGE_LIMIT = 10000; + private final Resume[] storage = new Resume[STORAGE_LIMIT]; private int size = 0; + public void clear() { - Arrays.fill(STORAGE_LIMIT, 0, size, null); + Arrays.fill(storage, 0, size, null); size = 0; System.out.println("\nThe array was successfully cleared"); } @@ -20,7 +22,7 @@ public void clear() { public void update(Resume r) { int index = findIndex(r.getUuid()); if (index >= 0) { - STORAGE_LIMIT[index].setUuid(r.getUuid()); + storage[index].setUuid(r.getUuid()); System.out.println("\nElement " + r + " successfully update"); } else { System.out.println("\nERROR: Element " + r + " not found"); @@ -29,8 +31,8 @@ public void update(Resume r) { public void save(Resume r) { int index = findIndex(r.getUuid()); - if (index < 0 && size < STORAGE_LIMIT.length) { - STORAGE_LIMIT[size] = r; + if (index < 0 && size < storage.length) { + storage[size] = r; size++; System.out.println("Element " + r + " successfully saved to array"); } else { @@ -41,7 +43,7 @@ public void save(Resume r) { public Resume get(String uuid) { int index = findIndex(uuid); if (index >= 0) { - return STORAGE_LIMIT[index]; + return storage[index]; } System.out.println("\nElement " + uuid + " not found"); return null; @@ -50,7 +52,7 @@ public Resume get(String uuid) { public void delete(String uuid) { int index = findIndex(uuid); if (index >= 0) { - STORAGE_LIMIT[index] = STORAGE_LIMIT[size - 1]; + storage[index] = storage[size - 1]; size--; System.out.println("\nElement " + uuid + " successfully deleted"); return; @@ -62,7 +64,7 @@ public void delete(String uuid) { * @return array, contains only Resumes in storage (without null) */ public Resume[] getAll() { - return Arrays.copyOf(STORAGE_LIMIT, size); + return Arrays.copyOf(storage, size); } public int size() { @@ -71,7 +73,7 @@ public int size() { public int findIndex(String uuid) { for (int i = 0; i < size; i++) { - if (STORAGE_LIMIT[i].getUuid().equals(uuid)) { + if (storage[i].getUuid().equals(uuid)) { return i; } } diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java index 37e5224c..4cece713 100644 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -6,7 +6,7 @@ * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { - static final ArrayStorage ARRAY_STORAGE = new ArrayStorage(); + private static final ArrayStorage ARRAY_STORAGE = new ArrayStorage(); public static void main(String[] args) { Resume r1 = new Resume(); From e5c08b7f81dd12e017b5a7fcb54789cc23115c17 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 3 Dec 2024 14:20:13 +0500 Subject: [PATCH 09/39] HW3: introduce interface --- .../urise/webapp/storage/ArrayStorage.java | 4 +-- .../webapp/storage/MainTestArrayStorage.java | 2 +- src/com/urise/webapp/storage/Storage.java | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/com/urise/webapp/storage/Storage.java diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index fb1fcb2c..e03c1fa4 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -7,7 +7,7 @@ /** * Array based storage for Resumes */ -public class ArrayStorage { +public class ArrayStorage implements Storage{ private static final int STORAGE_LIMIT = 10000; private final Resume[] storage = new Resume[STORAGE_LIMIT]; private int size = 0; @@ -71,7 +71,7 @@ public int size() { return size; } - public int findIndex(String uuid) { + private int findIndex(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { return i; diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java index 4cece713..442c3f55 100644 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -6,7 +6,7 @@ * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { - private static final ArrayStorage ARRAY_STORAGE = new ArrayStorage(); + private static final Storage ARRAY_STORAGE = new ArrayStorage(); public static void main(String[] args) { Resume r1 = new Resume(); diff --git a/src/com/urise/webapp/storage/Storage.java b/src/com/urise/webapp/storage/Storage.java new file mode 100644 index 00000000..fef5ceed --- /dev/null +++ b/src/com/urise/webapp/storage/Storage.java @@ -0,0 +1,26 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + +/** + * Array based storage for Resumes + */ +public interface Storage { + void clear(); + + void update(Resume r); + + void save(Resume r); + + Resume get(String uuid); + + void delete(String uuid); + + /** + * @return array, contains only Resumes in storage (without null) + */ + Resume[] getAll(); + + int size(); +} + From ee84f2a5f6ab4290f3fbf6f2742f239f467e98b2 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 3 Dec 2024 16:38:33 +0500 Subject: [PATCH 10/39] HW3: abstract storage & sorted storage --- src/com/urise/webapp/model/Resume.java | 9 ++++- .../webapp/storage/AbstractArrayStorage.java | 28 +++++++++++++ .../urise/webapp/storage/ArrayStorage.java | 12 +----- .../webapp/storage/SortedArrayStorage.java | 40 +++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 src/com/urise/webapp/storage/AbstractArrayStorage.java create mode 100644 src/com/urise/webapp/storage/SortedArrayStorage.java diff --git a/src/com/urise/webapp/model/Resume.java b/src/com/urise/webapp/model/Resume.java index d6b45cb6..8727871b 100644 --- a/src/com/urise/webapp/model/Resume.java +++ b/src/com/urise/webapp/model/Resume.java @@ -5,7 +5,7 @@ /** * Initial resume class */ -public class Resume { +public class Resume implements Comparable { // Unique identifier private String uuid; @@ -35,4 +35,9 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(uuid); } -} + + @Override + public int compareTo(Resume o) { + return uuid.compareTo(o.uuid); + } +} \ No newline at end of file diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java new file mode 100644 index 00000000..bbedd00d --- /dev/null +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -0,0 +1,28 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + +/** + * Array based storage for Resumes + */ +public abstract class AbstractArrayStorage implements Storage { + protected static final int STORAGE_LIMIT = 10000; + protected final Resume[] storage = new Resume[STORAGE_LIMIT]; + protected int size = 0; + + public int size() { + return size; + } + + public Resume get(String uuid) { + int index = findIndex(uuid); + if (index >= 0) { + return storage[index]; + } + System.out.println("\nElement " + uuid + " not found"); + return null; + } + + protected abstract int findIndex(String uuid); +} + diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index e03c1fa4..3198d022 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -7,11 +7,7 @@ /** * Array based storage for Resumes */ -public class ArrayStorage implements Storage{ - private static final int STORAGE_LIMIT = 10000; - private final Resume[] storage = new Resume[STORAGE_LIMIT]; - private int size = 0; - +public class ArrayStorage extends AbstractArrayStorage { public void clear() { Arrays.fill(storage, 0, size, null); @@ -67,11 +63,7 @@ public Resume[] getAll() { return Arrays.copyOf(storage, size); } - public int size() { - return size; - } - - private int findIndex(String uuid) { + protected int findIndex(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { return i; diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java new file mode 100644 index 00000000..dc1b0316 --- /dev/null +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -0,0 +1,40 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + +import java.util.Arrays; + +public class SortedArrayStorage extends AbstractArrayStorage { + + @Override + public void clear() { + + } + + @Override + public void update(Resume r) { + + } + + @Override + public void save(Resume r) { + + } + + @Override + public void delete(String uuid) { + + } + + @Override + public Resume[] getAll() { + return new Resume[0]; + } + + @Override + protected int findIndex(String uuid) { + Resume searchKey = new Resume(); + searchKey.setUuid(uuid); + return Arrays.binarySearch(storage, 0, size, searchKey); + } +} From e06f72b65ae90195622645925533d00524af9742 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 5 Dec 2024 16:21:44 +0500 Subject: [PATCH 11/39] HW3: abstract storage & sorted storage & array storage --- .../webapp/storage/AbstractArrayStorage.java | 18 ++++----- .../urise/webapp/storage/ArrayStorage.java | 11 ++++-- .../webapp/storage/MainTestArrayStorage.java | 4 +- .../webapp/storage/SortedArrayStorage.java | 38 ++++++++++++++++--- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index bbedd00d..ddd12a2b 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -5,7 +5,7 @@ /** * Array based storage for Resumes */ -public abstract class AbstractArrayStorage implements Storage { +public abstract class AbstractArrayStorage implements Storage{ protected static final int STORAGE_LIMIT = 10000; protected final Resume[] storage = new Resume[STORAGE_LIMIT]; protected int size = 0; @@ -13,16 +13,12 @@ public abstract class AbstractArrayStorage implements Storage { public int size() { return size; } - - public Resume get(String uuid) { - int index = findIndex(uuid); - if (index >= 0) { - return storage[index]; - } - System.out.println("\nElement " + uuid + " not found"); - return null; - } - + public abstract void clear(); + public abstract void update(Resume r); + public abstract void save(Resume r); + public abstract Resume get(String uuid); + public abstract void delete(String uuid); + public abstract Resume[] getAll(); protected abstract int findIndex(String uuid); } diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 3198d022..9d960ebd 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -8,13 +8,14 @@ * Array based storage for Resumes */ public class ArrayStorage extends AbstractArrayStorage { - + @Override public void clear() { Arrays.fill(storage, 0, size, null); size = 0; System.out.println("\nThe array was successfully cleared"); } + @Override public void update(Resume r) { int index = findIndex(r.getUuid()); if (index >= 0) { @@ -24,7 +25,7 @@ public void update(Resume r) { System.out.println("\nERROR: Element " + r + " not found"); } } - + @Override public void save(Resume r) { int index = findIndex(r.getUuid()); if (index < 0 && size < storage.length) { @@ -35,7 +36,7 @@ public void save(Resume r) { System.out.println("ERROR: array overflow! Element " + r + " not saved to array."); } } - + @Override public Resume get(String uuid) { int index = findIndex(uuid); if (index >= 0) { @@ -44,7 +45,7 @@ public Resume get(String uuid) { System.out.println("\nElement " + uuid + " not found"); return null; } - + @Override public void delete(String uuid) { int index = findIndex(uuid); if (index >= 0) { @@ -59,10 +60,12 @@ public void delete(String uuid) { /** * @return array, contains only Resumes in storage (without null) */ + @Override public Resume[] getAll() { return Arrays.copyOf(storage, size); } + @Override protected int findIndex(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java index 442c3f55..93a6d166 100644 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -6,8 +6,8 @@ * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { - private static final Storage ARRAY_STORAGE = new ArrayStorage(); - +// private static final Storage ARRAY_STORAGE = new ArrayStorage(); + private static final Storage ARRAY_STORAGE = new SortedArrayStorage(); public static void main(String[] args) { Resume r1 = new Resume(); r1.setUuid("uuid1"); diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index dc1b0316..ab674b8f 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -8,27 +8,55 @@ public class SortedArrayStorage extends AbstractArrayStorage { @Override public void clear() { - + Arrays.fill(storage, 0, size, null); + size = 0; + System.out.println("\nThe sorted array was successfully cleared"); } @Override public void update(Resume r) { - + if (findIndex(r.getUuid()) >= 0) { + storage[findIndex(r.getUuid())].setUuid(r.getUuid()); + System.out.println("\nElement " + r + " successfully update"); + } else { + System.out.println("\nERROR: Element " + r + " not found"); + } } @Override public void save(Resume r) { - + if (findIndex(r.getUuid()) < 0 && size < storage.length) { + storage[size] = r; + size++; + System.out.println("Element " + r + " successfully saved to array"); + } else { + System.out.println("ERROR: array overflow! Element " + r + " not saved to array."); + } + } + @Override + public Resume get(String uuid) { + int index = findIndex(uuid); + if (index >= 0) { + return storage[index]; + } + System.out.println("\nElement " + uuid + " not found"); + return null; } @Override public void delete(String uuid) { - + if (findIndex(uuid) >= 0) { + storage[findIndex(uuid)] = storage[size - 1]; + size--; + System.out.println("\nElement " + uuid + " successfully deleted"); + return; + } + System.out.println("\nElement " + uuid + " not found"); } @Override public Resume[] getAll() { - return new Resume[0]; + return Arrays.copyOf(storage, size); } @Override From ed69d01db179570ac20f0dbbb7dd311e6546086a Mon Sep 17 00:00:00 2001 From: Admin Date: Sat, 7 Dec 2024 21:33:08 +0500 Subject: [PATCH 12/39] HW3: templateMethod & save --- .../webapp/storage/AbstractArrayStorage.java | 57 ++++++++++++++++--- .../urise/webapp/storage/ArrayStorage.java | 14 +---- .../webapp/storage/MainTestArrayStorage.java | 43 ++------------ .../webapp/storage/SortedArrayStorage.java | 20 +++---- 4 files changed, 64 insertions(+), 70 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index ddd12a2b..81d57043 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -5,20 +5,63 @@ /** * Array based storage for Resumes */ -public abstract class AbstractArrayStorage implements Storage{ +public abstract class AbstractArrayStorage implements Storage { protected static final int STORAGE_LIMIT = 10000; protected final Resume[] storage = new Resume[STORAGE_LIMIT]; protected int size = 0; + public final void templateMethod() { + Resume r1 = new Resume(); + r1.setUuid("uuid4"); + Resume r2 = new Resume(); + r2.setUuid("uuid2"); + Resume r3 = new Resume(); + r3.setUuid("uuid1"); + save(r1); + save(r2); + save(r3); + printAll(); + + System.out.println("Get r1: " + get(r1.getUuid())); + System.out.println("Size: " + size()); + + System.out.println("Get dummy: " + get("dummy")); + + Resume r = new Resume(); + r.setUuid("uuid2"); + update(r); + save(r); + + printAll(); + delete(r1.getUuid()); + printAll(); + clear(); + printAll(); + + System.out.println("Size: " + size()); + size(); + } + + protected void printAll() { + System.out.println("\nGet All"); + for (Resume r : getAll()) { + System.out.println(r); + } + } + + public Resume get(String uuid) { + int index = findIndex(uuid); + if (index >= 0) { + return storage[index]; + } + System.out.println("\nElement " + uuid + " not found"); + return null; + } + public int size() { return size; } - public abstract void clear(); - public abstract void update(Resume r); - public abstract void save(Resume r); - public abstract Resume get(String uuid); - public abstract void delete(String uuid); - public abstract Resume[] getAll(); + protected abstract int findIndex(String uuid); } diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 9d960ebd..0cd558e0 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -25,6 +25,7 @@ public void update(Resume r) { System.out.println("\nERROR: Element " + r + " not found"); } } + @Override public void save(Resume r) { int index = findIndex(r.getUuid()); @@ -36,15 +37,7 @@ public void save(Resume r) { System.out.println("ERROR: array overflow! Element " + r + " not saved to array."); } } - @Override - public Resume get(String uuid) { - int index = findIndex(uuid); - if (index >= 0) { - return storage[index]; - } - System.out.println("\nElement " + uuid + " not found"); - return null; - } + @Override public void delete(String uuid) { int index = findIndex(uuid); @@ -74,5 +67,4 @@ protected int findIndex(String uuid) { } return -1; } -} - +} \ No newline at end of file diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java index 93a6d166..6f5bce81 100644 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ b/src/com/urise/webapp/storage/MainTestArrayStorage.java @@ -1,48 +1,13 @@ package com.urise.webapp.storage; -import com.urise.webapp.model.Resume; - /** * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { -// private static final Storage ARRAY_STORAGE = new ArrayStorage(); - private static final Storage ARRAY_STORAGE = new SortedArrayStorage(); - public static void main(String[] args) { - Resume r1 = new Resume(); - r1.setUuid("uuid1"); - Resume r2 = new Resume(); - r2.setUuid("uuid2"); - Resume r3 = new Resume(); - r3.setUuid("uuid3"); - - ARRAY_STORAGE.save(r1); - ARRAY_STORAGE.save(r2); - ARRAY_STORAGE.save(r3); - - System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid())); - System.out.println("Size: " + ARRAY_STORAGE.size()); - - System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); + // private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); + private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); - Resume r = new Resume(); - r.setUuid("uuid4"); - ARRAY_STORAGE.update(r); - ARRAY_STORAGE.save(r); - - printAll(); - ARRAY_STORAGE.delete(r1.getUuid()); - printAll(); - ARRAY_STORAGE.clear(); - printAll(); - - System.out.println("Size: " + ARRAY_STORAGE.size()); - } - - static void printAll() { - System.out.println("\nGet All"); - for (Resume r : ARRAY_STORAGE.getAll()) { - System.out.println(r); - } + public static void main(String[] args) { + ARRAY_STORAGE.templateMethod(); } } diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index ab674b8f..b98ff9db 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -25,23 +25,17 @@ public void update(Resume r) { @Override public void save(Resume r) { - if (findIndex(r.getUuid()) < 0 && size < storage.length) { - storage[size] = r; + int index = findIndex(r.getUuid()); + if (index < 0 && size < storage.length) { + int insertIndex = -(Arrays.binarySearch(storage, 0, size, r) + 1); + System.arraycopy(storage, insertIndex, storage, insertIndex + 1, size - insertIndex); + storage[insertIndex] = r; size++; - System.out.println("Element " + r + " successfully saved to array"); + System.out.println("Element " + r + " successfully saved to sorted array"); } else { - System.out.println("ERROR: array overflow! Element " + r + " not saved to array."); + System.out.println("ERROR: array overflow! Element " + r + " not saved to sorted array."); } } - @Override - public Resume get(String uuid) { - int index = findIndex(uuid); - if (index >= 0) { - return storage[index]; - } - System.out.println("\nElement " + uuid + " not found"); - return null; - } @Override public void delete(String uuid) { From 7934f8d719dfdfd8bfe8b0d4d581a6c76ed8060e Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 12 Dec 2024 12:26:06 +0500 Subject: [PATCH 13/39] HW3: increaseSize() & protected void reductionSize() --- lesson/lesson1.md | 2 +- .../urise/webapp/MainTestArrayStorage.java | 53 ++++++++++++ .../webapp/storage/AbstractArrayStorage.java | 84 +++++++++++-------- .../urise/webapp/storage/ArrayStorage.java | 54 +++--------- .../webapp/storage/MainTestArrayStorage.java | 13 --- .../webapp/storage/SortedArrayStorage.java | 53 +++--------- 6 files changed, 130 insertions(+), 129 deletions(-) create mode 100644 src/com/urise/webapp/MainTestArrayStorage.java delete mode 100644 src/com/urise/webapp/storage/MainTestArrayStorage.java diff --git a/lesson/lesson1.md b/lesson/lesson1.md index 1ebfc3fb..67f78ff0 100644 --- a/lesson/lesson1.md +++ b/lesson/lesson1.md @@ -77,7 +77,7 @@ r1, r2, r3,..., rn, null, null,..., null <----- size -----> <------- storage.length (10000) -------> ``` -- Проверьте вашу реализацию с помощью классов `com.urise.webapp.MainArray.main()` и `com.urise.webapp.storage.MainTestArrayStorage.main()` +- Проверьте вашу реализацию с помощью классов `com.urise.webapp.MainArray.main()` и `com.urise.webapp.MainTestArrayStorage.main()` - Изучите дополнительные материалы по IntelliJ IDEA: - [Idea Wiki](https://github.com/JavaOPs/topjava/wiki/IDEA) - [Отладка Java кода в IDEA. Основные возможности отладчика](https://youtu.be/Z1BQsf0A4xY) diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java new file mode 100644 index 00000000..fbe35951 --- /dev/null +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -0,0 +1,53 @@ +package com.urise.webapp; + +import com.urise.webapp.model.Resume; +import com.urise.webapp.storage.AbstractArrayStorage; +import com.urise.webapp.storage.ArrayStorage; + +/** + * Test for your com.urise.webapp.storage.ArrayStorage implementation + */ +public class MainTestArrayStorage { + private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); +// private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); + + public static void main(String[] args) { + Resume r1 = new Resume(); + r1.setUuid("uuid4"); + Resume r2 = new Resume(); + r2.setUuid("uuid2"); + Resume r3 = new Resume(); + r3.setUuid("uuid1"); + ARRAY_STORAGE.save(r1); + ARRAY_STORAGE.save(r2); + ARRAY_STORAGE.save(r3); + printAll(); + + System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid())); + System.out.println("Size: " + ARRAY_STORAGE.size()); + + System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); + + Resume r = new Resume(); + r.setUuid("uuid2"); + ARRAY_STORAGE.update(r); + ARRAY_STORAGE.save(r); + + + printAll(); + ARRAY_STORAGE.delete(r1.getUuid()); + printAll(); + ARRAY_STORAGE.clear(); + printAll(); + + System.out.println("Size: " + ARRAY_STORAGE.size()); + ARRAY_STORAGE.size(); + } + + static void printAll() { + System.out.println("\nGet All"); + for (Resume r : ARRAY_STORAGE.getAll()) { + System.out.println(r); + } + } +} \ No newline at end of file diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index 81d57043..bc7fd955 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -2,6 +2,8 @@ import com.urise.webapp.model.Resume; +import java.util.Arrays; + /** * Array based storage for Resumes */ @@ -10,42 +12,32 @@ public abstract class AbstractArrayStorage implements Storage { protected final Resume[] storage = new Resume[STORAGE_LIMIT]; protected int size = 0; - public final void templateMethod() { - Resume r1 = new Resume(); - r1.setUuid("uuid4"); - Resume r2 = new Resume(); - r2.setUuid("uuid2"); - Resume r3 = new Resume(); - r3.setUuid("uuid1"); - save(r1); - save(r2); - save(r3); - printAll(); - - System.out.println("Get r1: " + get(r1.getUuid())); - System.out.println("Size: " + size()); - - System.out.println("Get dummy: " + get("dummy")); - - Resume r = new Resume(); - r.setUuid("uuid2"); - update(r); - save(r); - - printAll(); - delete(r1.getUuid()); - printAll(); - clear(); - printAll(); - - System.out.println("Size: " + size()); - size(); + public void clear() { + Arrays.fill(storage, 0, size, null); + size = 0; + System.out.println("\nThe " + storage.getClass().getSimpleName() + " was successfully cleared"); } - protected void printAll() { - System.out.println("\nGet All"); - for (Resume r : getAll()) { - System.out.println(r); + + public void update(Resume r) { + int index = findIndex(r.getUuid()); + if (index < 0) { + System.out.println("\nERROR: Element " + r + " not found"); + return; + } + storage[index] = r; + System.out.println("\nElement " + r + " successfully update"); + } + + + public final void save(Resume r) { + int index = findIndex(r.getUuid()); + if (index < 0 && size < storage.length) { + System.out.println("ERROR: array overflow! Element " + r + " not saved to storage."); + } else { + insertResume(r); + increaseSize(); + System.out.println("Element " + r + " successfully saved to storage."); } } @@ -58,10 +50,34 @@ public Resume get(String uuid) { return null; } + public final void delete(String uuid) { + int index = findIndex(uuid); + if (index < 0) { + System.out.println("\nElement " + uuid + " not found"); + } else { + indexDelete(index); + reductionSize(); + System.out.println("\nElement " + uuid + " successfully deleted from storage"); + } + } + public int size() { return size; } protected abstract int findIndex(String uuid); + + protected abstract void insertResume(Resume r); + + protected void increaseSize() { + size++; + } + + protected abstract void indexDelete(int index); + + protected void reductionSize() { + size--; + } + } diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 0cd558e0..f99430ec 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -8,47 +8,6 @@ * Array based storage for Resumes */ public class ArrayStorage extends AbstractArrayStorage { - @Override - public void clear() { - Arrays.fill(storage, 0, size, null); - size = 0; - System.out.println("\nThe array was successfully cleared"); - } - - @Override - public void update(Resume r) { - int index = findIndex(r.getUuid()); - if (index >= 0) { - storage[index].setUuid(r.getUuid()); - System.out.println("\nElement " + r + " successfully update"); - } else { - System.out.println("\nERROR: Element " + r + " not found"); - } - } - - @Override - public void save(Resume r) { - int index = findIndex(r.getUuid()); - if (index < 0 && size < storage.length) { - storage[size] = r; - size++; - System.out.println("Element " + r + " successfully saved to array"); - } else { - System.out.println("ERROR: array overflow! Element " + r + " not saved to array."); - } - } - - @Override - public void delete(String uuid) { - int index = findIndex(uuid); - if (index >= 0) { - storage[index] = storage[size - 1]; - size--; - System.out.println("\nElement " + uuid + " successfully deleted"); - return; - } - System.out.println("\nElement " + uuid + " not found"); - } /** * @return array, contains only Resumes in storage (without null) @@ -58,6 +17,7 @@ public Resume[] getAll() { return Arrays.copyOf(storage, size); } + @Override protected int findIndex(String uuid) { for (int i = 0; i < size; i++) { @@ -67,4 +27,16 @@ protected int findIndex(String uuid) { } return -1; } + + @Override + protected void insertResume(Resume r) { + storage[size] = r; + } + + + @Override + protected void indexDelete(int index) { + storage[index] = storage[size - 1]; + } + } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/MainTestArrayStorage.java b/src/com/urise/webapp/storage/MainTestArrayStorage.java deleted file mode 100644 index 6f5bce81..00000000 --- a/src/com/urise/webapp/storage/MainTestArrayStorage.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.urise.webapp.storage; - -/** - * Test for your com.urise.webapp.storage.ArrayStorage implementation - */ -public class MainTestArrayStorage { - // private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); - private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); - - public static void main(String[] args) { - ARRAY_STORAGE.templateMethod(); - } -} diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index b98ff9db..eb806c12 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -7,56 +7,29 @@ public class SortedArrayStorage extends AbstractArrayStorage { @Override - public void clear() { - Arrays.fill(storage, 0, size, null); - size = 0; - System.out.println("\nThe sorted array was successfully cleared"); + public Resume[] getAll() { + return Arrays.copyOf(storage, size); } @Override - public void update(Resume r) { - if (findIndex(r.getUuid()) >= 0) { - storage[findIndex(r.getUuid())].setUuid(r.getUuid()); - System.out.println("\nElement " + r + " successfully update"); - } else { - System.out.println("\nERROR: Element " + r + " not found"); - } + protected int findIndex(String uuid) { + Resume searchKey = new Resume(); + searchKey.setUuid(uuid); + return Arrays.binarySearch(storage, 0, size, searchKey); } @Override - public void save(Resume r) { - int index = findIndex(r.getUuid()); - if (index < 0 && size < storage.length) { - int insertIndex = -(Arrays.binarySearch(storage, 0, size, r) + 1); - System.arraycopy(storage, insertIndex, storage, insertIndex + 1, size - insertIndex); - storage[insertIndex] = r; - size++; - System.out.println("Element " + r + " successfully saved to sorted array"); - } else { - System.out.println("ERROR: array overflow! Element " + r + " not saved to sorted array."); - } + protected void insertResume(Resume r) { + int insertIndex = -(Arrays.binarySearch(storage, 0, size, r) + 1); + System.arraycopy(storage, insertIndex, storage, insertIndex + 1, size - insertIndex); + storage[insertIndex] = r; } - @Override - public void delete(String uuid) { - if (findIndex(uuid) >= 0) { - storage[findIndex(uuid)] = storage[size - 1]; - size--; - System.out.println("\nElement " + uuid + " successfully deleted"); - return; - } - System.out.println("\nElement " + uuid + " not found"); - } @Override - public Resume[] getAll() { - return Arrays.copyOf(storage, size); + protected void indexDelete(int index) { + System.arraycopy(storage, index + 1, storage, index, size - index - 1); + storage[size - 1] = null; } - @Override - protected int findIndex(String uuid) { - Resume searchKey = new Resume(); - searchKey.setUuid(uuid); - return Arrays.binarySearch(storage, 0, size, searchKey); - } } From 83fedb6ddd75937fbae2f4495845e9f51a26c4eb Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 12 Dec 2024 13:10:02 +0500 Subject: [PATCH 14/39] HW3: template method --- .../urise/webapp/MainTestArrayStorage.java | 4 +-- .../webapp/storage/AbstractArrayStorage.java | 35 ++++++++----------- .../urise/webapp/storage/ArrayStorage.java | 13 ------- .../webapp/storage/SortedArrayStorage.java | 7 ---- 4 files changed, 17 insertions(+), 42 deletions(-) diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index fbe35951..a4fb5817 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -2,14 +2,14 @@ import com.urise.webapp.model.Resume; import com.urise.webapp.storage.AbstractArrayStorage; -import com.urise.webapp.storage.ArrayStorage; +import com.urise.webapp.storage.SortedArrayStorage; /** * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { - private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); // private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); + private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); public static void main(String[] args) { Resume r1 = new Resume(); diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index bc7fd955..d173c5f6 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -32,13 +32,14 @@ public void update(Resume r) { public final void save(Resume r) { int index = findIndex(r.getUuid()); - if (index < 0 && size < storage.length) { + if (index >= 0 && size < storage.length) { System.out.println("ERROR: array overflow! Element " + r + " not saved to storage."); - } else { - insertResume(r); - increaseSize(); - System.out.println("Element " + r + " successfully saved to storage."); + return; } + insertResume(r); + size++; + System.out.println("Element " + r + " successfully saved to storage."); + } public Resume get(String uuid) { @@ -54,11 +55,15 @@ public final void delete(String uuid) { int index = findIndex(uuid); if (index < 0) { System.out.println("\nElement " + uuid + " not found"); - } else { - indexDelete(index); - reductionSize(); - System.out.println("\nElement " + uuid + " successfully deleted from storage"); + return; } + indexDelete(index); + size--; + System.out.println("\nElement " + uuid + " successfully deleted from storage"); + } + + public Resume[] getAll() { + return Arrays.copyOf(storage, size); } public int size() { @@ -69,15 +74,5 @@ public int size() { protected abstract void insertResume(Resume r); - protected void increaseSize() { - size++; - } - protected abstract void indexDelete(int index); - - protected void reductionSize() { - size--; - } - -} - +} \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index f99430ec..6d896d91 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -2,22 +2,11 @@ import com.urise.webapp.model.Resume; -import java.util.Arrays; - /** * Array based storage for Resumes */ public class ArrayStorage extends AbstractArrayStorage { - /** - * @return array, contains only Resumes in storage (without null) - */ - @Override - public Resume[] getAll() { - return Arrays.copyOf(storage, size); - } - - @Override protected int findIndex(String uuid) { for (int i = 0; i < size; i++) { @@ -33,10 +22,8 @@ protected void insertResume(Resume r) { storage[size] = r; } - @Override protected void indexDelete(int index) { storage[index] = storage[size - 1]; } - } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index eb806c12..30e30964 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -6,11 +6,6 @@ public class SortedArrayStorage extends AbstractArrayStorage { - @Override - public Resume[] getAll() { - return Arrays.copyOf(storage, size); - } - @Override protected int findIndex(String uuid) { Resume searchKey = new Resume(); @@ -25,11 +20,9 @@ protected void insertResume(Resume r) { storage[insertIndex] = r; } - @Override protected void indexDelete(int index) { System.arraycopy(storage, index + 1, storage, index, size - index - 1); storage[size - 1] = null; } - } From 00efee79071de4dce81db2dc014b5edc1d5adf4c Mon Sep 17 00:00:00 2001 From: Admin Date: Mon, 16 Dec 2024 23:06:35 +0500 Subject: [PATCH 15/39] HW3: finally with removeLastElement() --- src/com/urise/webapp/MainTestArrayStorage.java | 3 +-- .../urise/webapp/storage/AbstractArrayStorage.java | 13 +++++++++---- src/com/urise/webapp/storage/ArrayStorage.java | 4 ++-- .../urise/webapp/storage/SortedArrayStorage.java | 3 +-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index a4fb5817..271e79e4 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -33,9 +33,8 @@ public static void main(String[] args) { ARRAY_STORAGE.update(r); ARRAY_STORAGE.save(r); - printAll(); - ARRAY_STORAGE.delete(r1.getUuid()); + ARRAY_STORAGE.delete(r2.getUuid()); printAll(); ARRAY_STORAGE.clear(); printAll(); diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index d173c5f6..92c475d1 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -19,7 +19,7 @@ public void clear() { } - public void update(Resume r) { + public final void update(Resume r) { int index = findIndex(r.getUuid()); if (index < 0) { System.out.println("\nERROR: Element " + r + " not found"); @@ -42,7 +42,7 @@ public final void save(Resume r) { } - public Resume get(String uuid) { + public final Resume get(String uuid) { int index = findIndex(uuid); if (index >= 0) { return storage[index]; @@ -57,7 +57,8 @@ public final void delete(String uuid) { System.out.println("\nElement " + uuid + " not found"); return; } - indexDelete(index); + fillDeletedElement(index); + removeLastElement(); size--; System.out.println("\nElement " + uuid + " successfully deleted from storage"); } @@ -74,5 +75,9 @@ public int size() { protected abstract void insertResume(Resume r); - protected abstract void indexDelete(int index); + protected abstract void fillDeletedElement(int index); + + protected void removeLastElement() { + storage[size - 1] = null; + } } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 6d896d91..20544991 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -19,11 +19,11 @@ protected int findIndex(String uuid) { @Override protected void insertResume(Resume r) { - storage[size] = r; + storage[size] = r; } @Override - protected void indexDelete(int index) { + protected void fillDeletedElement(int index) { storage[index] = storage[size - 1]; } } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index 30e30964..a6b50e98 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -21,8 +21,7 @@ protected void insertResume(Resume r) { } @Override - protected void indexDelete(int index) { + protected void fillDeletedElement(int index) { System.arraycopy(storage, index + 1, storage, index, size - index - 1); - storage[size - 1] = null; } } From e8aa68f8ceb71150197b84ae40fba4ad9e2e60da Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 21 Jan 2025 15:15:53 +0500 Subject: [PATCH 16/39] HW4: exception --- src/com/urise/webapp/MainArray.java | 6 ++--- .../urise/webapp/MainTestArrayStorage.java | 12 ++++------ .../exception/ExistStorageException.java | 7 ++++++ .../exception/NotExistStorageException.java | 7 ++++++ .../webapp/exception/StorageException.java | 14 +++++++++++ src/com/urise/webapp/model/Resume.java | 17 ++++++++------ .../webapp/storage/AbstractArrayStorage.java | 23 ++++++++----------- .../webapp/storage/SortedArrayStorage.java | 3 +-- 8 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 src/com/urise/webapp/exception/ExistStorageException.java create mode 100644 src/com/urise/webapp/exception/NotExistStorageException.java create mode 100644 src/com/urise/webapp/exception/StorageException.java diff --git a/src/com/urise/webapp/MainArray.java b/src/com/urise/webapp/MainArray.java index 59f08bd3..e4e2d6c6 100644 --- a/src/com/urise/webapp/MainArray.java +++ b/src/com/urise/webapp/MainArray.java @@ -36,8 +36,7 @@ public static void main(String[] args) throws IOException { System.out.println(ARRAY_STORAGE.size()); break; case "save": - r = new Resume(); - r.setUuid(uuid); + r = new Resume(uuid); ARRAY_STORAGE.save(r); printAll(); break; @@ -53,8 +52,7 @@ public static void main(String[] args) throws IOException { printAll(); break; case "update": - r = new Resume(); - r.setUuid(uuid); + r = new Resume(uuid); ARRAY_STORAGE.update(r); break; case "exit": diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index 271e79e4..24198508 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -12,12 +12,9 @@ public class MainTestArrayStorage { private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); public static void main(String[] args) { - Resume r1 = new Resume(); - r1.setUuid("uuid4"); - Resume r2 = new Resume(); - r2.setUuid("uuid2"); - Resume r3 = new Resume(); - r3.setUuid("uuid1"); + Resume r1 = new Resume("uuid4"); + Resume r2 = new Resume("uuid2"); + Resume r3 = new Resume("uuid1"); ARRAY_STORAGE.save(r1); ARRAY_STORAGE.save(r2); ARRAY_STORAGE.save(r3); @@ -28,8 +25,7 @@ public static void main(String[] args) { System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); - Resume r = new Resume(); - r.setUuid("uuid2"); + Resume r = new Resume("uuid2"); ARRAY_STORAGE.update(r); ARRAY_STORAGE.save(r); diff --git a/src/com/urise/webapp/exception/ExistStorageException.java b/src/com/urise/webapp/exception/ExistStorageException.java new file mode 100644 index 00000000..c494629a --- /dev/null +++ b/src/com/urise/webapp/exception/ExistStorageException.java @@ -0,0 +1,7 @@ +package com.urise.webapp.exception; + +public class ExistStorageException extends StorageException{ + public ExistStorageException(String uuid) { + super("Resume" + uuid + "already exist",uuid); + } +} diff --git a/src/com/urise/webapp/exception/NotExistStorageException.java b/src/com/urise/webapp/exception/NotExistStorageException.java new file mode 100644 index 00000000..0c25de7b --- /dev/null +++ b/src/com/urise/webapp/exception/NotExistStorageException.java @@ -0,0 +1,7 @@ +package com.urise.webapp.exception; + +public class NotExistStorageException extends StorageException{ + public NotExistStorageException(String uuid) { + super("Resume" + uuid + "not exist",uuid); + } +} diff --git a/src/com/urise/webapp/exception/StorageException.java b/src/com/urise/webapp/exception/StorageException.java new file mode 100644 index 00000000..852b0ea2 --- /dev/null +++ b/src/com/urise/webapp/exception/StorageException.java @@ -0,0 +1,14 @@ +package com.urise.webapp.exception; + +public class StorageException extends RuntimeException{ + private final String uuid; + + public StorageException(String message, String uuid) { + super(message); + this.uuid = uuid; + } + + public String getUuid() { + return uuid; + } +} diff --git a/src/com/urise/webapp/model/Resume.java b/src/com/urise/webapp/model/Resume.java index 8727871b..1a2176d2 100644 --- a/src/com/urise/webapp/model/Resume.java +++ b/src/com/urise/webapp/model/Resume.java @@ -1,24 +1,27 @@ package com.urise.webapp.model; import java.util.Objects; +import java.util.UUID; /** * Initial resume class */ public class Resume implements Comparable { + private final String uuid; - // Unique identifier - private String uuid; - - public String getUuid() { - return uuid; + public Resume() { + this(UUID.randomUUID().toString()); } - public void setUuid(String uuid) { + public Resume(String uuid) { this.uuid = uuid; } - @Override + public String getUuid() { + return uuid; + } + + @Override public String toString() { return uuid; } diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index 92c475d1..d2c3dd3f 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -1,5 +1,8 @@ package com.urise.webapp.storage; +import com.urise.webapp.exception.ExistStorageException; +import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.exception.StorageException; import com.urise.webapp.model.Resume; import java.util.Arrays; @@ -22,8 +25,7 @@ public void clear() { public final void update(Resume r) { int index = findIndex(r.getUuid()); if (index < 0) { - System.out.println("\nERROR: Element " + r + " not found"); - return; + throw new NotExistStorageException(r.getUuid()); } storage[index] = r; System.out.println("\nElement " + r + " successfully update"); @@ -32,14 +34,14 @@ public final void update(Resume r) { public final void save(Resume r) { int index = findIndex(r.getUuid()); - if (index >= 0 && size < storage.length) { - System.out.println("ERROR: array overflow! Element " + r + " not saved to storage."); - return; + if (index >= 0) { + throw new ExistStorageException(r.getUuid()); + } else if (size == STORAGE_LIMIT) { + throw new StorageException("Storage overflow", r.getUuid()); } insertResume(r); size++; System.out.println("Element " + r + " successfully saved to storage."); - } public final Resume get(String uuid) { @@ -54,11 +56,10 @@ public final Resume get(String uuid) { public final void delete(String uuid) { int index = findIndex(uuid); if (index < 0) { - System.out.println("\nElement " + uuid + " not found"); - return; + throw new NotExistStorageException(uuid); } fillDeletedElement(index); - removeLastElement(); + storage[size - 1] = null; size--; System.out.println("\nElement " + uuid + " successfully deleted from storage"); } @@ -76,8 +77,4 @@ public int size() { protected abstract void insertResume(Resume r); protected abstract void fillDeletedElement(int index); - - protected void removeLastElement() { - storage[size - 1] = null; - } } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index a6b50e98..b3ca91e4 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -8,8 +8,7 @@ public class SortedArrayStorage extends AbstractArrayStorage { @Override protected int findIndex(String uuid) { - Resume searchKey = new Resume(); - searchKey.setUuid(uuid); + Resume searchKey = new Resume(uuid); return Arrays.binarySearch(storage, 0, size, searchKey); } From 4db0eba63f00f8817b4fc5e4e9b821ad0e1a22fd Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 21 Jan 2025 16:56:26 +0500 Subject: [PATCH 17/39] HW4: begin test --- .../exception/ExistStorageException.java | 4 +-- .../exception/NotExistStorageException.java | 4 +-- .../webapp/exception/StorageException.java | 2 +- .../webapp/storage/AbstractArrayStorage.java | 7 ++-- .../urise/webapp/storage/MainReflection.java | 18 ++++++++++ .../storage/AbstractArrayStorageTest.java | 36 +++++++++++++++++++ 6 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 src/com/urise/webapp/storage/MainReflection.java create mode 100644 test/com/urise/webapp/storage/AbstractArrayStorageTest.java diff --git a/src/com/urise/webapp/exception/ExistStorageException.java b/src/com/urise/webapp/exception/ExistStorageException.java index c494629a..73fbbf8f 100644 --- a/src/com/urise/webapp/exception/ExistStorageException.java +++ b/src/com/urise/webapp/exception/ExistStorageException.java @@ -1,7 +1,7 @@ package com.urise.webapp.exception; -public class ExistStorageException extends StorageException{ +public class ExistStorageException extends StorageException { public ExistStorageException(String uuid) { - super("Resume" + uuid + "already exist",uuid); + super("Resume " + uuid + " already exist", uuid); } } diff --git a/src/com/urise/webapp/exception/NotExistStorageException.java b/src/com/urise/webapp/exception/NotExistStorageException.java index 0c25de7b..68164920 100644 --- a/src/com/urise/webapp/exception/NotExistStorageException.java +++ b/src/com/urise/webapp/exception/NotExistStorageException.java @@ -1,7 +1,7 @@ package com.urise.webapp.exception; -public class NotExistStorageException extends StorageException{ +public class NotExistStorageException extends StorageException { public NotExistStorageException(String uuid) { - super("Resume" + uuid + "not exist",uuid); + super("Resume " + uuid + " not exist", uuid); } } diff --git a/src/com/urise/webapp/exception/StorageException.java b/src/com/urise/webapp/exception/StorageException.java index 852b0ea2..d6bfa9ed 100644 --- a/src/com/urise/webapp/exception/StorageException.java +++ b/src/com/urise/webapp/exception/StorageException.java @@ -1,6 +1,6 @@ package com.urise.webapp.exception; -public class StorageException extends RuntimeException{ +public class StorageException extends RuntimeException { private final String uuid; public StorageException(String message, String uuid) { diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index d2c3dd3f..2729b4b4 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -39,9 +39,10 @@ public final void save(Resume r) { } else if (size == STORAGE_LIMIT) { throw new StorageException("Storage overflow", r.getUuid()); } - insertResume(r); - size++; - System.out.println("Element " + r + " successfully saved to storage."); + insertResume(r); + size++; + System.out.println("Element " + r + " successfully saved to storage."); + } public final Resume get(String uuid) { diff --git a/src/com/urise/webapp/storage/MainReflection.java b/src/com/urise/webapp/storage/MainReflection.java new file mode 100644 index 00000000..508b458a --- /dev/null +++ b/src/com/urise/webapp/storage/MainReflection.java @@ -0,0 +1,18 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + +import java.lang.reflect.Field; + +public class MainReflection { + public static void main(String[] args) throws IllegalAccessException { + Resume r = new Resume(); + Field field = r.getClass().getDeclaredFields()[0]; + field.setAccessible(true); + System.out.println(field.getName()); + System.out.println(field.get(r)); + field.set(r, "new_uuid"); + //invoke r.toString via reflection + System.out.println(r); + } +} diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java new file mode 100644 index 00000000..f56a122a --- /dev/null +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -0,0 +1,36 @@ +package com.urise.webapp.storage; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class AbstractArrayStorageTest { + + @Test + public void clear() { + } + + @Test + public void update() { + } + + @Test + public void save() { + } + + @Test + public void get() { + } + + @Test + public void delete() { + } + + @Test + public void getAll() { + } + + @Test + public void size() { + } +} \ No newline at end of file From e90cd9dc9167ef53167f868cab57eaf51068602e Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 21 Jan 2025 17:25:20 +0500 Subject: [PATCH 18/39] HW4: method get() --- .../webapp/storage/AbstractArrayStorage.java | 14 +++++------ .../storage/AbstractArrayStorageTest.java | 24 +++++++++++++++++-- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index 2729b4b4..ff020e90 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -39,19 +39,17 @@ public final void save(Resume r) { } else if (size == STORAGE_LIMIT) { throw new StorageException("Storage overflow", r.getUuid()); } - insertResume(r); - size++; - System.out.println("Element " + r + " successfully saved to storage."); - + insertResume(r); + size++; + System.out.println("Element " + r + " successfully saved to storage."); } public final Resume get(String uuid) { int index = findIndex(uuid); - if (index >= 0) { - return storage[index]; + if (index < 0) { + throw new NotExistStorageException(uuid); } - System.out.println("\nElement " + uuid + " not found"); - return null; + return storage[index]; } public final void delete(String uuid) { diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index f56a122a..e79d00b1 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -1,10 +1,24 @@ package com.urise.webapp.storage; +import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.model.Resume; +import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - public class AbstractArrayStorageTest { + private Storage storage = new ArrayStorage(); + private static final String UUID_1 = "uuid1"; + private static final String UUID_2 = "uuid2"; + private static final String UUID_3 = "uuid3"; + + @Before + public void setUp() throws Exception { + storage.clear(); + storage.save(new Resume(UUID_1)); + storage.save(new Resume(UUID_2)); + storage.save(new Resume(UUID_3)); + } @Test public void clear() { @@ -22,6 +36,11 @@ public void save() { public void get() { } + @Test (expected = NotExistStorageException.class) + public void getNotExist() throws Exception { + storage.get("dummy"); + } + @Test public void delete() { } @@ -32,5 +51,6 @@ public void getAll() { @Test public void size() { + Assert.assertEquals(3, storage.size()); } } \ No newline at end of file From 8e7013ec087a699a9045538de182a64dd84d63fa Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 23 Jan 2025 13:53:15 +0500 Subject: [PATCH 19/39] HW4: method toString through reflection --- src/com/urise/webapp/storage/MainReflection.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/com/urise/webapp/storage/MainReflection.java b/src/com/urise/webapp/storage/MainReflection.java index 508b458a..6c15934b 100644 --- a/src/com/urise/webapp/storage/MainReflection.java +++ b/src/com/urise/webapp/storage/MainReflection.java @@ -3,16 +3,22 @@ import com.urise.webapp.model.Resume; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; public class MainReflection { - public static void main(String[] args) throws IllegalAccessException { + public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Resume r = new Resume(); Field field = r.getClass().getDeclaredFields()[0]; field.setAccessible(true); System.out.println(field.getName()); System.out.println(field.get(r)); field.set(r, "new_uuid"); - //invoke r.toString via reflection System.out.println(r); + Method method = r.getClass().getMethod("toString"); + method.invoke(r); + System.out.println(method.getClass()); + System.out.println(method.getName()); + System.out.println(method); } } From 800f91c3c81189edac31bdc8832d4c6bc8c15300 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 23 Jan 2025 16:35:05 +0500 Subject: [PATCH 20/39] HW4: tests --- .../storage/AbstractArrayStorageTest.java | 62 ++++++++++++++++++- .../webapp/storage/ArrayStorageTest.java | 9 +++ .../storage/SortedArrayStorageTest.java | 7 +++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 test/com/urise/webapp/storage/ArrayStorageTest.java create mode 100644 test/com/urise/webapp/storage/SortedArrayStorageTest.java diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index e79d00b1..15535942 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -1,17 +1,25 @@ package com.urise.webapp.storage; +import com.urise.webapp.exception.ExistStorageException; import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.exception.StorageException; import com.urise.webapp.model.Resume; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -public class AbstractArrayStorageTest { - private Storage storage = new ArrayStorage(); +import java.util.Arrays; + +public abstract class AbstractArrayStorageTest { + private Storage storage; private static final String UUID_1 = "uuid1"; private static final String UUID_2 = "uuid2"; private static final String UUID_3 = "uuid3"; + public AbstractArrayStorageTest(Storage storage) { + this.storage = storage; + } + @Before public void setUp() throws Exception { storage.clear(); @@ -22,35 +30,83 @@ public void setUp() throws Exception { @Test public void clear() { + storage.clear(); + Assert.assertEquals(0, storage.size()); } @Test public void update() { + Resume r = new Resume("uuid2"); + storage.update(r); + Assert.assertEquals(r, storage.get("uuid2")); + } + + @Test(expected = NotExistStorageException.class) + public void updateNotExist() { + storage.update(new Resume("dummy")); } @Test public void save() { + Resume r = new Resume("uuid4"); + storage.save(r); + Assert.assertEquals(r, storage.get("uuid4")); + Assert.assertEquals(4, storage.size()); + } + + @Test(expected = ExistStorageException.class) + public void saveExist() { + storage.save(new Resume(UUID_1)); } @Test public void get() { + Resume r = new Resume("uuid1"); + Assert.assertEquals(storage.get("uuid1"), r); } - @Test (expected = NotExistStorageException.class) + @Test(expected = NotExistStorageException.class) public void getNotExist() throws Exception { storage.get("dummy"); } @Test public void delete() { + Resume r = new Resume("uuid2"); + storage.delete(r.getUuid()); + Assert.assertEquals(2, storage.size()); + Assert.assertThrows(NotExistStorageException.class, () -> storage.get("uuid2")); + } + + @Test(expected = NotExistStorageException.class) + public void deleteNotExist() { + storage.delete("dummy"); } @Test public void getAll() { + Resume[] r = storage.getAll(); + Assert.assertEquals(3, r.length); + Assert.assertTrue(Arrays.asList(r).contains(storage.get("uuid1"))); + Assert.assertTrue(Arrays.asList(r).contains(storage.get("uuid2"))); + Assert.assertTrue(Arrays.asList(r).contains(storage.get("uuid3"))); } @Test public void size() { Assert.assertEquals(3, storage.size()); } + @Test + public void testStorageOverflow() { + try { + Assert.assertEquals(3, storage.size()); + } catch (StorageException e) { + Assert.fail("Переполнение произошло раньше времени: " + e.getMessage()); + } + try { + storage.save(new Resume("uuid4")); + } catch (StorageException e) { + Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище"); + } + } } \ No newline at end of file diff --git a/test/com/urise/webapp/storage/ArrayStorageTest.java b/test/com/urise/webapp/storage/ArrayStorageTest.java new file mode 100644 index 00000000..7bf87268 --- /dev/null +++ b/test/com/urise/webapp/storage/ArrayStorageTest.java @@ -0,0 +1,9 @@ +package com.urise.webapp.storage; + +import static org.junit.Assert.*; + +public class ArrayStorageTest extends AbstractArrayStorageTest { + public ArrayStorageTest() { + super(new ArrayStorage()); + } +} \ No newline at end of file diff --git a/test/com/urise/webapp/storage/SortedArrayStorageTest.java b/test/com/urise/webapp/storage/SortedArrayStorageTest.java new file mode 100644 index 00000000..8bdb1190 --- /dev/null +++ b/test/com/urise/webapp/storage/SortedArrayStorageTest.java @@ -0,0 +1,7 @@ +package com.urise.webapp.storage; + +public class SortedArrayStorageTest extends AbstractArrayStorageTest { + public SortedArrayStorageTest() { + super(new SortedArrayStorage()); + } +} \ No newline at end of file From b70137157749c812f1cc2e83227046a630cb3cec Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 28 Jan 2025 17:11:00 +0500 Subject: [PATCH 21/39] HW4: correction of methods --- .../storage/AbstractArrayStorageTest.java | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 15535942..75fc5adf 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -8,13 +8,14 @@ import org.junit.Before; import org.junit.Test; -import java.util.Arrays; - public abstract class AbstractArrayStorageTest { - private Storage storage; + private static final String UUID_1 = "uuid1"; private static final String UUID_2 = "uuid2"; private static final String UUID_3 = "uuid3"; + private final Storage storage; + private final int size = 3; + private final Resume SAVE_RESUME = new Resume("uuid4"); public AbstractArrayStorageTest(Storage storage) { this.storage = storage; @@ -31,14 +32,15 @@ public void setUp() throws Exception { @Test public void clear() { storage.clear(); - Assert.assertEquals(0, storage.size()); + assertSize(0); + Assert.assertArrayEquals(new Resume[0], storage.getAll()); } @Test public void update() { - Resume r = new Resume("uuid2"); + Resume r = new Resume(UUID_2); storage.update(r); - Assert.assertEquals(r, storage.get("uuid2")); + Assert.assertSame(r, storage.get(UUID_2)); } @Test(expected = NotExistStorageException.class) @@ -48,10 +50,9 @@ public void updateNotExist() { @Test public void save() { - Resume r = new Resume("uuid4"); - storage.save(r); - Assert.assertEquals(r, storage.get("uuid4")); - Assert.assertEquals(4, storage.size()); + storage.save(SAVE_RESUME); + assertGet(SAVE_RESUME); + assertSize(size+1); } @Test(expected = ExistStorageException.class) @@ -61,8 +62,12 @@ public void saveExist() { @Test public void get() { - Resume r = new Resume("uuid1"); - Assert.assertEquals(storage.get("uuid1"), r); + Resume resume = new Resume("uuid1"); + assertGet(resume); + } + + public void assertGet(Resume resume) { + Assert.assertEquals(resume, storage.get(resume.getUuid())); } @Test(expected = NotExistStorageException.class) @@ -72,10 +77,10 @@ public void getNotExist() throws Exception { @Test public void delete() { - Resume r = new Resume("uuid2"); - storage.delete(r.getUuid()); - Assert.assertEquals(2, storage.size()); - Assert.assertThrows(NotExistStorageException.class, () -> storage.get("uuid2")); + Resume resume = new Resume("uuid2"); + storage.delete(resume.getUuid()); + assertSize(size-1); + Assert.assertThrows(NotExistStorageException.class, () -> storage.get(resume.getUuid())); } @Test(expected = NotExistStorageException.class) @@ -85,17 +90,24 @@ public void deleteNotExist() { @Test public void getAll() { - Resume[] r = storage.getAll(); - Assert.assertEquals(3, r.length); - Assert.assertTrue(Arrays.asList(r).contains(storage.get("uuid1"))); - Assert.assertTrue(Arrays.asList(r).contains(storage.get("uuid2"))); - Assert.assertTrue(Arrays.asList(r).contains(storage.get("uuid3"))); + Resume[] actual = storage.getAll(); + Assert.assertEquals(size, actual.length); + assertGet(new Resume(UUID_1)); + assertGet(new Resume(UUID_2)); + assertGet(new Resume(UUID_3)); + Resume[] expected = new Resume[] {new Resume(UUID_1), new Resume(UUID_2), new Resume(UUID_3)}; + Assert.assertArrayEquals(expected, actual); } @Test public void size() { - Assert.assertEquals(3, storage.size()); + assertSize(size); } + + public void assertSize(int expectedSize) { + Assert.assertEquals(expectedSize, storage.size()); + } + @Test public void testStorageOverflow() { try { From ed0b2cbe75d2cff04548284f3978e1e636773321 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 30 Jan 2025 15:58:53 +0500 Subject: [PATCH 22/39] HW4: error testStorageOverflow --- .../storage/AbstractArrayStorageTest.java | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 75fc5adf..2cd88370 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -8,6 +8,8 @@ import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertThrows; + public abstract class AbstractArrayStorageTest { private static final String UUID_1 = "uuid1"; @@ -15,14 +17,14 @@ public abstract class AbstractArrayStorageTest { private static final String UUID_3 = "uuid3"; private final Storage storage; private final int size = 3; - private final Resume SAVE_RESUME = new Resume("uuid4"); + private final Resume SAVE_RESUME = new Resume("uuid4"); public AbstractArrayStorageTest(Storage storage) { this.storage = storage; } @Before - public void setUp() throws Exception { + public void setUp() { storage.clear(); storage.save(new Resume(UUID_1)); storage.save(new Resume(UUID_2)); @@ -44,7 +46,7 @@ public void update() { } @Test(expected = NotExistStorageException.class) - public void updateNotExist() { + public void updateNotExist() throws Exception { storage.update(new Resume("dummy")); } @@ -52,19 +54,19 @@ public void updateNotExist() { public void save() { storage.save(SAVE_RESUME); assertGet(SAVE_RESUME); - assertSize(size+1); + assertSize(size + 1); } - @Test(expected = ExistStorageException.class) - public void saveExist() { - storage.save(new Resume(UUID_1)); + @Test + public void saveExist(){ + assertThrows(ExistStorageException.class, () ->storage.save(new Resume(UUID_1))); } @Test public void get() { Resume resume = new Resume("uuid1"); assertGet(resume); - } + } public void assertGet(Resume resume) { Assert.assertEquals(resume, storage.get(resume.getUuid())); @@ -79,13 +81,13 @@ public void getNotExist() throws Exception { public void delete() { Resume resume = new Resume("uuid2"); storage.delete(resume.getUuid()); - assertSize(size-1); - Assert.assertThrows(NotExistStorageException.class, () -> storage.get(resume.getUuid())); + assertSize(size - 1); + assertThrows(NotExistStorageException.class, () -> storage.get(resume.getUuid())); } - @Test(expected = NotExistStorageException.class) - public void deleteNotExist() { - storage.delete("dummy"); + @Test + public void deleteNotExist() throws Exception { + assertThrows(NotExistStorageException.class, () ->storage.delete("dummy")); } @Test @@ -95,7 +97,7 @@ public void getAll() { assertGet(new Resume(UUID_1)); assertGet(new Resume(UUID_2)); assertGet(new Resume(UUID_3)); - Resume[] expected = new Resume[] {new Resume(UUID_1), new Resume(UUID_2), new Resume(UUID_3)}; + Resume[] expected = new Resume[]{new Resume(UUID_1), new Resume(UUID_2), new Resume(UUID_3)}; Assert.assertArrayEquals(expected, actual); } @@ -108,6 +110,7 @@ public void assertSize(int expectedSize) { Assert.assertEquals(expectedSize, storage.size()); } + @Test public void testStorageOverflow() { try { @@ -115,10 +118,19 @@ public void testStorageOverflow() { } catch (StorageException e) { Assert.fail("Переполнение произошло раньше времени: " + e.getMessage()); } - try { - storage.save(new Resume("uuid4")); - } catch (StorageException e) { - Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище"); - } + StorageException thrown = assertThrows(StorageException.class, () -> storage.save(new Resume("uuid5"))); + Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище" + thrown); +// public void testStorageOverflow() { +// try { +// Assert.assertEquals(3, storage.size()); +// } catch (StorageException e) { +// Assert.fail("Переполнение произошло раньше времени: " + e.getMessage()); +// } +// try { +// storage.save(new Resume("uuid4")); +// } catch (StorageException e) { +// Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище"); +// } +// } } -} \ No newline at end of file +} From d6963a388044ddd44a544ac5a97dc07ea7196007 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 7 Feb 2025 11:06:05 +0500 Subject: [PATCH 23/39] HW4: testStorageOverflow --- .../storage/AbstractArrayStorageTest.java | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 2cd88370..2039306a 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -18,6 +18,7 @@ public abstract class AbstractArrayStorageTest { private final Storage storage; private final int size = 3; private final Resume SAVE_RESUME = new Resume("uuid4"); + private final Resume UUID_NOT_EXIST = new Resume("dummy"); public AbstractArrayStorageTest(Storage storage) { this.storage = storage; @@ -46,8 +47,8 @@ public void update() { } @Test(expected = NotExistStorageException.class) - public void updateNotExist() throws Exception { - storage.update(new Resume("dummy")); + public void updateNotExist() { + storage.update(UUID_NOT_EXIST); } @Test @@ -58,13 +59,13 @@ public void save() { } @Test - public void saveExist(){ - assertThrows(ExistStorageException.class, () ->storage.save(new Resume(UUID_1))); + public void saveExist() { + assertThrows(ExistStorageException.class, () -> storage.save(new Resume(UUID_1))); } @Test public void get() { - Resume resume = new Resume("uuid1"); + Resume resume = new Resume(UUID_1); assertGet(resume); } @@ -73,21 +74,21 @@ public void assertGet(Resume resume) { } @Test(expected = NotExistStorageException.class) - public void getNotExist() throws Exception { - storage.get("dummy"); + public void getNotExist() { + storage.get(UUID_NOT_EXIST.getUuid()); } @Test public void delete() { - Resume resume = new Resume("uuid2"); + Resume resume = new Resume(UUID_2); storage.delete(resume.getUuid()); assertSize(size - 1); assertThrows(NotExistStorageException.class, () -> storage.get(resume.getUuid())); } @Test - public void deleteNotExist() throws Exception { - assertThrows(NotExistStorageException.class, () ->storage.delete("dummy")); + public void deleteNotExist() { + assertThrows(NotExistStorageException.class, () -> storage.delete(UUID_NOT_EXIST.getUuid())); } @Test @@ -113,24 +114,15 @@ public void assertSize(int expectedSize) { @Test public void testStorageOverflow() { + storage.clear(); try { - Assert.assertEquals(3, storage.size()); + for (int i = 0; i < AbstractArrayStorage.STORAGE_LIMIT; i++) { + storage.save(new Resume()); + } } catch (StorageException e) { Assert.fail("Переполнение произошло раньше времени: " + e.getMessage()); } - StorageException thrown = assertThrows(StorageException.class, () -> storage.save(new Resume("uuid5"))); - Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище" + thrown); -// public void testStorageOverflow() { -// try { -// Assert.assertEquals(3, storage.size()); -// } catch (StorageException e) { -// Assert.fail("Переполнение произошло раньше времени: " + e.getMessage()); -// } -// try { -// storage.save(new Resume("uuid4")); -// } catch (StorageException e) { -// Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище"); -// } -// } + assertThrows(StorageException.class, () -> storage.save(SAVE_RESUME)); + // Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище" ); } } From 081e3baa9d96e2bcd07acf044e1666f91beca657 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 7 Feb 2025 11:31:38 +0500 Subject: [PATCH 24/39] HW4: MainReflection_HW --- src/com/urise/webapp/storage/MainReflection.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/com/urise/webapp/storage/MainReflection.java b/src/com/urise/webapp/storage/MainReflection.java index 6c15934b..89c964be 100644 --- a/src/com/urise/webapp/storage/MainReflection.java +++ b/src/com/urise/webapp/storage/MainReflection.java @@ -9,16 +9,15 @@ public class MainReflection { public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Resume r = new Resume(); - Field field = r.getClass().getDeclaredFields()[0]; + Class resumeClass = r.getClass(); + Field field = resumeClass.getDeclaredFields()[0]; field.setAccessible(true); System.out.println(field.getName()); System.out.println(field.get(r)); field.set(r, "new_uuid"); System.out.println(r); - Method method = r.getClass().getMethod("toString"); - method.invoke(r); - System.out.println(method.getClass()); - System.out.println(method.getName()); - System.out.println(method); + Method method = resumeClass.getMethod("toString"); + Object result = method.invoke(r); + System.out.println(result); } } From 828d01fa1f663b2afd4556ee7c4535422af0234c Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 7 Feb 2025 16:10:09 +0500 Subject: [PATCH 25/39] HW4: finally --- .../storage/AbstractArrayStorageTest.java | 48 +++++++++---------- .../webapp/storage/ArrayStorageTest.java | 2 - 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 2039306a..587ddd1c 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -8,28 +8,34 @@ import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; public abstract class AbstractArrayStorageTest { private static final String UUID_1 = "uuid1"; + private static final Resume RESUME_1 = new Resume(UUID_1); private static final String UUID_2 = "uuid2"; + private static final Resume RESUME_2 = new Resume(UUID_2); private static final String UUID_3 = "uuid3"; + private static final Resume RESUME_3 = new Resume(UUID_3); + private static final String UUID_4 = "uuid4"; + private final Resume SAVE_RESUME = new Resume(UUID_4); + private final Resume UUID_NOT_EXIST = new Resume("dummy"); + private final Storage storage; private final int size = 3; - private final Resume SAVE_RESUME = new Resume("uuid4"); - private final Resume UUID_NOT_EXIST = new Resume("dummy"); - public AbstractArrayStorageTest(Storage storage) { + protected AbstractArrayStorageTest(Storage storage) { this.storage = storage; } @Before public void setUp() { storage.clear(); - storage.save(new Resume(UUID_1)); - storage.save(new Resume(UUID_2)); - storage.save(new Resume(UUID_3)); + storage.save(RESUME_1); + storage.save(RESUME_2); + storage.save(RESUME_3); } @Test @@ -41,9 +47,8 @@ public void clear() { @Test public void update() { - Resume r = new Resume(UUID_2); - storage.update(r); - Assert.assertSame(r, storage.get(UUID_2)); + storage.update(RESUME_2); + Assert.assertSame(RESUME_2, storage.get(UUID_2)); } @Test(expected = NotExistStorageException.class) @@ -60,13 +65,12 @@ public void save() { @Test public void saveExist() { - assertThrows(ExistStorageException.class, () -> storage.save(new Resume(UUID_1))); + assertThrows(ExistStorageException.class, () -> storage.save(RESUME_1)); } @Test public void get() { - Resume resume = new Resume(UUID_1); - assertGet(resume); + assertGet(RESUME_1); } public void assertGet(Resume resume) { @@ -80,10 +84,9 @@ public void getNotExist() { @Test public void delete() { - Resume resume = new Resume(UUID_2); - storage.delete(resume.getUuid()); + storage.delete(UUID_2); assertSize(size - 1); - assertThrows(NotExistStorageException.class, () -> storage.get(resume.getUuid())); + assertThrows(NotExistStorageException.class, () -> storage.get(UUID_2)); } @Test @@ -93,13 +96,11 @@ public void deleteNotExist() { @Test public void getAll() { - Resume[] actual = storage.getAll(); - Assert.assertEquals(size, actual.length); - assertGet(new Resume(UUID_1)); - assertGet(new Resume(UUID_2)); - assertGet(new Resume(UUID_3)); - Resume[] expected = new Resume[]{new Resume(UUID_1), new Resume(UUID_2), new Resume(UUID_3)}; - Assert.assertArrayEquals(expected, actual); + Resume[] array = storage.getAll(); + Assert.assertEquals(size, array.length); + assertEquals(RESUME_1, array[0]); + assertEquals(RESUME_2, array[1]); + assertEquals(RESUME_3, array[2]); } @Test @@ -107,7 +108,7 @@ public void size() { assertSize(size); } - public void assertSize(int expectedSize) { + private void assertSize(int expectedSize) { Assert.assertEquals(expectedSize, storage.size()); } @@ -123,6 +124,5 @@ public void testStorageOverflow() { Assert.fail("Переполнение произошло раньше времени: " + e.getMessage()); } assertThrows(StorageException.class, () -> storage.save(SAVE_RESUME)); - // Assert.fail("Ожидалось исключение StorageException при добавлении в переполненное хранилище" ); } } diff --git a/test/com/urise/webapp/storage/ArrayStorageTest.java b/test/com/urise/webapp/storage/ArrayStorageTest.java index 7bf87268..4af5bf08 100644 --- a/test/com/urise/webapp/storage/ArrayStorageTest.java +++ b/test/com/urise/webapp/storage/ArrayStorageTest.java @@ -1,7 +1,5 @@ package com.urise.webapp.storage; -import static org.junit.Assert.*; - public class ArrayStorageTest extends AbstractArrayStorageTest { public ArrayStorageTest() { super(new ArrayStorage()); From 2abe819701ffd71f7b30d0050e3fcfaa56c97405 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 11 Feb 2025 17:27:01 +0500 Subject: [PATCH 26/39] HW5: begin --- src/com/urise/webapp/MainCollections.java | 59 +++++++++++++++++++ .../webapp/{storage => }/MainReflection.java | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/com/urise/webapp/MainCollections.java rename src/com/urise/webapp/{storage => }/MainReflection.java (95%) diff --git a/src/com/urise/webapp/MainCollections.java b/src/com/urise/webapp/MainCollections.java new file mode 100644 index 00000000..7ef9b502 --- /dev/null +++ b/src/com/urise/webapp/MainCollections.java @@ -0,0 +1,59 @@ +package com.urise.webapp; + +import com.urise.webapp.model.Resume; + +import java.util.*; + +public class MainCollections { + private static final String UUID_1 = "uuid1"; + private static final Resume RESUME_1 = new Resume(UUID_1); + + private static final String UUID_2 = "uuid2"; + private static final Resume RESUME_2 = new Resume(UUID_2); + + private static final String UUID_3 = "uuid3"; + private static final Resume RESUME_3 = new Resume(UUID_3); + + private static final String UUID_4 = "uuid4"; + private static final Resume RESUME_4 = new Resume(UUID_4); + + public static void main(String[] args) { + Collection collection = new ArrayList<>(); + collection.add(RESUME_1); + collection.add(RESUME_2); + collection.add(RESUME_3); + + for (Resume r : collection) { + System.out.println(r); + if (Objects.equals(r.getUuid(), UUID_1)) { +// collection.remove(r); + } + } + + Iterator iterator = collection.iterator(); + while (iterator.hasNext()) { + Resume r = iterator.next(); + System.out.println(r); + if (Objects.equals(r.getUuid(), UUID_1)) { + iterator.remove(); + } + } + System.out.println(collection.toString()); + + + Map map = new HashMap<>(); + map.put(UUID_1, RESUME_1); + map.put(UUID_2, RESUME_2); + map.put(UUID_3, RESUME_3); + + // Bad! + for (String uuid : map.keySet()) { + System.out.println(map.get(uuid)); + } + + for (Map.Entry entry : map.entrySet()) { + System.out.println(entry.getValue()); + } + } +} + diff --git a/src/com/urise/webapp/storage/MainReflection.java b/src/com/urise/webapp/MainReflection.java similarity index 95% rename from src/com/urise/webapp/storage/MainReflection.java rename to src/com/urise/webapp/MainReflection.java index 89c964be..a6066412 100644 --- a/src/com/urise/webapp/storage/MainReflection.java +++ b/src/com/urise/webapp/MainReflection.java @@ -1,4 +1,4 @@ -package com.urise.webapp.storage; +package com.urise.webapp; import com.urise.webapp.model.Resume; From c9519eef31f26f25ce6d218d77fe718cd0c80f41 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 11 Feb 2025 17:36:06 +0500 Subject: [PATCH 27/39] HW5: static initializer block --- .../storage/AbstractArrayStorageTest.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 587ddd1c..2eab1534 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -13,17 +13,27 @@ public abstract class AbstractArrayStorageTest { + private Storage storage; + private static final String UUID_1 = "uuid1"; - private static final Resume RESUME_1 = new Resume(UUID_1); private static final String UUID_2 = "uuid2"; - private static final Resume RESUME_2 = new Resume(UUID_2); private static final String UUID_3 = "uuid3"; - private static final Resume RESUME_3 = new Resume(UUID_3); private static final String UUID_4 = "uuid4"; - private final Resume SAVE_RESUME = new Resume(UUID_4); - private final Resume UUID_NOT_EXIST = new Resume("dummy"); - private final Storage storage; + private static final Resume RESUME_1; + private static final Resume RESUME_2; + private static final Resume RESUME_3; + private static final Resume SAVE_RESUME; + + static { + RESUME_1 = new Resume(UUID_1); + RESUME_2 = new Resume(UUID_2); + RESUME_3 = new Resume(UUID_3); + SAVE_RESUME = new Resume(UUID_4); + } + + private final Resume UUID_NOT_EXIST = new Resume("dummy"); + private final int size = 3; protected AbstractArrayStorageTest(Storage storage) { From 7770e8c68f679d28bc657555e3bbfc708bb9fd4e Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 11 Feb 2025 17:37:57 +0500 Subject: [PATCH 28/39] HW5: final Storage storage --- test/com/urise/webapp/storage/AbstractArrayStorageTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 2eab1534..8e928c25 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -13,7 +13,7 @@ public abstract class AbstractArrayStorageTest { - private Storage storage; + private final Storage storage; private static final String UUID_1 = "uuid1"; private static final String UUID_2 = "uuid2"; From a7b68f4634283fc2835c08d101be5c9785214ec3 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 18 Feb 2025 12:00:29 +0500 Subject: [PATCH 29/39] HW5: class AbstractStorage --- src/com/urise/webapp/MainCollections.java | 47 +++++++ .../urise/webapp/MainTestArrayStorage.java | 6 +- .../webapp/storage/AbstractArrayStorage.java | 2 +- .../urise/webapp/storage/AbstractStorage.java | 27 ++++ src/com/urise/webapp/storage/ListStorage.java | 57 +++++++++ .../urise/webapp/storage/ListStorageTest.java | 120 ++++++++++++++++++ 6 files changed, 255 insertions(+), 4 deletions(-) create mode 100644 src/com/urise/webapp/storage/AbstractStorage.java create mode 100644 src/com/urise/webapp/storage/ListStorage.java create mode 100644 test/com/urise/webapp/storage/ListStorageTest.java diff --git a/src/com/urise/webapp/MainCollections.java b/src/com/urise/webapp/MainCollections.java index 7ef9b502..df6f297b 100644 --- a/src/com/urise/webapp/MainCollections.java +++ b/src/com/urise/webapp/MainCollections.java @@ -1,6 +1,8 @@ package com.urise.webapp; import com.urise.webapp.model.Resume; +import com.urise.webapp.storage.AbstractStorage; +import com.urise.webapp.storage.ListStorage; import java.util.*; @@ -16,8 +18,45 @@ public class MainCollections { private static final String UUID_4 = "uuid4"; private static final Resume RESUME_4 = new Resume(UUID_4); +// private static final Resume UUID_NOT_EXIST = new Resume("dummy"); + private static final AbstractStorage ABSTRACT_STORAGE = new ListStorage(); public static void main(String[] args) { + ABSTRACT_STORAGE.save(RESUME_1); + ABSTRACT_STORAGE.save(RESUME_2); + ABSTRACT_STORAGE.save(RESUME_3); + ABSTRACT_STORAGE.getAll(); + printAll(); + + System.out.println("Get r1: " + ABSTRACT_STORAGE.get(RESUME_1.getUuid())); + System.out.println("Size: " + ABSTRACT_STORAGE.size()); + +// System.out.println("Get dummy: " + ABSTRACT_STORAGE.get(UUID_NOT_EXIST.getUuid())); + + ABSTRACT_STORAGE.update(RESUME_2); + System.out.println("ok update"); + printAll(); + System.out.println("--------"); + + ABSTRACT_STORAGE.save(RESUME_4); + System.out.println("ok save"); + printAll(); + System.out.println("--------"); + + ABSTRACT_STORAGE.delete(RESUME_3.getUuid()); + System.out.println("ok delete"); + printAll(); + System.out.println("--------"); + + ABSTRACT_STORAGE.clear(); + System.out.println("ok clear"); + printAll(); + System.out.println("--------"); + + System.out.println("Size: " + ABSTRACT_STORAGE.size()); + ABSTRACT_STORAGE.size(); + + System.out.println("Collection"); Collection collection = new ArrayList<>(); collection.add(RESUME_1); collection.add(RESUME_2); @@ -55,5 +94,13 @@ public static void main(String[] args) { System.out.println(entry.getValue()); } } + + static void printAll() { + System.out.println("\nGet All"); + for (Resume r : ABSTRACT_STORAGE.getAll()) { + System.out.println(r); + } + } } + diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index 24198508..809f9248 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -23,11 +23,11 @@ public static void main(String[] args) { System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid())); System.out.println("Size: " + ARRAY_STORAGE.size()); - System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); + // System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); Resume r = new Resume("uuid2"); - ARRAY_STORAGE.update(r); - ARRAY_STORAGE.save(r); + // ARRAY_STORAGE.update(r); + // ARRAY_STORAGE.save(r); printAll(); ARRAY_STORAGE.delete(r2.getUuid()); diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index ff020e90..f099ff66 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -10,7 +10,7 @@ /** * Array based storage for Resumes */ -public abstract class AbstractArrayStorage implements Storage { +public abstract class AbstractArrayStorage extends AbstractStorage { protected static final int STORAGE_LIMIT = 10000; protected final Resume[] storage = new Resume[STORAGE_LIMIT]; protected int size = 0; diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java new file mode 100644 index 00000000..a1f29d42 --- /dev/null +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -0,0 +1,27 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; + +public abstract class AbstractStorage implements Storage { + + @Override + public abstract void clear(); + + @Override + public abstract void update(Resume r); + + @Override + public abstract void save(Resume r); + + @Override + public abstract Resume get(String uuid); + + @Override + public abstract void delete(String uuid); + + @Override + public abstract Resume[] getAll(); + + @Override + public abstract int size(); +} \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java new file mode 100644 index 00000000..87115249 --- /dev/null +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -0,0 +1,57 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.exception.ExistStorageException; +import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.model.Resume; + +import java.util.ArrayList; +import java.util.List; + +public class ListStorage extends AbstractStorage{ + protected List storage = new ArrayList<>(); + + @Override + public void clear() { + storage.clear(); + } + + @Override + public void update(Resume r) { + Resume resume = get(r.getUuid()); + storage.set(storage.indexOf(resume), r); + } + + @Override + public void save(Resume r) { + if (storage.contains(r)) { + throw new ExistStorageException(r.getUuid()); + } + storage.add(r); + } + + @Override + public Resume get(String uuid) { + for (Resume resume : storage) { + if (resume.getUuid().equals(uuid)) { + return resume; + } + } + throw new NotExistStorageException(uuid); + } + + @Override + public void delete(String uuid) { + Resume resume = get(uuid); + storage.remove(resume); + } + + @Override + public Resume[] getAll() { + return storage.toArray(new Resume[0]); + } + + @Override + public int size() { + return storage.size(); + } +} diff --git a/test/com/urise/webapp/storage/ListStorageTest.java b/test/com/urise/webapp/storage/ListStorageTest.java new file mode 100644 index 00000000..a8ac88b8 --- /dev/null +++ b/test/com/urise/webapp/storage/ListStorageTest.java @@ -0,0 +1,120 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.exception.ExistStorageException; +import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.model.Resume; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class ListStorageTest { + private ListStorage listStorage; + + private static final String UUID_1 = "uuid1"; + private static final String UUID_2 = "uuid2"; + private static final String UUID_3 = "uuid3"; + private static final String UUID_4 = "uuid4"; + + private static final Resume RESUME_1; + private static final Resume RESUME_2; + private static final Resume RESUME_3; + private static final Resume SAVE_RESUME; + + static { + RESUME_1 = new Resume(UUID_1); + RESUME_2 = new Resume(UUID_2); + RESUME_3 = new Resume(UUID_3); + SAVE_RESUME = new Resume(UUID_4); + } + + private final Resume UUID_NOT_EXIST = new Resume("dummy"); + + private final int size = 3; + + @Before + public void setUp() { + listStorage = new ListStorage(); + listStorage.clear(); + listStorage.save(RESUME_1); + listStorage.save(RESUME_2); + listStorage.save(RESUME_3); + } + + @Test + public void clear() { + listStorage.clear(); + assertSize(0); + Assert.assertArrayEquals(new Resume[0], listStorage.getAll()); + } + + @Test + public void update() { + listStorage.update(RESUME_2); + Assert.assertSame(RESUME_2, listStorage.get(UUID_2)); + } + + @Test(expected = NotExistStorageException.class) + public void updateNotExist() { + listStorage.update(UUID_NOT_EXIST); + } + + @Test + public void save() { + listStorage.save(SAVE_RESUME); + assertGet(SAVE_RESUME); + assertSize(size + 1); + } + + @Test + public void saveExist() { + assertThrows(ExistStorageException.class, () -> listStorage.save(RESUME_1)); + } + + @Test + public void get() { + assertGet(RESUME_1); + } + + public void assertGet(Resume resume) { + assertEquals(resume, listStorage.get(resume.getUuid())); + } + + @Test(expected = NotExistStorageException.class) + public void getNotExist() { + listStorage.get(UUID_NOT_EXIST.getUuid()); + } + + @Test + public void delete() { + listStorage.delete(UUID_2); + assertSize(size - 1); + assertThrows(NotExistStorageException.class, () -> listStorage.get(UUID_2)); + } + + @Test + public void deleteNotExist() { + assertThrows(NotExistStorageException.class, () -> listStorage.delete(UUID_NOT_EXIST.getUuid())); + } + + @Test + public void getAll() { + Resume[] array = listStorage.getAll(); + assertEquals(size, array.length); + assertEquals(RESUME_1, array[0]); + assertEquals(RESUME_2, array[1]); + assertEquals(RESUME_3, array[2]); + } + + @Test + public void size() { + assertSize(size); + } + + private void assertSize(int expectedSize) { + assertEquals(expectedSize, listStorage.size()); + } +} + From 3f05c407e25eb1b2815fcf4ab4a70e2de46f67b5 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 18 Feb 2025 12:47:20 +0500 Subject: [PATCH 30/39] HW5: method exist()&&doUpdate() --- .../webapp/storage/AbstractArrayStorage.java | 7 +++++-- src/com/urise/webapp/storage/AbstractStorage.java | 15 ++++++++++++++- src/com/urise/webapp/storage/ArrayStorage.java | 1 + src/com/urise/webapp/storage/ListStorage.java | 12 +++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index f099ff66..44bf3d1a 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -22,7 +22,7 @@ public void clear() { } - public final void update(Resume r) { + public final void doUpdate(Resume r) { int index = findIndex(r.getUuid()); if (index < 0) { throw new NotExistStorageException(r.getUuid()); @@ -62,7 +62,9 @@ public final void delete(String uuid) { size--; System.out.println("\nElement " + uuid + " successfully deleted from storage"); } - + protected boolean exist(String uuid) { + return findIndex(uuid)>=0; + }; public Resume[] getAll() { return Arrays.copyOf(storage, size); } @@ -76,4 +78,5 @@ public int size() { protected abstract void insertResume(Resume r); protected abstract void fillDeletedElement(int index); + } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index a1f29d42..2c25089a 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -1,5 +1,6 @@ package com.urise.webapp.storage; +import com.urise.webapp.exception.NotExistStorageException; import com.urise.webapp.model.Resume; public abstract class AbstractStorage implements Storage { @@ -8,7 +9,16 @@ public abstract class AbstractStorage implements Storage { public abstract void clear(); @Override - public abstract void update(Resume r); + public void update(Resume r) { + if (r == null) { + throw new IllegalArgumentException("Resume must not be null"); + } + String uuid = r.getUuid(); + if (!exist(uuid)) { + throw new NotExistStorageException(uuid); + } + doUpdate(r); + } @Override public abstract void save(Resume r); @@ -24,4 +34,7 @@ public abstract class AbstractStorage implements Storage { @Override public abstract int size(); + + protected abstract boolean exist(String uuid); + public abstract void doUpdate(Resume r); } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 20544991..e3bdf591 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -26,4 +26,5 @@ protected void insertResume(Resume r) { protected void fillDeletedElement(int index) { storage[index] = storage[size - 1]; } + } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java index 87115249..e83cf55a 100644 --- a/src/com/urise/webapp/storage/ListStorage.java +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -16,7 +16,7 @@ public void clear() { } @Override - public void update(Resume r) { + public void doUpdate(Resume r) { Resume resume = get(r.getUuid()); storage.set(storage.indexOf(resume), r); } @@ -54,4 +54,14 @@ public Resume[] getAll() { public int size() { return storage.size(); } + + @Override + protected boolean exist(String uuid) { + for (Resume resume : storage) { + if (resume.getUuid().equals(uuid)) { + return true; + } + } + return false; + } } From cbdb919b4ca3d31e48c56f4811e768568a7d2bf5 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 18 Feb 2025 13:13:11 +0500 Subject: [PATCH 31/39] HW5: method doSave()&&save() --- src/com/urise/webapp/MainTestArrayStorage.java | 6 +++--- .../urise/webapp/storage/AbstractArrayStorage.java | 6 ++++-- src/com/urise/webapp/storage/AbstractStorage.java | 14 ++++++++++++-- src/com/urise/webapp/storage/ListStorage.java | 5 ++++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index 809f9248..e7b3361a 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -23,11 +23,11 @@ public static void main(String[] args) { System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid())); System.out.println("Size: " + ARRAY_STORAGE.size()); - // System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); +// System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); Resume r = new Resume("uuid2"); - // ARRAY_STORAGE.update(r); - // ARRAY_STORAGE.save(r); + ARRAY_STORAGE.update(r); +//ARRAY_STORAGE.save(r); printAll(); ARRAY_STORAGE.delete(r2.getUuid()); diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index 44bf3d1a..b7e0e76f 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -31,8 +31,10 @@ public final void doUpdate(Resume r) { System.out.println("\nElement " + r + " successfully update"); } - public final void save(Resume r) { + doSave(r); + } + public void doSave(Resume r) { int index = findIndex(r.getUuid()); if (index >= 0) { throw new ExistStorageException(r.getUuid()); @@ -64,7 +66,7 @@ public final void delete(String uuid) { } protected boolean exist(String uuid) { return findIndex(uuid)>=0; - }; + } public Resume[] getAll() { return Arrays.copyOf(storage, size); } diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 2c25089a..2cad798c 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -21,7 +21,16 @@ public void update(Resume r) { } @Override - public abstract void save(Resume r); + public void save(Resume r) { + if (r == null) { + throw new IllegalArgumentException("Resume must not be null"); + } + String uuid = r.getUuid(); + if (!exist(uuid)) { + throw new NotExistStorageException(uuid); + } + doSave(r); + } @Override public abstract Resume get(String uuid); @@ -36,5 +45,6 @@ public void update(Resume r) { public abstract int size(); protected abstract boolean exist(String uuid); - public abstract void doUpdate(Resume r); + protected abstract void doUpdate(Resume r); + protected abstract void doSave(Resume r); } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java index e83cf55a..c3f57720 100644 --- a/src/com/urise/webapp/storage/ListStorage.java +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -20,9 +20,12 @@ public void doUpdate(Resume r) { Resume resume = get(r.getUuid()); storage.set(storage.indexOf(resume), r); } + public final void save(Resume r) { + doSave(r); + } @Override - public void save(Resume r) { + public void doSave(Resume r) { if (storage.contains(r)) { throw new ExistStorageException(r.getUuid()); } From 0d26281268c1a005b37e391f419f5cb9edd02093 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 18 Feb 2025 13:17:46 +0500 Subject: [PATCH 32/39] HW5: method doGet()&&get() --- .../urise/webapp/storage/AbstractArrayStorage.java | 2 +- src/com/urise/webapp/storage/AbstractStorage.java | 14 +++++++++++++- src/com/urise/webapp/storage/ListStorage.java | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index b7e0e76f..bba99d90 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -46,7 +46,7 @@ public void doSave(Resume r) { System.out.println("Element " + r + " successfully saved to storage."); } - public final Resume get(String uuid) { + public final Resume doGet(String uuid) { int index = findIndex(uuid); if (index < 0) { throw new NotExistStorageException(uuid); diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 2cad798c..57aed175 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -33,7 +33,15 @@ public void save(Resume r) { } @Override - public abstract Resume get(String uuid); + public Resume get(String uuid) { + if (uuid == null) { + throw new IllegalArgumentException("Resume must not be null"); + } + if (!exist(uuid)) { + throw new NotExistStorageException(uuid); + } + return doGet(uuid); + } @Override public abstract void delete(String uuid); @@ -45,6 +53,10 @@ public void save(Resume r) { public abstract int size(); protected abstract boolean exist(String uuid); + protected abstract void doUpdate(Resume r); + protected abstract void doSave(Resume r); + + protected abstract Resume doGet(String uuid); } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java index c3f57720..19128a9e 100644 --- a/src/com/urise/webapp/storage/ListStorage.java +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -33,7 +33,7 @@ public void doSave(Resume r) { } @Override - public Resume get(String uuid) { + public Resume doGet(String uuid) { for (Resume resume : storage) { if (resume.getUuid().equals(uuid)) { return resume; From a8d41c706f52b0beb1d148c4de0f676808b9f3cb Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 18 Feb 2025 13:30:51 +0500 Subject: [PATCH 33/39] HW5: method doDelete()&&delete() --- src/com/urise/webapp/MainTestArrayStorage.java | 8 ++++---- .../webapp/storage/AbstractArrayStorage.java | 7 +++++-- src/com/urise/webapp/storage/AbstractStorage.java | 15 +++++++++++++-- src/com/urise/webapp/storage/ListStorage.java | 5 +++-- .../webapp/storage/AbstractArrayStorageTest.java | 2 +- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index e7b3361a..91d2f37c 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -8,7 +8,7 @@ * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { -// private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); + // private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); public static void main(String[] args) { @@ -26,8 +26,8 @@ public static void main(String[] args) { // System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy")); Resume r = new Resume("uuid2"); - ARRAY_STORAGE.update(r); -//ARRAY_STORAGE.save(r); + ARRAY_STORAGE.update(r); + ARRAY_STORAGE.save(new Resume("dummy")); printAll(); ARRAY_STORAGE.delete(r2.getUuid()); @@ -39,7 +39,7 @@ public static void main(String[] args) { ARRAY_STORAGE.size(); } - static void printAll() { + static void printAll() { System.out.println("\nGet All"); for (Resume r : ARRAY_STORAGE.getAll()) { System.out.println(r); diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index bba99d90..f6bd1fff 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -34,6 +34,7 @@ public final void doUpdate(Resume r) { public final void save(Resume r) { doSave(r); } + public void doSave(Resume r) { int index = findIndex(r.getUuid()); if (index >= 0) { @@ -54,7 +55,7 @@ public final Resume doGet(String uuid) { return storage[index]; } - public final void delete(String uuid) { + public final void doDelete(String uuid) { int index = findIndex(uuid); if (index < 0) { throw new NotExistStorageException(uuid); @@ -64,9 +65,11 @@ public final void delete(String uuid) { size--; System.out.println("\nElement " + uuid + " successfully deleted from storage"); } + protected boolean exist(String uuid) { - return findIndex(uuid)>=0; + return findIndex(uuid) >= 0; } + public Resume[] getAll() { return Arrays.copyOf(storage, size); } diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 57aed175..8d337a9c 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -44,7 +44,15 @@ public Resume get(String uuid) { } @Override - public abstract void delete(String uuid); + public void delete(String uuid) { + if (uuid == null) { + throw new IllegalArgumentException("Resume must not be null"); + } + if (!exist(uuid)) { + throw new NotExistStorageException(uuid); + } + doDelete(uuid); + } @Override public abstract Resume[] getAll(); @@ -59,4 +67,7 @@ public Resume get(String uuid) { protected abstract void doSave(Resume r); protected abstract Resume doGet(String uuid); -} \ No newline at end of file + + protected abstract void doDelete(String uuid); +} + diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java index 19128a9e..b12c49f9 100644 --- a/src/com/urise/webapp/storage/ListStorage.java +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -7,7 +7,7 @@ import java.util.ArrayList; import java.util.List; -public class ListStorage extends AbstractStorage{ +public class ListStorage extends AbstractStorage { protected List storage = new ArrayList<>(); @Override @@ -20,6 +20,7 @@ public void doUpdate(Resume r) { Resume resume = get(r.getUuid()); storage.set(storage.indexOf(resume), r); } + public final void save(Resume r) { doSave(r); } @@ -43,7 +44,7 @@ public Resume doGet(String uuid) { } @Override - public void delete(String uuid) { + public void doDelete(String uuid) { Resume resume = get(uuid); storage.remove(resume); } diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 8e928c25..8cc5ab7b 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -32,7 +32,7 @@ public abstract class AbstractArrayStorageTest { SAVE_RESUME = new Resume(UUID_4); } - private final Resume UUID_NOT_EXIST = new Resume("dummy"); + private final Resume UUID_NOT_EXIST = new Resume("dummy"); private final int size = 3; From 16b70f0b52a564ca84ed11b1cae8bcf6ed5126bc Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 21 Feb 2025 17:21:17 +0500 Subject: [PATCH 34/39] HW5: delete clear(), getAll(), size() --- src/com/urise/webapp/storage/AbstractStorage.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 8d337a9c..5e53cc23 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -5,9 +5,6 @@ public abstract class AbstractStorage implements Storage { - @Override - public abstract void clear(); - @Override public void update(Resume r) { if (r == null) { @@ -54,12 +51,6 @@ public void delete(String uuid) { doDelete(uuid); } - @Override - public abstract Resume[] getAll(); - - @Override - public abstract int size(); - protected abstract boolean exist(String uuid); protected abstract void doUpdate(Resume r); From 643a355eddfcde4f84d59a6df93c70b3e6dd0b45 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 25 Feb 2025 14:02:04 +0500 Subject: [PATCH 35/39] HW5: getSearchKey()&&isExisting() --- .../webapp/storage/AbstractArrayStorage.java | 30 +++++++++---------- .../urise/webapp/storage/AbstractStorage.java | 12 ++++---- .../urise/webapp/storage/ArrayStorage.java | 6 +++- src/com/urise/webapp/storage/ListStorage.java | 30 ++++++++++++------- .../webapp/storage/SortedArrayStorage.java | 7 ++++- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index f6bd1fff..bb546b20 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -23,11 +23,11 @@ public void clear() { public final void doUpdate(Resume r) { - int index = findIndex(r.getUuid()); - if (index < 0) { + Object searchKey= getSearchKey(r.getUuid()); + if (!isExisting(searchKey)) { throw new NotExistStorageException(r.getUuid()); } - storage[index] = r; + storage[(Integer)searchKey] = r; System.out.println("\nElement " + r + " successfully update"); } @@ -36,8 +36,8 @@ public final void save(Resume r) { } public void doSave(Resume r) { - int index = findIndex(r.getUuid()); - if (index >= 0) { + Object searchKey = getSearchKey(r.getUuid()); + if (isExisting(searchKey)) { throw new ExistStorageException(r.getUuid()); } else if (size == STORAGE_LIMIT) { throw new StorageException("Storage overflow", r.getUuid()); @@ -48,27 +48,24 @@ public void doSave(Resume r) { } public final Resume doGet(String uuid) { - int index = findIndex(uuid); - if (index < 0) { + Object searchKey = getSearchKey(uuid); + if (!isExisting(searchKey)) { throw new NotExistStorageException(uuid); } - return storage[index]; + return storage[(Integer) searchKey]; } public final void doDelete(String uuid) { - int index = findIndex(uuid); - if (index < 0) { + Object searchKey = getSearchKey(uuid); + if (!isExisting(searchKey)) { throw new NotExistStorageException(uuid); } - fillDeletedElement(index); + fillDeletedElement((Integer)searchKey); storage[size - 1] = null; size--; System.out.println("\nElement " + uuid + " successfully deleted from storage"); } - protected boolean exist(String uuid) { - return findIndex(uuid) >= 0; - } public Resume[] getAll() { return Arrays.copyOf(storage, size); @@ -78,10 +75,11 @@ public int size() { return size; } - protected abstract int findIndex(String uuid); - protected abstract void insertResume(Resume r); protected abstract void fillDeletedElement(int index); + protected abstract Object getSearchKey(String uuid); + + protected abstract boolean isExisting(Object searchKey); } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 5e53cc23..8de9fc7d 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -11,7 +11,7 @@ public void update(Resume r) { throw new IllegalArgumentException("Resume must not be null"); } String uuid = r.getUuid(); - if (!exist(uuid)) { + if (!isExisting(uuid)) { throw new NotExistStorageException(uuid); } doUpdate(r); @@ -23,7 +23,7 @@ public void save(Resume r) { throw new IllegalArgumentException("Resume must not be null"); } String uuid = r.getUuid(); - if (!exist(uuid)) { + if (!isExisting(uuid)) { throw new NotExistStorageException(uuid); } doSave(r); @@ -34,7 +34,7 @@ public Resume get(String uuid) { if (uuid == null) { throw new IllegalArgumentException("Resume must not be null"); } - if (!exist(uuid)) { + if (!isExisting(uuid)) { throw new NotExistStorageException(uuid); } return doGet(uuid); @@ -45,13 +45,13 @@ public void delete(String uuid) { if (uuid == null) { throw new IllegalArgumentException("Resume must not be null"); } - if (!exist(uuid)) { + if (!isExisting(uuid)) { throw new NotExistStorageException(uuid); } doDelete(uuid); } - protected abstract boolean exist(String uuid); + protected abstract boolean isExisting(Object searchKey); protected abstract void doUpdate(Resume r); @@ -60,5 +60,7 @@ public void delete(String uuid) { protected abstract Resume doGet(String uuid); protected abstract void doDelete(String uuid); + + protected abstract Object getSearchKey(String uuid); } diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index e3bdf591..e887d0b5 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -8,7 +8,7 @@ public class ArrayStorage extends AbstractArrayStorage { @Override - protected int findIndex(String uuid) { + protected Object getSearchKey(String uuid) { for (int i = 0; i < size; i++) { if (storage[i].getUuid().equals(uuid)) { return i; @@ -27,4 +27,8 @@ protected void fillDeletedElement(int index) { storage[index] = storage[size - 1]; } + @Override + protected boolean isExisting(Object searchKey) { + return (Integer) searchKey >= 0; + } } \ No newline at end of file diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java index b12c49f9..aec1113a 100644 --- a/src/com/urise/webapp/storage/ListStorage.java +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -25,6 +25,16 @@ public final void save(Resume r) { doSave(r); } + @Override + protected boolean isExisting(Object searchKey) { + for (Resume resume : storage) { + if (resume.getUuid().equals(searchKey)) { + return true; + } + } + return false; + } + @Override public void doSave(Resume r) { if (storage.contains(r)) { @@ -49,6 +59,16 @@ public void doDelete(String uuid) { storage.remove(resume); } + @Override + protected Object getSearchKey(String uuid) { + for (Resume resume : storage) { + if (resume.getUuid().equals(uuid)) { + return uuid; + } + } + return null; + } + @Override public Resume[] getAll() { return storage.toArray(new Resume[0]); @@ -58,14 +78,4 @@ public Resume[] getAll() { public int size() { return storage.size(); } - - @Override - protected boolean exist(String uuid) { - for (Resume resume : storage) { - if (resume.getUuid().equals(uuid)) { - return true; - } - } - return false; - } } diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index b3ca91e4..bca75b37 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -7,7 +7,7 @@ public class SortedArrayStorage extends AbstractArrayStorage { @Override - protected int findIndex(String uuid) { + protected Object getSearchKey(String uuid) { Resume searchKey = new Resume(uuid); return Arrays.binarySearch(storage, 0, size, searchKey); } @@ -23,4 +23,9 @@ protected void insertResume(Resume r) { protected void fillDeletedElement(int index) { System.arraycopy(storage, index + 1, storage, index, size - index - 1); } + + @Override + protected boolean isExisting(Object searchKey) { + return (Integer) searchKey >= 0; + } } From 5fb2322c096130b73acbc938d8d3d29fc164139c Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 25 Feb 2025 15:44:36 +0500 Subject: [PATCH 36/39] HW5: getExistingSearchKey() --- .../urise/webapp/storage/AbstractStorage.java | 36 +++++++------------ .../urise/webapp/storage/ArrayStorage.java | 2 +- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 8de9fc7d..70639c6d 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -7,51 +7,39 @@ public abstract class AbstractStorage implements Storage { @Override public void update(Resume r) { - if (r == null) { - throw new IllegalArgumentException("Resume must not be null"); - } - String uuid = r.getUuid(); - if (!isExisting(uuid)) { - throw new NotExistStorageException(uuid); - } + getExistingSearchKey(r.getUuid()); doUpdate(r); } @Override public void save(Resume r) { - if (r == null) { - throw new IllegalArgumentException("Resume must not be null"); - } - String uuid = r.getUuid(); - if (!isExisting(uuid)) { - throw new NotExistStorageException(uuid); - } + getExistingSearchKey(r.getUuid()); doSave(r); } @Override public Resume get(String uuid) { - if (uuid == null) { - throw new IllegalArgumentException("Resume must not be null"); - } - if (!isExisting(uuid)) { - throw new NotExistStorageException(uuid); - } - return doGet(uuid); + return getExistingSearchKey(uuid); } @Override public void delete(String uuid) { + getExistingSearchKey(uuid); + doDelete(uuid); + } + + protected Resume getExistingSearchKey(String uuid) { if (uuid == null) { throw new IllegalArgumentException("Resume must not be null"); } - if (!isExisting(uuid)) { + Resume r = doGet(uuid); + if (r == null) { throw new NotExistStorageException(uuid); } - doDelete(uuid); + return r; } - protected abstract boolean isExisting(Object searchKey); + protected abstract boolean isExisting(Object searchKey); protected abstract void doUpdate(Resume r); diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index e887d0b5..aa2601fd 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -29,6 +29,6 @@ protected void fillDeletedElement(int index) { @Override protected boolean isExisting(Object searchKey) { - return (Integer) searchKey >= 0; + return searchKey instanceof Integer && (Integer) searchKey >= 0; } } \ No newline at end of file From 54d23bab0739f9a12fd4c0212fa968920fe538a9 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 25 Feb 2025 16:29:20 +0500 Subject: [PATCH 37/39] HW5: delete save() from AbstractArrayStorage && SortedArrayStorage --- .../urise/webapp/storage/AbstractArrayStorage.java | 4 ---- src/com/urise/webapp/storage/AbstractStorage.java | 11 ++++++++++- src/com/urise/webapp/storage/SortedArrayStorage.java | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index bb546b20..298448a5 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -31,10 +31,6 @@ public final void doUpdate(Resume r) { System.out.println("\nElement " + r + " successfully update"); } - public final void save(Resume r) { - doSave(r); - } - public void doSave(Resume r) { Object searchKey = getSearchKey(r.getUuid()); if (isExisting(searchKey)) { diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 70639c6d..96a2bdab 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -13,7 +13,7 @@ public void update(Resume r) { @Override public void save(Resume r) { - getExistingSearchKey(r.getUuid()); + getNotExistingSearchKey(r.getUuid()); doSave(r); } @@ -39,6 +39,15 @@ protected Resume getExistingSearchKey(String uuid) { return r; } + protected void getNotExistingSearchKey(String uuid) { + if (uuid == null) { + throw new IllegalArgumentException("Resume must not be null"); + } + if (isExisting(uuid)) { + throw new IllegalArgumentException("Resume already exists: " + uuid); + } + } + protected abstract boolean isExisting(Object searchKey); protected abstract void doUpdate(Resume r); diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java index bca75b37..5fc45e14 100644 --- a/src/com/urise/webapp/storage/SortedArrayStorage.java +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -26,6 +26,6 @@ protected void fillDeletedElement(int index) { @Override protected boolean isExisting(Object searchKey) { - return (Integer) searchKey >= 0; + return searchKey instanceof Integer && (Integer) searchKey >= 0; } } From 85982b7f3d1da9be51d9ca7ef78045ac226ed0d5 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 27 Feb 2025 13:19:50 +0500 Subject: [PATCH 38/39] HW5: MapStorage && MapStorageTest --- src/com/urise/webapp/MainCollections.java | 24 ++-- src/com/urise/webapp/storage/MapStorage.java | 70 +++++++++++ .../urise/webapp/storage/ListStorageTest.java | 2 +- .../urise/webapp/storage/MapStorageTest.java | 118 ++++++++++++++++++ 4 files changed, 202 insertions(+), 12 deletions(-) create mode 100644 src/com/urise/webapp/storage/MapStorage.java create mode 100644 test/com/urise/webapp/storage/MapStorageTest.java diff --git a/src/com/urise/webapp/MainCollections.java b/src/com/urise/webapp/MainCollections.java index df6f297b..52c67512 100644 --- a/src/com/urise/webapp/MainCollections.java +++ b/src/com/urise/webapp/MainCollections.java @@ -2,7 +2,7 @@ import com.urise.webapp.model.Resume; import com.urise.webapp.storage.AbstractStorage; -import com.urise.webapp.storage.ListStorage; +import com.urise.webapp.storage.MapStorage; import java.util.*; @@ -18,8 +18,9 @@ public class MainCollections { private static final String UUID_4 = "uuid4"; private static final Resume RESUME_4 = new Resume(UUID_4); -// private static final Resume UUID_NOT_EXIST = new Resume("dummy"); - private static final AbstractStorage ABSTRACT_STORAGE = new ListStorage(); + // private static final Resume UUID_NOT_EXIST = new Resume("dummy"); + // private static final AbstractStorage ABSTRACT_STORAGE = new ListStorage(); + private static final AbstractStorage ABSTRACT_STORAGE = new MapStorage(); public static void main(String[] args) { ABSTRACT_STORAGE.save(RESUME_1); @@ -31,7 +32,7 @@ public static void main(String[] args) { System.out.println("Get r1: " + ABSTRACT_STORAGE.get(RESUME_1.getUuid())); System.out.println("Size: " + ABSTRACT_STORAGE.size()); -// System.out.println("Get dummy: " + ABSTRACT_STORAGE.get(UUID_NOT_EXIST.getUuid())); + // System.out.println("Get dummy: " + ABSTRACT_STORAGE.get(UUID_NOT_EXIST.getUuid())); ABSTRACT_STORAGE.update(RESUME_2); System.out.println("ok update"); @@ -56,6 +57,7 @@ public static void main(String[] args) { System.out.println("Size: " + ABSTRACT_STORAGE.size()); ABSTRACT_STORAGE.size(); + System.out.println("Collection"); Collection collection = new ArrayList<>(); collection.add(RESUME_1); @@ -65,7 +67,7 @@ public static void main(String[] args) { for (Resume r : collection) { System.out.println(r); if (Objects.equals(r.getUuid(), UUID_1)) { -// collection.remove(r); + // collection.remove(r); } } @@ -80,20 +82,20 @@ public static void main(String[] args) { System.out.println(collection.toString()); - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put(UUID_1, RESUME_1); map.put(UUID_2, RESUME_2); map.put(UUID_3, RESUME_3); - // Bad! + // Bad! for (String uuid : map.keySet()) { - System.out.println(map.get(uuid)); - } + System.out.println(map.get(uuid)); + } for (Map.Entry entry : map.entrySet()) { - System.out.println(entry.getValue()); - } + System.out.println(entry.getValue()); } +} static void printAll() { System.out.println("\nGet All"); diff --git a/src/com/urise/webapp/storage/MapStorage.java b/src/com/urise/webapp/storage/MapStorage.java new file mode 100644 index 00000000..5486becb --- /dev/null +++ b/src/com/urise/webapp/storage/MapStorage.java @@ -0,0 +1,70 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.model.Resume; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class MapStorage extends AbstractStorage { + private final Map storage = new LinkedHashMap<>(); + + @Override + protected boolean isExisting(Object searchKey) { + return storage.containsKey(searchKey); + } + + @Override + protected void doUpdate(Resume r) { + if (!isExisting(r.getUuid())) { + throw new NotExistStorageException(r.getUuid()); + } + storage.put(r.getUuid(), r); + } + + @Override + protected void doSave(Resume r) { + if (isExisting(r.getUuid())) { + throw new IllegalArgumentException(r.getUuid()); + } + storage.put(r.getUuid(), r); + } + + @Override + protected Resume doGet(String uuid) { + if (!isExisting(uuid)) { + throw new NotExistStorageException(uuid); + } + return storage.get(uuid); + } + + @Override + protected void doDelete(String uuid) { + if (!isExisting(uuid)) { + throw new NotExistStorageException(uuid); + } + storage.remove(uuid); + } + + @Override + protected Object getSearchKey(String uuid) { + return storage.get(uuid); + } + + @Override + public void clear() { + storage.clear(); + } + + @Override + public Resume[] getAll() { + return storage.values().toArray(new Resume[0]); +// List resumeList = new ArrayList<>(storage.values()); +// return resumeList.toArray(new Resume[0]); + } + + @Override + public int size() { + return storage.size(); + } +} diff --git a/test/com/urise/webapp/storage/ListStorageTest.java b/test/com/urise/webapp/storage/ListStorageTest.java index a8ac88b8..3ead1ae3 100644 --- a/test/com/urise/webapp/storage/ListStorageTest.java +++ b/test/com/urise/webapp/storage/ListStorageTest.java @@ -78,7 +78,7 @@ public void get() { assertGet(RESUME_1); } - public void assertGet(Resume resume) { + private void assertGet(Resume resume) { assertEquals(resume, listStorage.get(resume.getUuid())); } diff --git a/test/com/urise/webapp/storage/MapStorageTest.java b/test/com/urise/webapp/storage/MapStorageTest.java new file mode 100644 index 00000000..48602420 --- /dev/null +++ b/test/com/urise/webapp/storage/MapStorageTest.java @@ -0,0 +1,118 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.exception.NotExistStorageException; +import com.urise.webapp.model.Resume; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class MapStorageTest { + + private MapStorage mapStorage; + + private static final String UUID_1 = "uuid1"; + private static final String UUID_2 = "uuid2"; + private static final String UUID_3 = "uuid3"; + private static final String UUID_4 = "uuid4"; + + private static final Resume RESUME_1; + private static final Resume RESUME_2; + private static final Resume RESUME_3; + private static final Resume SAVE_RESUME; + + static { + RESUME_1 = new Resume(UUID_1); + RESUME_2 = new Resume(UUID_2); + RESUME_3 = new Resume(UUID_3); + SAVE_RESUME = new Resume(UUID_4); + } + + private final Resume UUID_NOT_EXIST = new Resume("dummy"); + + private final int size = 3; + + @Before + public void setUp() { + mapStorage = new MapStorage(); + mapStorage.clear(); + mapStorage.save(RESUME_1); + mapStorage.save(RESUME_2); + mapStorage.save(RESUME_3); + } + + @Test + public void clear() { + mapStorage.clear(); + assertSize(0); + Assert.assertArrayEquals(new Resume[0], mapStorage.getAll()); + } + + @Test + public void update() { + mapStorage.update(RESUME_2); + Assert.assertSame(RESUME_2, mapStorage.get(UUID_2)); + } + + @Test(expected = NotExistStorageException.class) + public void updateNotExist() { + mapStorage.update(UUID_NOT_EXIST); + } + + @Test + public void save() { + mapStorage.save(SAVE_RESUME); + assertGet(SAVE_RESUME); + assertSize(size + 1); + } + + @Test + public void saveExist() { + assertThrows(IllegalArgumentException.class, () -> mapStorage.save(RESUME_1)); + } + + @Test + public void get() { + assertGet(RESUME_1); + } + + private void assertGet(Resume resume) { + assertEquals(resume, mapStorage.get(resume.getUuid())); + } + + @Test(expected = NotExistStorageException.class) + public void getNotExist() { + mapStorage.get(UUID_NOT_EXIST.getUuid()); + } + + @Test + public void delete() { + mapStorage.delete(UUID_2); + assertSize(size - 1); + assertThrows(NotExistStorageException.class, () -> mapStorage.get(UUID_2)); + } + + @Test + public void deleteNotExist() { + assertThrows(NotExistStorageException.class, () -> mapStorage.delete(UUID_NOT_EXIST.getUuid())); + } + + @Test + public void getAll() { + Resume[] array = mapStorage.getAll(); + assertEquals(size, array.length); + Assert.assertArrayEquals(new Resume[]{RESUME_1, RESUME_2, RESUME_3}, array); + } + + @Test + public void size() { + assertSize(size); + } + + private void assertSize(int expectedSize) { + assertEquals(expectedSize, mapStorage.size()); + } + +} From 1890dfd545689c56a86be8312db4c620fe9e3249 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 13 Mar 2025 11:12:42 +0500 Subject: [PATCH 39/39] HW5: isExisting in AbstractStorage --- src/com/urise/webapp/MainCollections.java | 2 +- .../urise/webapp/MainTestArrayStorage.java | 6 +-- .../webapp/storage/AbstractArrayStorage.java | 26 +++-------- .../urise/webapp/storage/AbstractStorage.java | 40 ++++++++++------- src/com/urise/webapp/storage/ListStorage.java | 44 +++++++------------ src/com/urise/webapp/storage/MapStorage.java | 30 ++++--------- .../storage/AbstractArrayStorageTest.java | 2 +- .../urise/webapp/storage/MapStorageTest.java | 1 - 8 files changed, 62 insertions(+), 89 deletions(-) diff --git a/src/com/urise/webapp/MainCollections.java b/src/com/urise/webapp/MainCollections.java index 52c67512..6ba2135b 100644 --- a/src/com/urise/webapp/MainCollections.java +++ b/src/com/urise/webapp/MainCollections.java @@ -19,7 +19,7 @@ public class MainCollections { private static final String UUID_4 = "uuid4"; private static final Resume RESUME_4 = new Resume(UUID_4); // private static final Resume UUID_NOT_EXIST = new Resume("dummy"); - // private static final AbstractStorage ABSTRACT_STORAGE = new ListStorage(); +// private static final AbstractStorage ABSTRACT_STORAGE = new ListStorage(); private static final AbstractStorage ABSTRACT_STORAGE = new MapStorage(); public static void main(String[] args) { diff --git a/src/com/urise/webapp/MainTestArrayStorage.java b/src/com/urise/webapp/MainTestArrayStorage.java index 91d2f37c..d5a63d72 100644 --- a/src/com/urise/webapp/MainTestArrayStorage.java +++ b/src/com/urise/webapp/MainTestArrayStorage.java @@ -2,14 +2,14 @@ import com.urise.webapp.model.Resume; import com.urise.webapp.storage.AbstractArrayStorage; -import com.urise.webapp.storage.SortedArrayStorage; +import com.urise.webapp.storage.ArrayStorage; /** * Test for your com.urise.webapp.storage.ArrayStorage implementation */ public class MainTestArrayStorage { - // private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); - private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); + private static final AbstractArrayStorage ARRAY_STORAGE = new ArrayStorage(); +// private static final AbstractArrayStorage ARRAY_STORAGE = new SortedArrayStorage(); public static void main(String[] args) { Resume r1 = new Resume("uuid4"); diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java index 298448a5..83d9cc6d 100644 --- a/src/com/urise/webapp/storage/AbstractArrayStorage.java +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -1,6 +1,5 @@ package com.urise.webapp.storage; -import com.urise.webapp.exception.ExistStorageException; import com.urise.webapp.exception.NotExistStorageException; import com.urise.webapp.exception.StorageException; import com.urise.webapp.model.Resume; @@ -21,21 +20,13 @@ public void clear() { System.out.println("\nThe " + storage.getClass().getSimpleName() + " was successfully cleared"); } - - public final void doUpdate(Resume r) { - Object searchKey= getSearchKey(r.getUuid()); - if (!isExisting(searchKey)) { - throw new NotExistStorageException(r.getUuid()); - } - storage[(Integer)searchKey] = r; + public final void doUpdate(Resume r, Object searchKey) { + storage[(Integer) searchKey] = r; System.out.println("\nElement " + r + " successfully update"); } - public void doSave(Resume r) { - Object searchKey = getSearchKey(r.getUuid()); - if (isExisting(searchKey)) { - throw new ExistStorageException(r.getUuid()); - } else if (size == STORAGE_LIMIT) { + public final void doSave(Object searchKey, Resume r) { + if (size == STORAGE_LIMIT) { throw new StorageException("Storage overflow", r.getUuid()); } insertResume(r); @@ -43,11 +34,7 @@ public void doSave(Resume r) { System.out.println("Element " + r + " successfully saved to storage."); } - public final Resume doGet(String uuid) { - Object searchKey = getSearchKey(uuid); - if (!isExisting(searchKey)) { - throw new NotExistStorageException(uuid); - } + public final Resume doGet(Object searchKey) { return storage[(Integer) searchKey]; } @@ -56,13 +43,12 @@ public final void doDelete(String uuid) { if (!isExisting(searchKey)) { throw new NotExistStorageException(uuid); } - fillDeletedElement((Integer)searchKey); + fillDeletedElement((Integer) searchKey); storage[size - 1] = null; size--; System.out.println("\nElement " + uuid + " successfully deleted from storage"); } - public Resume[] getAll() { return Arrays.copyOf(storage, size); } diff --git a/src/com/urise/webapp/storage/AbstractStorage.java b/src/com/urise/webapp/storage/AbstractStorage.java index 96a2bdab..259d08c2 100644 --- a/src/com/urise/webapp/storage/AbstractStorage.java +++ b/src/com/urise/webapp/storage/AbstractStorage.java @@ -1,5 +1,6 @@ package com.urise.webapp.storage; +import com.urise.webapp.exception.ExistStorageException; import com.urise.webapp.exception.NotExistStorageException; import com.urise.webapp.model.Resume; @@ -7,19 +8,23 @@ public abstract class AbstractStorage implements Storage { @Override public void update(Resume r) { - getExistingSearchKey(r.getUuid()); - doUpdate(r); + Object searchKey = getExistingSearchKey(r.getUuid()); + if (!isExisting(searchKey)) { + throw new NotExistStorageException(r.getUuid()); + } + doUpdate(r, searchKey); } @Override public void save(Resume r) { - getNotExistingSearchKey(r.getUuid()); - doSave(r); + Object searchKey = getNotExistingSearchKey(r.getUuid()); + doSave(searchKey, r); } @Override public Resume get(String uuid) { - return getExistingSearchKey(uuid); + Object searchKey = getExistingSearchKey(uuid); + return doGet(searchKey); } @Override @@ -28,33 +33,38 @@ public void delete(String uuid) { doDelete(uuid); } - protected Resume getExistingSearchKey(String uuid) { + protected Object getExistingSearchKey(String uuid) { if (uuid == null) { throw new IllegalArgumentException("Resume must not be null"); } - Resume r = doGet(uuid); - if (r == null) { + Object searchKey = getSearchKey(uuid); + if (!isExisting(searchKey)) { throw new NotExistStorageException(uuid); } - return r; + return searchKey; } - protected void getNotExistingSearchKey(String uuid) { + protected Object getNotExistingSearchKey(String uuid) { if (uuid == null) { throw new IllegalArgumentException("Resume must not be null"); } - if (isExisting(uuid)) { - throw new IllegalArgumentException("Resume already exists: " + uuid); + Object searchKey = getSearchKey(uuid); + if (isExisting(searchKey)) { + if (searchKey.equals(uuid)) { + throw new IllegalArgumentException("Resume already exists: " + uuid); + } + throw new ExistStorageException(uuid); } + return searchKey; } protected abstract boolean isExisting(Object searchKey); - protected abstract void doUpdate(Resume r); + protected abstract void doUpdate(Resume r, Object searchKey); - protected abstract void doSave(Resume r); + protected abstract void doSave(Object searchKey, Resume r); - protected abstract Resume doGet(String uuid); + protected abstract Resume doGet(Object searchKey); protected abstract void doDelete(String uuid); diff --git a/src/com/urise/webapp/storage/ListStorage.java b/src/com/urise/webapp/storage/ListStorage.java index aec1113a..937b2814 100644 --- a/src/com/urise/webapp/storage/ListStorage.java +++ b/src/com/urise/webapp/storage/ListStorage.java @@ -15,28 +15,20 @@ public void clear() { storage.clear(); } - @Override - public void doUpdate(Resume r) { - Resume resume = get(r.getUuid()); - storage.set(storage.indexOf(resume), r); - } - - public final void save(Resume r) { - doSave(r); - } @Override protected boolean isExisting(Object searchKey) { - for (Resume resume : storage) { - if (resume.getUuid().equals(searchKey)) { - return true; - } + return searchKey != null && (Integer) searchKey >= 0; } - return false; + + @Override + public void doUpdate(Resume r, Object searchKey) { + Resume resume = get(r.getUuid()); + storage.set(storage.indexOf(resume), r); } @Override - public void doSave(Resume r) { + public void doSave(Object searchKey, Resume r) { if (storage.contains(r)) { throw new ExistStorageException(r.getUuid()); } @@ -44,13 +36,12 @@ public void doSave(Resume r) { } @Override - public Resume doGet(String uuid) { - for (Resume resume : storage) { - if (resume.getUuid().equals(uuid)) { - return resume; - } + public Resume doGet(Object searchKey) { + int index = (Integer) searchKey; + if (index >= 0 && index < storage.size()) { + return storage.get(index); } - throw new NotExistStorageException(uuid); + throw new NotExistStorageException((String) searchKey); } @Override @@ -60,13 +51,12 @@ public void doDelete(String uuid) { } @Override - protected Object getSearchKey(String uuid) { - for (Resume resume : storage) { - if (resume.getUuid().equals(uuid)) { - return uuid; + protected Integer getSearchKey(String uuid) { + for (int i = 0; i < storage.size(); i++) + if (storage.get(i).getUuid().equals(uuid)) { + return i; } - } - return null; + return -1; } @Override diff --git a/src/com/urise/webapp/storage/MapStorage.java b/src/com/urise/webapp/storage/MapStorage.java index 5486becb..b9f03b6d 100644 --- a/src/com/urise/webapp/storage/MapStorage.java +++ b/src/com/urise/webapp/storage/MapStorage.java @@ -1,6 +1,5 @@ package com.urise.webapp.storage; -import com.urise.webapp.exception.NotExistStorageException; import com.urise.webapp.model.Resume; import java.util.LinkedHashMap; @@ -11,44 +10,35 @@ public class MapStorage extends AbstractStorage { @Override protected boolean isExisting(Object searchKey) { - return storage.containsKey(searchKey); + return searchKey != null && storage.containsKey(searchKey); } @Override - protected void doUpdate(Resume r) { - if (!isExisting(r.getUuid())) { - throw new NotExistStorageException(r.getUuid()); - } + protected void doUpdate(Resume r, Object searchKey) { storage.put(r.getUuid(), r); } @Override - protected void doSave(Resume r) { - if (isExisting(r.getUuid())) { + protected void doSave(Object searchKey, Resume r) { + if (searchKey != null) { throw new IllegalArgumentException(r.getUuid()); } storage.put(r.getUuid(), r); } @Override - protected Resume doGet(String uuid) { - if (!isExisting(uuid)) { - throw new NotExistStorageException(uuid); - } - return storage.get(uuid); + protected Resume doGet(Object searchKey) { + return storage.get(searchKey); } @Override protected void doDelete(String uuid) { - if (!isExisting(uuid)) { - throw new NotExistStorageException(uuid); - } storage.remove(uuid); } @Override - protected Object getSearchKey(String uuid) { - return storage.get(uuid); + protected String getSearchKey(String uuid) { + return storage.containsKey(uuid) ? uuid : null; } @Override @@ -58,9 +48,7 @@ public void clear() { @Override public Resume[] getAll() { - return storage.values().toArray(new Resume[0]); -// List resumeList = new ArrayList<>(storage.values()); -// return resumeList.toArray(new Resume[0]); + return storage.values().toArray(new Resume[0]); } @Override diff --git a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java index 8cc5ab7b..48d2470e 100644 --- a/test/com/urise/webapp/storage/AbstractArrayStorageTest.java +++ b/test/com/urise/webapp/storage/AbstractArrayStorageTest.java @@ -75,7 +75,7 @@ public void save() { @Test public void saveExist() { - assertThrows(ExistStorageException.class, () -> storage.save(RESUME_1)); + assertThrows(ExistStorageException.class, () -> storage.save(RESUME_2)); } @Test diff --git a/test/com/urise/webapp/storage/MapStorageTest.java b/test/com/urise/webapp/storage/MapStorageTest.java index 48602420..03a70df5 100644 --- a/test/com/urise/webapp/storage/MapStorageTest.java +++ b/test/com/urise/webapp/storage/MapStorageTest.java @@ -114,5 +114,4 @@ public void size() { private void assertSize(int expectedSize) { assertEquals(expectedSize, mapStorage.size()); } - }