Skip to content

Instantly share code, notes, and snippets.

@tipochka
Created September 23, 2016 12:53
Show Gist options
  • Save tipochka/ea5f07c02758701da3890cbb7ca2f1b4 to your computer and use it in GitHub Desktop.
Save tipochka/ea5f07c02758701da3890cbb7ca2f1b4 to your computer and use it in GitHub Desktop.
Блинов. Глава 3. Вариант А. 5 (**). Book: id, Название, Автор (ы), Издательство, Год издания, Количество стра-ниц, Цена, Тип переплета. Создать массив объектов. Вывести: a) список книг заданного автора; b) список книг, выпущенных заданным издательством; c) список книг, выпущенных после заданного года.
package oop.lesson2.homework.books;
import java.util.Arrays;
public class Book {
private static int nextId = 1;
private final int id = nextId++;
private final String name;
private final String[] authors;
private final String publisher;
private final int year;
private final int pages;
private int price;
private final String bindingType;
public Book(String name, String[] authors, String publisher, int year, int pages, int price, String bindingType) {
this.name = name;
this.authors = authors;
this.publisher = publisher;
this.year = year;
this.pages = pages;
this.price = price;
this.bindingType = bindingType;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static int getNextId() {
return nextId;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String[] getAuthors() {
return (String[]) authors.clone();
}
public String getPublisher() {
return publisher;
}
public int getYear() {
return year;
}
public int getPages() {
return pages;
}
public String getBindingType() {
return bindingType;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", authors=" + Arrays.toString(authors) +
", publisher='" + publisher + '\'' +
", year=" + year +
", pages=" + pages +
", price=" + price +
", bindingType='" + bindingType + '\'' +
'}';
}
}
package oop.lesson2.homework.books;
import java.util.ArrayList;
import java.util.List;
public class BookRunner {
public static void main(String[] args) {
String[] authorsBook1 = {"Stephen Hawking"};
Book book1 = new Book("The Grand Design", authorsBook1, "Transworld Digital", 2015, 256, 406, "limp");
String[] authorBook2 = {"Robert Winston"};
Book book2 = new Book("SuperHuman Encyclopedia", authorBook2, "Dorling Kindersley", 2014, 208, 432, "hard");
String[] authorBook3 = {"Johnny Ball", "Richard Hammond", "Robert Winston"};
Book book3 = new Book("Think of Number. Can You Feel the Force? What Makes Me Me?", authorBook3, "Reader's Digest", 2008, 288, 198, "hard");
Book[] books = {book1, book2, book3};
System.out.println("Book by Robert Winston: " + booksByAuthor("Robert Winston", books));
System.out.println("Book by Transworld Digital: " + booksByPublisher("Transworld Digital", books));
System.out.println("Book after 2009: " + booksAfterYear(2009, books));
}
public static List<Book> booksByAuthor(String author, Book[] books) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
for (String authorBook : book.getAuthors()) {
if (author.equals(authorBook)) {
result.add(book);
}
}
}
return result;
}
public static List<Book> booksByPublisher(String publisher, Book[] books) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (publisher.equals(book.getPublisher())) {
result.add(book);
}
}
return result;
}
public static List<Book> booksAfterYear(int year, Book[] books) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (year < book.getYear()) {
result.add(book);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment