Advertisement
Josif_tepe

Untitled

Nov 7th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Zadaca {
  3.  
  4.     public static void main(String[] args) {
  5.         System.out.println("Enter detaisl for 3 books: ");
  6.         Library library = new Library();
  7.         Scanner sc = new Scanner(System.in);
  8.         for(int i = 0; i < 3; i++) {
  9.             String title = sc.nextLine();
  10.             String author = sc.nextLine();
  11.             double price = Double.parseDouble(sc.nextLine());
  12.  
  13.             Book book = new Book(title, author, price);
  14.             library.addBook(book);
  15.         }
  16.         library.displayAllBooks();
  17.         System.out.println("Enter author: ");
  18.         String author = sc.nextLine();
  19.         System.out.println(author);
  20.         library.findBooksByAuthor(author);
  21.  
  22.  
  23.         library.sortBooksByPrice();
  24.         System.out.println("SORTED ORDER");
  25.         library.displayAllBooks();
  26.     }
  27.    
  28. }
  29.  
  30. class Book {
  31.     String title;
  32.     String author;
  33.     double price;
  34.  
  35.     public Book(String title, String author, double price) {
  36.         this.title = title;
  37.         this.author = author;
  38.         this.price = price;
  39.     }
  40.     public void print() {
  41.         System.out.println("Title: " + title);
  42.         System.out.println("Author: " + author);
  43.         System.out.println("Price: " + price);
  44.     }
  45.     public String getTitle() {
  46.         return title;
  47.     }
  48.     public String getAuthor() {
  49.         return author;
  50.     }
  51.     public double getPrice() {
  52.         return price;
  53.     }
  54. }
  55.  
  56. class Library {
  57.     Book books[];
  58.     int at;
  59.  
  60.     public Library() {
  61.         books = new Book[100];
  62.         at = 0;
  63.     }
  64.  
  65.     public void addBook(Book book) {
  66.         books[at] = book;
  67.         at++;
  68.     }
  69.     public void displayAllBooks() {
  70.         for(int i = 0; i < at; i++){
  71.             books[i].print();
  72.         }
  73.     }
  74.     public void findBooksByAuthor(String author) {
  75.         for(int i = 0; i < at; i++) {
  76.             if(books[i].getAuthor().equals(author)) {
  77.                 books[i].print();
  78.             }
  79.         }
  80.     }
  81.     public void sortBooksByPrice() {
  82.         for(int i = 0; i < at; i++) {
  83.             for(int j = i + 1; j < at; j++) {
  84.                 if(books[i].getPrice() > books[j].getPrice()) {
  85.                     Book tmp = books[i];
  86.                     books[i] = books[j];
  87.                     books[j] = tmp;
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement