1.确定功能

进入系统前要输入姓名并选择身份,进入对应身份的菜单中选择操作,不同身份下的菜单是不同的

所以要设计两份菜单 ,基础功能共有七类

2.确定包和基本功能

      具体需要几个包及各自的功能

1.book    需要定义好书本(包括书名、作者、价格、种类、是否被借出)和书架(数组)后续的                   操作都需要在书架中操作

2.user     确定好两中身份下的菜单,将各自身份下的功能列举出来(数组)

3.operation   将其中功能初始化好(用到接口,方便后续生成功能的数组)

3.搭建基本框架

3.1 book包

1.Book

初始化好类的属性(用private修饰),set和get,toString

public class Book {
    private  String name;
    private  String author;
    private  int price;
    private  String type;
    private  boolean isBorrowed;

    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='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ( (isBorrowed==true)?"已借出":"未借出") +
                '}';
    }
}

2.Booklist

书架(数组)其中默认放入三本书,并用usedSIze表示书架中图书的数量,set和get功能

特殊之处在于需要一个函数返回整个书架(数组)的长度,为后续新增图书打基础

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

    public  BookList() {
       this.books[0]=new Book("三国演义","罗贯中",10,"小说");
       this.books[1]=new Book("西游记","吴承恩",12,"小说");
       this.books[2]=new Book("红楼梦","曹雪芹",11,"小说");

       this.usedSize=3;
    }

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

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

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book[] getBooks(){
        return books;
    }
}

3.2 user包

1.User(抽象)

初始化好姓名

菜单(需要用户选择功能并返回选择值)

初始化功能数组(在搭建好Operation包后弄)

需要一个函数(doOperation)来接收用户选择的功能,调用功能数组中对应的功能

public abstract class User {
    String name;
    public IOperation[] operations=new IOperation[10];
    public User(String name) {
        this.name = name;
    }

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

2.AdminiUser(管理员)

public class AdminiUser extends User {
    public AdminiUser(String name) {
        super(name);
        this.operations=new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation(),
        };

    }
    @Override
    int manu() {
        System.out.println("***欢迎"+name+"来到图书馆****");
        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 scanner=new Scanner(System.in);
        System.out.println("请输入你的操作:");
        int choice =scanner.nextInt();
        return choice;
    }
}

3.NormalUser(普通用户)

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

    @Override
    int manu() {
        System.out.println("");
        System.out.println("****欢迎"+name+"来到图书馆****");
        System.out.println("*********普通用户菜单*********");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("****************************");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的操作:");
        int choice =scanner.nextInt();
        return choice;
    }
}

4.Main(主函数)

需要一个返回User型的函数(login)返回用户选择的身份,方便后续调用对应的菜单

public class Main {
    public static User login(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名");
        String name=scanner.nextLine();
        System.out.println("请输入你的身份  1.管理员  2.普通用户 ");
        int user = scanner.nextInt();
        if(user==1){
            return new AdminiUser(name);
        }else{
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        User user=login();
        BookList bookList=new BookList();
        while(true){
            int choice=user.manu();
            user.doOperation(choice,bookList);
        }
    }

}

3.3 operation包

1.  IOperation接口

后续的功能函数都需要  implements IOperation

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

2.AddOpration(新增图书)

public class AddOperation  implements IOPeration{
    public void work(BookList bookList) {
        System.out.println("新增图书.....");

        //1. 判满
        int currentSize = bookList.getUsedSize();
        if(currentSize == bookList.getBooks().length) {
            System.out.println("书架满了 不能放了.....");
            return;
        }


        //2. 构建对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();

        System.out.println("请输入作者:");
        String author = scanner.nextLine();

        System.out.println("请输入书的类型:");
        String type = scanner.nextLine();

        System.out.println("请输入价格:");
        int price = scanner.nextInt();



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

        //3. 判断书架有没有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("有这本书不能插入!");
                return;
            }
        }

        //4.插入这本书
        bookList.setBook(currentSize,newBook);

        bookList.setUsedSize(currentSize+1);

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

    }
}

3.BorrowOperation(借阅图书)

判断有这本书的前提下,只需要看这本书isBorrowed是true还是false

true:已经被借走了

false:用set改为true

public class BorrowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");


        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你借阅的书名:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);

            if(book.getName().equals(name)) {
                if(book.isBorrowed()) {
                    System.out.println("这本书已经被借出了!");
                    return;
                }
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有你要借阅的这本书....");
    }
}

4.DelOperation(删除图书)

public class DelOperation implements IOPeration{
    public void work(BookList bookList) {
        System.out.println("删除图书.....");

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你删除的书名:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();

        int pos = -1;
        //找到这本书
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        if(i == currentSize) {
            System.out.println("没有你要删除的书!");
            return ;
        }

        //这里开始删除
        for (int j = pos; j < currentSize-1; j++) {
            //bookList[j] = bookList[j+1];
            Book book = bookList.getBook(j+1);

            bookList.setBook(j,book);
        }

        bookList.setBook(currentSize-1,null);

        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功!!!!");
    }
}

5.ExitOperation(离开系统)

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

6.FindOperation(查找图书)

public class FindOperation implements IOPeration{
    public void work(BookList bookList) {
        System.out.println("查找图书.....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你的书名:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("找到了这本书:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要找的这本书....");
    }
}

7.Return OPeration(归还图书)

public class ReturnOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你归还的书名:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);

            if(book.getName().equals(name)) {
                if(book.isBorrowed()) {
                    book.setBorrowed(false);
                    System.out.println("归还成功!");
                    return;
                }
            }
        }
        System.out.println("没有你要归还的图书!!!");
    }
}

8.ShowOperation(显示图书)

public class ShowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书....");
        int currentSize = bookList.getUsedSize();//3
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
            //Book book1 = bookList[i];
        }
    }
}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐