Java面向对象总结性项目 之 图书馆管理代码

Java面向对象总结性项目 之 图书馆管理代码

  • 简介
  • 核心需求
  • 类的设计
    • 架构
    • 创建图书相关的类
    • 创建书架相关的类
    • 创建用户相关的类
      • 对于管理员
      • 对于用户
    • 创建 Main 类和 main 方法, 搭建整体逻辑
    • 实现具体的每个 Operation
  • 该Java程序运行结果
    • 用户端
    • 管理员

简介

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

核心需求

  1. 简单的登录功能
  2. 对于管理员,实现以下功能:
    查找书籍
    增加书籍
    删除书籍
    显示所有图书
    退出系统
  3. 对于用户,实现以下功能:
    查询书籍
    借阅书籍
    归还书籍
    退出系统

类的设计

架构

该Java程序架构如下
Java面向对象总结性项目 之 图书馆管理代码_第1张图片

创建图书相关的类

package book;

public class Book {
     
    private String name;//书名
    private String author;//作者
    private int price;//价格
    private String type;//类型
    private boolean status;//状态

    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 isStatus() {
     
        return status;
    }

    public void setStatus(boolean status) {
     
        this.status = status;
    }

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

创建书架相关的类

package book;

public class BookList {
     

    private Book[] books;
    private int usedSize;

    public BookList() {
     
        this.books = new Book[10];
        books[0] = new Book("三国演义","罗贯中",56,"小说");
        books[1] = new Book("水浒传","施耐庵",58,"小说");
        books[2] = new Book("西游记","吴承恩",66,"小说");
        books[3] = new Book("红楼梦","曹雪芹",46,"小说");
        this.usedSize = 4;
    }
    //尾插法
    public void setBooks(int pos,Book book){
     
        this.books[pos] = book;
    }

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

    public int getUsedSize() {
     
        return usedSize;
    }

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

创建用户相关的类

package user;

import book.BookList;
import operation.IOperation;

public abstract class User {
     
    public String name;
    public IOperation[] operations;

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

    public abstract int menu();

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

对于管理员

package user;

import operation.*;

import java.util.Scanner;

/**
 * 管理员
 */
public class AdminUser extends User{
     
    public AdminUser(String name) {
     
        super(name);
        this.operations = new IOperation[]{
     
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }

    @Override
    public int menu() {
     
        Scanner scanner = new Scanner(System.in);
        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("===========================");
        int choice = scanner.nextInt();
        return choice;
    }
}

对于用户

package user;

import operation.*;

import java.util.Scanner;

/**
 * 普通用户
 */
public class NormalUser extends User{
     
    public NormalUser(String name) {
     
        super(name);
        this.operations = 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("1.查找书籍");
        System.out.println("2.借阅书籍");
        System.out.println("3.归还书籍");
        System.out.println("0.退出系统");
        System.out.println("===========================");
        int choice = scanner.nextInt();
        return choice;
    }
}

创建 Main 类和 main 方法, 搭建整体逻辑

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){
     
            return new AdminUser(name);
        }else {
     
            return new NormalUser(name);
        }
    }
    
    //main方法
    public static void main(String[] args) {
     
        BookList bookList = new BookList();
        //1.登录 user 会引用一个对象,这个对象可能是AdminUser或者NormalUser
        User user = login();
        while (true) {
     
            int choice = user.menu();
            //用choice去调用对应的类的work方法
            user.doOperation(choice, bookList);
            /*
            会调用user引用的对象的menu方法
            返回对应的选择
             */
            if (choice == 0){
     
                break;
            }
        }
    }
}

实现具体的每个 Operation

  1. 管理端
    查阅书籍
    增加书籍
    删除书籍
    打印书籍列表
    退出

  2. 用户端
    查询书籍
    借阅书籍
    归还书籍
    退出

AddOperation实现添加功能

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("添加书籍");
        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.next();

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

        bookList.setBooks(currentSize,book);
        bookList.setUsedSize(currentSize+1);

    }
}

BorrowOperation 实现借阅功能

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

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

        for (int i = 0; i < bookList.getUsedSize() ; i++) {
     
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
     
                //条件成立,说明有这本书
                //逻辑:将状态修改为true
                book.setStatus(true);
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要借阅的书");
    }
}

DelOperation 实现删除功能

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);
        System.out.println("请输入要删除图书的书名");
        String name = scanner.nextLine();
        int i = 0;
        for ( ; i < bookList.getUsedSize(); i++) {
     
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
     
                //条件成立,说明有这本书
                break;
            }
        }
        if (i >= bookList.getUsedSize()) {
     
            System.out.println("没有想要删除的这本书");
            return;
        }
        int pos = i;
        for (int j = pos; j <  bookList.getUsedSize() -1; j++) {
     
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        int currentSize = bookList.getUsedSize();
        bookList.setUsedSize(currentSize-1);
        System.out.println("成功将该书籍删除");
    }
}

DisplayOperation 实现展示功能

package operation;

import book.Book;
import book.BookList;

public class DisplayOperation implements IOperation {
     
    @Override
    public void work(BookList bookList) {
     
        System.out.println("显示所有图书");
        for (int i = 0;i < bookList.getUsedSize();i++) {
     
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

ExitOperation 实现退出功能

package operation;

import book.BookList;

public class ExitOperation implements IOperation{
     
    @Override
    public void work(BookList bookList) {
     
        System.out.println("退出系统");
        System.exit(1);
    }
}

FindOperation 实现查找功能

package operation;

import book.Book;
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);
        System.out.println("请输入要查找的书名");
        String name = scanner.nextLine();

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

ReturnOperation 实现归还功能

package operation;

import book.Book;
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);
        System.out.println("请输入要归还图书的书名");
        String name = scanner.nextLine();
        int i = 0;
        for ( ; i < bookList.getUsedSize(); i++) {
     
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
     
                //条件成立,说明有这本书
                book.setStatus(false);
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有此书籍");
    }
}

IOperation 实现接口

package operation;

import book.BookList;

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

该Java程序运行结果

用户端

请输入你的姓名:
ykk
请输入你的身份:1:管理员 0:普通用户
0
===========================
1.查找书籍
2.借阅书籍
3.归还书籍
0.退出系统
===========================
1
查找书籍
请输入要查找的书名
水浒传
有这本书
Book{
     name='水浒传', author='施耐庵', price=58, type='小说', status=‘未借出’}
===========================
1.查找书籍
2.借阅书籍
3.归还书籍
0.退出系统
===========================
2
借阅书籍
请输入借阅图书的书名
水浒传
Book{
     name='水浒传', author='施耐庵', price=58, type='小说', status=‘借出’}
===========================
1.查找书籍
2.借阅书籍
3.归还书籍
0.退出系统
===========================
1
查找书籍
请输入要查找的书名
水浒传
有这本书
Book{
     name='水浒传', author='施耐庵', price=58, type='小说', status=‘借出’}
===========================
1.查找书籍
2.借阅书籍
3.归还书籍
0.退出系统
===========================
3
归还书籍
请输入要归还图书的书名
水浒传
Book{
     name='水浒传', author='施耐庵', price=58, type='小说', status=‘未借出’}
===========================
1.查找书籍
2.借阅书籍
3.归还书籍
0.退出系统
===========================
0
退出系统

管理员

请输入你的姓名:
ykk
请输入你的身份:1:管理员 0:普通用户
1
===========================
1.查找书籍
2.添加书籍
3.删除书籍
4.显示书籍
0.退出系统
===========================
1
查找书籍
请输入要查找的书名
水浒传
有这本书
Book{
     name='水浒传', author='施耐庵', price=58, type='小说', status=‘未借出’}
===========================
1.查找书籍
2.添加书籍
3.删除书籍
4.显示书籍
0.退出系统
===========================
2
添加书籍
请输入书名
Java
请输入图书的作者
ykk
请输入图书的价格
49
请输入图书的类型
学术
===========================
1.查找书籍
2.添加书籍
3.删除书籍
4.显示书籍
0.退出系统
===========================
1
查找书籍
请输入要查找的书名
Java
有这本书
Book{
     name='Java', author='ykk', price=49, type='学术', status=‘未借出’}
===========================
1.查找书籍
2.添加书籍
3.删除书籍
4.显示书籍
0.退出系统
===========================
3
删除书籍
请输入要删除图书的书名
Java
成功将该书籍删除
===========================
1.查找书籍
2.添加书籍
3.删除书籍
4.显示书籍
0.退出系统
===========================
4
显示所有图书
Book{
     name='三国演义', author='罗贯中', price=56, type='小说', status=‘未借出’}
Book{
     name='水浒传', author='施耐庵', price=58, type='小说', status=‘未借出’}
Book{
     name='西游记', author='吴承恩', price=66, type='小说', status=‘未借出’}
Book{
     name='红楼梦', author='曹雪芹', price=46, type='小说', status=‘未借出’}
===========================
1.查找书籍
2.添加书籍
3.删除书籍
4.显示书籍
0.退出系统
===========================
0
退出系统

你可能感兴趣的:(java,面向对象编程,intellij,idea)