【Java题】简易的图书管理系统

本题主要是用Java语言写一个简易的图书管理系统,实现简单的增删改查




 

main.java

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
    public class Main {

        public static User login() {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你的姓名:");
            String name = scanner.nextLine();
            System.out.println("请输入你的身份:1-》管理员  0-》普通用户");
            int choice = scanner.nextInt();
            if(choice == 1) {
                AdminUser adminUser = new AdminUser(name);
                return adminUser;
            }else {
                NormalUser normalUser = new NormalUser(name);
                return normalUser;
            }
        }
        public static void main(String[] args) {

            BookList bookList = new BookList();

            User user = login();
            while (true) {
                int choice = user.menu();
                user.doOperation(choice, bookList);
            }
        }
    }



创建了uesr包,有 User   AmindUser    NormalUser三个类

User.java

package user;

import book.BookList;
import operation.IOPeration;

public abstract class User {
    protected String name;//姓名
    public IOPeration[] ioPerations;//只是定义并没有初始化大小

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

    public abstract int menu();

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

AmdinUser.java

package user;

import operation.*;

import java.util.Scanner;

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()
        };
   
    }

    public int menu() {
        System.out.println("************************");
        System.out.println("hello "+ this.name + " 欢迎来到管理员菜单!");
        System.out.println("1. 查找图书");
        System.out.println("2. 新增图书");
        System.out.println("3. 删除图书");
        System.out.println("4. 显示图书");
        System.out.println("0. 退出系统");
        System.out.println("************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }

}

 NormalUser.java

package user;

import operation.*;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description:普通用户
 */
public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowedOperation(),
                new ReturnOperation()
        };
    }

    public int menu() {
        System.out.println("************************");
        System.out.println("hello "+ this.name + " 欢迎来到普通用户菜单!");
        System.out.println("1. 查找图书");
        System.out.println("2. 借阅图书");
        System.out.println("3. 归还图书");
        System.out.println("0. 退出系统");
        System.out.println("************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }

}



 

 创建一个book包,有Book  BookList 两个类

Book.java 

package book;


public class Book {
    private String name;//书名
    private String author;//作者
    private int price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出 默认是false

    public Book(String name, String author, int 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 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 isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }


    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrowed == true) ? "  已经被借出":"  未被借出")+
                /*", isBorrowed=" + isBorrowed +*/
                '}';
    }
}

 BookList.java

package book;
    public class BookList {

        private Book[] books = new Book[10];

        private int usedSize;//计数器 来记录 当前实际放的书的书目!

        public BookList() {
            //构造方法 来初始化成员
            this.books[0] = new Book("离散数学","张小峰",30,"教材");
            this.books[1] = new Book("线性代数","程迪祥",35,"教材");
            this.books[2] = new Book("高等数学","严忠",40,"教材");
          
            this.usedSize = 3;
        }

        public Book getBook(int pos) {
            return books[pos];
        }

        public void setBooks(int pos,Book book) {
            
            books[pos] = book;
        }

        public int getUsedSize() {
            
            return usedSize;
        }

        public void setUsedSize(int usedSize) {
            
            this.usedSize = usedSize;
        }
    }

 




创建了一个Opertion包,有 AddOperation   BorrowOperation  DelOperation   ShowOperation  ReturnOperation   FindOperation   ExitOperation 7个类,有IOperation一个接口

AddOperation.java
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description:
 */
public class AddOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("新增图书!");
        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();
        scanner.nextLine();
        System.out.println("请输入类型:");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();

        //如果有这本书 就不能添加了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)) {
                System.out.println("书架存在这种书,不能进行添加!");
                return;
            }
        }

        //默认是放到了 数组的最后的位置
        bookList.setBooks(currentSize,book);

        //让usedSize++
        bookList.setUsedSize(currentSize+1);

    }
}

 BorrowOperation.java

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description:
 */
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 currentSize = bookList.getUsedSize();

        //如果有这本书 就不能添加了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)) {
                if(!book1.isBorrowed()) {
                    book1.setBorrowed(true);
                    System.out.println("借阅成功!");
                }else {
                    System.out.println("这本书已经被借走了!");
                }
                return;
            }
        }
        System.out.println("没有你要借阅的图书!");
    }
}

 DelOperation.java

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description:
 */
public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");

        //1、找到你要删除的书
        int currentSize = bookList.getUsedSize();
        if(currentSize == 0) {
            System.out.println("书架为空,不能删除!");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();


        int index = -1;
        //如果有这本书 就不能添加了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //2、index != -1  有这本书 开始删除
        if(index == -1) {
            System.out.println("没有你要删除的书!");
            return;
        }

        for (int i = index; i < currentSize-1; i++) {
            Book book1 = bookList.getBook(i+1);
            bookList.setBooks(i,book1);
        }

        //当书放删掉之后 需要维持usedSize
        bookList.setUsedSize(currentSize-1);
    }
}

  ShowOperation.java

package operation;
import book.Book;
import book.BookList;

public class ShowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书!");

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            //Book book = bookList[i];
            System.out.println(book);
        }
    }
}

 ReturnOperation.java

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description:
 */
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();
        int currentSize = bookList.getUsedSize();

        //如果有这本书 就不能添加了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)) {
                book1.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }

        System.out.println("没有你要归还的图书!");
    }
}

 FindOperation.java

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

    public class FindOperation implements IOPeration{
        public void work(BookList bookList){
            System.out.println("查找图书!");

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你要查找的图书:");
            String name = scanner.nextLine();

            int currentSize = bookList.getUsedSize();

            for (int i = 0; i < currentSize; i++) {
                Book book = bookList.getBook(i);
                if(book.getName().equals(name)) {
                    System.out.println("找到了你查找的书:");
                    System.out.println(book);
                    return;
                }
            }

            System.out.println("不好意思,你好像没有这本书!!");
        }
    }

 ExitOperation.java

package operation;

import book.BookList;

public class ExitOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            bookList.setBooks(i,null);
        }
        bookList.setUsedSize(0);

        System.exit(0);
    }
}

 IOperation

package operation;

import book.BookList;

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



运行结果

 【Java题】简易的图书管理系统_第1张图片

 【Java题】简易的图书管理系统_第2张图片

 【Java题】简易的图书管理系统_第3张图片

 

你可能感兴趣的:(Java,java,开发语言)