diff --git a/OOP/Java/Assignments/FISAC/Q3.java b/OOP/Java/Assignments/FISAC/Q3.java new file mode 100644 index 0000000..e9b823c --- /dev/null +++ b/OOP/Java/Assignments/FISAC/Q3.java @@ -0,0 +1,66 @@ +// Package: users +package users; +public class User { + String name; + int userID; + public User(String name, int userID) { this.name = name; this.userID = userID; } +} + +// Package: books +package books; +public class Book { + String title; + boolean isAvailable; + public Book(String title) { this.title = title; this.isAvailable = true; } +} + +// Package: exceptions +package exceptions; +public class BookNotAvailableException extends Exception {} +public class BookNotBorrowedException extends Exception {} +public class BookCannotBeIssued extends Exception {} + +// Interface LibraryOperations +package users; +import books.Book; +import exceptions.*; +interface LibraryOperations { + void borrowBook(Book book) throws BookNotAvailableException, BookCannotBeIssued; + void returnBook(Book book) throws BookNotBorrowedException; + int calculatePenalty(int daysLate); +} + +// Class LibraryUser +package users; +import books.Book; +import exceptions.*; +public class LibraryUser extends User implements LibraryOperations { + public LibraryUser(String name, int userID) { super(name, userID); } + public void borrowBook(Book book) throws BookNotAvailableException, BookCannotBeIssued { + if (!book.isAvailable) throw new BookNotAvailableException(); + book.isAvailable = false; + } + public void returnBook(Book book) throws BookNotBorrowedException { + if (book.isAvailable) throw new BookNotBorrowedException(); + book.isAvailable = true; + } + public int calculatePenalty(int daysLate) { return daysLate * 2; } +} + +// Main class for testing +package main; +import users.LibraryUser; +import books.Book; +import exceptions.*; +public class Main { + public static void main(String[] args) { + LibraryUser user = new LibraryUser("Alice", 1); + Book book = new Book("Java Programming"); + try { + user.borrowBook(book); + user.returnBook(book); + } catch (Exception e) { + System.out.println(e); + } + } +}