-
Notifications
You must be signed in to change notification settings - Fork 182
Добавил всё #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Добавил всё #68
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.epam.izh.rd.online.repository; | ||
|
|
||
| import com.epam.izh.rd.online.entity.Author; | ||
|
|
||
| public class SimpleAuthorRepository implements AuthorRepository { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Комментарии всегда нужно писать. Описывать назначение класса. Указывать тег @author |
||
| private Author[] authors = new Author[0]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему здесь не использовать коллекции? Например, интерфейс List и его реализация ArrayList. |
||
|
|
||
| @Override | ||
| public boolean save(Author author) { | ||
| int numOfAuthors = this.authors.length; | ||
| if (numOfAuthors > 0) { | ||
| if (findByFullNameGetIndex(author.getName(), author.getLastName()) >= 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Обращение к методу текущего класса лучше выполнять через this. |
||
| return false; | ||
| } | ||
| } | ||
| Author[] updateAuthors = new Author[numOfAuthors + 1]; | ||
| System.arraycopy(this.authors, 0, updateAuthors, 0, numOfAuthors); | ||
| updateAuthors[numOfAuthors] = author; | ||
| this.authors = updateAuthors; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Author findByFullName(String name, String lastname) { | ||
| int i = findByFullNameGetIndex(name, lastname); | ||
| if (i >= 0) { | ||
| return this.authors[i]; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private int findByFullNameGetIndex(String name, String lastname) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Методы обязательно снабжаются комментариями, вкратце описывающими их алгоритм работы и входные параметры |
||
| for (int i = 0; i < this.authors.length; i++) { | ||
| if (this.authors[i].getName().equals(name) && this.authors[i].getLastName().equals(lastname)) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean remove(Author author) { | ||
| int numOfAuthors = this.authors.length; | ||
| if (numOfAuthors > 0) { | ||
| int j0 = findByFullNameGetIndex(author.getName(), author.getLastName()); | ||
| if (j0 >= 0) { | ||
| Author[] updateAuthors = new Author[numOfAuthors - 1]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Весь этот код можно было не писать, используя ArrayList |
||
| for (int i = 0, j = 0; i < numOfAuthors; i++) { | ||
| if (i != j0) { | ||
| updateAuthors[j] = this.authors[i]; | ||
| j++; | ||
| } | ||
| } | ||
| this.authors = updateAuthors; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return this.authors.length; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.epam.izh.rd.online.repository; | ||
|
|
||
| import com.epam.izh.rd.online.entity.SchoolBook; | ||
|
|
||
| public class SimpleSchoolBookRepository implements BookRepository<SchoolBook> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В этом репозитории те же замечания, что и в предыдущем |
||
| private SchoolBook[] schoolBooks = new SchoolBook[0]; | ||
|
|
||
| @Override | ||
| public boolean save(SchoolBook book) { | ||
| int numOfBooks = this.schoolBooks.length; | ||
| SchoolBook[] updateBooks = new SchoolBook[numOfBooks + 1]; | ||
| System.arraycopy(this.schoolBooks, 0, updateBooks, 0, numOfBooks); | ||
| updateBooks[numOfBooks] = book; | ||
| this.schoolBooks = updateBooks; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public SchoolBook[] findByName(String name) { | ||
| int[] indexes = findByNameGetIndexes(name); | ||
| int indexesLen = indexes.length; | ||
| if (indexesLen > 0) { | ||
| SchoolBook[] arrayOfBooks = new SchoolBook[indexesLen]; | ||
| for (int i = 0; i < indexesLen; i++) { | ||
| arrayOfBooks[i] = this.schoolBooks[indexes[i]]; | ||
| } | ||
| return arrayOfBooks; | ||
| } | ||
| return new SchoolBook[0]; | ||
| } | ||
|
|
||
| private int[] findByNameGetIndexes(String name) { | ||
| int numOfBooks = this.schoolBooks.length; | ||
| int[] indexes = new int[numOfBooks]; | ||
| int j = 0; | ||
| for (int i = 0; i < numOfBooks; i++) { | ||
| if (this.schoolBooks[i].getName().equals(name)) { | ||
| indexes[j] = i; | ||
| j++; | ||
| } | ||
| } | ||
| if (j > 0) { | ||
| int[] indexes2 = new int[j]; | ||
| System.arraycopy(indexes, 0, indexes2, 0, j); | ||
| return indexes2; | ||
| } | ||
| return new int[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean removeByName(String name) { | ||
| int numOfBooks = this.schoolBooks.length; | ||
| if (numOfBooks > 0) { | ||
| int[] indexes = findByNameGetIndexes(name); | ||
| int indexesLen = indexes.length; | ||
| if (indexesLen > 0) { | ||
| SchoolBook[] arrayOfBooks = new SchoolBook[numOfBooks - indexesLen]; | ||
| boolean flag; | ||
| for (int i = 0, j = 0, k = 0; i < numOfBooks; i++) { | ||
| flag = true; | ||
| if (k < indexesLen) | ||
| if (i == indexes[k]) { | ||
| k++; | ||
| flag = false; | ||
| } | ||
| if (flag) { | ||
| arrayOfBooks[j] = this.schoolBooks[i]; | ||
| j++; | ||
| } | ||
| } | ||
| this.schoolBooks = arrayOfBooks; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return this.schoolBooks.length; | ||
| } | ||
| } | ||
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.