当前位置:网站首页>Library borrowing system "suggested collection"
Library borrowing system "suggested collection"
2022-07-28 21:28:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Library borrowing system One 、 Library borrowing system is an information management system for the operation of book information . The book information includes the title 、 author 、 Number 、 Whether to lend . This system is aimed at two types of users ( Librarians and students ) Use , For librarians , You can view the list of books 、 Find books 、 New book information 、 Delete books 、 Exit the login operation ; For students , You can view the list of books 、 Find books 、 Borrow books 、 Return the books 、 Exit the login operation .
Two 、 Code section 1.book Package created Book The establishment of class is important for library borrowing system , Regard books as an object , According to the book The attributes of , Define the private properties of a series of books , And accessors that operate on these properties (get()) And setters (set()).
package book;
public class Book {
private String name;// Title
private String author;// The author of the book
private int num;// The number of the book
private boolean flag;// Judge whether the book has been borrowed
public Book(String name, String author, int num) {
this.name = name;
this.author = author;
this.num = num;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getNum() {
return num;
}
public void setFlag(boolean borrowed) {
flag = borrowed;
}
public boolean getFlag() {
return flag;
}
public String toString() {
return "name:" + name + " author:" + author + " num:" + num +" "+ (flag ? " It has been lent out " : " Not lent ");
}
}Booklist Class establishment Booklist Class is used to store book information , And aiming at Booklist Class to complete addition, deletion, modification and other related operations .
package book;
public class Booklist {
public Book books[];
private int number;
public Booklist() {
this.books = new Book[20];
this.books[0] = new Book("Java Basic language course ", " Li Dongming 、 Zhang Lijuan ", 1);
this.books[1] = new Book("C Programming ", " Tan Haoqiang ", 2); this.books[2] = new Book(" Advanced mathematics I", " Tongji ", 3); this.books[3] = new Book(" Advanced mathematics II", " Tongji ", 4); this.books[4] = new Book(" data structure ", " YanWeiMin ", 5); this.number = 5;
}
public void setBook(int b, Book book) {
books[b] = book;
}
public Book getBook(int b) {
return books[b]; } public void setNumber(int n) {
number = n; }
public int getNumber( ) {
return number; }
}2.operation Package created Operation Class establishment ① This is all operation ( lookup , increase , Delete , Borrow , The return , sign out ) Common interface ;② Need one oper() Method , This method is aimed at Booklist Class to operate on .
package operation;
import book.Booklist;// Interface
public interface Operation {
void oper(Booklist booklist);
}Find book information ( According to the book title or number ) Select the corresponding search function , According to your input name perhaps number Search for .
package operation;
import java.util.Scanner;
import book.Booklist;// Find books
public class Find implements Operation {
public void oper(Booklist booklist) {
int i;
Scanner reader = new Scanner(System.in); System.out.println(" Please enter the title of the book you are looking for :"); String name = reader.next();
for (i = 0; i < booklist.getNumber(); i++) {
//System.out.println(booklist.getBook(i).getName().equals(name));
if (booklist.getBook(i).getName().equals(name)) {
System.out.println(" Find the book you need :"+booklist.books[i]);
return;
}
}
if (i >= booklist.getNumber()) {
System.out.println(" I'm sorry ! The book you need is not found !");
}
}
}package operation;
import java.util.Scanner;
import book.Booklist; Find books by number
public class Find1 implements Operation{
public void oper(Booklist booklist) {
int i;
Scanner reader = new Scanner(System.in);
System.out.println(" Please enter the number of the book you are looking for :");
int number = reader.nextInt();
for (i = 0; i < booklist.getNumber(); i++) {
//System.out.println(booklist.getBook(i).getNum()==number);
if (booklist.getBook(i).getNum()==number){
System.out.println(" Find the book you need :"+booklist.books[i]);
return;
}
}
if (i >= booklist.getNumber()) {
System.out.println(" I'm sorry ! The book you need is not found !"); }
}
}Add books Input the information of the new book in the main function interface , Assign these information to Book Class corresponding information , And insert the book Booklist Class books Array .
package operation;import java.util.Scanner;import book.Book;import book.Booklist;// Add books
public class Add implements Operation {
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in); System.out.println(" Please enter the books to be added :");
String name = reader.nextLine(); System.out.println(" Please enter the author of the book :");
String author = reader.nextLine(); System.out.println(" Please enter the book number :");
int num = reader.nextInt();
Book book = new Book(name, author, num);
int a = booklist.getNumber();
booklist.setBook(a, book);
booklist.setNumber(a + 1);
System.out.println(" Success increases "+name+" Books !");
}
}Delete books ( According to the book title or number )
package operation;import java.util.Scanner;import book.Book;import book.Booklist;// Delete books
public class Delete implements Operation {
public void oper(Booklist booklist) {
System.out.println(" Please enter the books you need to delete :"); Scanner reader = new Scanner(System.in);
String name = reader.nextLine();
int de = booklist.getNumber();
int b = 0;
for (int i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
b = i;
break;
}
}
for (int j = b; j < de - 1; j++) {
booklist.setBook(j, booklist.getBook(j + 1));
}
booklist.setNumber(de - 1); System.out.println(" Successfully deleted the book !");
}
}package operation;import java.util.Scanner;import book.Book;import book.Booklist;// Delete books by number
public class Delete1 implements Operation{
public void oper(Booklist booklist) {
System.out.println(" Please enter the books you need to delete :"); Scanner reader = new Scanner(System.in);
int number = reader.nextInt();
int de = booklist.getNumber();
int i;
for (i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getNum()==number) {
break;
}
}
if(i>booklist.getNumber()) {
return ;
}
Book book1=booklist.getBook(de-1);
booklist.setBook(i, book1);
booklist.setNumber(de-1);
System.out.println(" Successfully deleted the book !");
}
}Borrow books ( According to the book title or number )
package operation;import java.util.Scanner;import book.Book;import book.Booklist;// Borrow books
public class Borrow implements Operation {
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in); System.out.println(" Please enter the books you need to borrow :"); String name = reader.next();
int i;
for (i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (booklist.getBook(i).getName().equals(name)) {
if (book.getFlag()) {
System.out.println(" I'm sorry ! This book "+book.getName()+" Has been borrowed !");
}
else {
book.setFlag(true); System.out.println(" Successfully borrow books "+book.getName()+"!");
break;
}
}
} }}package operation;import java.util.Scanner;import book.Book;import book.Booklist;
public class Borrow1 implements Operation{
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in); System.out.println(" Please enter the number of the book you need to borrow :"); int number = reader.nextInt();
int i;
for (i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getNum()==number) {
if (book.getFlag()) {
System.out.println(" I'm sorry ! This book "+book.getName()+" Has been borrowed !");
}
else {
book.setFlag(true);
System.out.println(" Successfully borrow books "+book.getName()+"!");
break;
} } } }}Return the books ( According to the book title or number )
package operation;import java.util.Scanner;import book.Book;import book.Booklist;// Return the books
public class Return implements Operation {
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in);
System.out.println(" Please enter the books you need to return :");
String name = reader.nextLine();
for (int i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
System.out.println(" Successful return of books !");
}
book.setFlag(false); } }}package operation;import java.util.Scanner;import book.Book;import book.Booklist;
public class Return1 implements Operation{
public void oper(Booklist booklist) {
Scanner reader = new Scanner(System.in);
System.out.println(" Please enter the books you need to return :");
int number = reader.nextInt();
for (int i = 0; i < booklist.getNumber(); i++) {
Book book = booklist.getBook(i);
if (book.getNum()==number) {
System.out.println(" Successful return of books !");
}
book.setFlag(false); } }}Exit the system
package operation;import book.Booklist;
public class Exit implements Operation {
public void oper(Booklist booklist) {
System.out.println(" Thank you for using the library system !"); System.exit(0);// Run the program normally and exit the program
}}3.user Package created This system is designed for two types of users ( Administrators and students ) user ( abstract class )
package users;import book.Booklist;import operation.Operation;
public abstract class User {
public String name;
public Operation operations[];
public User(String name) {
this.name = name;
}
public abstract int meanu();
public void opera(int choice, Booklist booklist) {
operations[choice].oper(booklist);
}}Administrators
package users;import java.util.Scanner;import operation.Add;import operation.Delete;import operation.Delete1;import operation.Display;import operation.Exit;import operation.Find;import operation.Find1;import operation.Operation;// Administrators
public class Administrator extends User {
public int meanu() {
Scanner reader = new Scanner(System.in);
System.out.println("\t-----------------------------------");
System.out.println("\t\t********0. List of books ********");
System.out.println("\t********1. Find books ( According to the title )********");
System.out.println("\t********2. Find books ( According to the number )********");
System.out.println("\t\t********3. Add books ********");
System.out.println("\t********4. Delete books ( According to the title )********");
System.out.println("\t********5. Delete books ( According to the number )********");
System.out.println("\t\t********6. Log out ********");
System.out.println("\t-----------------------------------");
int choice = reader.nextInt();
return choice;
}
public Administrator(String name) {
super(name);
this.operations = new Operation[] {
new Display(), new Find(),new Find1(), new Add(), new Delete(),new Delete1(), new Exit() };
}}Student
package users;import java.util.Scanner;import operation.Borrow;import operation.Borrow1;import operation.Display;import operation.Exit;import operation.Find;import operation.Find1;import operation.Operation;import operation.Return;import operation.Return1;// Student
public class Student extends User {
public int meanu() {
Scanner reader = new Scanner(System.in);
System.out.println("\t-----------------------------------");
System.out.println("\t\t********0. List of books ********");
System.out.println("\t********1. Find books ( According to the title )********");
System.out.println("\t********2. Find books ( According to the number )********");
System.out.println("\t********3. Borrow books ( According to the title )********");
System.out.println("\t********4. Borrow books ( According to the number )********");
System.out.println("\t********5. Return the books ( According to the title )********");
System.out.println("\t********6. Return the books ( According to the number )********");
System.out.println("\t\t********7. Log out ********");
System.out.println("\t-----------------------------------");
int choice = reader.nextInt();
return choice; }
public Student(String name) {
super(name);
this.operations = new Operation[] {
new Display(), new Find(),new Find1(), new Borrow(),new Borrow1(), new Return(),new Return1(), new Exit() };
}}4.Main Class establishment stay Main Class has a main function interface of system entry , In this interface , Select the function that the user wants to achieve by entering the function number , The corresponding functions of this main function are realized through operation The methods of classes in the package interact .
package main;import java.util.Scanner;import book.Booklist;import users.Administrator;import users.Student;import users.User;// Login screen
class Enter {
private static String password = "1234567";// Set the password to 123456
private static String password1 = "123456";// Set the password to 123456
public static User login() {
Scanner reader = new Scanner(System.in);
System.out.println("******** Sign in ********");
System.out.println(" Please enter a user name :");
String name = reader.nextLine(); // Administrator login
if ("administrator".equals(name)) {
System.out.println(" Please input a password :");
if (reader.next().equals(password)) {
System.out.println(" Welcome to the library borrowing system !");
User Administrator = new Administrator(name);
return Administrator;
}
else {
System.out.println(" Wrong password ! Please re-enter the password !"); } } // Students log in
if ("student".equals(name)) {
System.out.println(" Please input a password :");
if (reader.next().equals(password1)) {
System.out.println(" Welcome to the library borrowing system !");
User Student = new Student(name);
return Student;
}
else {
System.out.println(" Wrong password ! Please re-enter the password !"); }
}
return null; }}
public class Main {
public static void main(String args[]) {
Booklist booklist = new Booklist();
User user = Enter.login();
while (true) {
int choice = user.meanu();
user.opera(choice, booklist);
}
}}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/128815.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

Why on earth is it not recommended to use select *?

Ijcai2022 tutorial | dialogue recommendation system

Guanghetong & Qualcomm Internet of things technology open day successfully held

Study and use of cobalt strike
![[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles](/img/4f/e7806d75cd719e181d8455e4fdc1e7.jpg)
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles

八、QOS队列调度与报文丢弃

Eureka registers with each other, only showing each other or only showing problems in one

ctfshow 网络迷踪做题记录(2)

Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind

The ref value ‘xxx‘ will likely have changed by the time this effect function runs. If this ref......
随机推荐
Using El date picker to report errors in sub components
ICML2022 | 时序自监督视频transformer
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Kubeadm搭建kubernetes集群
Study and use of cobalt strike
Redis缓存雪崩、缓存穿透、缓存击穿
How to understand data mesh
Four methods of multi-threaded sequential operation. Ask casually during the interview
编码用这16个命名规则能让你少写一半以上的注释!
SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
Ctfshow network lost track record (2)
Nacos principle
速卖通测评自养号,国外环境如何搭建?需要多少成本?
小程序容器技术,让移动研发效率提升500%
Eureka相互注册,只显示对方或只在一个中显示问题
First week of internship diary
30. Learn highcharts label rotation histogram
牛客打开摄像头几秒后画面消失 | 相机打开画面一闪一闪
Discussion: if you want to land Devops, is it enough to only consider a good PAAS container platform?
BUUCTF做题Upload-Labs记录pass-01~pass-10