Add OOP/Java/Assignments/FISAC/Q3.java
This commit is contained in:
parent
f9d68be8f0
commit
adb5985077
66
OOP/Java/Assignments/FISAC/Q3.java
Normal file
66
OOP/Java/Assignments/FISAC/Q3.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user