Final Exam
Topic: Java OOP Programming 1
1. Implement of OOP base programming of the following UML diagram:
public class Main {
public static void main(String[] args) {
// Create some books
Book book1 = new Book(1, "Java Programming", "Youlong", 2020, 29.99,
"Available");
Book book2 = new Book(2, "Python Basics", "Vitu", 2019, 24.99, "Available");
Library library = new Library();
library.addBook(book1);
library.addBook(book2);
library.displayBooks();
User user = new User(101, "Chin Jen", "123 Main St", "555-1234");
user.borrowBook(book1);
user.displayBorrowedBooks();
user.returnBook(book1);
user.displayBorrowedBooks();
}
}
public class Library {
private List<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("Book added successfully!");
}
public void deleteBook(int bookId) {
Book bookToRemove = null;
for (Book book : books) {
if (book.getId() == bookId) {
bookToRemove = book;
break;
}
}
if (bookToRemove != null) {
books.remove(bookToRemove);
System.out.println("Book with ID " + bookId + " deleted successfully!");
} else {
System.out.println("Book with ID " + bookId + " not found.");
}
}
public void displayBooks() {
if (books.isEmpty()) {
System.out.println("No books in the library.");
} else {
for (Book book : books) {
book.getBookDetails();
System.out.println("-----------------------------");
}
}
}
public Book findBookById(int bookId) {
for (Book book : books) {
if (book.getId() == bookId) {
return book;
}
}
return null;
}
}
public class User {
// Attributes
private int userId;
private String userName;
private String address;
private String phone;
private List<Book> borrowedBooks;
public User(int userId, String userName, String address, String phone) {
this.userId = userId;
this.userName = userName;
this.address = address;
this.phone = phone;
this.borrowedBooks = new ArrayList<>();
}
public void borrowBook(Book book) {
if (book.getStatus().equals("Available")) {
borrowedBooks.add(book);
book.setStatus("Borrowed");
System.out.println("Book borrowed successfully!");
} else {
System.out.println("Book is not available for borrowing.");
}
}
public void returnBook(Book book) {
if (borrowedBooks.contains(book)) {
borrowedBooks.remove(book);
book.setStatus("Available");
System.out.println("Book returned successfully!");
} else {
System.out.println("This book was not borrowed by the user.");
}
}
public void displayBorrowedBooks() {
if (borrowedBooks.isEmpty()) {
System.out.println("No books borrowed by the user.");
} else {
System.out.println("Books borrowed by " + userName + ":");
for (Book book : borrowedBooks) {
book.getBookDetails();
System.out.println("-----------------------------");
}
}
}
}
public class Book {
private int id;
private String title;
private String author;
private int publishedYear;
private double price;
private String status;
public Book(int id, String title, String author, int publishedYear, double price,
String status) {
this.id = id;
this.title = title;
this.author = author;
this.publishedYear = publishedYear;
this.price = price;
this.status = status;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPublishedYear() {
return publishedYear;
}
public double getPrice() {
return price;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public void getBookDetails() {
System.out.println("Book ID: " + id);
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Published Year: " + publishedYear);
System.out.println("Price: $" + price);
System.out.println("Status: " + status);
}
}
2. Implement function main to create the menu options as the following:
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
User user = new User(101, "John Youlong", "Phnom Penh", "93383965"); // Create
a user
int choice;
do {
System.out.println("Library Management System");
System.out.println("*******************************");
System.out.println("1. Add Book");
System.out.println("2. Display Books");
System.out.println("3. Borrow Book");
System.out.println("4. Return Book");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter Book ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Book Name: ");
String title = scanner.nextLine();
System.out.print("Enter Author: ");
String author = scanner.nextLine();
System.out.print("Enter Year of Publication: ");
int year = scanner.nextInt();
System.out.print("Enter Price: ");
double price = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter Status: ");
String status = scanner.nextLine();
Book newBook = new Book(id, title, author, year, price, status);
library.addBook(newBook);
System.out.println("Book added successfully!");
break;
case 2:
library.displayBooks();
break;
case 3:
System.out.print("Enter Book ID to borrow: ");
int borrowId = scanner.nextInt();
Book borrowBook = library.findBookById(borrowId);
if (borrowBook != null) {
user.borrowBook(borrowBook);
} else {
System.out.println("Book not found!");
}
break;
case 4:
System.out.print("Enter Book ID to return: ");
int returnId = scanner.nextInt();
Book returnBook = library.findBookById(returnId);
if (returnBook != null) {
user.returnBook(returnBook);
} else {
System.out.println("Book not found!");
}
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
scanner.close();
}
}