图书管理系统(JAVA)

该文章已生成可运行项目,

目录

 

一.效果

 1.身份选择:

 2.展示所有图书: 

​ 3.借阅图书:

 4. 查找图书:​

 5.归还图书: 

​ 6.添加图书:​

 7.删除图书:​

 8.退出系统:

二.完整代码展示

1.book包:

(1).主界面main:

(2).book书籍类:

(3).booklist书架类:

2.user包:

(1).user使用者类:

(2).user子类(普通学生类):

(3).user子类(图书馆老师类):

3.operation包:

(1).添加书籍:

(2).借阅书籍:

(3).删除书籍:

(4).退出系统:

(5).查找书籍:

(6).归还书籍:

(7).展示现有书籍:

(8).IOperation接口:

三.思路讲解:

1.book包:

2.user包:

3.operation包:

4.main主界面:


 

一.效果

先来看一下效果图:

1.身份选择:

2.展示所有图书: 

 3.借阅图书:

4. 查找图书:

5.归还图书: 

 6.添加图书:

 7.删除图书:

 8.退出系统:

二.完整代码展示

1.book包:

(1).主界面main:

package com.bookroom.book;

import com.bookroom.user.Normal;
import com.bookroom.user.User;
import com.bookroom.user.Vip;
import com.sun.org.apache.xpath.internal.operations.Neg;

import java.util.Scanner;
//请选择您要进行的操作:
public class Main {
    public static User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的姓名:");
        String name = scanner.nextLine();
        //User user = new User(name);
        System.out.println("请确认您的身份:1>>普通用户  2>>管理员");
        int choice = scanner.nextInt();
        if (choice == 1) {
            Normal normal = new Normal(name);
            System.out.println("欢迎您"+name+"同学");
            return normal;
        }else if (choice == 2) {
            Vip vip = new Vip(name);
            System.out.println("欢迎您"+name+"老师");
            return vip;
        }
        return null;
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        System.out.println("*****欢迎来到图书馆*****");
         User user = login();
        while (true) {
         user.menu();
         Scanner scanner = new Scanner(System.in);
         int choice = scanner.nextInt();

            user.doOperation(choice,bookList);
        }
    }
}

(2).book书籍类:

package com.bookroom.book;
public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean use;

    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isUse() {
        return use;
    }

    public void setUse(boolean use) {
        this.use = use;
    }

    @Override
    public String toString() {
        if (use == true){
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                  name + "已被借出"+
                '}';
        }else return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                name + "未被借出"+
                '}';
    }
}

(3).booklist书架类:

package com.bookroom.book;

public class BookList {
    private Book[] books = new Book[10];
    private int number;
    public BookList() {
        this.books[0] = new Book("三国演义","罗贯中",19,"小说");
        this.books[1] = new Book("西游记","罗贯中",19,"小说");
        this.books[2] = new Book("红楼梦","罗贯中",19,"小说");
        this.books[3] = new Book("水浒传","罗贯中",19,"小说");
        this.number = 4;
    }
    public Book getBook(int i) {
        return books[i];
    }
    public void setBooks(Book books,int i) {
        this.books[i] = books;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

2.user包:

(1).user使用者类:

package com.bookroom.user;

import com.bookroom.book.BookList;
import com.bookroom.operation.AddOperation;
import com.bookroom.operation.IOperation;
import com.bookroom.operation.ShowOperation;

public abstract class User {
    private String name;
    public IOperation[] iOperations;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract void menu();
    public void doOperation(int choice,BookList bookList) {
        iOperations[choice].work(bookList);
    }

}

(2).user子类(普通学生类):

package com.bookroom.user;

import com.bookroom.operation.*;

public class Normal extends User{
    public Normal(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
               new ShowOperation(),
                new BorrowedOperation(),
                new FindOperation(),
                new ReturnOperation()
        };
    }
    public void menu() {
        System.out.println("*****"+getName()+"同学,欢迎来到图书馆*****");

        System.out.println("*****1.显示图书*****");
        System.out.println("*****2.借阅图书*****");
        System.out.println("*****3.查找图书*****");
        System.out.println("*****4.归还图书*****");
        System.out.println("*****0.退出系统*****");
        System.out.println("请选择您要进行的操作:");
    }
}

(3).user子类(图书馆老师类):

package com.bookroom.user;

import com.bookroom.operation.*;

public class Vip extends User{
    public Vip(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new ShowOperation(),
                new BorrowedOperation(),
                new FindOperation(),
                new ReturnOperation(),
                new AddOperation(),
                new DelOperation()
        };
    }
    public void menu() {
        System.out.println("*****"+getName()+"同学,欢迎来到图书馆*****");

        System.out.println("*****1.显示图书*****");
        System.out.println("*****2.借阅图书*****");
        System.out.println("*****3.查找图书*****");
        System.out.println("*****4.归还图书*****");
        System.out.println("*****5.添加图书*****");
        System.out.println("*****6.删除图书*****");
        System.out.println("*****0.退出系统*****");
        System.out.println("请选择您要进行的操作:");
    }
}

3.operation包:

(1).添加书籍:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("该书籍已存在,请返回菜单重新选择");
                return;
            }
        }
        System.out.println("请输入书籍作者:");
        String author = scanner.nextLine();
        System.out.println("请输入书籍价格:");
        double price = scanner.nextDouble();
        System.out.println("请输入书籍类型:");
        String type = scanner.nextLine();
        bookList.setBooks(new Book(name,author,price,type),bookList.getNumber());
        bookList.setNumber(bookList.getNumber()+1);
        System.out.println("书籍《"+name+"》添加成功");
    }
}

(2).借阅书籍:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class BorrowedOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要借阅的书籍名称:");
        String name = scanner.nextLine();
        int a = 0;
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                a = 1;
                if (bookList.getBook(i).isUse() == false){
                    bookList.getBook(i).setUse(true);
                    System.out.println("成功借阅《"+name+"》");
                    return;
                }else {
                    System.out.println("该书寄已被借出");
                }
            }
        }
        if (a == 0){
            System.out.println("该书籍不存在");
        }
    }
}

(3).删除书籍:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                int a = i;
                for (int j = a ; j <= bookList.getNumber() ; j++){
                    bookList.setBooks(bookList.getBook(j+1),j);
                }
                bookList.setNumber(bookList.getNumber()-1);
                System.out.println("该书籍已被删除");
                return;
            }
        }
        System.out.println("未找到该书籍");
    }
}

(4).退出系统:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            bookList.setBooks(null,i);
        }
        bookList.setNumber(0);
        System.exit(0);
    }
}

(5).查找书籍:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找书籍");
        int a = 1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查找的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println(book.toString());
                a = 0;
            }
        }
        if (a == 1){
        System.out.println("该书籍不存在,请返回菜单重新选择");
        return;
        }
    }
}

(6).归还书籍:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要归还的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                if (bookList.getBook(i).isUse() == true){
                    bookList.getBook(i).setUse(false);
                    System.out.println("成功归还《"+name+"》");
                    return;
                }else {
                    System.out.println("该书寄暂未借出");
                }
            }
        }
    }
}

(7).展示现有书籍:

package com.bookroom.operation;

import com.bookroom.book.Book;
import com.bookroom.book.BookList;

import java.util.Scanner;

public class ShowOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("显示书籍");
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            System.out.println(book.toString());
            }
    }
}

(8).IOperation接口:

package com.bookroom.operation;

import com.bookroom.book.BookList;

public interface IOperation {
    void work(BookList bookList);
}

三.思路讲解:

1.book包:

建立一个图书馆首先是要定义一本书有什么性质,于是便有了book这个类:

public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean use;

    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isUse() {
        return use;
    }

    public void setUse(boolean use) {
        this.use = use;
    }

因为变量是用private修饰的,所以要重写 构造方法。

有了书还要有书架,所以要建立一个booklist类:

package com.bookroom.book;

public class BookList {
    private Book[] books = new Book[10];
    private int number;
    public BookList() {
        this.books[0] = new Book("三国演义","罗贯中",19,"小说");
        this.books[1] = new Book("西游记","罗贯中",19,"小说");
        this.books[2] = new Book("红楼梦","罗贯中",19,"小说");
        this.books[3] = new Book("水浒传","罗贯中",19,"小说");
        this.number = 4;
    }
    public Book getBook(int i) {
        return books[i];
    }
    public void setBooks(Book books,int i) {
        this.books[i] = books;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

这里用数组来储存每一本书,并初始化了四本小说(四大名著),并将数组现有数量初始化为4。

这里把main主界面放在最后讲,因为现在没有用户信息,没办法构造主界面,所以这里先开始讲user包的思路。

2.user包:

先定义使用者,重写构造方法,并定义一个menu菜单方法,因为菜单方法后续要在子类中重写,所以用abstract修饰,并把user也修饰为抽象类,第三行的public IOperation[] iOperations;这是用来储存后续菜单中的各个选项,doOperation是用来接收用户选择,并调用各个选项。

public abstract class User {
    private String name;
    public IOperation[] iOperations;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract void menu();

    public void doOperation(int choice,BookList bookList) {
        iOperations[choice].work(bookList);
    }

}

接着是他的子类,普通学生类和管理员老师类:

public class Normal extends User{
    public Normal(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
               new ShowOperation(),
                new BorrowedOperation(),
                new FindOperation(),
                new ReturnOperation()
        };
    }
    public void menu() {
        System.out.println("*****"+getName()+"同学,欢迎来到图书馆*****");

        System.out.println("*****1.显示图书*****");
        System.out.println("*****2.借阅图书*****");
        System.out.println("*****3.查找图书*****");
        System.out.println("*****4.归还图书*****");
        System.out.println("*****0.退出系统*****");
        System.out.println("请选择您要进行的操作:");
    }
}
public class Vip extends User{
    public Vip(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new ShowOperation(),
                new BorrowedOperation(),
                new FindOperation(),
                new ReturnOperation(),
                new AddOperation(),
                new DelOperation()
        };
    }
    public void menu() {
        System.out.println("*****"+getName()+"同学,欢迎来到图书馆*****");

        System.out.println("*****1.显示图书*****");
        System.out.println("*****2.借阅图书*****");
        System.out.println("*****3.查找图书*****");
        System.out.println("*****4.归还图书*****");
        System.out.println("*****5.添加图书*****");
        System.out.println("*****6.删除图书*****");
        System.out.println("*****0.退出系统*****");
        System.out.println("请选择您要进行的操作:");
    }
}

他们两个可以说是一模一样,只是因为管理员的特权多,所以menu方法多写了两个,第三行的

this.iOperations = new IOperation[]{
                new ExitOperation(),
                new ShowOperation(),
                new BorrowedOperation(),
                new FindOperation(),
                new ReturnOperation(),
                new AddOperation(),
                new DelOperation()
        };

是用继承父类的数组来按菜单顺序储存operation。

3.operation包:

先写一个接口方便后续代码的编写和应用,work方法是写各个选项的运行逻辑:

public interface IOperation {
    void work(BookList bookList);
}

然后是各个operation,先是AddOperation:

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("该书籍已存在,请返回菜单重新选择");
                return;
            }
        }
        System.out.println("请输入书籍作者:");
        String author = scanner.nextLine();
        System.out.println("请输入书籍价格:");
        double price = scanner.nextDouble();
        System.out.println("请输入书籍类型:");
        String type = scanner.nextLine();
        bookList.setBooks(new Book(name,author,price,type),bookList.getNumber());
        bookList.setNumber(bookList.getNumber()+1);
        System.out.println("书籍《"+name+"》添加成功");
    }
}

先输入要添加书籍的name,再用循环历遍图书架数组,如果重复便返回菜单,反之输入该书籍的作者,价格,类型,并添加入图书架数组中,让现有书籍数加一。

然后是BorrowedOperation:

public class BorrowedOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要借阅的书籍名称:");
        String name = scanner.nextLine();
        int a = 0;
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                a = 1;
                if (bookList.getBook(i).isUse() == false){
                    bookList.getBook(i).setUse(true);
                    System.out.println("成功借阅《"+name+"》");
                    return;
                }else {
                    System.out.println("该书寄已被借出");
                }
            }
        }
        if (a == 0){
            System.out.println("该书籍不存在");
        }
    }
}

先输入要借阅的书籍name,历遍数组如果有且借阅变量为false则将其借阅变量改为true,输出“成功借阅《name》”并返回菜单,如果借阅变量为true则输出“该书籍已被借出”,a用来判断该书籍是否存在。

然后是DelOperation:

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                int a = i;
                for (int j = a ; j <= bookList.getNumber() ; j++){
                    bookList.setBooks(bookList.getBook(j+1),j);
                }
                bookList.setNumber(bookList.getNumber()-1);
                System.out.println("该书籍已被删除");
                return;
            }
        }
        System.out.println("未找到该书籍");
    }
}

输入要删除书籍的name,历遍数组,如果找到了,便让后续书籍位置向前加一,现有书籍数减一,如果没找到便输出“未找到该书籍”。

接着是ExitOperation:

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            bookList.setBooks(null,i);
        }
        bookList.setNumber(0);
        System.exit(0);
    }
}

这个用exit便可结束运行。

然后是FindOperation:

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找书籍");
        int a = 1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查找的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println(book.toString());
                a = 0;
            }
        }
        if (a == 1){
        System.out.println("该书籍不存在,请返回菜单重新选择");
        return;
        }
    }
}

输入要查找的书籍name,如果有则用toString输出该书籍信息,如果没有则输出“该书籍不存在,请返回菜单重新选择”并返回菜单。

接着是ReturnOperation:

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要归还的书籍名称:");
        String name = scanner.nextLine();
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                if (bookList.getBook(i).isUse() == true){
                    bookList.getBook(i).setUse(false);
                    System.out.println("成功归还《"+name+"》");
                    return;
                }else {
                    System.out.println("该书寄暂未借出");
                }
            }
        }
    }
}

与借阅书籍一样,只是将true和false调换一下。

最后是ShowOperation:

public class ShowOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("显示书籍");
        for (int i = 0 ; i < bookList.getNumber() ; i++){
            Book book = bookList.getBook(i);
            System.out.println(book.toString());
            }
    }
}

这个只用历遍数组并全部输出就可以了。

4.main主界面:

public static User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的姓名:");
        String name = scanner.nextLine();
        //User user = new User(name);
        System.out.println("请确认您的身份:1>>普通用户  2>>管理员");
        int choice = scanner.nextInt();
        if (choice == 1) {
            Normal normal = new Normal(name);
            System.out.println("欢迎您"+name+"同学");
            return normal;
        }else if (choice == 2) {
            Vip vip = new Vip(name);
            System.out.println("欢迎您"+name+"老师");
            return vip;
        }
        return null;
    }

首先定义一个登陆界面,判断是哪个身份,并返回该身份。

public static void main(String[] args) {
        BookList bookList = new BookList();
        System.out.println("*****欢迎来到图书馆*****");
         User user = login();
        while (true) {
         user.menu();
         Scanner scanner = new Scanner(System.in);
         int choice = scanner.nextInt();

            user.doOperation(choice,bookList);
        }
    }

接着在main中用死循环重复菜单和选择选项并调用操作。

简易图书管理系统便是结束了,如有好的建议欢迎评论‘- ̗̀ ෆ( ˶'ᵕ'˶)ෆ ̖́- 

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值