11package com .epam .izh .rd .online .repository ;
22
33import com .epam .izh .rd .online .entity .Author ;
4+ import com .epam .izh .rd .online .service .AuthorService ;
45
56import java .util .Arrays ;
67
78public class SimpleAuthorRepository implements AuthorRepository {
89
910 @ Override
1011 public boolean save (Author author ) {
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 ) {
12+ if (authors .length == 0 ) {
1513 authors = new Author [1 ];
16- authors [0 ] = author ;
14+ authors [authors .length -1 ] = author ;
15+ return true ;
1716 } else {
1817 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 ];
18+ if (author .getName ().equals (authors [i ].getName ()) && author .getLastName ().equals (authors [i ].getLastName ())) {
19+ return false ;
2320 }
2421 }
25- }
2622
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 ) {
34- authors [authors .length -1 ] = author ;
23+ Author [] arrayForCopy = Arrays .copyOf (authors , authors .length + 1 );
24+ authors = Arrays .copyOf (arrayForCopy , arrayForCopy .length );
25+ authors [authors .length - 1 ] = author ;
3526 return true ;
3627 }
37-
38- return false ;
3928 }
4029
4130 @ Override
4231 public Author findByFullName (String name , String lastname ) {
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 ] ;
32+ for (Author element : authors ) {
33+ if (element .getName ().equals (name ) && element .getLastName ().equals (lastname )){
34+ return element ;
4635 }
4736 }
48- // for (Author element : authors) {
49- // if (element.getName().equals(name) && element.getLastName().equals(lastname)) {
50- // return element;
51- // }
52- // }
53-
54- return null ;
37+ return null ;
5538 }
5639
5740 @ Override
@@ -77,10 +60,6 @@ public boolean remove(Author author) {
7760 }
7861 }
7962
80- // public void countInArray(){
81- // System.out.println(Arrays.toString(authors));
82- // }
83-
8463 @ Override
8564 public int count () {
8665 return authors .length ;
0 commit comments