Programming Assignment Unit 2
Programming Assignment Unit 2
26 April, 2024
Programming Assignment Unit 2: Library Management System
In this assignment I am going to create a simple library management system which will
be terminal based and you can add books in it borrow book, return book and exit the program. I
am going to use to built-in function of java for this one HashMap and another will be Scanner.
First of all I am going to import two packages HashMap and Scanner on top of my
program. After that I created a class named LibrarySystem, in this class all my code and logic
will be written. After that I created main method / function. First I defined Scanner object name
scanner (to get input from the user) after that I am creating a Hashmap name library in which my
HashMap will be stored (HashMap is a data type. It similar to dictionaries in python, both stores
key and value pairs). After this I am creating a do while loop which will ask from user three
question 1. Add Book, 2 Borrow Books, and 3 Return Books and last 4 Exit. User have to enter a
number (you cannot type Add Books to add a book you have to type 1 to do it). After that I am
using switch statement to handle cases check the user have given right input or not if user have
given right input it will break the loop, and call the co responding function, otherwise it will keep
repeating the loop. For better understanding please read the code below.
Code:
import java.util.HashMap;
import java.util.Scanner;
int choice;
do {
System.out.println("4. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBooks(library, scanner);
break;
case 2:
borrowBooks(library, scanner);
break;
case 3:
returnBooks(library, scanner);
break;
case 4:
System.out.println("Exiting Library System.");
break;
default:
scanner.close();
Now I am going to create function to which are going to be called when user have given
the right input to the program. It will takes the library, HashMap and scanner as aruguments.
First I am going to write addBooks function this will ask three question from user one will title
of the book, second will be author of the book and lastly quantity of the books available I am
also going to add an if condition in this to check that have given the values or not, After checking
it, it will create a new object book and store it in the HashMap. If a book exists, it will update the
quantity of the book and if it does not exists it will create a new book object and add that into the
Hashmap library. For better understanding please read the code below.
Code:
if (book != null) {
book.setQuantity(book.getQuantity() + quantity);
} else {
Now I am going to write another function which will used for borrowing books from
library. It will takes the library HashMap and scanner as aruguments. It will ask the title the book
and the quantity of the book which user wants to borrow from our library. It will retrieve the
book object from the library using library.get(title) and update the quantity of the book by
subtracting it from the books stored in the HashMap. If a book does not exists and has enough
copies it gives error message, and if the book is in HashMap it prints success message. For better
Code:
private static void borrowBooks(HashMap<String, Book> library, Scanner scanner) {
book.setQuantity(book.getQuantity() - quantity);
} else {
if (book == null) {
} else {
This will be the last function of this program. It will take library HashMap and scanner as
arguments. It will ask for book title and the number of books which are going to return, then it
will retrieve the book object from the library using library.get(title). After that it will check the
if-else statement to see if the book exists in the library or not. If the book exists it will update the
quantity and return the quantity of the books, and if not then it prints an error.
Code:
if (book != null) {
book.setQuantity(book.getQuantity() + quantity);
} else {
Book Class:
Lastly I am going to write a class name book this class represents a book in the library
and it have all the attributes related to it, for example title author and quantity. I am going to use
all the methods in this class to get the set attributes title, author and quantity in this class. For
Code:
class Book {
this.title = title;
this.author = author;
this.quantity = quantity;
return title;
return author;
return quantity;
this.quantity = quantity;
}
Output:
I am going to run this program and show you the result of this program. Please take a
look at the figure 1.1 below to see the result of this program.
Figure 1.1
As you can see in the figure 1.1 the program functions correctly. There is no error in it.
References
https://math.hws.edu/javanotes/c3/s4.html
https://math.hws.edu/javanotes/c3/s3.html
https://www.w3schools.com/java/java_hashmap.asp