Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Zadaca {
- public static void main(String[] args) {
- System.out.println("Enter detaisl for 3 books: ");
- Library library = new Library();
- Scanner sc = new Scanner(System.in);
- for(int i = 0; i < 3; i++) {
- String title = sc.nextLine();
- String author = sc.nextLine();
- double price = Double.parseDouble(sc.nextLine());
- Book book = new Book(title, author, price);
- library.addBook(book);
- }
- library.displayAllBooks();
- System.out.println("Enter author: ");
- String author = sc.nextLine();
- System.out.println(author);
- library.findBooksByAuthor(author);
- library.sortBooksByPrice();
- System.out.println("SORTED ORDER");
- library.displayAllBooks();
- }
- }
- class Book {
- String title;
- String author;
- double price;
- public Book(String title, String author, double price) {
- this.title = title;
- this.author = author;
- this.price = price;
- }
- public void print() {
- System.out.println("Title: " + title);
- System.out.println("Author: " + author);
- System.out.println("Price: " + price);
- }
- public String getTitle() {
- return title;
- }
- public String getAuthor() {
- return author;
- }
- public double getPrice() {
- return price;
- }
- }
- class Library {
- Book books[];
- int at;
- public Library() {
- books = new Book[100];
- at = 0;
- }
- public void addBook(Book book) {
- books[at] = book;
- at++;
- }
- public void displayAllBooks() {
- for(int i = 0; i < at; i++){
- books[i].print();
- }
- }
- public void findBooksByAuthor(String author) {
- for(int i = 0; i < at; i++) {
- if(books[i].getAuthor().equals(author)) {
- books[i].print();
- }
- }
- }
- public void sortBooksByPrice() {
- for(int i = 0; i < at; i++) {
- for(int j = i + 1; j < at; j++) {
- if(books[i].getPrice() > books[j].getPrice()) {
- Book tmp = books[i];
- books[i] = books[j];
- books[j] = tmp;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement