Java《图书管理系统》练习

发布于:2022-12-21 ⋅ 阅读:(295) ⋅ 点赞:(0)

书的对象(book)

Book 书

**package book;

/**
 * 定义一本书
 */
public class Book {
    private String name;// 书名
    private String author;//书的作者
    private int price;// 书的价格
    private String type;// 书的类型
    private boolean isBorrowed;//判断书是否有被借出去

    /**
     * 这里是没有初始化 isBorrowed
     * 原因:最开始一本书是默认没有被借出的
     * 如果没有初始化,boolean 默认是 flase
     */
    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 + '\'' +
                ", 作者='" + author + '\'' +
                ", 价格=" + price +
                ", 类型='" + type + '\'' +
                ((isBorrowed == true) ? " 以借出":" 未借出") +
                '}';
    }
}

BookList 书架

package book;
/**
 *  书架
 */

public class BookList {
    // 用数组来当做书架,存根所以的书
    private  Book[] books = new Book[10];
    // 存储当前书架里有多少本书
    public int usedSize;

    // 通过构造方法来初始化数组(书架),给数组预存三本的书
    public BookList() {
       books[0] = new Book("三国演义","罗贯中",45,"小说");
       books[1] = new Book("西游记","吴承恩",46,"小说");
       books[2] = new Book("斗破苍穹","唐家三少",47,"小说");
       //书的数量
       this.usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    // 存储一本书到指定的位置,指的是最后的位置
    public void setBooks(Book book,int pos){
        books[pos] = book;
    }
}

系统功能(operations)

接口

package operations;
import book.BookList;

/**
 * 接口
 */

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

Findbook 查找书籍

package operations;
import book.BookList;
import book.Book;
import java.util.Scanner;

/**
 *  查找图书
 */

public class Findbook implements IOperation{
    public void work(BookList bookList){
        System.out.println("查找图书");
        System.out.println("请输入你要查找的图书!");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int cun = bookList.getUsedSize();
        for (int i = 0; i < cun; i++) {
            Book book = bookList.getPos(i);
            // 判断通过历遍数组找到和输入的书名相同的书
            if(name.equals(book.getName())){
                System.out.println("找到了,该书信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书!");
    }
}

AddBook 增加书籍

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 *   添加图书
 */

public class Addbook implements IOperation{
    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("请输入你要新增的图书价格:");
        int price = scanner.nextInt();
        // 读取空格
        scanner.nextLine();
        System.out.println("请输入你要新增的图书的类型:");
        String type= scanner.nextLine();

        Book book = new Book(name,author,price,type);

        // 获取到当前可以存放书的位置
        int cun = bookList.getUsedSize();
        // 把书放入指定的位置
        bookList.setBooks(book,cun);
        // 书的数量+1
        bookList.setUsedSize(cun+1);
    }
}

Delbook 删除书籍

package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;

/**
 *  删除图书
 */

public class Delbook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        System.out.println("请输入要删除的图书!");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        // 遍历数组当中 判断删除的书是否存在
        int cun = bookList.getUsedSize();
        // 如果找到 index存放下标
        int index = -1;
        for (int i = 0; i < cun; 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 < cun-1; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }
        // 把每次删除的最后一个元素置空
        bookList.setBooks(null,cun-1);
        bookList.setUsedSize(cun-1);
        System.out.println("删除成功!");
    }
}

Display 显示所有书籍

package operations;
import book.Book;
import book.BookList;

/**
 *  显示图书
 */

public class Display implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书!");
        // 获取当前书的数量
        int cun = bookList.getUsedSize();
        for (int i = 0; i < cun; i++) {
            Book book = bookList.getPos(i);
            System.out.println(book);
        }
    }
}

Exitbook 退出系统

package operations;
import book.BookList;

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

        System.exit(0);
    }
}

BorrowOperation 借阅书籍

package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;


/**
 * 借阅书籍
 */
public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍!");
        System.out.println("请输入你要借阅书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int cun = bookList.getUsedSize();
        for (int i = 0; i < cun; i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())){
                if(book.isBorrowed()){
                    System.out.println("这本书以被借出!");
                }else {
                    book.setBorrowed(true);
                    System.out.println("借阅成功!");
                }
                return;
            }
        }
        System.out.println("没有你要借阅的书!");
    }
}

ReturnOper 归还书籍

package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;

/**
 *  归还书本
 */

public class ReturnOper implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍!");
        System.out.println("请输入你要归还书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int index = -1;
        int cun = bookList.getUsedSize();
        for (int i = 0; i < cun; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
        System.out.println("没有你要归还的书!");
    }
}

成员

管理员

package user;
import operations.*;
import java.util.Scanner;

/**
 *  管理员
 */

public class AdminUser extends User{

    // 初始化成员变量
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new Exitbook(),
                new Findbook(),
                new Addbook(),
                new Delbook(),
                new Display()
        };
    }
    public int menu(){
        System.out.println("*****************************");
        System.out.println("你好" + 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;

    }
}

用户

package user;
import operations.*;
import java.util.Scanner;

/**
 *   普通成员
 */
public class NormalUser extends User{

    //这是构造方法 初始化成员变量
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new Exitbook(),
                new Findbook(),
                new BorrowOperation(),
                new ReturnOper()
        };
    }
    public int menu(){
        System.out.println("*****************************");
        System.out.println("你好 " + 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;
    }
}

package user;
import book.BookList;
import operations.IOperation;

/**
 *
 */

public abstract class User {
    protected String name;
    protected IOperation[] iOperations;// 定义数组,没有初始化
    
    public User(String name) {
        this.name = name;
    }
    
    public abstract int menu();

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

主函数

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) {
        //实例化对象
        BookList bookList = new BookList();
        //登陆
        User user = login();
        while(true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }

    }
}
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到