Skip to content

Commit c35044a

Browse files
committed
add changes in Main and SimpleAuthorRepository files, changes method (save)
1 parent 0779581 commit c35044a

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/main/java/com/epam/izh/rd/online/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import com.epam.izh.rd.online.repository.SimpleAuthorRepository;
55
import com.epam.izh.rd.online.service.AuthorService;
66
import com.epam.izh.rd.online.service.SimpleAuthorService;
7-
import sun.java2d.pipe.SpanShapeRenderer;
87

98
import java.time.LocalDate;
10-
import java.util.Arrays;
119

1210
import static java.lang.System.console;
1311
import static java.lang.System.out;
@@ -26,11 +24,13 @@ public static void main(String[] args) {
2624
out.println(author1.findByFullName(authorSecond.getName(), authorSecond.getLastName()));
2725
out.println(author1.count());
2826
out.println(author1.save(authorThird));
27+
// author1.countInArray();
2928
out.println(author1.findByFullName(authorThird.getName(), authorThird.getLastName()));
3029
out.println(author1.count());
3130
out.println(author1.save(authorSecond));
3231
out.println(author1.findByFullName(authorSecond.getName(), authorSecond.getLastName()));
3332
out.println(author1.count());
33+
// author1.countInArray();
3434

3535
}
3636

src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,48 @@ public class SimpleAuthorRepository implements AuthorRepository {
88

99
@Override
1010
public boolean save(Author author) {
11-
SimpleAuthorRepository checkAuthor = new SimpleAuthorRepository();
12-
13-
if (checkAuthor.findByFullName(author.getName(), author.getLastName()) == author) {
14-
return false;
11+
int countPropInArray = authors.length;
12+
System.out.println(Arrays.toString(authors));
13+
System.out.println(author.getName() + " " + author.getLastName());
14+
if (authors.length == 0 ) {
15+
authors = new Author[1];
16+
authors[0] = author;
1517
} else {
16-
Author[] arrayForCopy = Arrays.copyOf(authors, authors.length+1);
17-
authors = new Author[authors.length+1];
18-
authors = Arrays.copyOf(arrayForCopy, arrayForCopy.length);
18+
for (int i = 0; i < authors.length; i++) {
19+
if (authors[i].getName().equals(author.getName()) && authors[i].getLastName().equals(author.getLastName())) {
20+
break;
21+
} else {
22+
authors = new Author[countPropInArray + 1];
23+
}
24+
}
25+
}
26+
27+
// authors
28+
Author[] arraysForCopy = Arrays.copyOf(authors, authors.length);
29+
30+
// System.out.println(Arrays.toString(arraysForCopy));
31+
// authors = Arrays.copyOf(arraysForCopy, arraysForCopy.length)
32+
// System.out.println(Arrays.toString(authors));
33+
if (authors.length != countPropInArray) {
1934
authors[authors.length-1] = author;
2035
return true;
2136
}
37+
38+
return false;
2239
}
2340

2441
@Override
2542
public Author findByFullName(String name, String lastname) {
26-
for (Author element : authors) {
27-
if (element.getName().equals(name) && element.getLastName().equals(lastname)) {
28-
return element;
43+
for (int i = 0; i < authors.length; i++) {
44+
if (authors[i].getName().equals(name) && authors[i].getLastName().equals(lastname)) {
45+
return authors[i];
2946
}
3047
}
48+
// for (Author element : authors) {
49+
// if (element.getName().equals(name) && element.getLastName().equals(lastname)) {
50+
// return element;
51+
// }
52+
// }
3153

3254
return null;
3355
}
@@ -55,6 +77,10 @@ public boolean remove(Author author) {
5577
}
5678
}
5779

80+
// public void countInArray(){
81+
// System.out.println(Arrays.toString(authors));
82+
// }
83+
5884
@Override
5985
public int count() {
6086
return authors.length;

0 commit comments

Comments
 (0)