Share a library management system java actual combat project, it is recommended to do java development have a look!

Bright future 2021-01-21 13:39:31
share library management java actual


Preface

The core requirements of the project :

Simple login

The administrator side implements the following functions

① Find books
② Add books
③ Delete books
④ Show all the books
⑤ Exit the system

The general user realizes the following functions

① Query books
② Borrow books
③ Return the books
④ Exit the system

Design exhibition of project class

Books related classes

Book: Defining information about books BookList: Represents a library , There are books in it

package book;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:54
*/
public class Book {
// Define member variables
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed = false; // The state of borrowing a book
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
// this.isBorrowed = isBorrowed;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getPrice() {
return price;
}
public String getType() {
return type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(int price) {
this.price = price;
}
public void setType(String type) {
this.type = type;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
// @Override
// public String toString() {
// return "Book{" +
// "name='" + name + '\'' +
// ", author='" + author + '\'' +
// ", price=" + price +
// ", type='" + type + '\'' +
// ", isBorrowed=" + isBorrowed +
// '}';
// }
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true) ? " Borrowing state : Lent out " : " Borrowing state : Not lent ") +
'}';
}
}
package book;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:54
*/
public class BookList {
public int usedSize = 3; // bookshelf
public Book[] books = new Book[10]; // The type of book is Book, Using a sequential array book De storage
public BookList() {
books[0] = new Book(" The romance of The Three Kingdoms "," Luo Guanzhong ", 100, " A novel ");
books[1] = new Book(" Water margin ", " Shi Naian ", 100, " A novel ");
books[2] = new Book(" Journey to the west ", " Wu chengen ", 100, " A novel ");
}
// Put books in designated places
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
// Get the book at the designated location
public Book getBooks(int pos){
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}

Yes, the library ( The order sheet ) Operation of class

// newly added
public class AddOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Add books ");
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the title of the book ");
String name = sc.nextLine();
System.out.println(" Please enter the author of the book ");
String author = sc.nextLine();
System.out.println(" Please enter the price of the book ");
int price = sc.nextInt();
System.out.println(" Please enter the type of book ");
String type = sc.next();
Book newBook = new Book(name,author,price,type); // Building new books ( object )
int size = booklist.getUsedSize(); // adopt bookList Reference access to the current sequence table length
booklist.setBooks(size,newBook); // Put the new book at the end of the list
booklist.setUsedSize(size+1); // After the new book in the order table , The length of the add 1
}
}
// Borrow
public class BorrowOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Borrow books ");
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the title of the book ");
String name = sc.nextLine(); //name I want to borrow the title of the book
for (int i = 0; i < booklist.getUsedSize(); i++) {
// adopt booklist Subscript traverses every book
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
// If true, Explain the existence of the book to be borrowed , What we need to do is modify the borrowing status of the book
book.setBorrowed(true); // by true The book has come out
return ;
}
}
System.out.println(" So sorry , There is no book you want to borrow in our library !"); //
}
}
// Delete
public class DelOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Delete books ");
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the title of the book ");
String name = sc.nextLine(); //name For the title of the book to be deleted
int i = 0;
for (; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
break;
}
}
if(i >= booklist.getUsedSize()){
System.out.println(" There is no book to delete !");
return ;
}
// here i Subscript the book you want to delete
for (int j = i; j < booklist.getUsedSize()-1; j++) {
Book book = booklist.getBooks(j+1); // get j+1 The book of location
booklist.setBooks(j,book); // take j+1 Location book for j Location
}
int size = booklist.getUsedSize(); // Get the length of the sequence table
booklist.setUsedSize(size-1); // After deleting the book , Length minus 1
System.out.println(" The book has been deleted !");
}
}
// Exhibition
public class DisplayOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Show books ");
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBooks(i);
System.out.println(book);
}
}
}
// lookup
public class FindOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Find books ");
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the title of the book ");
String name = sc.nextLine(); //name Search for the title of the book
for (int i = 0; i < booklist.getUsedSize(); i++) {
// adopt booklist Subscript traverses every book
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
System.out.println(" The book exists !");
System.out.println(book); // Print book information directly ,toString Method has been overridden
return ;
}
}
System.out.println(" There is no book !");
}
}
// The return
public class ReturnOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Return the books ");
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the title of the book ");
String name = sc.nextLine(); //name For the title of the book to be returned
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBooks(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println(book); // Print book information directly ,toString Method has been overridden
return ;
}
}
System.out.println(" There is no book you want to return !");
}
}
// sign out
public class ExitOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println(" Exit the system ");
System.exit(1); // Exit the system
}
}
// Main interface
public interface IOperation {
void work(BookList booklist);
}

User related classes

<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;"> User class </mark>

package user;
import book.BookList;
import operation.IOperation;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:55
*/
abstract public class User {
public String name;
public IOperation[] operations;
public User(String name){
this.name = name;
}
/*
newly build menu Method , User menu
because SpecialPerson and OrdinaryPerson Inherited User, So let two subclasses override menu Method , The menu display of the two is inconsistent
here menu There can be no concrete implementation , So it's designed as an abstract method , therefore User Classes become abstract classes
*/
abstract public int menu();
//operations What operation methods are stored in , It depends on the subclasses
public void doOperation(int choice, BookList bookList){
this.operations[choice].work(bookList);
}
}

<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;"> Administrators </mark>

package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:56
* Administrators
*/
public class SpecialPerson extends User{
// Construction method
public SpecialPerson(String name){
super(name);
this.operations = new IOperation[]{
new ExitOperation(), //0 Exit the system
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
/*
Override parent class User Of menu Method
menu effect : Return to what you want to do
*/
@Override
public int menu() {
System.out.println(" Administrator menu !");
System.out.println("=================================");
System.out.println("hello " + this.name + " Welcome to our library management system !");
System.out.println("1. Look for books ");
System.out.println("2. New books ");
System.out.println("3. Delete books ");
System.out.println("4. Show all books ");
System.out.println("0. Exit the system ");
System.out.println("================================");
System.out.println(" Please follow the prompts to select the appropriate operation : ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}

<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;"> Ordinary users </mark>

package user;
import operation.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:57
* Ordinary users
*/
public class OrdinaryPerson extends User{
// Construction method
public OrdinaryPerson(String name){
super(name);
this.operations = new IOperation[]{
new ExitOperation(), //0 Exit the system
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
// Override parent class User Of menu Method
@Override
public int menu() {
System.out.println(" General user menu !");
System.out.println("=================================");
System.out.println("hello" + this.name + " Welcome to our library management system !");
System.out.println("1. Look for books ");
System.out.println("2. Borrow books ");
System.out.println("3. Return books ");
System.out.println("0. Exit the system ");
System.out.println("================================");
System.out.println(" Please follow the prompts to select the appropriate operation : ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}

Main function program

import book.BookList;
import user.OrdinaryPerson;
import user.SpecialPerson;
import user.User;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA
* Details about unstoppable_t:
* User: Administrator
* Date: 2021 -01-19
* Time: 16:30
*/
public class TrqTest {
//login( Sign in ) Method returns the user instance , The type is User
public static User login(){
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter your name : ");
String name = sc.nextLine();
System.out.println(" Please enter your identity > 1: Administrators 0: Ordinary users ");
int number = sc.nextInt();
if(number == 1){
return new SpecialPerson(name);
}else{
return new OrdinaryPerson(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
// Sign in
User user = login(); // The parent class reference points to the child class instance ( Upward transformation )
while(true) {
//user.menu() Call which menu Method , According to the login identity
int choice = user.menu();
// this doOperation The method is currently user Own way
//choice Select the current user operations The corresponding class in the array , The object generated by this class calls its own work Method
user.doOperation(choice, bookList);
}
}
}

Let's run it and see how it works
 Insert picture description here


Thank you for seeing here , What's wrong with the article , If you think the article is helpful to you, please give me a compliment , Share every day java Related technical articles or industry information , Welcome to follow and forward the article !
Welcome to the official account : Bright future , Receive the first tier factory Java Interview question summary + Learning and thinking guidance of each knowledge point + One copy 300 page pdf Document Java Summary of core knowledge points !

版权声明
本文为[Bright future]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121133802508W.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云