首页 > 编程语言 >JavaSE(六) 图书管理系统

JavaSE(六) 图书管理系统

时间:2024-05-28 21:30:26浏览次数:25  
标签:管理系统 System bookList book println JavaSE public 图书 out

book包

Book类:

package book;

public class Book {
    private String name;
    private String autho;
    private double price;
    private String type;
    private boolean isLend;

    public Book(String name, String autho, double price, String type) {
        this.name = name;
        this.autho = autho;
        this.price = price;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAutho() {
        return autho;
    }

    public void setAutho(String autho) {
        this.autho = autho;
    }

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

    public void setLend(boolean lend) {
        isLend = lend;
    }

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

BookList类:

package book;

public class BookList {
    private Book[] books = new Book[10];
    private int userSized;

    public BookList() {
        books[0] = new Book("红楼梦","",45,"小说");
        books[1] = new Book("龙族","",47,"小说");
        books[2] = new Book("斗破苍穹","天蚕土豆",40,"小说");
        this.userSized = 3;
    }

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

    public int getUserSized() {
        return userSized;
    }

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

    public void setUserSized(int userSized) {
        this.userSized = userSized;
    }

    public boolean fullof() {
        if(this.userSized == books.length) {
            return true;
        }
        return false;
    }



}

user包

User类:

package user;

import book.BookList;
import operation.IOperation;

public abstract class User {
    protected String name;
    protected IOperation[] iOperation;
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

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

NormalUser类:

import operation.*;

import java.util.Scanner;

public class NormalUser extends User{


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

    @Override
    public int menu() {
        System.out.println("============普通用户菜单============");
        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);
        System.out.println("请输入你的选择:");
        int choice = sc.nextInt();
        return choice;
    }




}

AdministratorsUser类:

package user;

import operation.*;

import java.util.Scanner;

public class AdministratorsUser extends User {

    public AdministratorsUser(String name) {
        super(name);
        this.iOperation = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisOperation()};
    }

    @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("0.退出系统");
        System.out.println("==================================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的选择:");
        int choice = sc.nextInt();
        return choice;
    }
}

operation包

IOperation接口:

package operation;

import book.BookList;

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

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("新增图书......");
        if(bookList.fullof()) {
            System.out.println("书架存放的书已满,无法添加!");
            return;
        }
        System.out.println("请输入你要新增的图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String bookname = scanner.nextLine();
        System.out.println("请输入你要新增的图书的作者:");
        String autho = scanner.nextLine();
        System.out.println("请输入你要新增的图书的价格:");
        double price = scanner.nextDouble();
        scanner.nextLine();
        System.out.println("请输入你要新增的图书的类型:");
        String type = scanner.nextLine();

        Book book = new Book(bookname,autho,price,type);

        int correctSized = bookList.getUserSized();

        bookList.setBook(correctSized,book);

        bookList.setUserSized(correctSized+1);

        System.out.println("新增图书成功!");

    }


    }

BorrewordOperation类:

package operation;

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

public class BorrewordOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书......");
        //借阅图书
        //借助图书名借出
        System.out.println("请输入你要借阅的图书的名字:");
        Scanner scan = new Scanner(System.in);
        String bookname = scan.nextLine();

        int correctSized = bookList.getUserSized();
        for(int i = 0; i < correctSized; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookname)) {
                book.setLend(true);
                System.out.println("借阅成功!");
                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("删除图书......");
        System.out.println("请输入你要删除的图书:");
        Scanner scanner = new Scanner(System.in);
        String bookname = scanner.nextLine();
        int corruect = bookList.getUserSized();
        int pos = -1;//数组下标没有-1
        int i = 0;
        for(; i < corruect; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookname)) {
               //找到了这本书,我们要知道它的下标
                pos = i;
                break;
            }
        }
        if(i >= corruect) {
            //说明没有找到对应的书
            //直接返回就好
            System.out.println("无相应的书籍,请检查后再进行操作");
            return;
        }
        //说明此时有相应的书,我们进行删除操作

            for(int j = pos;j < corruect - 1; j++) {
                Book book = bookList.getBook(j+1);
                bookList.setBook(j,book);
            }
            bookList.setUserSized(corruect - 1);

            bookList.setBook(corruect - 1,null);

            System.out.println("删除完成!");



    }
}

DisOperation类:

package operation;

import book.Book;
import book.BookList;

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


    }
}

ExitOperation类:

package operation;

import book.BookList;
import book.Book;
public class ExitOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统......");
        int correctSized = bookList.getUserSized();
        for(int i = 0; i < correctSized; i++) {
            bookList.setBook(i,null);
        }
        System.exit(0);
    }
}

FindOperation类:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("查找图书......");
        System.out.println("请输入你要查找的图书:");
        Scanner scanner = new Scanner(System.in);
        String bookname = scanner.nextLine();
        int corruect = bookList.getUserSized();
        for(int i = 0; i < corruect; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookname)) {
                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("归还图书......");
        System.out.println("请输入你要归还的图书的名字:");
        Scanner scan = new Scanner(System.in);
        String bookname = scan.nextLine();
        int correctSized = bookList.getUserSized();
        for(int i = 0; i < correctSized; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookname)) {
                //找到了
               book.setLend(false);
                System.out.println("归还成功!");
                return;
            }

        }
        System.out.println("归还失败!!");
    }
}

 Main类(测试):

import book.BookList;
import user.AdministratorsUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {

    public static User login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的名字:");
        String name = sc.next();
        System.out.println("请输入你的身份: 1.管理员   2.普通用户   ");
        int choice = sc.nextInt();
        if(choice == 1) {
            AdministratorsUser administratorsUser = new AdministratorsUser(name);
            return administratorsUser;
        } else {
            NormalUser normalUser = new NormalUser(name);
            return normalUser;
        }


    }

    public static void main(String[] args) {
        BookList bookList = new BookList();

     User user = login();
     while(true) {
         int choice = user.menu();
         user.doOperation(choice,bookList);

     }

    }

}

标签:管理系统,System,bookList,book,println,JavaSE,public,图书,out
From: https://blog.csdn.net/2301_79760109/article/details/139251114

相关文章