@@ -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