Problem: Designing a library management system using Java console application.
Solution:
1. Create a class called Book with the following attributes: title, author, publication year, and availability status (boolean).
2. Create a class called User with the following attributes: name, age, and borrowedBooks (array of Book objects).
3. Create another class called Library with the following attributes: books (array of Book objects), users (array of User objects), and totalNumberOfBooks (int).
4. Implement the following methods in the Library class:
- addBook(): to add a book to the library.
- removeBook(): to remove a book from the library.
- displayAvailableBooks(): to display the list of available books.
- displayBorrowedBooks(): to display the list of borrowed books.
- borrowBook(): to allow a user to borrow a book.
- returnBook(): to allow a user to return a book.
- addUser(): to add a user to the library.
- removeUser(): to remove a user from the library.
- displayUsers(): to display the list of users.
5. Use loops to iterate over arrays and perform the required operations.
6. Use constructors to initialize objects with initial values.
7. Use inheritance to create the following classes:
- FictionBook (subclass of Book): with an additional attribute called genre.
- NonFictionBook (subclass of Book): with an additional attribute called topic.
8. Use information hiding by making attributes private and providing getters and setters for accessing/modifying their values.
9. Write unit tests to validate the functionality of the code.
Example Code:
```java
public class Book {
private String title;
private String author;
private int publicationYear;
private boolean availability;
// Constructor
public Book(String title, String author, int publicationYear) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
this.availability = true;
}
// Getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublicationYear() {
return publicationYear;
}
public void setPublicationYear(int publicationYear) {
this.publicationYear = publicationYear;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
}
public class User {
private String name;
private int age;
private Book[] borrowedBooks;
// Constructor
public User(String name, int age, Book[] borrowedBooks) {
this.name = name;
this.age = age;
this.borrowedBooks = borrowedBooks;
}
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Book[] getBorrowedBooks() {
return borrowedBooks;
}
public void setBorrowedBooks(Book[] borrowedBooks) {
this.borrowedBooks = borrowedBooks;
}
}
public class Library {
private Book[] books;
private User[] users;
private int totalNumberOfBooks;
// Constructor
public Library(Book[] books, User[] users, int totalNumberOfBooks) {
this.books = books;
this.users = users;
this.totalNumberOfBooks = totalNumberOfBooks;
}
// Methods
public void addBook(Book book) {
// Add book to the library
}
public void removeBook(Book book) {
// Remove book from the library
}
public void displayAvailableBooks() {
// Display list of available books
}
public void displayBorrowedBooks() {
// Display list of borrowed books
}
public void borrowBook(Book book, User user) {
// Allows a user to borrow a book
}
public void returnBook(Book book, User user) {
// Allows a user to return a book
}
public void addUser(User user) {
// Add user to the library
}
public void removeUser(User user) {
// Remove user from the library
}
public void displayUsers() {
// Display list of users
}
}
public class FictionBook extends Book {
private String genre;
// Constructor
public FictionBook(String title, String author, int publicationYear, String genre) {
super(title, author, publicationYear);
this.genre = genre;
}
// Getter and setter for genre
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
}
public class NonFictionBook extends Book {
private String topic;
// Constructor
public NonFictionBook(String title, String author, int publicationYear, String topic) {
super(title, author, publicationYear);
this.topic = topic;
}
// Getter and setter for topic
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
}
// Unit tests
public class LibraryTest {
// Write unit tests to validate the functionality of the Library class
}
```
This solution uses the concepts of arrays (for managing books and users), loops (for iterating over arrays), inheritance (for creating FictionBook and NonFictionBook subclasses), constructors (for initializing objects), and information hiding (by making attributes private and providing getters and setters). The output is displayed in the console as a report. Unit tests are also written to validate the code.
You have to design your own Java console application for any valid problem that your application
must solve. Your solution can include solving a business problem, a new idea, or a game. Your
application must use concepts such as arrays, loops, inheritance, constructors, and information
hiding. Output must be shown in the form of a report using the console. You must also write unit
tests to validate that your code achieves the desired result.
In your solution, make use of as many concepts and components dealt with in this course, but pay
special attention to the following learning units:
• Learning Unit 1: Advanced arrays
• Learning Unit 2: Introduction to Inheritance
in java
1 answer