CCCPaste

Untitled

import java.util.Scanner;
public class Zadaca {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter details for 3 books: ");

        Library library = new Library(3);
        for(int i = 0; i < 3; i++) {
            System.out.println("Enter title: ");
            String title = sc.nextLine();
            System.out.println("Enter author: ");
            String author = sc.nextLine();
            System.out.println("Enter price: ");
            double price = Double.parseDouble(sc.nextLine());

            Book book = new Book(title, author, price);
            library.addBook(book);
        }
        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);
    }
}
class Library {
    Book[] books;
    int at;
    public Library(int n) {
        books = new Book[n];
        at = 0;
    }

    public void addBook(Book book) {
        books[at] = book;
        at++;   
    }

    public void displayAllBooks() {
        for(int i = 0; i < at; i++) {
            books[i].print();
        }
    }

}