"I want to create a full-fledged application to practice Java."
"I want to learn how to manage and process data, but I don't know where to start."
For those who have such concerns,Creating a library management systemis perfect.
This system allows you to register books, display lists, manage loans, and more.
Array, list, class design, conditional branching, repetitive processing, etc.You can learn comprehensively.
In this article,A simple and practical library system that can be built using only basic JavaWe will explain how to make it in detail, with code included.
What is a Library Management System?
This is an example of a system that can help you learn.
Conclusion: You can learn all the basics of Java by registering, searching, and borrowing books.
As you continue to learn Java, many of you will feel like you want to take on more practical challenges.
This is perfect for such people.Creating a library management systemis.
Through this system you will learn the following:
- Object thinking (class design)
- Data management using arrays and lists
- Combination of conditional branching and repetition
- Separation of multiple methods and call flow
In fact, the Ministry of Education, Culture, Sports, Science and Technology's "Survey on the Actual Situation of Information Education" also stated thatAcquire skills in data management and information utilizationis considered important (Source: https://www.mext.go.jp/b_menu/houdou/mext_00034.html).
Organize the entire system configuration
It's easier to understand if you separate the functions
Conclusion: If you create the following functions individually, the whole picture will become clear.
- Book registration (adding a new book to the system)
- List display (output all books)
- Loan process (change loan status)
- Return process (return)
The class design is as follows:
Book
Class: Manage title, author, and loan statusLibrary System
Class: Holds a list of books and accepts menu operations
Defining the Book class
This class handles book information and status.
1 | public class Book { private String title; private String author; private boolean isBorrowed; public Book(String title, String author) { this .title = title; this .author = author; this .isBorrowed = false ; } public String getTitle() { return title; } public String getAuthor() { return author; } public boolean isBorrowed() { return isBorrowed; } public void borrow() { this .isBorrowed = true ; } public void returnBook() { this .isBorrowed = false ; } public String toString() { return title + " / " + author + " / " + (isBorrowed ? "Borrowed" : "In stock" ); } } |
Creating the main process and menu
Separate the processing by allowing you to select the operation
1 | import java.util.*; public class LibrarySystem { private static List books = new ArrayList<>(); private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { while ( true ) { System.out.println( "\n=== Library Menu ===" ); System.out.println( "1. Add a book" ); System.out.println( "2. Display list" ); System.out.println( "3. Borrow a book" ); System.out.println( "4. Return a book" ); System.out.println( "0. Exit" ); System.out.print( "Choose a number:" ); int choice = scanner.nextInt(); scanner.nextLine(); // Remove newline switch (choice) { case 1: addBook(); break; case 2: listBooks(); break; case 3: borrowBook(); break; case 4: returnBook(); break; case 0: System.out.println("Exit."); return; default: System.out.println("Invalid input."); } } } private static void addBook() { System.out.print("Title:"); String title = scanner.nextLine(); System.out.print("Author:"); String author = scanner.nextLine(); books.add(new Book(title, author)); System.out.println("Book has been added."); } private static void listBooks() { if (books.isEmpty()) { System.out.println("No books added."); } else { for (int i = 0; i < books.size(); i++) { System.out.println((i + 1) + ". " + books.get(i)); } } } private static void borrowBook() { listBooks(); System.out.print("Book number to borrow:"); int index = scanner.nextInt() - 1; scanner.nextLine(); if (index >= 0 && index < books.size()) { Book book = books.get(index); if (book.isBorrowed()) { System.out.println("This book is already borrowed."); } else { book.borrow(); System.out.println("The borrowing has been processed."); } } else { System.out.println("Invalid number."); } } private static void returnBook() { listBooks(); System.out.print("Book number to return:"); int index = scanner.nextInt() - 1; scanner.nextLine(); if (index >= 0 && index < books.size()) { Book book = books.get(index); if (!book.isBorrowed()) { System.out.println("This book is already in stock."); } else { book.returnBook(); System.out.println("The return has been processed."); } } else { System.out.println("Invalid number"); } } } |
Common mistakes and how to fix them
Check the cause of the error and how to solve it
Conclusion: Array ranges, null checks, and input checks are important.
- When choosing a number
0
The beginning and1
Note the difference in the beginning scanner.nextLine()
When using- Don't forget to take measures to display the list when it's empty
Application: The next function you want to try
Evolve into a more professional system
Extension ideas:
- Date and time stamped loan history (
LocalDateTime
Introduction of - Saving and loading to a CSV file
- Transition to GUI version (
Swing
orJavaFX
(Screen construction with
Completed code summary
Book.java
: A class that manages book information and statusLibrarySystem.java
: Manage operations such as registration, lending, and returning
Summary: What I learned from the Library Management System
In this article,A simple library management system using JavaWe explained about the following:
What I learned:
- Class and method split design
- List and input processing flow
- Practical concepts of conditional branching and exception handling