Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Programming Assignment Unit 2

The document outlines a programming assignment for creating a terminal-based Library Management System using Java, which allows users to add, borrow, and return books. It includes a main program structure with a do-while loop for user interaction, as well as functions for adding, borrowing, and returning books, along with a Book class to represent book attributes. The assignment also references relevant Java documentation for further understanding of the concepts used.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Programming Assignment Unit 2

The document outlines a programming assignment for creating a terminal-based Library Management System using Java, which allows users to add, borrow, and return books. It includes a main program structure with a do-while loop for user interaction, as well as functions for adding, borrowing, and returning books, along with a Book class to represent book attributes. The assignment also references relevant Java documentation for further understanding of the concepts used.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Assignment Unit 2: Library Management System

Department of Computer Science, University of People

CS 1102-01 Programming 1– AY2024-t4

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.

References of these will be provided on the reference page.


Main Program:

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;

public class LibrarySystem {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

HashMap<String, Book> library = new HashMap<>();

int choice;
do {

System.out.println("\nLibrary Management System");

System.out.println("1. Add Books");

System.out.println("2. Borrow Books");

System.out.println("3. Return Books");

System.out.println("4. Exit");

System.out.print("Enter your choice: ");

choice = scanner.nextInt();

scanner.nextLine(); // Consume newline character

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:

System.out.println("Invalid choice. Please try again.");

} while (choice != 4);

scanner.close();

Function for Adding Books:

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:

private static void addBooks(HashMap<String, Book> library, Scanner scanner) {

System.out.print("Enter book title: ");

String title = scanner.nextLine();


System.out.print("Enter author: ");

String author = scanner.nextLine();

System.out.print("Enter quantity: ");

int quantity = scanner.nextInt();

Book book = library.get(title);

if (book != null) {

book.setQuantity(book.getQuantity() + quantity);

System.out.println(quantity + " copies of " + title + " added successfully.");

} else {

library.put(title, new Book(title, author, quantity));

System.out.println(title + " by " + author + " added to the library.");

Function for Borrow Books:

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

understanding please read the code below.

Code:
private static void borrowBooks(HashMap<String, Book> library, Scanner scanner) {

System.out.print("Enter book title: ");

String title = scanner.nextLine();

System.out.print("Enter number of books to borrow: ");

int quantity = scanner.nextInt();

Book book = library.get(title);

if (book != null && book.getQuantity() >= quantity) {

book.setQuantity(book.getQuantity() - quantity);

System.out.println(quantity + " copies of " + title + " borrowed successfully.");

} else {

if (book == null) {

System.out.println("Book not found in the library.");

} else {

System.out.println("Insufficient copies of " + title + " available.");

Function for Return Books:

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:

private static void returnBooks(HashMap<String, Book> library, Scanner scanner) {

System.out.print("Enter book title: ");

String title = scanner.nextLine();

System.out.print("Enter number of books to return: ");

int quantity = scanner.nextInt();

Book book = library.get(title);

if (book != null) {

book.setQuantity(book.getQuantity() + quantity);

System.out.println(quantity + " copies of " + title + " returned successfully.");

} else {

System.out.println("Book does not belong to the library system.");

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

better understanding please read the code below.

Code:

class Book {

private String title;

private String author;

private int quantity;

public Book(String title, String author, int quantity) {

this.title = title;

this.author = author;

this.quantity = quantity;

public String getTitle() {

return title;

public String getAuthor() {

return author;

public int getQuantity() {

return quantity;

public void setQuantity(int 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

Javanotes 9, Section 3.4 -- the for statement. (n.d.-b).

https://math.hws.edu/javanotes/c3/s4.html

Javanotes 9, Section 3.3 -- The while and do..while Statements. (n.d.-b).

https://math.hws.edu/javanotes/c3/s3.html

Java HashMap. (n.d.).

https://www.w3schools.com/java/java_hashmap.asp

You might also like