<< Java >> 图书管理系统

简介: << Java >> 图书管理系统

1.Main

import book.BookList;
import com.sun.xml.internal.ws.wsdl.writer.document.Operation;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
    private static User login(){
        System.out.println("请输入你的姓名");
        String name;
        Scanner scanner = new Scanner(System.in);
        name = scanner.nextLine();
        System.out.println("请输入你的身份---> 1.管理员  2.普通用户");
        int flag = scanner.nextInt();
        if(flag == 1){
            return new AdminUser(name);
        }else {
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        //初始化图书
        BookList bookList = new BookList();
        //管理员/用户登录
        User user = login();
        while(true){
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

2.User包

User父类


package user;
import book.BookList;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import operation.IOperation;
public abstract class User {
    protected String name;
    protected IOperation[] operation;
    public User(String name) {
        this.name = name;
    }
    public void doOperation(int choice , BookList bookList){
        this.operation[choice].work(bookList);
    }
    public abstract int menu();
}


2.1管理员子类

package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.operation = new IOperation[]{new Exit(),new Find(),new Add(),new Delete(),new Display()};
    }
    @Override
    public int menu() {
        System.out.println("1.查找图书!");
        System.out.println("2.新增图书!");
        System.out.println("3.删除图书!");
        System.out.println("4.显示图书!");
        System.out.println("0.退出系统!");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        return flag;
    }
}

2.2普通用户子类

package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends  User{
    public NormalUser(String name) {
        super(name);
        this.operation = new IOperation[]{new Exit(),new Find(),new Borrow(),new Return()};
    }
    @Override
    public int menu() {
        System.out.println("1.查找图书!");
        System.out.println("2.借阅图书!");
        System.out.println("3.归还图书!");
        System.out.println("0.推出系统!");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        return flag;
    }
}

3Book包

3.1 Book

package book;
import java.util.Objects;
public class Book {
    private String name;//书名
    private String author;//作者
    private int price;//价格
    private String type;//类别
    private boolean isborrowed;//是否被借出
    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        this.isborrowed = false;
    }
    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 int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public boolean isIsborrowed() {
        return isborrowed;
    }
    public void setIsborrowed(boolean isborrowed) {
        this.isborrowed = isborrowed;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isborrowed=" + isborrowed +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return price == book.price && isborrowed == book.isborrowed && Objects.equals(name, book.name) && Objects.equals(author, book.author) && Objects.equals(type, book.type);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, author, price, type, isborrowed);
    }
}

3.2BookList

package book;
public class BookList {
    //目前最多可以放十本书
    private Book[] books = new Book[10];
    private int usedSize;//用来记录现在有几本
    public BookList() {
        books[0]=new Book("三国演义","罗贯中",30,"四大名著");
        books[1]=new Book("西游记","吴承恩",40,"四大名著");
        books[2]=new Book("红楼梦","曹雪芹",40,"四大名著");
        books[3]=new Book("水浒传","施耐庵",30,"四大名著");
        usedSize = 4;
    }
    public void setBooks(Book[] books) {
        this.books = books;
    }
    public Book getBooks(int pos) {
        return books[pos];
    }
    public void setBooks(int pos,Book books) {//pos位置合法,把book书放到pos位置
        this.books[pos] = books;
    }
    public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
}


4 .operation

IOperation接口

package operation;
import book.BookList;
public interface IOperation {
     public void work(BookList bookList);
}


4.1Add

package operation;
import book.Book;
import book.BookList;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.util.Scanner;
public class Add implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        //增加这本书的信息
        System.out.println("请输入要增加的书名:");
        String name = scanner.nextLine();
        System.out.println("请输入这本书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入这本书的价格:");
        int price = scanner.nextInt();
        System.out.println("请输入这本书的类型:");
        String type = scanner.nextLine();
        Book book = new Book(name,author,price,type);
        bookList.setBooks(bookList.getUsedSize(), book);
        //将书架的数目增加 1
        bookList.setUsedSize(bookList.getUsedSize()+1);
    }
}

4.2 Borrow

package operation;
import book.BookList;
import java.util.Scanner;
public class Borrow implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要借的书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name == bookList.getBooks(i).getName()){
                bookList.getBooks(i).setIsborrowed(true);
                System.out.println("借书成功!");
                return;
            }
        }
    }
}


4.3 Delete

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Delete implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要删除的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){
                for (int j = i; j<bookList.getUsedSize()-1;j++){
                    Book book = bookList.getBooks(j+1);
                    bookList.getBooks(j).setName(book.getName());
                    bookList.getBooks(j).setPrice(book.getPrice());
                    bookList.getBooks(j).setType(book.getType());
                    bookList.getBooks(j).setIsborrowed(book.isIsborrowed());
                }
                bookList.setUsedSize(bookList.getUsedSize()-1);
                System.out.println("已经成功删除这本书!!");
                return;
            }
        }
        System.out.println("没有这本书!!!");
        return;
    }
}

4.4 Display

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class Delete implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要删除的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){
                for (int j = i; j<bookList.getUsedSize()-1;j++){
                    Book book = bookList.getBooks(j+1);
                    bookList.getBooks(j).setName(book.getName());
                    bookList.getBooks(j).setPrice(book.getPrice());
                    bookList.getBooks(j).setType(book.getType());
                    bookList.getBooks(j).setIsborrowed(book.isIsborrowed());
                }
                bookList.setUsedSize(bookList.getUsedSize()-1);
                System.out.println("已经成功删除这本书!!");
                return;
            }
        }
        System.out.println("没有这本书!!!");
        return;
    }
}

4.5 Exit

package operation;
import book.BookList;
public class Exit implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("退出成功");
        System.exit(0);
    }
}

4.6Find

package operation;
import book.BookList;
import java.util.Scanner;
public class Find implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要查找的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            System.out.println("i=="+i);
            if(name.equals(bookList.getBooks(i).getName())){
                System.out.println("这本书的位置是"+i);
                System.out.println("这本书的信息是"+bookList.getBooks(i).toString());
                return;
            }
        }
        System.out.println("没有这本书!!");
        return;
    }
}

4.7 Return


package operation;
import book.BookList;
import user.AdminUser;
import user.User;
import java.util.Scanner;
public class Return implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要归还的书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name == bookList.getBooks(i).getName()){
                bookList.getBooks(i).setIsborrowed(false);
                System.out.println("成功还书!");
                return;
            }
        }
        System.out.println("这本书不是图书馆的书,but已成功捐赠!!!");
        User user = new AdminUser("shan");
        user.doOperation(2,bookList);        
    }
}


相关文章
|
6月前
|
JavaScript Java 测试技术
基于Java的图书管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的图书管理系统的设计与实现(源码+lw+部署文档+讲解等)
180 1
|
6月前
|
Java 关系型数据库 MySQL
java和mysql数据库实现的图书管理系统
java和mysql数据库学生信息管理系统
105 1
|
6月前
|
Java 数据安全/隐私保护
java图书管理系统
java图书管理系统
|
6月前
|
存储 Java 关系型数据库
图书管理系统【GUI/Swing+MySQL】(Java课设)
图书管理系统【GUI/Swing+MySQL】(Java课设)
44 0
|
6月前
|
Java
JAVA实现图书管理系统(思路,和完整代码)
JAVA实现图书管理系统(思路,和完整代码)
794 0
|
2月前
|
存储 Java
Java——图书管理系统
该文档详细介绍了一个图书管理系统的设计与实现。系统包含普通用户和管理员两种角色,通过书架操作图书,如添加、查找、借阅、归还及删除图书等功能。文档展示了各个功能的具体代码实现,并使用继承和接口等方式优化了系统结构。通过多态技术实现了不同用户角色调用相应功能。整体设计清晰,逻辑严谨,便于理解和实现。
103 17
Java——图书管理系统
|
5月前
|
前端开发 JavaScript Java
计算机Java项目|图书大厦图书管理系统的设计与实现
计算机Java项目|图书大厦图书管理系统的设计与实现
|
6月前
|
Java
Java实现图书管理系统
Java实现图书管理系统
67 0
|
6月前
|
JavaScript Java 测试技术
Java项目基于ssm+vue.js图书管理系统的附带文章和源代码设计说明文档ppt
Java项目基于ssm+vue.js图书管理系统的附带文章和源代码设计说明文档ppt
54 0
|
6月前
|
Java
<Java> 图书管理系统. 课程设计之经典
<Java> 图书管理系统. 课程设计之经典
41 0