图书管理系统

文章目录

  • 前言
  • 一、系统框架
  • 二、具体实现
    • 1.BOOK
    • 2.Operation
    • 3.USER
    • 4.Main


前言

总结Java的语法,围绕基础语法写一个图书管理系统,图书管理系统主要有管理员和普通用户两种身份,通过不同身份对图书进行不同操作的一个系统。

一、系统框架

该系统主要分为图书、功能、用户三个模块,图书模块用来存储图书信息;功能模块用来存储系统的操作功能,如添加图书、查找图书等;用户模块将用户分为普通用户和管理员两种身份。最后,通过Main函数对所有模块进行融合是最重要的地方。
图书管理系统_第1张图片
图书管理系统_第2张图片

二、具体实现

1.BOOK

Book(图书信息):

public class Book {
    private String name;//书名
    private String author;//作者
    private double price;//价格
    private String type;//图书类型
    private boolean isBorrow;//图书是否被借阅

    public Book(String name, String author, double 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 double getPrice() {
        return price;
    }

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

    public String getType() {
        return type;
    }

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

    public boolean isBorrow() {
        return isBorrow;
    }

    public void setBorrow(boolean borrow) {
        isBorrow = borrow;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrow=" + isBorrow +
                '}';
    }
}

BookList(书架信息):

public class BookList {
    private Book[] books = new Book[10];//书架
    private int UsedSize;//已经存放的图书个数
    public BookList() {
        books[0] = new Book("三国演义","罗贯中",19.9,"小说");
        books[1] = new Book("西游记","吴承恩",59.9,"小说");
        books[2] = new Book("水浒传","施耐庵",39.9,"小说");
        this.UsedSize = 3;
    }

    public int getUsedSize() {
        return UsedSize;
    }

    public void setUsedSize(int usedSize) {
        UsedSize = usedSize;
    }

    /**
     *获取某个位置上的书
     * @param pos 是在合法的情况下
     * @return
     */
    public Book getBook(int pos) {
        return books[pos];
    }

    /**
     *设置某个位置的图书
     * @param pos 是合法的情况下
     * @param book
     */
    public void setBook(int pos,Book book) {
        books[pos] = book;
    }
}

2.Operation

通过定义一个接口,其他各个功能通过实现接口完成各个操作工能。
接口:

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

查找图书:

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

展示图书:

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            System.out.println(bookList.getBook(i));
        }
    }
}

添加图书:

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图书的名字:");
        String name = sc.nextLine();
        System.out.println("请输入作者名字:");
        String author = sc.nextLine();
        System.out.println("请输入图书的类型:");
        String type = sc.nextLine();
        System.out.println("请输入图书的价格:");
        double price = sc.nextDouble();
        Book book = new Book(name,author,price,type);
        int size = bookList.getUsedSize();;
        bookList.setBook(size,book);
        bookList.setUsedSize(size+1);
        System.out.println("添加成功!");
    }
}

删除图书:

public class DelOperation implements IOperation{
    public void delete(int pos,BookList bookList) {
        for (int i = pos; i < bookList.getUsedSize()-1; i++) {
            bookList.setBook(i, bookList.getBook(i+1));
        }
    }
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入图书的名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                delete(i,bookList);
                bookList.setUsedSize(currentSize-1);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没有找到该图书");
    }
}

借阅图书:

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        System.out.println("请输入图书的名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                book.setBorrow(true);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

归还图书:

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        System.out.println("请输入图书的名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                book.setBorrow(false);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

退出系统:

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

3.USER

User(示例):

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

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

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

AdminUser(管理员):

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

    @Override
    public int menu() {
        System.out.println("hello "+this.name+" 欢迎来到图书管理系统");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");
        System.out.println("请输入你的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

NormalUser(普通用户):

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() {
        System.out.println("hello "+this.name+" 欢迎来到图书管理系统");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("请输入你的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

4.Main

Main:

public class Main {
    public static User login() {
        System.out.println("请输入你的姓名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入你的身份:1->管理员;0->普通用户");
        int choice = sc.nextInt();
        if(choice == 1) {
            return new AdminUser(name);
        } else {
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();//登录,发生动态绑定
        while (true) {
            int choice = user.menu();//根据用户的类型分别调用菜单
            user.doIOperation(choice,bookList);//根据choice实现某个功能
        }
    }
}

你可能感兴趣的:(JavaSE,java,c++,servlet)