Java-图书管理系统

该项目学习目的:
利用前面所学的知识点:类,抽象类,封装,继承,多态,接口、ComparableTo接口等进行的一个简单的代码练习。

核心需求:

1、简单的登录
2、管理端
整理书籍
查阅书籍
增加书籍
删除书籍
打印书籍列表
退出

3、用户端
查询书籍
借阅书籍
归还书籍
退出


代码如下:

package Book;

public class Book implements Comparable<Book> {
    private String nema;
    private String author;
    private Float price;
    private String type;
    private Boolean brrower;

    public Book(String nema, String author, Float price, String type) {
        this.nema = nema;
        this.author = author;
        this.price = price;
        this.type = type;
        this.brrower = false;
    }

    public Book() {
        this.nema = "0";
        this.author = "0";
        this.price = 0f;
        this.type = "0";
        this.brrower = false;
    }

    @Override
    public String toString() {
        return "Book{" +
                "nema= '" + nema + '\'' +
                ", author= '" + author + '\'' +
                ", price= " + price +
                ", type= '" + type + '\'' +
                (brrower ? " 已借出" : " 未借出") +
                '}';
    }

    public String getNema() {
        return nema;
    }

    public void setNema(String nema) {
        this.nema = nema;
    }

    public String getAuthor() {
        return author;
    }

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

    public Float getPrice() {
        return price;
    }

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

    public String getType() {
        return type;
    }

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

    public Boolean getBrrower() {
        return brrower;
    }

    public void setBrrower(Boolean brrower) {
        this.brrower = brrower;
    }

    @Override
    public int compareTo(Book o) {
        return (int) (o.getPrice() - this.getPrice());
    }
}

package Book;

import java.util.Arrays;

public class BookList {
    private Book[] books;
    private int usedSize;

    public BookList() {
        books = new Book[10];
        books[0] = new Book("HappyDay", "YaYa", 19.9f, "little");
        books[1] = new Book("SunDay", "XiaoXiao", 29.9f, "little");
        books[2] = new Book("EveryDay", "DiDi", 39.9f, "little");
        for (int i = 3; i < 10; i++) {
            books[i] = new Book();
        }
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

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

    public void setBooks(Book book) {
        this.books[usedSize] = book;
        this.usedSize++;
    }

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

    public void disPlay() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(books[i].toString());
        }
    }

    public void sortBookList() {
        Arrays.sort(this.books);
    }
}

package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("ok");
        Scanner scanner = new Scanner(System.in);
        Book book = new Book();
        System.out.println("输入书名" + " 作者" + " 价格" + " 类型");
        book.setNema(scanner.next());
        book.setAuthor(scanner.next());
        book.setPrice(scanner.nextFloat());
        book.setType(scanner.next());
        bookList.setBooks(book);
        System.out.println("成功!!!");
    }
}

package Operation;

import Book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要借的书名");
        String name = scanner.next();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks(i).getNema().equals(name)) {
                if(bookList.getBooks(i).getBrrower()) {
                    System.out.println("此书已借出!");
                    return;
                }
                bookList.getBooks(i).setBrrower(true);
                System.out.println("借书成功");
                return;
            }
        }
        System.out.println("无此书!!!");
    }
}

package Operation;

import Book.Book;
import 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);
        String name = scanner.next();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks(i).getNema().equals(name)) {
                for (int j = i; j < bookList.getUsedSize() - 1; j++) {
                    Book book = bookList.getBooks(j + 1);
                    bookList.setBook(j, book);
                }
                bookList.setUsedSize(bookList.getUsedSize() - 1);
                System.out.println("删除成功!");
                return;
            }
        }
        System.out.println("无此书!");
    }
}

package Operation;

import Book.BookList;

import java.util.Arrays;

public class DisplayOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        bookList.disPlay();
    }
}

package Operation;

import Book.BookList;

public class ExitOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("EXIT");
        System.exit(0);
    }
}

package Operation;

import Book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("输入你要查找的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks(i).getNema().equals(name)) {
                System.out.println("成功找到此书!");
                byte Id = 0;
                do {
                    System.out.println("是否查看此书所有信息  1 是  0  否");
                     Id = scanner.nextByte();
                }while (Id != 1 && Id != 0);
                if(Id == 1) {
                    System.out.println(bookList.getBooks(i).toString());
                }
                return;
            }
        }
        System.out.println("无此书!");
    }
}

package Operation;

import Book.BookList;

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

package Operation;

import Book.BookList;

public class NeatenOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        bookList.sortBookList();
    }
}

package Operation;

import 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);
        String name = scanner.next();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks(i).getNema().equals(name)) {
                if( ! bookList.getBooks(i).getBrrower()) {
                    System.out.println("此书未被借出,无需归还!");
                    return;
                }
                bookList.getBooks(i).setBrrower(false);
                System.out.println("还书成功!");
                return;
            }
        }
        System.out.println("无此书!");
    }
}

package User;

import Operation.*;

import Operation.IOperation;

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 DisplayOperation(),
                new NeatenOperation()
        };
    }

    @Override
    public int menu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("================================");
        System.out.println("===Hello "+ super.getName() +" 欢迎来到Knowledgable图书系统===");
        System.out.println("1.查找图书");
        System.out.println("2.新增书籍");
        System.out.println("3.删除书籍");
        System.out.println("4.打印书籍");
        System.out.println("5.整理书籍");
        System.out.println("0.退出系统");
        return scanner.nextInt();
    }
}

package User;

import Operation.*;

import java.util.Scanner;

public class NormalUser extends User {

    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };
    }

    @Override
    public int menu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("================================");
        System.out.println("Hello"+ super.getName() +" 欢迎来到Knowledgable图书系统");
        System.out.println("1.查找图书");
        System.out.println("2.借阅书籍");
        System.out.println("3.归还书籍");
        System.out.println("0.退出系统");
        return scanner.nextInt();
    }
}

package User;

import Book.BookList;
import Operation.IOperation;

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

    abstract public int menu();

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

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

    public String getName() {
        return name;
    }

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

import Book.*;
import User.*;

import java.util.Scanner;

public class Test {

    public static User forReady() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的名字 ! ");
        String name = scanner.next();
        byte Id = 0;
        do {
        System.out.println("请输入你的身份:1 管理员 0 普通用户");
        Id = scanner.nextByte();
        }while (Id != 1 && Id != 0);

        if(Id == 1) {
            return new AdminUser(name);
        }
            return new NormalUser(name);
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = forReady();
        while (true) {
            int choice = user.menu();
            user.doIOperations(choice, bookList);
        }
    }
}



代码演示:

演示图书整理功能

请输入你的名字 ! 
天才
请输入你的身份:1 管理员 0 普通用户
1
================================
===Hello  欢迎来到Knowledgable图书系统===
1.查找图书
2.新增书籍
3.删除书籍
4.打印书籍
5.整理书籍
0.退出系统
4
Book{nema= 'HappyDay', author= 'YaYa', price= 19.9, type= 'little' 未借出}
Book{nema= 'SunDay', author= 'XiaoXiao', price= 29.9, type= 'little' 未借出}
Book{nema= 'EveryDay', author= 'DiDi', price= 39.9, type= 'little' 未借出}
================================
===Hello 天才 欢迎来到Knowledgable图书系统===
1.查找图书
2.新增书籍
3.删除书籍
4.打印书籍
5.整理书籍
0.退出系统
5
================================
===Hello 天才 欢迎来到Knowledgable图书系统===
1.查找图书
2.新增书籍
3.删除书籍
4.打印书籍
5.整理书籍
0.退出系统
4
Book{nema= 'EveryDay', author= 'DiDi', price= 39.9, type= 'little' 未借出}
Book{nema= 'SunDay', author= 'XiaoXiao', price= 29.9, type= 'little' 未借出}
Book{nema= 'HappyDay', author= 'YaYa', price= 19.9, type= 'little' 未借出}
================================
===Hello 天才 欢迎来到Knowledgable图书系统===
1.查找图书
2.新增书籍
3.删除书籍
4.打印书籍
5.整理书籍
0.退出系统
0
EXIT

Process finished with exit code 0