Skip to content

Commit

Permalink
8. Что такое валидация?;
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Nov 29, 2023
1 parent 0956bce commit 069b4fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/main/java/ru/j4j/tracker/Tracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/ru/j4j/validate/DivByZero.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 0 additions & 4 deletions src/test/java/ru/j4j/tracker/TrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down

0 comments on commit 069b4fa

Please sign in to comment.