From 069b4fa3d175319056a2f3f17e31b642e98f1331 Mon Sep 17 00:00:00 2001 From: temzor Date: Wed, 29 Nov 2023 12:15:51 +0300 Subject: [PATCH] =?UTF-8?q?8.=20=D0=A7=D1=82=D0=BE=20=D1=82=D0=B0=D0=BA?= =?UTF-8?q?=D0=BE=D0=B5=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=3F;=20https://job4j.ru/profile/exercise/22/task-view/162?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ru/j4j/tracker/Tracker.java | 12 ++++++----- src/main/java/ru/j4j/validate/DivByZero.java | 21 +++++++++++++++++++ src/test/java/ru/j4j/tracker/TrackerTest.java | 4 ---- 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ru/j4j/validate/DivByZero.java diff --git a/src/main/java/ru/j4j/tracker/Tracker.java b/src/main/java/ru/j4j/tracker/Tracker.java index fa206c0..3a2c71b 100644 --- a/src/main/java/ru/j4j/tracker/Tracker.java +++ b/src/main/java/ru/j4j/tracker/Tracker.java @@ -37,12 +37,12 @@ public Item findById(int id) { public boolean replace(int id, Item item) { int index = indexOf(id); - if (index != -1) { + boolean rsl = index != -1; + if (rsl) { item.setId(id); items[index] = item; - return true; } - return false; + return rsl; } public int indexOf(int id) { @@ -56,12 +56,14 @@ public int indexOf(int id) { return rsl; } - public void delete(int id) { + public boolean delete(int id) { int index = indexOf(id); - if (index != -1) { + boolean rsl = index != -1; + if (rsl) { System.arraycopy(items, index + 1, items, index, size - index - 1); items[size - 1] = null; size--; } + return rsl; } } \ No newline at end of file diff --git a/src/main/java/ru/j4j/validate/DivByZero.java b/src/main/java/ru/j4j/validate/DivByZero.java new file mode 100644 index 0000000..524fa0a --- /dev/null +++ b/src/main/java/ru/j4j/validate/DivByZero.java @@ -0,0 +1,21 @@ +package ru.j4j.validate; + +public class DivByZero { + public static int div(int first, int second) { + int rsl = -1; + if (second == 0) { + System.out.println("Div by 0, return def value -1"); + } else { + rsl = first / second; + } + return rsl; + } + + public static void main(String[] args) { + int rsl = div(10, 5); + System.out.println(rsl); + + int rsl1 = div(10, 0); + System.out.println(rsl1); + } +} diff --git a/src/test/java/ru/j4j/tracker/TrackerTest.java b/src/test/java/ru/j4j/tracker/TrackerTest.java index 000cd18..d65cf04 100644 --- a/src/test/java/ru/j4j/tracker/TrackerTest.java +++ b/src/test/java/ru/j4j/tracker/TrackerTest.java @@ -61,10 +61,6 @@ public void whenReplaceItemIsSuccessful() { int id = item.getId(); Item updateItem = new Item("Bug with description"); tracker.replace(id, updateItem); - System.out.println(item.getId()); - System.out.println(tracker.indexOf(id)); - System.out.println(tracker.findById(id)); - System.out.println(item.getId() == id); assertThat(tracker.findById(id).getName()).isEqualTo("Bug with description"); }