Advertisement
Josif_tepe

Untitled

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