Created
June 13, 2016 21:13
-
-
Save YanchevskayaAnna/e456edc3babb514bb87e4d7a7a5a0b0f to your computer and use it in GitHub Desktop.
Library_DMurchkov
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package library2; | |
import java.util.ArrayList; | |
public class Client implements Comparable<Client> { | |
private String name; | |
private String surName; | |
private int phoneNum; | |
private boolean inBlackList; | |
private ArrayList<PrintedPublication> debt; //Yanchevskaya A private List<PrintedPublication> debt; | |
public Client(String name, String surName, int phoneNum) { | |
this.name = name; | |
this.surName = surName; | |
this.phoneNum = phoneNum; | |
inBlackList = false; | |
debt = new ArrayList<>(); | |
} | |
public String getName() { | |
return name; | |
} | |
public String getSurName() { | |
return surName; | |
} | |
public int getPhoneNum() { | |
return phoneNum; | |
} | |
public boolean isInBlackList() { | |
return inBlackList; | |
} | |
public ArrayList<PrintedPublication> getDebt() { | |
return debt; | |
} | |
public void setInBlackList(boolean inBlackList) { | |
this.inBlackList = inBlackList; | |
} | |
public boolean equals(Object o) { | |
if (o == null || o.getClass() != Client.class) return false; | |
if (this == o) return true; | |
Client client = (Client) o; | |
return this.name.equals(client.name) && this.surName.equals(client.surName) && this.phoneNum == client.phoneNum; | |
} | |
@Override | |
public String toString() { | |
return "Client{" + | |
"name='" + name + '\'' + | |
", surName='" + surName + '\'' + | |
", phoneNum=" + phoneNum + | |
'}'; | |
} | |
public int compareTo(Client c) { | |
return this.name.compareTo(c.name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package library2; | |
import library2.comparators.ClientsByNameComparator; | |
import library2.comparators.PrintedPublicationsByTitleComparator; | |
import library2.comparators.PrintedPublicationsByYearComparator; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class Library { | |
private List<PrintedPublication> printedPublications; | |
private List<Client> clients; | |
public Library(List<PrintedPublication> printedPublications) { | |
if (printedPublications == null) { | |
this.printedPublications = new ArrayList<>(); | |
} | |
this.printedPublications = printedPublications; //Yanchevskaya A. Нужно добавить в else,т.е. если printedPublications != null | |
clients = new ArrayList<>(); | |
} | |
public List<Client> getClients() { | |
clients.sort(new ClientsByNameComparator()); | |
return clients; | |
} | |
public List<PrintedPublication> getAvailablePublications() { | |
List<PrintedPublication> tmp = new ArrayList<>(); | |
for (int i = 0; i < printedPublications.size(); i++) { | |
if (printedPublications.get(i).isAvailable()) { | |
tmp.add(printedPublications.get(i)); //Yanchevskaya A. если у нас несколько одинаковых книг, будет дублирование? | |
} | |
} | |
tmp.sort(new PrintedPublicationsByTitleComparator()); | |
return tmp; | |
} | |
public boolean addPublication(PrintedPublication o) { | |
if (o == null) return false; | |
printedPublications.add(o); | |
return true; | |
/*Yanchevskaya A. Получается, мы можем одну и ту же книгу добавить несколько раз? | |
В методах получения доступных книг, поиска книг по автору и т.д. одна и та же книга будет выводиться несколько раз | |
Возможно стоит вместо признака isAvailable вести учет количества доступных книг?*/ | |
} | |
public boolean addClient(Client o) { | |
if (o == null || clients.contains(o)) return false; | |
clients.add(o); | |
return true; | |
} | |
public boolean givePublicationToClient(PrintedPublication p, Client c) { //Yanchevskaya A Нужен аналогичный метод, который позволяет читателю вернуть книгу | |
if (p == null || c == null || !printedPublications.contains(p) || !p.isAvailable() || c.getDebt().size() >= 3 || c.isInBlackList()) | |
return false; | |
for (int i = 0; i < printedPublications.size(); i++) { //Yanchevskaya A. А здесь нужен цикл? Ведь уже проверили, что книга есть в перечне книг и она доступна | |
if (printedPublications.get(i) != null && printedPublications.get(i).equals(p)) { | |
c.getDebt().add(p); | |
printedPublications.get(i).setIsAvailable(false); //Yanchevskaya A. а если у нас несколько таких книг? и какая-то из них уже оддана другому читателю, получается мы её оддадим дважды? а вторая книга останется доступной? | |
return true; | |
} | |
} | |
return false; | |
} | |
public List<PrintedPublication> getAllPublicationsOutOfLibrary() { | |
List<PrintedPublication> tmp = new ArrayList<>(); | |
for (int i = 0; i < printedPublications.size(); i++) { | |
if (printedPublications.get(i) != null && !printedPublications.get(i).isAvailable()) { | |
tmp.add(printedPublications.get(i)); //Yanchevskaya A. если мы храним только признак isAvailable, то одна и таже книга выведется несколько раз - не очень информатично | |
} | |
} | |
tmp.sort(new PrintedPublicationsByTitleComparator()); | |
return tmp; | |
} | |
public List<PrintedPublication> getClientDebt(Client o) { | |
if (o == null || o.getDebt() == null) return null; //Yanchevskaya A. В каком случае o.getDebt() == null? | |
o.getDebt().sort(new PrintedPublicationsByTitleComparator()); | |
return o.getDebt(); | |
} | |
public boolean addClientToBlackList(Client o) { | |
if (o == null || !clients.contains(o)) return false; | |
o.setInBlackList(true); | |
return true; | |
} | |
public List<PrintedPublication> getPublicationsByAuthor(Author o) { | |
if (o == null) return null; | |
List<PrintedPublication> tmp = new ArrayList<>(); | |
for (int i = 0; i < printedPublications.size(); i++) { | |
if (printedPublications.get(i) != null && printedPublications.get(i).getAuthors().contains(o)) { | |
tmp.add(printedPublications.get(i)); | |
} | |
} | |
tmp.sort(new PrintedPublicationsByTitleComparator()); //Yanchevskaya A. если у нас несколько одинаковых книг, будет дублирование | |
return tmp; | |
} | |
public List<PrintedPublication> getPublicationsByYear(int year) { | |
if (year <= 0) return null; | |
List<PrintedPublication> tmp = new ArrayList<PrintedPublication>(); | |
for (int i = 0; i < printedPublications.size(); i++) { | |
if (printedPublications.get(i) != null && printedPublications.get(i).getYear() == year) { | |
tmp.add(printedPublications.get(i)); //Yanchevskaya A. если у нас несколько одинаковых книг, будет дублирование | |
} | |
} | |
tmp.sort(new PrintedPublicationsByYearComparator()); | |
return tmp; | |
} | |
public List<PrintedPublication> getPublicationsByTitle(String title) { | |
if (title == null) return null; | |
List<PrintedPublication> tmp = new ArrayList<>(); | |
for (int i = 0; i < printedPublications.size(); i++) { | |
if (printedPublications.get(i) != null && printedPublications.get(i).getTitle().contains(title)) { | |
tmp.add(printedPublications.get(i)); //Yanchevskaya A. если у нас несколько одинаковых книг, будет дублирование | |
} | |
} | |
tmp.sort(new PrintedPublicationsByTitleComparator()); | |
return tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment