bilibiliJava学习图书管理系统

 1.进行架构

写我这个项目初期他需要什么东西

首先去确定我有管理员,我有普通人员,然后书有书名,作者名,价格,类型,是否被借出

(1)写大的User

package user;

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

(2)写管理员和普通人员

package user;

public class AdminUser extends User{
     protected String name;
     public AdminUser(String name){
         super(name);
     }
}
package user;

public class Normaluser extends User{
    public Normaluser(String name){
        super(name);
    }
}

注意:继承过来的类要进行初始化,super一下

(3)写书book

package book;

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

}

注意:写构造方法,private权限的进行getset,toString要不要先写上

package book;

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;
    }

    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=" + isBorrowed +
            '}';
}
}

(4)写一个书架,用来管理图书

package book;

public class BookList {
    public Book[] books= new Book[100];
    public int usedSize;//存储当前书架存储多少书

    /**
     *事先通过构造方法,初始化的时候,给数组里面预存三本书,usedSize = 3;
     */
    public BookList(){
        books[0] = new Book("三国演义","罗贯中",89,"小说") ;
        books[1] = new Book("西游记","吴承恩",78,"小说");
        books[2] = new Book("红楼梦","曹雪芹",67,"小说") ;
        this.usedSize = 3;

    }
}

(5)写一些操作

总操作,接口,用于实现多态

package operations;

import book.BookList;

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

管理员图书的添加

package operations;

import book.BookList;

public class AddOperation implements IOOperation{
    public void work(BookList bookList){
        //添加图书
        System.out.println("添加图书");
    }
}

管理员图书的寻找

package operations;

import book.BookList;

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


管理员图书的删除
package operations;

import book.BookList;

public class DelOperation implements IOOperation{
    public void work(BookList bookList){
        //删除图书
        System.out.println("删除图书");
    }
}

管理员图书的展示

package operations;

import book.BookList;


public class DisplayOperation implements IOOperation {
    public void work(BookList bookList){
        //展示图书
        System.out.println("展示图书");
    }
}

管理员退出

package operations;

import book.BookList;

public class ExitOperation {
    public void work(BookList bookList){
        //退出系统
        System.out.println("退出系统");
    }
}

普通人员借书

package operations;

import book.BookList;

public class BorrowBook implements IOOperation{

        public void work(BookList bookList){
            //退出系统
            System.out.println("借书");
        }
    }

普通人员还书

package operations;

import book.BookList;

public class ReturnBook {
    public void work(BookList bookList){
       
        System.out.println("归还图书");
    }

}

2.写main函数,前两步,准备数据和登录

import book.BookList;
import user.AdminUser;
import user.Normaluser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        System.out.println("请输入你的姓名");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();

        System.out.println("请输入你的身份1-》管理员,0-》普通员工");
        int choice = scanner.nextInt();
        if(choice == 1){
            return new AdminUser(userName);
        }else{
            return new Normaluser(userName);
        }


    }

    public static void main(String[] args) {
        //0.准备数据
        BookList bookList = new BookList();
        //1.登录
        User user = login();

    }

登录后,引用的对象不一样,打印的菜单不一样

接着在管理员界面和用户界面新增

public void menu(){
    System.out.println("hello" + name + "欢迎来到管理员界面");
    System.out.println("----------------------------");
    System.out.println("1.查找图书");
    System.out.println("2.新增图书");
    System.out.println("3.删除图书");
    System.out.println("4.显示图书");
    System.out.println("0.退出系统");
    System.out.println("----------------------------");

}
  public void menu(){
        System.out.println("hello" + name + "欢迎来到用户界面");
        System.out.println("----------------------------");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("----------------------------");
    }
}

并将User改为抽象类,因为我在menu里啥也不写

public abstract void menu();

3.思考在 System.out.println("请输入你的身份1-》管理员,0-》普通员工");这步进入界面后,如何按菜单上的数字进行对应操作

答案:构建一个接口数组,把对应的每一步给他写进去

在User类中加入

protected IOOperation[] iooperations;

在管理员和普通类中的构造方法后面加入IOOperation接口的一个数组

this.iooperations = new IOOperation[]{
        new FindOperation(),
        new AddOperation(),
        new DelOperation(),
        new DisplayOperation(),
        new ExitOperation(),
};
this.iooperations = new IOOperation[]{
  new FindOperation(),
  new BorrowBook(),
  new ReturnBook(),
  new ExitOperation()
};

思考加入后如何引用数字进行操作

写一个操作接口数组的方法

在User类中写一个doOperation方法

public void doOperation(int choice, BookList bookList){
    iooperations[choice].work(bookList);
}
public static void main(String[] args) {
        //0.准备数据
        BookList bookList = new BookList();
        //1.登录
        User user = login();
        while (true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }
}

4.写方法

(1)寻找方法

将BookList中的UsedSize和Book方法写出getset,并新写一个方法,目的是获取图书的下标

public class BookList {
    public Book[] books= new Book[100];
    private int usedSize;//存储当前书架存储多少书

    /**
     *事先通过构造方法,初始化的时候,给数组里面预存三本书,usedSize = 3;
     */
    public BookList(){
        books[0] = new Book("三国演义","罗贯中",89,"小说") ;
        books[1] = new Book("西游记","吴承恩",78,"小说");
        books[2] = new Book("红楼梦","曹雪芹",67,"小说") ;
        this.usedSize = 3;

    }

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

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

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book getPos(int pos){
        return books[pos];
    }
}

然后再findOperation中写方法

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOOperation{
    public void work(BookList bookList){
        //查找图书
        System.out.println("查找图书");
        System.out.println("请输入查找的图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book  = bookList.getPos(i);
            if(name.equals(book.getName())){
                System.out.println("找到了这本书");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

(2)新增方法

在BookList当中写入一个setBooks方法

/**
 * 存储一本书,到指定的位置
 * @param book
 * @param pos 在这里指的是数 当前最后可以存储书的位置
 */
public void setBooks(Book book,int pos){
    books[pos] = book;
}

再写AddOperation

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOOperation{
    public void work(BookList bookList){
        //添加图书
        System.out.println("添加图书");
        System.out.println("请输入图书的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格");
        int price = scanner.nextInt();

        Book book = new Book(name,author,price,type);
        //1.获取到当前可以存放的位置
        int currenSize = bookList.getUsedSize();
        //2.把书放入指定的位置
        bookList.setBooks(book,currenSize);
        //3.书的有效个数+1
        bookList.setUsedSize(currenSize+1);
        System.out.println("新增成功");



    }
}
注意:

1.其实也不是先写setBookst方法,是我在思考AddOperation应该具体怎么做的时候,去进行具体类实现的方法再写

2.在添加图书的这个键盘输入方面,要做到的是,Scanner.nextLine()应该在Scanner.nextInt()之前

(3)显示图书

package operations;

import book.Book;
import book.BookList;


public class DisplayOperation implements IOOperation {
    public void work(BookList bookList){
        //展示图书
        System.out.println("展示图书");
        int currentSize = bookList.getUsedSize();

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

(4)退出图书

package operations;

import book.BookList;

public class ExitOperation implements IOOperation{
    public void work(BookList bookList){
        //退出系统
        System.out.println("退出系统");
        System.exit(0);
    }
}

(5)删除图书

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOOperation {
    public void work(BookList bookList) {
        //删除图书
        System.out.println("删除图书");
        System.out.println("请输入你要删除的图书");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        //1.遍历数组当中有没有你要删除的图书,有记录下标
        int index = -1;
        int currenSize = bookList.getUsedSize();
        for (int i = 0; i < currenSize; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        //
        if (index == -1){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int i = index; i < currenSize-1; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }
        //每次删除都要置为空值
        bookList.setBooks(null,currenSize-1);
        bookList.setUsedSize(currenSize-1);
    }
}

 所以每次删除都得把他置为空值

(6)借书方法

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowBook implements IOOperation{

        public void work(BookList bookList){
            //退出系统
            System.out.println("借书");
            System.out.println("请输入你要借阅的图书的名字");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();

            int currenSize = bookList.getUsedSize();
            for (int i = 0; i < currenSize; i++) {
                Book book = bookList.getPos(i);
                if (name.equals(book.getName())) {
                    book.setBorrowed(true);
                    System.out.println("借阅图书成功");
                    return;
                }
            }
            System.out.println("没有你要借阅的图书");
        }
    }

(7)还书方法

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnBook implements IOOperation{
    public void work(BookList bookList){
        //查找图书
        System.out.println("归还图书");
        System.out.println("请输入你要归还的图书的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currenSize = bookList.getUsedSize();
        for (int i = 0; i < currenSize; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                book.setBorrowed(false);
                System.out.println("借阅图书成功");
                return;
            }
        }
        System.out.println("没有你要借阅的图书");
    }
    }

-------------------------------------------------------------------------------------------------------------------

一.Book

package book;

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;
    }

    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) ? "已借出":"未借出")+
                '}';
    }
}

二。BookList

package book;

public class BookList {
    public Book[] books= new Book[100];
    private int usedSize;//存储当前书架存储多少书

    /**
     *事先通过构造方法,初始化的时候,给数组里面预存三本书,usedSize = 3;
     */
    public BookList(){
        books[0] = new Book("三国演义","罗贯中",89,"小说") ;
        books[1] = new Book("西游记","吴承恩",78,"小说");
        books[2] = new Book("红楼梦","曹雪芹",67,"小说") ;
        this.usedSize = 3;

    }

    /**
     * 存储一本书,到指定的位置
     * @param book
     * @param pos 在这里指的是数 当前最后可以存储书的位置
     */
    public void setBooks(Book book,int pos){
        books[pos] = book;
    }

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

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

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book getPos(int pos){
        return books[pos];
    }
//这里应该写一些 操作这个数组的方法 但是我们这里不这么做,我们换一种做法
}

三。AddOperation

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOOperation{
    public void work(BookList bookList){
        //添加图书
        System.out.println("添加图书");
        System.out.println("请输入图书的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格");
        int price = scanner.nextInt();

        Book book = new Book(name,author,price,type);
        //1.获取到当前可以存放的位置
        int currenSize = bookList.getUsedSize();
        //2.把书放入指定的位置
        bookList.setBooks(book,currenSize);
        //3.书的有效个数+1
        bookList.setUsedSize(currenSize+1);
        System.out.println("新增成功");



    }
}

四。BorrowBook

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowBook implements IOOperation{

        public void work(BookList bookList){
            //退出系统
            System.out.println("借书");
            System.out.println("请输入你要借阅的图书的名字");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();

            int currenSize = bookList.getUsedSize();
            for (int i = 0; i < currenSize; i++) {
                Book book = bookList.getPos(i);
                if (name.equals(book.getName())) {
                    book.setBorrowed(true);
                    System.out.println("借阅图书成功");
                    return;
                }
            }
            System.out.println("没有你要借阅的图书");
        }
    }

五。DelOperation

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOOperation {
    public void work(BookList bookList) {
        //删除图书
        System.out.println("删除图书");
        System.out.println("请输入你要删除的图书");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //1.遍历数组当中有没有你要删除的图书,有记录下标
        int index = -1;
        int currenSize = bookList.getUsedSize();
        for (int i = 0; i < currenSize; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        //
        if (index == -1){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int i = index; i < currenSize-1; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }
        //每次删除都要置为空值
        bookList.setBooks(null,currenSize-1);
        bookList.setUsedSize(currenSize-1);
    }
}

六。DisplayOperation

package operations;

import book.Book;
import book.BookList;


public class DisplayOperation implements IOOperation {
    public void work(BookList bookList){
        //展示图书
        System.out.println("展示图书");
        int currentSize = bookList.getUsedSize();

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

七。ExitOperation

package operations;

import book.BookList;

public class ExitOperation implements IOOperation{
    public void work(BookList bookList){
        //退出系统
        System.out.println("退出系统");
        System.exit(0);
    }
}

八。FindOperation

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOOperation{
    public void work(BookList bookList){
        //查找图书
        System.out.println("查找图书");
        System.out.println("请输入查找的图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book  = bookList.getPos(i);
            if(name.equals(book.getName())){
                System.out.println("找到了这本书");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

九。IOOperation

package operations;

import book.BookList;

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

十。ReturnBook

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnBook implements IOOperation{
    public void work(BookList bookList){
        //查找图书
        System.out.println("归还图书");
        System.out.println("请输入你要归还的图书的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currenSize = bookList.getUsedSize();
        for (int i = 0; i < currenSize; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                book.setBorrowed(false);
                System.out.println("借阅图书成功");
                return;
            }
        }
        System.out.println("没有你要借阅的图书");
    }
    }


十一。AdminUser

package user;

import operations.*;

import java.util.Scanner;

public class AdminUser extends User{
     protected String name;

     public AdminUser(String name){
         super(name);
         this.iooperations = new IOOperation[]{new ExitOperation(),
                 new FindOperation(),
                 new AddOperation(),
                 new DelOperation(),
                 new DisplayOperation()

         };
     }
     public int menu(){
         System.out.println("hello" + name + "欢迎来到管理员界面");
         System.out.println("----------------------------");
         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 choice = scanner.nextInt();
         return choice;
     }
}

十二。Normaluser

package user;

import operations.*;

import java.util.Scanner;

public class Normaluser extends User{
    public Normaluser(String name){
        super(name);
        this.iooperations = new IOOperation[]{

                new ExitOperation(),
          new FindOperation(),
          new BorrowBook(),
          new ReturnBook()
        };

    }
    public int menu(){
        System.out.println("hello" + name + "欢迎来到用户界面");
        System.out.println("----------------------------");
        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;
    }
}

十三。User

package user;

import book.BookList;
import operations.IOOperation;

public abstract class User {
    protected String name;
    protected IOOperation[] iooperations;
    public User(String name){
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList){
        iooperations[choice].work(bookList);
    }
}

十四。main

import book.BookList;
import operations.AddOperation;
import user.AdminUser;
import user.Normaluser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        System.out.println("请输入你的姓名");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();

        System.out.println("请输入你的身份1-》管理员,0-》普通员工");
        int choice = scanner.nextInt();
        if(choice == 1){
            return new AdminUser(userName);
        }else{
            return new Normaluser(userName);
        }


    }

    public static void main(String[] args) {
        //0.准备数据
        BookList bookList = new BookList();
        //1.登录
        User user = login();
        while (true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值