Java面向对象-- “ 图书管理系统 ”

运行环境:IDEA

1 需求

1、实现根据不同身份登陆;
2、用户身份:管理员、普通用户
管理员实现功能:增加图书、删除图书、查看所有书籍的列表、查阅某个书籍的信息、退出程序;
普通用户:借阅书籍、归还书籍、查阅某个书籍的信息、退出程序。

2 代码实现

  我们找到这个系统的一个个的对象,逐步来实现。

2.1 Book、BookList

  图书管理系统,首先最重要的就是书。
  在创建类里面的属性时,明确封装是面向对象的重要核心之一,设计这个 book 类的时候,book 类的属性,尽可能封装起来,用 private,使其他的程序猿能不用理解具体细节,降低使用成本。

package Test1_28_LibraryManagementSystem;

public class Book {
     
    private String name;
    private String author;
    private double price;
    private String type;
    // 默认是false ,但是显示的写出来可读性更好
    private boolean isBorrowed = false;

    // 每本书的名字、价格、作者、类型,可能不同
    // 使用构造方法,构造的时候以传入参数的形式,确定具体的值
    // 构造实例时,显示的创建相关参数,来传入相关信息
    public Book(String name, String author, double price, String type) {
     
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

// 重写 toString 方法才能在打印书籍信息时,以字符串的样子打印出来
    @Override
    public String toString() {
     
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
// getter / setter 和封装是相悖的 ,但是封装并不是百分百藏起来,完全包裹起来调用者不好使用。
    public String getName() {
     
        return name;
    }

    public boolean isBorrowed() {
     
        return isBorrowed;
    }

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

为了记录所有书籍信息,创建一个书籍列表

package Test1_28_LibraryManagementSystem;

public class BookList {
     
    Book books[] = new Book[100];
    int size = 0;

// 这样就写死了这些书,创建实例它们就存在
    public BookList() {
     
        books[0] = new Book("西游记","吴承恩",50.0,"古典名著");
        books[1] = new Book("水浒传","施耐庵",50.0,"古典名著");
        books[2] = new Book("三国演义","罗贯中",50.0,"古典名著");
        books[3] = new Book("红楼梦","曹雪芹",50.0,"古典名著");
        size = 4;
    }

// 通过 getter和setter 来获取和修改书籍列表 
    public Book getBook(int index) {
     
        return books[index];
    }

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

    public int getSize() {
     
        return size;
    }

    public void setSize(int size) {
     
        this.size = size;
    }
}

2.2 使用者

1、管理员

package Test1_28_LibraryManagementSystem;
import Test1_28_LibraryManagementSystem.IOperation.*;
import java.util.Scanner;

public class Admin extends User{
     

    public Admin(String name) {
     
        this.name = name;
        this.operations = new IOperation[]{
     
                new AddOperation(),
                new DelOperation(),
                new DisplayBookList(),
                new FindOperation(),
                new ExitOperation()
        };
    }

    @Override
    public int menu() {
     
        System.out.println("*********************");
        System.out.println(" 1、新增书籍 ");
        System.out.println(" 2、删除书籍 ");
        System.out.println(" 3、展示书籍列表 ");
        System.out.println(" 4、查找某本书籍 ");
        System.out.println(" 5、退出系统 ");
        System.out.println("*********************");
        System.out.println("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

2、普通用户

package Test1_28_LibraryManagementSystem;
import Test1_28_LibraryManagementSystem.IOperation.*;
import java.util.Scanner;

public class NormalUser extends User{
     

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

    public int menu(){
     
        System.out.println("*********************");
        System.out.println(" 1、借阅书籍 ");
        System.out.println(" 2、归还书籍 ");
        System.out.println(" 3、查找某本书籍 ");
        System.out.println(" 4、退出系统 ");
        System.out.println("*********************");
        System.out.println("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

3、用户
创建抽象类 User ,这样在使用时,调用者可以不关注是管理员还是普通用户,管理员和用户继承 User 。

package Test1_28_LibraryManagementSystem;
import Test1_28_LibraryManagementSystem.IOperation.IOperation;

abstract public class User {
     
    protected String name;
    protected IOperation[] operations;
    public abstract int menu();

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

}

2.3 操作

  设计用户操作,每种类都可以设置成一个类,每个操作类中都有一个 work 方法,public 修饰、返回void、传入BookList。因为他们有共性,就可以把共性信息提取出来,做成一个接口。(消除重复 并且 在不相关的类之间建立联系)
  当前 IOperation 这个接口其实就是把所有的用户操作给统一规范起来了,什么样的类作为一种用户的操作?只要实现了这个接口,就可以作为用户的操作。
1、接口

package Test1_28_LibraryManagementSystem.IOperation;

import Test1_28_LibraryManagementSystem.BookList;

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

注意:
如果多各类之间,公共的部分只有方法的话,此时使用接口;
如果多个类之间,公共的部分不光有方法还有属性,此时使用继承(普通父类 / 抽象父类)。

2.3.1 管理员操作

1、新增书籍

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.Book;
import Test1_28_LibraryManagementSystem.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.next();
        System.out.println("请输入书籍作者的名字:");
        String author = scanner.next();
        System.out.println("请输入书籍的价格:");
        Double price = scanner.nextDouble();
        System.out.println("请输入书籍的类型:");
        String type = scanner.next();
        Book newBook = new Book(name,author,price,type);
        bookList.setBook(bookList.getSize(),newBook);
        bookList.setSize(bookList.getSize() + 1);
    }
}

2、删除书籍

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;
import java.util.Scanner;
import java.util.jar.JarOutputStream;

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.next();
        int i = 0;
        for (; i < bookList.getSize(); i++) {
     
            if(name.equals(bookList.getBook(i).getName())) {
     
                if(i == bookList.getSize() - 1) {
     
                    bookList.setSize(bookList.getSize() - 1);
                    System.out.println("删除成功");
                    return;
                }
                bookList.setBook(i,bookList.getBook(bookList.getSize() - 1));
                bookList.setSize(bookList.getSize() - 1);
                System.out.println("删除成功");
                return;
            }
        }
        if (i >= bookList.getSize() - 1) {
     
            System.out.println("未找到《" + name + "》,删除失败");
        }
    }
}

3、展示书籍列表

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;

public class DisplayBookList implements IOperation{
     
    @Override
    public void work(BookList bookList) {
     
        System.out.println("展示书籍列表 >> ");
        for (int i = 0; i < bookList.getSize(); i++) {
     
            System.out.println(bookList.getBook(i));
        }
    }
}

4、查找某本书籍信息

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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.next();
        int i = 0;
        for (; i < bookList.getSize(); i++) {
     
            if(bookList.getBook(i).getName().contains(name)) {
     
                System.out.println(bookList.getBook(i));
                System.out.println("查找成功");
                return;
            }
        }
        if(i >= bookList.getSize() - 1) {
     
            System.out.println("未找到《" + name + "》,查找失败");
        }
    }
}

5、退出系统

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;

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

2.3.2 用户操作

1、借阅书籍

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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.next();
        int i = 0;
        for (; i < bookList.getSize(); i++) {
     
            if(name.equals(bookList.getBook(i).getName())) {
     
                if(!bookList.getBook(i).isBorrowed()) {
     
                    bookList.getBook(i).setBorrowed(true);
                    System.out.println("借阅成功");
                    return;
                }
                System.out.println("《" + name + "》已被借阅,借阅失败");
                return;
            }
        }
        if(i >= bookList.getSize() - 1) {
     
            System.out.println("未找到 《" + name + "》,借阅失败");
        }
    }
}

2、归还书籍

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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.next();
        for (int i = 0; i < bookList.getSize(); i++) {
     
            if(name.equals(bookList.getBook(i).getName())) {
     
                if(!bookList.getBook(i).isBorrowed()) {
     
                    System.out.println("《" + name + "》未被借阅,归还失败");
                    return;
                }
                bookList.getBook(i).setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("未找到《" + name + "》,归还失败");
    }
}

3、查找某本书的信息( 与管理员的查找相同 )

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.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.next();
        int i = 0;
        for (; i < bookList.getSize(); i++) {
     
            if(bookList.getBook(i).getName().contains(name)) {
     
                System.out.println(bookList.getBook(i));
                System.out.println("查找成功");
                return;
            }
        }
        if(i >= bookList.getSize() - 1) {
     
            System.out.println("未找到《" + name + "》,查找失败");
        }
    }
}

4、退出系统( 与管理员的查找相同 )

package Test1_28_LibraryManagementSystem.IOperation;
import Test1_28_LibraryManagementSystem.BookList;

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

2.4 调用

package Test1_28_LibraryManagementSystem;
import java.util.Scanner;

public class Main {
     
    public static void main(String[] args) {
     
        BookList bookList = new BookList();
        // 向上转型,login返回管理员或普通用户
        User user = login();
        while (true) {
     
        //多态
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }

    }

    private static User login() {
     
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的姓名:");
        String name = scanner.next();
        System.out.println(name + "欢迎使用!");
        System.out.println("请选择您的身份:");
        System.out.println(" 0、管理员  1、用户 ");
        int who = scanner.nextInt();
        if(who == 0){
     
            return new Admin(name);
        }
        return new NormalUser(name);
    }
}

你可能感兴趣的:(Java,java,封装)