Skip to content

Instantly share code, notes, and snippets.

@13andrew13
Created January 30, 2017 16:34
Show Gist options
  • Save 13andrew13/af29ac372c2d7e2d7a0a3741817ebd17 to your computer and use it in GitHub Desktop.
Save 13andrew13/af29ac372c2d7e2d7a0a3741817ebd17 to your computer and use it in GitHub Desktop.
public class Book {
private int id;
private String name;
private String author;
private String publisher;
private int yearofpublication;
private int numberOfPages;
private double price;
private String type;
public Book(){
}
public Book(int id, String name, String author, String publisher, int yearofpublication, int numberOfPages, double price, String type) {
checkNumber(id);
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
checkNumber(yearofpublication);
this.yearofpublication = yearofpublication;
checkNumber(numberOfPages);
this.numberOfPages = numberOfPages;
checkNumber(price);
this.price = price;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
checkNumber(id);
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getYearofpublication() {
return yearofpublication;
}
public void setYearofpublication(int yearofpublication) {
checkNumber(yearofpublication);
this.yearofpublication = yearofpublication;
}
public int getNumberOfPages() {
return numberOfPages;
}
public void setNumberOfPages(int numberOfPages) {
checkNumber(numberOfPages);
this.numberOfPages = numberOfPages;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
checkNumber(price);
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Book: id: " + id + " name: \"" + name +"\"\n author: " +author+ "\n publisher: " + publisher + "\n year: " + yearofpublication + "\n pages: " + numberOfPages +"\n price: " + price + "\n type of bindig: " + type;
}
private void checkNumber(int number){
if(number<1)
throw new IllegalArgumentException("This value shouldn't be less than 0!");
}
private void checkNumber(double number){
if(number<1)
throw new IllegalArgumentException("This value shouldn't be less than 0!");
}
}
import java.util.ArrayList;
import java.util.Date;
public class Books {
private ArrayList<Book> books = new ArrayList<Book>();
public void addBook(Book book){
books.add(book);
}
public ArrayList<Book> getBookAuthors(String author){
checkList(getBooksAuthor(author));
return getBooksAuthor(author);
}
public ArrayList<Book> getBookPublisher(String publisher){
return getBooksPublisher(publisher);
}
public ArrayList<Book> getBookYear(int year){
checkYear(year);
return getBooksYear(year);
}
public void printBooks(){
for(Book book: books){
System.out.println(book.toString());
}
}
public void printListBooks(ArrayList<Book> list){
listBookstoString(list);
}
private void listBookstoString(ArrayList<Book> list){
checkList(list);
for(Book book: list){
System.out.println(book.toString());
}
System.out.println();
}
private ArrayList<Book> getBooksAuthor(String author){
ArrayList<Book> search= new ArrayList<Book>();
for(Book book: books){
if(book.getAuthor().equals(author))
{
search.add(book);
}
}
return search;
}
private ArrayList<Book> getBooksPublisher(String publisher){
ArrayList<Book> search= new ArrayList<Book>();
for(Book book: books){
if(book.getPublisher().equals(publisher))
{
search.add(book);
}
}
return search;
}
private ArrayList<Book> getBooksYear(int year){
ArrayList<Book> search= new ArrayList<Book>();
for(Book book: books) {
if (book.getYearofpublication() >= year) {
search.add(book);
}
}
return search;
}
private void checkList(ArrayList< Book> list){
if(list.isEmpty())
System.out.println("No results for your request");
else
System.out.println("Results for your request");
}
private void checkYear(int givenyear){
Date date = new Date();
int year = date.getYear()+1900;
if(givenyear>year){
throw new IllegalArgumentException("This book hasn't created yet ");}
}
}
import java.util.ArrayList;
public class BooksRunner {
public static void main(String[] args) {
//creating books
Book book1 = new Book(25,"Kingston","Aston Martin","Veselka",1959, 10,25,"soft");
Book book2 = new Book(25,"White","Aston Martin","Veselka",1959, 250,25,"soft");
Book book3 = new Book();
book3.setId(2234);
book3.setName("");
book3.setAuthor("Stiven King");
book3.setPublisher("Ves");
book3.setYearofpublication(1978);
book3.setNumberOfPages(233);
book3.setPrice(23);
book3.setType("Soft");
//creating Books object and adding Book to the list
Books books = new Books();
books.addBook(book1);
books.addBook(book2);
books.addBook(book3);
//creating list of books by some rule
ArrayList<Book> veselkaBooks = books.getBookPublisher("Veselka");
ArrayList<Book> yearbooks = books.getBookYear(1960);
//printing lists of books
System.out.println();
//System.out.print(book1.toString());
//books.printBooks();
books.printListBooks(veselkaBooks);
books.printListBooks(yearbooks);
}
}
@W1zarDddD
Copy link

W1zarDddD commented Dec 9, 2023

A book written in java is really cool. A person does not waste time. On the contrary, I got lazy, I even found top writers, I use a professional essay writer to help. I need to be more careful with this, because I realized that I was starting to get used to it. By the way, you motivated me with this. I just thought about this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment