0% found this document useful (0 votes)
147 views

Object-Oriented Programming (Ccs0023) : College of Computer Studies

This document provides documentation for a library management system final project. It includes objectives, main menu options, figures demonstrating adding books, showing books, borrowing books, returning books, and exiting. It also includes source code for the project with classes for books, students, borrowed books, and handling the library functions.

Uploaded by

Kross Ford
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Object-Oriented Programming (Ccs0023) : College of Computer Studies

This document provides documentation for a library management system final project. It includes objectives, main menu options, figures demonstrating adding books, showing books, borrowing books, returning books, and exiting. It also includes source code for the project with classes for books, students, borrowed books, and handling the library functions.

Uploaded by

Kross Ford
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

College of Computer Studies

OBJECT-ORIENTED PROGRAMMING (CCS0023)

Final Project

Grade

Submitted by:

Submitted to:
OBJECTIVES

HELLO WORLD LIBRARY MANAGEMENT SYSTEM aims to manage the system of the
library in terms of adding, renting, etc. (see main menu for further details) This system also aims
to provide easy access materials and usage for the users. Through this system, the process will be
much easier.

Figure 1.1 MAIN MENU

Shows the following menu

1. ADD NEW/REGISTER STUDENT – this menu creates a new file where data is to be
stored.
2. BORROW BOOK – this menu let the user remove or rent an existing file.
3. RETURN BOOK – this menu will help the user to bring back the file that have been removed
or borrowed.
4. SHOW BOOK/SHOW STUDENTB – this menu will make the user enter of what existing
file should be shown.
5. SHOW ALL BOOK – this menu shows all the existing files that are stored with a correct
structure.
6. EXIT – this option will exit the application.

Figure 2.1 ADDING BOOK

Enter Choice: [0] –Add New Book—

The user who’ll add new book must only indicate the title of the book and the author because
the book code is already automated.

Figure 2.2 SHOWING ALL BOOKS

Enter Choice: [4] –Show All Books—

Displaying all the books that have been added. It is all sorted in the file.
Figure
3.1 SHOWING BOOK

Enter Choice: [3] –Show Book—

The user will enter one book code number to display the existing file and to know if it is available
or not.

Figure 3.1.1

Enter Choice: [3] –Show Book—

If the user entered a non-existing book code.

3.2 ENTERING STUDENT

Enter Choice: [5] –Register Student—

The user who’ll register should only enter the name because the student code is automated
just like the book code.

Figure 3.3 SHOWING STUDENT

Enter Choice: [6] –Show Student—

The user will enter the student code to see the name and to know if she already borrowed a book
or not.

3.3.1

Enter Choice: [6] Show Student—

If the user entered a non-existing student code.

4.1 BORROWING BOOK

Figure 4.1.1 RENTING A BOOK

Enter Choice: [1] –Borrow Book—


Figure


The user must indicate and enter the student code and book code to be able to borrow it.

Figure 4.1.2

Enter Choice: [1] Borrow Book—

Students are limited to borrow one book at a time.

4.2 SHOW BORROWED BOOK

Figure 4.2.1

Enter Choice: [4] –Show All Books—

This section displays all the book stored files. Book code number 3, on the other hand, is
already unavailable because it was rented by the student.

Figure 4.2.2

Enter Choice: [3] Show Book—

If the user entered the book code number 3 which is already rented, it will also show that the book
is not available.

Figure 4.3 SHOW STUDENT WHO RENTED

Enter Choice: [6] –Show Student—

If the user entered the student code who rented, it will show what book the student rented.

Figure 4.4 RETURNING A BOOK

Enter Choice: [2] –Return Book—

The user must indicate and enter the student code to be able to return the rented book.

5.1 SHOW RETURNED BOOK

Figure 5.1.1

Enter Choice: [4] –Show All Books—

This section displays all the stored files. The rented book code number 3 is available again
because the user returned it.
Figure 5.1.2

Enter Choice: [3] –Show Book—

If the user entered the rented book code number 3 which is already returned, it will also
show that the book is available again.

Figure 5.2 SHOW STUDENT WHO RENTED

Enter Choice: [6] –Show Student—

If the user entered the student code who rented which the book has already returned, it will
show that the student has no borrowed book.

Figure 5.3 EXITING THE PROGRAM

Enter Choice: [7] –EXIT—


SOURCE CODE
CODIO

Main.java
package pack;

import pack.Model.BookCollection; import


pack.Model.BorrowBookCollection;
import pack.Model.LibraryBookHandler;
import pack.Model.StudentCollection;
import pack.Tools.*;
import pack.View.*;

import java.io.FileWriter;

public class Main {

public static void main(String[] args) throws Exception {


var menu = new MainMenu("Library Management System");
var bookCollection = new BookCollection(); var
studentCollection = new StudentCollection(); var
borrowCollection = new BorrowBookCollection();

var studentFilename = "student-file.txt";


var bookFilename = "book-file.txt"; var
borrowBookFilename = "borrow-file.txt";

var libraryBookHandler = new LibraryBookHandler(bookCollection, studentCollection, borrowCollection);

var studentReaderStream = new StudentReaderStream(


new CloseableFileReader(studentFilename));

var bookReaderStream = new BookReaderStream(


new CloseableFileReader(bookFilename));

var borrowBookReaderStream = new BorrowBookReaderStream(


new CloseableFileReader(borrowBookFilename),
studentCollection, bookCollection);

var libraryFileHandler = new LibraryFileHandler(bookCollection, studentCollection, borrowCollection);


libraryFileHandler.read(studentReaderStream, bookReaderStream, borrowBookReaderStream);

menu.add(new AddBookMenuItem("Add new book"));


menu.add(new BorrowBookMenuItem("Borrow book"));
menu.add(new ReturnBookMenuItem("Return book"));
menu.add(new ShowBookMenuItem("Show book")); menu.add(new
ShowAllBooksMenuItem("Show All book")); menu.add(new
RegisterStudentMenuItem("Register Student")); menu.add(new
ShowStudentMenuItem("Show student"));

menu.render(libraryBookHandler);

var studentWriterStream = new StudentWriterStream(


new FileWriter(studentFilename));

var bookWriterStream = new BookWriterStream(


new FileWriter(bookFilename));

var borrowBookWriterStream = new BorrowBookWriterStream(


new FileWriter(borrowBookFilename));

libraryFileHandler.write(studentWriterStream, bookWriterStream, borrowBookWriterStream);


}
}

Package Model Book.java


package pack.Model;

public class Book extends CodeGenerator {


private int code; private
final String author; private
final String title; private
boolean available;
private static int instanceCounter = 0;

public Book(String author, String title) {


this.author = author; this.title = title;
this.available = true; code = -1;
}

public static void setInstanceCounter(int instanceCounter) {


Book.instanceCounter = instanceCounter;
}

public static int getTempCode(){


return instanceCounter;
}
BookCollection.java
package pack.Model;
import java.io.IOException;

public class BookCollection extends Collection<Book> {


@Override
public void add(Book book) throws IOException {
if(book == null)
throw new NullPointerException();
if(book.getCode() == -1)
throw new IOException("Uninitialize book code");

if(!super.list.contains(book))
super.list.add(book);
}

@Override public Book


get(int code){
return getValue(code);
}

@Override public Book


remove(int code) { var book
= getValue(code); if(book ==
null)
return null;

int index = list.indexOf(book);


return super.list.remove(index);
}

@Override protected Book


getValue(int code){ return
super.list.stream()
.filter(book -> book.getCode() == code)
.findAny()
.orElse(null);
}}
BookHandler.java
package pack.Model;

import java.io.IOException;

public interface BookHandler { void addBook(Book book) throws


Exception; int borrowBook(int book_code, int stud_code) throws
IOException;
int returnBorrowBook(int stud_code);
Book showBook(int code); Iterator<Book>
showAllBooks();
void registerStudent(Student student) throws IOException;
Student showStudent(int code);
BorrowBook showStudentBorrowBook(int code);
}
BorrowBook.java package
pack.Model; public class
BorrowBook{ private
Book book;
private final Student student;

public BorrowBook(Student student) {


this.student = student;
}

public Book getBook() {


return book;
}

public void setBook(Book book) {


this.book = book;
}

public Student getStudent() {


return student;
}
}

BorrowBookCollection.java
package pack.Model;

import java.io.IOException;

public class BorrowBookCollection extends Collection<BorrowBook> {

@Override
public void add(BorrowBook borrowBook) throws IOException {
if(borrowBook == null) throw new
NullPointerException();
if(borrowBook.getStudent().getCode() == -1)
throw new IOException("Uninitialize student code");

if(!super.list.contains(borrowBook))
super.list.add(borrowBook);
}

@Override public BorrowBook


get(int code){
return getValue(code);
}

@Override public BorrowBook


remove(int code) { var borrowBook
= getValue(code); if(borrowBook ==
null)
return null;

int index = list.indexOf(borrowBook);


return super.list.remove(index);
}

@Override
protected BorrowBook getValue(int code){
return super.list.stream()
.filter(borrowBook -> borrowBook.getStudent().getCode() == code)
.findAny()
.orElse(null);
}
}

CodeGenerator.java
package pack.Model;

public abstract class CodeGenerator{

public static void generate(CodeGenerator generator){


generator.doGenerate();
}

public static void setCode(CodeGenerator generator, int code){


generator.doSetCode(code);
}

protected abstract void doGenerate();


protected abstract void doSetCode(int code);
}

Collection.java
package pack.Model;

import java.io.IOException;
import java.util.ArrayList;

public abstract class Collection<T> {


protected ArrayList<T> list = new ArrayList<T>();

public abstract void add(T value) throws IOException;


public abstract T get(int code);
public abstract T remove(int code);
protected abstract T getValue(int code);
public int size(){
return list.size();
}

public Iterator<T> createIterator(){


if (list.isEmpty())
return null;
return new CollectionIterate<>(this);
}

private static class CollectionIterate<T> implements Iterator<T> {


private final Collection<T> collection;
private int counter = 0;

private CollectionIterate(Collection<T> collection) {


this.collection = collection;
}

@Override public boolean


hasNext() { return counter <
collection.list.size();
}

@Override
public T next() {
return collection.list.get(counter++);
}
}
}

Iterator.java
package pack.Model;

public interface Iterator<E> {


boolean hasNext();
E next();
}

LibraryBookHandler.java
package pack.Model;

import java.io.IOException;

public class LibraryBookHandler implements BookHandler {


private final BookCollection bookCollection; private final
StudentCollection studentCollection;
private final BorrowBookCollection borrowCollection;
public LibraryBookHandler(BookCollection bookCollection,
StudentCollection studentCollection,
BorrowBookCollection borrowCollection) {

this.bookCollection = bookCollection;
this.studentCollection = studentCollection;
this.borrowCollection = borrowCollection;
}

@Override public void addBook(Book book) throws


IOException { try {
bookCollection.add(book);
}catch (NullPointerException | IOException e){
e.printStackTrace(); throw e;
}
}

@Override
public int borrowBook(int book_code, int stud_code) throws IOException {
var student = studentCollection.get(stud_code);
if(student == null)
return 1;

var borrowBook = borrowCollection.get(stud_code);


if(borrowBook != null)
return 2;

var book = bookCollection.get(book_code);


if(book == null)
return 3;

if(!book.isAvailable())
return 4;

book.setAvailability(false);

var newBorrowBook = new BorrowBook(student);


newBorrowBook.setBook(book);
try
{
borrowCollection.add(newBorrowBook);
}catch (NullPointerException | IOException e){
throw e;
}
return 0;
}

@Override
public int returnBorrowBook(int stud_code) {
var borrowBook = borrowCollection.get(stud_code);
if(borrowBook == null)
return 1;

var book = borrowCollection.remove(stud_code);


book.getBook().setAvailability(true);
return 0;
}

@Override
public Book showBook(int code) {
return bookCollection.get(code);
}

@Override public Iterator<Book>


showAllBooks() {
return bookCollection.createIterator();
}

@Override
public void registerStudent(Student student) throws IOException {
try {
studentCollection.add(student); }catch
(NullPointerException | IOException e){ throw
e;
}
}

@Override public Student


showStudent(int code) {
return studentCollection.get(code);
}

@Override
public BorrowBook showStudentBorrowBook(int code) {
return borrowCollection.get(code);
}
}

Student.java
package pack.Model;

public class Student extends CodeGenerator{


private int code;
private final String name;
private static int instanceCounter = 0;

public Student(String name) {


code = -1;
this.name = name;
}

public static void setInstanceCounter(int instanceCounter) {


Student.instanceCounter = instanceCounter;
}

public int getCode() {


return code;
}

public String getName() {


return name;
}

public static int getTempCode(){


return instanceCounter;
}

@Override protected void


doGenerate() { code =
Student.instanceCounter;
Student.instanceCounter++;
}

@Override
protected void doSetCode(int code) {
this.code = code;
}

}
StudentCollection.java
package pack.Model;

import java.io.IOException;

public class StudentCollection extends Collection<Student> {

@Override
public void add(Student student) throws IOException {
if(student == null) throw new
NullPointerException(); if(student.getCode() == -1)
throw new IOException("Uninitialize student code");

if(!super.list.contains(student))
super.list.add(student);
}

@Override public Student


get(int code){
return getValue(code);
}

@Override public Student


remove(int code) { var student
= getValue(code); if(student ==
null)
return null;

int index = list.indexOf(student);


return super.list.remove(index);
}

@Override protected Student


getValue(int code){ return
super.list.stream()
.filter(student -> student.getCode() == code)
.findAny()
.orElse(null);
}
}

Package Tools
BookReaderStream.java
package pack.Tools;

import pack.Model.*;

import java.io.BufferedReader;
import java.io.IOException;

public class BookReaderStream extends ReaderStream<BookCollection>{


public BookReaderStream(CloseableFileReader reader) {
super(reader);
}

private void _read(BookCollection bookCollection){

try(var bfreader = new BufferedReader(this.getReader())) {


String temp;
System.out.print("BookReaderStream._read");
while ((temp = bfreader.readLine()) != null){
var str = temp.split(",");

var book = new Book(str[1], str[2]);


book.setAvailability(str[3].equals("1"));
CodeGenerator.setCode(book, Integer.parseInt(str[0]));
bookCollection.add(book);
}
System.out.println(":Success");
}catch (IOException e) {
e.printStackTrace();
}

Book.setInstanceCounter(bookCollection.size());
}

@Override
public void read(BookCollection bookCollection) {
_read(bookCollection);
}}
BookWriterStream.java
package pack.Tools;

import pack.Model.BookCollection;

import java.io.FileWriter;
import java.io.IOException;

public class BookWriterStream extends WriterStream<BookCollection>{


public BookWriterStream(FileWriter writer) {
super(writer);
}

private boolean _write(BookCollection value) throws IOException{


var it = value.createIterator(); var writer = this.getWriter();
if (it == null)
return false;

System.out.print("BookWriterStream._write");
while (it.hasNext()){

var book = it.next();


var isAvailable = book.isAvailable() ? 1 : 0;
writer.write(book.getCode() + "," + book.getTitle() + ","
+ book.getAuthor() + "," + isAvailable + "\n");
}
System.out.println(":Success");
writer.close();
return true;
}

@Override
public boolean write(BookCollection value) throws IOException {
return _write(value);
}
}
BorrowBookReaderStream.java
package pack.Tools;

import pack.Model.*;

import java.io.BufferedReader;
import java.io.IOException;

public class BorrowBookReaderStream extends ReaderStream<BorrowBookCollection> {


private final StudentCollection studentCollection; private final BookCollection
bookCollection;

public BorrowBookReaderStream(CloseableFileReader reader, StudentCollection studentCollection,


BookCollection bookCollection) { super(reader);
this.studentCollection = studentCollection;
this.bookCollection = bookCollection;
}

private void _read(BorrowBookCollection borrowBook){


try(var bfreader = new BufferedReader(this.getReader())) {
String temp;
System.out.print("BorrowBookReaderStream._read");
while ((temp = bfreader.readLine()) != null){
var str = temp.split(",");

int stud_code = Integer.parseInt(str[0]);


int book_code = Integer.parseInt(str[1]);
var borrow = new BorrowBook(studentCollection.get(stud_code));
borrow.setBook(bookCollection.get(book_code));

borrowBook.add(borrow);
}
System.out.println(":Success");
}catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void read(BorrowBookCollection value) {
_read(value);
}

BorrowBookWriterStream.java
package pack.Tools;

import pack.Model.BorrowBookCollection;
import java.io.FileWriter;
import java.io.IOException;

public class BorrowBookWriterStream extends WriterStream<BorrowBookCollection>{


public BorrowBookWriterStream(FileWriter writer) {
super(writer);
}

private boolean _write(BorrowBookCollection value) throws IOException {


var it = value.createIterator();
var writer = this.getWriter();
if (it == null)
return false;

System.out.print("BorrowBookWriterStream._write");
while (it.hasNext()){
var borrowBook = it.next();
writer.write(borrowBook.getStudent().getCode() + "," +
borrowBook.getBook().getCode()+"\n");
}
System.out.println(":Success");
writer.close();
return true;
}

@Override
public boolean write(BorrowBookCollection value) throws IOException {
return _write(value);
}
}

CloseableFileReader.java
package pack.Tools;

import java.io.*;
import java.nio.charset.Charset;

public class CloseableFileReader extends FileReader implements AutoCloseable {


public CloseableFileReader(String fileName) throws FileNotFoundException {
super(fileName);
}

public CloseableFileReader(File file) throws FileNotFoundException {


super(file);
}

public CloseableFileReader(FileDescriptor fd) {


super(fd);
}

public CloseableFileReader(String fileName, Charset charset) throws IOException {


super(fileName, charset);
}

public CloseableFileReader(File file, Charset charset) throws IOException {


super(file, charset);
}

@Override
public void close() throws IOException {
try{
super.close(); }
catch (IOException e){
throw e;
}
}
}

LibraryFileHandler.java
package pack.Tools;

import pack.Model.BookCollection; import


pack.Model.BorrowBookCollection;
import pack.Model.StudentCollection;

import java.io.IOException;

public class LibraryFileHandler { private final


BookCollection bookCollection; private final
StudentCollection studentCollection;
private final BorrowBookCollection borrowCollection;

public LibraryFileHandler(BookCollection bookCollection,


StudentCollection studentCollection,
BorrowBookCollection borrowCollection) {
this.bookCollection = bookCollection;
this.studentCollection = studentCollection;
this.borrowCollection = borrowCollection;
}

public void read(StudentReaderStream studentReaderStream,


BookReaderStream bookReaderStream,
BorrowBookReaderStream borrowBookReaderStream){

studentReaderStream.read(studentCollection);
bookReaderStream.read(bookCollection);
borrowBookReaderStream.read(borrowCollection);
}
public void write(StudentWriterStream studentWriterStream,
BookWriterStream bookWriterStream,
BorrowBookWriterStream borrowBookWriterStream) throws IOException {

studentWriterStream.write(studentCollection);
bookWriterStream.write(bookCollection);
borrowBookWriterStream.write(borrowCollection);
}
}

ReaderStream.java
package pack.Tools;

import java.io.FileWriter;

public abstract class ReaderStream<T> {


private final CloseableFileReader reader;

public ReaderStream(CloseableFileReader reader) {


if(reader == null)
throw new NullPointerException();
this.reader = reader;
}

public abstract void read(T value);

public CloseableFileReader getReader() {


return reader;
}
}

StudentReaderStream.java
package pack.Tools;

import pack.Model.CodeGenerator; import


pack.Model.Student;
import pack.Model.StudentCollection;

import java.io.BufferedReader;
import java.io.IOException;

public class StudentReaderStream extends ReaderStream<StudentCollection>{

public StudentReaderStream(CloseableFileReader reader) {


super(reader);
}

@Override
public void read(StudentCollection studentCollection) {
_read(studentCollection);
}

private void _read(StudentCollection studentCollection){

try(var bfreader = new BufferedReader(this.getReader())) {


String temp;
System.out.print("StudentReaderStream._read");
while ((temp = bfreader.readLine()) != null){
var str = temp.split(",");

var student = new Student(str[1]);


CodeGenerator.setCode(student, Integer.parseInt(str[0]));
studentCollection.add(student);
}
System.out.println(":Success");
}catch (IOException e) {
e.printStackTrace();
}

Student.setInstanceCounter(studentCollection.size());
}

StudentWriterStream.java
package pack.Tools;

import pack.Model.StudentCollection;

import java.io.FileWriter;
import java.io.IOException;

public class StudentWriterStream extends WriterStream<StudentCollection>{


public StudentWriterStream(FileWriter writer) {
super(writer);
}

@Override
public boolean write(StudentCollection value) throws IOException {
var it = value.createIterator();
if (it == null)
return false;
System.out.print("StudentWriterStream.write");
while (it.hasNext()){
var student = it.next();
this.getWriter().write(student.getCode() + "," + student.getName()+"\n");
}
this.getWriter().close();
System.out.println(":Success");
return true;
}
}
WriterStream.java package
pack.Tools;

import java.io.FileWriter;
import java.io.IOException;

public abstract class WriterStream<T>{


private final FileWriter writer;

protected WriterStream(FileWriter writer) {


this.writer = writer;
}

public abstract boolean write(T value) throws IOException;

public FileWriter getWriter() {


return writer;
}
}

Package View
AddBookMenuItem.java
package pack.View;

import pack.Model.Book; import


pack.Model.BookHandler;
import pack.Model.CodeGenerator;

import java.io.IOException;

public class AddBookMenuItem extends MenuItem{


public AddBookMenuItem(String title) {
super(title);
}
@Override public void render(BookHandler handler) throws
Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n");
System.out.println("\tBook Code : " + Book.getTempCode());
String title = Console.get_string("\tTitle : ");
String author = Console.get_string("\tAuthor : ");

var book = new Book(author, title);

CodeGenerator.generate(book);
handler.addBook(book);

System.out.println("\n\t<Operation Finished>");
Console.get_string("\tEnter <Any Key> to continue");
}
}

BorrowBookMenuItem.java
package pack.View;

import pack.Model.Book;
import pack.Model.BookHandler;

public class BorrowBookMenuItem extends MenuItem{


public BorrowBookMenuItem(String title) {
super(title);
}

@Override public void render(BookHandler handler) throws


Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n"); int stud_code =
Console.get_int("\tStudent Code: ",
"\n\t<Please enter a valid input>");

int book_code = Console.get_int("\tBook Code: ",


"\n\t<Please enter a valid input>");

String caption;
switch (handler.borrowBook(book_code, stud_code)) {
case 1 -> caption = "<Student Code not found>"; case 2 ->
caption = "<Only 1 Book to Borrow per Student>"; case 3 -
> caption = "<Book Code not found>";
case 4 -> caption = "<Book is unavailable>";
default -> {
System.out.println("\n\t<Operation Finished>");
Console.get_string("\tEnter <Any Key> to continue");
return;
}
}
System.out.println("\n\t"+caption);

Console.get_string("\n\tEnter <Any Key> to continue");


}
}
Console.java
package pack.View;

import java.util.Scanner;

public class Console {


private static final Scanner scan = new Scanner(System.in);

public static int get_int(String prompt, String error){

System.out.print(prompt);
while (!scan.hasNextInt()){
System.out.println(error);
System.out.print(prompt);
scan.nextLine();
}
int ret = scan.nextInt();
scan.nextLine();
return ret;
}

public static String get_string(String prompt){


System.out.print(prompt);
return scan.nextLine();
}

public static void clear(){


System.out.println("\n\n");
}
}

MainMenu.java
package pack.View;

import pack.Model.BookHandler;

import java.io.IOException;

public class MainMenu extends Menu{


public MainMenu(String title) {
super(title);
}

@Override
public void render(BookHandler handler) throws Exception {
while (true){
System.out.println("\n\n\t\t" +this.getTitle().toUpperCase() + "\n");
System.out.println(this);

int input;
while (true){
input = Console.get_int("\n\tEnter choice: ",
"\n\t<Please enter a valid input>"); if(input >= 0 &&
input <= getItemList().size()) break;

System.out.println("\n\t<Please enter a valid input>");


}

if(input == getItemList().size()){
System.out.println("\n");
return;
}

Console.clear();
getItemList().get(input).render(handler);
Console.clear();
}

@Override public String toString() { var


str = new StringBuilder(); for (int i = 0; i <
this.getItemList().size(); i++) {
str.append("\t[" + i + "]");
str.append("\t" + this.getItemList().get(i).getTitle());
str.append("\n");
}
str.append("\t[" + this.getItemList().size() + "]");
str.append("\tExit\n");
return str.toString();
}
}

Menu.java
package pack.View;

import pack.Model.BookHandler;

import java.util.ArrayList;
import java.util.List;

public abstract class Menu {


private final List<MenuItem> itemList = new ArrayList<>();
private final String title;

public Menu(String title) {


this.title = title;
}

public Menu add(MenuItem item){


if(item != null)
itemList.add(item);
return this;
}

public abstract void render(BookHandler handler) throws Exception;

public String getTitle() {


return title;
}

public List<MenuItem> getItemList() {


return itemList;
}
}

MenuItem.java
package pack.View;

public abstract class MenuItem extends Menu{


public MenuItem(String title) {
super(title);
}}
RegisterStudentMenuItem.java
package pack.View;

import pack.Model.Book; import


pack.Model.BookHandler; import
pack.Model.CodeGenerator;
import pack.Model.Student;

public class RegisterStudentMenuItem extends MenuItem{


public RegisterStudentMenuItem(String title) {
super(title);
}

@Override public void render(BookHandler handler) throws


Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n");
System.out.println("\tStudent Code : " + Student.getTempCode());
String name = Console.get_string("\tName : ");
var student = new Student(name);
CodeGenerator.generate(student);
handler.registerStudent(student);

System.out.println("\n\t<Operation Finished>");
Console.get_string("\tEnter <Any Key> to continue");
}
}

ReturnBookMenuItem.java
package pack.View;

import pack.Model.BookHandler;

public class ReturnBookMenuItem extends MenuItem{


public ReturnBookMenuItem(String title) {
super(title);
}

@Override public void render(BookHandler handler) throws


Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n"); int stud_code =
Console.get_int("\tStudent Code: ",
"\n\t<Please enter a valid input>");

if (handler.returnBorrowBook(stud_code) == 1)
System.out.println("\n\t<No book to be return>"); else
System.out.println("\n\t<Operation Finished>");

Console.get_string("\n\tEnter <Any Key> to continue");


}}
ShowAllBooksMenuItem.java
package pack.View;

import pack.Model.BookHandler;

public class ShowAllBooksMenuItem extends MenuItem{


public ShowAllBooksMenuItem(String title) {
super(title);
}

@Override public void render(BookHandler handler) throws


Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n"); var it =
handler.showAllBooks(); if(it == null)
System.out.println("\n\t<Book list is empty>");
else{
while (it.hasNext()){
var book = it.next();
var caption = book.isAvailable() ? "Available" : "Unavailable";
System.out.println("\t" + book.getCode() + "\t" + book.getTitle() +
"\t" + book.getAuthor() + "\t" + caption);
}
}

Console.get_string("\n\tEnter <Any Key> to continue");


}
}

ShowBookMenuItem.java
package pack.View;

import pack.Model.BookHandler;

public class ShowBookMenuItem extends MenuItem{


public ShowBookMenuItem(String title) {
super(title);
}

@Override public void render(BookHandler handler) throws


Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n"); int book_code =
Console.get_int("\tBook Code: ",
"\n\t<Please enter a valid input>");

var book = handler.showBook(book_code);


if(book == null)
System.out.println("\n\t<Book Code not found>");
else{
var caption = book.isAvailable() ? "Available" : "Unavailable";
System.out.println("\t" + book.getTitle() + "\t" + book.getAuthor() +
"\t" + caption);

System.out.println("\n\t<Operation Finished>");
}
Console.get_string("\n\tEnter <Any Key> to continue");
}
}

ShowStudentMenuItem.java
package pack.View;

import pack.Model.BookHandler;

public class ShowStudentMenuItem extends MenuItem{


public ShowStudentMenuItem(String title) {
super(title);
}
@Override public void render(BookHandler handler) throws
Exception { System.out.println("\t\t" +
this.getTitle().toUpperCase()+"\n"); int stud_code =
Console.get_int("\tStudent Code: ",
"\n\t<Please enter a valid input>");

var student = handler.showStudent(stud_code);


if(student == null)
System.out.println("\n\t<Student Code not found>");
else{
System.out.println("\t" + student.getName());

var borrow = handler.showStudentBorrowBook(stud_code);


if(borrow == null)
System.out.println("\t<No borrowed book>");
else
System.out.println("\t" + borrow.getBook().getCode() +
"\t" + borrow.getBook().getTitle() +
"\t" + borrow.getBook().getAuthor());

System.out.println("\n\t<Operation Finished>");
}
Console.get_string("\n\tEnter <Any Key> to continue");
}
}
SUMMARY

CLASS DIAGRAM

You might also like