Created
October 23, 2019 18:15
-
-
Save gordinmitya/39158a3a52c4e24a6bc6e19ceafa93f1 to your computer and use it in GitHub Desktop.
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 ru.gordinmitya; | |
import java.util.Arrays; | |
class Animal { | |
String name; | |
void say() { } | |
} | |
class Dog extends Animal { | |
} | |
class Cat extends Animal { | |
} | |
class Book extends Object implements Comparable<Book> { | |
String authorSurname; | |
String authorName; | |
String name; | |
int pageCount; | |
String year; | |
public Book(String authorSurname, String authorName, String name, int pageCount, String year) { | |
this.authorSurname = authorSurname; | |
this.authorName = authorName; | |
this.name = name; | |
this.pageCount = pageCount; | |
this.year = year; | |
} | |
String getInfo() { | |
return String.format("%s %s; %s (%s)", authorName, authorSurname, name, year); | |
} | |
@Override | |
public String toString() { | |
return getInfo(); | |
} | |
@Override | |
public int compareTo(Book o) { | |
if (this.pageCount > o.pageCount) { | |
return 1; | |
} else if (this.pageCount == o.pageCount) { | |
return 0; | |
} else { | |
return -1; | |
} | |
} | |
} | |
public class Main { | |
static void printAll(String title, Object[] array) { | |
System.out.println(title); | |
for (Object o : array) { | |
System.out.println(o); | |
} | |
} | |
public static void main(String[] args) { | |
Book[] books = new Book[]{ | |
new Book("Достоевский", "Федор", "Бесы", 578, "1887"), | |
new Book("Достоевский", "Федор", "Униженные и оскорбленные", 300, "1887"), | |
new Book("Братья", "Стругацких", "Улитка на склоне", 400, "1887") | |
}; | |
printAll("Initial", books); | |
Arrays.sort(books); | |
printAll("Sorted", books); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment