关于面试:分享一个图书馆管理系统的java实战项目建议做java开发的都看看

49次阅读

共计 8526 个字符,预计需要花费 22 分钟才能阅读完成。

前言

该我的项目外围需要:

实现简略登录

管理员端实现下列性能

①查找书籍
②减少书籍
③删除书籍
④展现全副书籍
⑤退出零碎

通用户实现下列性能

①查问书籍
②借阅书籍
③偿还书籍
④退出零碎

我的项目类的设计展现

图书相干的类

Book:定义书籍的信息 BookList:示意书库,外面寄存书籍

package book;

/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021 -01-19
 * Time: 16:54
 */
public class Book {

    // 定义成员变量
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed = false; // 示意书的借阅状态

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
//        this.isBorrowed = isBorrowed;
    }

    public String getName() {return name;}

    public String getAuthor() {return author;}

    public int getPrice() {return price;}

    public String getType() {return type;}

    public boolean isBorrowed() {return isBorrowed;}

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

    public void setAuthor(String author) {this.author = author;}

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

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

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

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrowed == true) ? "借阅状态: 已借出" : "借阅状态: 未借出") +
                '}';
    }
}
package book;

/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021 -01-19
 * Time: 16:54
 */
public class BookList {

    public int usedSize = 3;  // 书架
    public Book[] books = new Book[10]; // 书的类型为 Book,用顺数组 book 去存储

    public BookList() {books[0] = new Book("三国演义","罗贯中", 100, "小说");
        books[1] = new Book("水浒传", "施耐庵", 100, "小说");
        books[2] = new Book("西游记", "吴承恩", 100, "小说");
    }

    // 给指定地位放书
    public void setBooks(int pos,Book book) {this.books[pos] = book;
    }

    // 拿到指定地位的书
    public Book getBooks(int pos){return this.books[pos];
    }

    public int getUsedSize() {return usedSize;}

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

对书库(程序表)操作的类

// 新增
public class AddOperation implements IOperation{
    @Override
    public void work(BookList booklist) {System.out.println("增加书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输出书名");
        String name = sc.nextLine();
        System.out.println("请输出书的作者");
        String author = sc.nextLine();
        System.out.println("请输出书的价格");
        int price = sc.nextInt();
        System.out.println("请输出书的类型");
        String type = sc.next();

        Book newBook = new Book(name,author,price,type); // 构建新书(对象)int size = booklist.getUsedSize(); // 通过 bookList 援用拜访以后程序表长度
        booklist.setBooks(size,newBook); // 将新书放在程序表最初面
        booklist.setUsedSize(size+1); // 程序表放了新书之后,长度加 1

    }
}

// 借阅
public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList booklist) {System.out.println("借阅书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输出书名");
        String name = sc.nextLine();  //name 为所要借阅书名

        for (int i = 0; i < booklist.getUsedSize(); i++) {
            // 通过 booklist 下标遍历每一本书
            Book book = booklist.getBooks(i);
            if(book.getName().equals(name)){
                // 如果为 true,阐明要借阅的书存在,咱们须要做的是批改书的借阅状态
                book.setBorrowed(true);  // 为 true 示意书曾经结出
                return ;
            }
        }
        System.out.println("十分道歉,本馆没有您要借阅的书!"); //
    }
}

// 删除
public class DelOperation implements IOperation{
    @Override
    public void work(BookList booklist) {System.out.println("删除书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输出书名");
        String name = sc.nextLine();  //name 为所要删除的书名

        int i = 0;
        for (; i < booklist.getUsedSize(); i++) {Book book = booklist.getBooks(i);
            if(book.getName().equals(name)){break;}
        }

        if(i >= booklist.getUsedSize()){System.out.println("没有要删除的这本书!");
            return ;
        }

        // 此时 i 为所要删除书的下标
        for (int j = i; j < booklist.getUsedSize()-1; j++) {Book book = booklist.getBooks(j+1); // 取得 j + 1 地位的书
            booklist.setBooks(j,book);  // 将 j + 1 地位的书给 j 地位
        }
        int size = booklist.getUsedSize();  // 取得程序表长度
        booklist.setUsedSize(size-1);   // 删除书后,长度减去 1
        System.out.println("书已被删除!");
    }
}

// 展现
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.getBooks(i);
            System.out.println(book);
        }
    }
}

// 查找
public class FindOperation implements IOperation{
    @Override
    public void work(BookList booklist) {System.out.println("查找书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输出书名");
        String name = sc.nextLine();  //name 为所要查找书名

        for (int i = 0; i < booklist.getUsedSize(); i++) {
            // 通过 booklist 下标遍历每一本书
            Book book = booklist.getBooks(i);
            if(book.getName().equals(name)){System.out.println("该书存在!");
                System.out.println(book);  // 间接打印书的信息,toString 办法已被重写
                return ;
            }
        }
        System.out.println("没有这本书!");
    }
}

// 偿还
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList booklist) {System.out.println("偿还书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输出书名");
        String name = sc.nextLine();  //name 为所要偿还的书名

        for (int i = 0; i < booklist.getUsedSize(); i++) {Book book = booklist.getBooks(i);
            if(book.getName().equals(name)){book.setBorrowed(false);
                System.out.println(book);  // 间接打印书的信息,toString 办法已被重写
                return ;
            }
        }
        System.out.println("没有你要偿还的这本书!");
    }
}

// 退出
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList booklist) {System.out.println("退出零碎");

        System.exit(1); // 示意退出零碎
    }
}

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

用户相干类

<mark style=”box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;”> 用户类 </mark>

package user;

import book.BookList;
import operation.IOperation;

/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021 -01-19
 * Time: 16:55
 */
abstract public class User {

    public String name;
    public IOperation[] operations;

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

    /*
    新建 menu 办法,了解为用户菜单
    因为 SpecialPerson 和 OrdinaryPerson 继承了 User,所以让两个子类重写 menu 办法,二者的菜单展现不统一
    此时 menu 能够没有具体实现,因此将它设计为形象办法,因而 User 类成为抽象类

     */
    abstract public int menu();

    //operations 中寄存的是哪些操作方法,得看子类
    public void doOperation(int choice, BookList bookList){this.operations[choice].work(bookList);
    }
}

<mark style=”box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;”> 管理员 </mark>

package user;
import operation.*;

import java.util.Scanner;
/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021 -01-19
 * Time: 16:56
 * 管理员
 */
public class SpecialPerson extends User{

    // 构造方法
    public SpecialPerson(String name){super(name);
        this.operations = new IOperation[]{new ExitOperation(), // 0 退出零碎
        new FindOperation(),
        new AddOperation(),
        new DelOperation(),
        new DisplayOperation()};
    }

    /*
    重写父类 User 的 menu 办法
    menu 作用:返回你要执行的操作
     */
    @Override
    public int menu() {System.out.println("管理员菜单!");

        System.out.println("=================================");
        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("================================");
        System.out.println("请依照提醒抉择相应操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;

    }
}

<mark style=”box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;”> 普通用户 </mark>

package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021 -01-19
 * Time: 16:57
 * 普通用户
 */
public class OrdinaryPerson extends User{

    // 构造方法
    public OrdinaryPerson(String name){super(name);

        this.operations = new IOperation[]{new ExitOperation(), // 0 退出零碎
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()};
    }

    // 重写父类 User 的 menu 办法
    @Override
    public int menu() {System.out.println("普通用户菜单!");

        System.out.println("=================================");
        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("================================");
        System.out.println("请依照提醒抉择相应操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

主函数程序

import book.BookList;
import user.OrdinaryPerson;
import user.SpecialPerson;
import user.User;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021 -01-19
 * Time: 16:30
 */
public class TrqTest {

    //login(登录)办法返回用户实例,类型为 User
    public static User login(){Scanner sc = new Scanner(System.in);
        System.out.println("请输出你的姓名:");
        String name = sc.nextLine();
        System.out.println("请输出你的身份 > 1: 管理员 0: 普通用户");
        int number = sc.nextInt();
        if(number == 1){return new SpecialPerson(name);
        }else{return new OrdinaryPerson(name);
        }
    }

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

        // 登录
        User user = login(); // 父类援用指向子类实例(向上转型)while(true) {//user.menu() 调用哪个 menu 办法,依据登录身份决定
            int choice = user.menu();

            // 此 doOperation 办法是以后 user 本人的办法
            //choice 抉择以后用户 operations 数组中的相应的类,该类产生的对象调用本人的 work 办法
            user.doOperation(choice, bookList);
        }

    }
}

运行一下看看具体怎么样吧


感激你看到这里,文章有什么有余还请斧正,感觉文章对你有帮忙的话记得给我点个赞,每天都会分享 java 相干技术文章或行业资讯,欢送大家关注和转发文章!
欢送关注公众号:前程有光,支付一线大厂 Java 面试题总结 + 各知识点学习思维导 + 一份 300 页 pdf 文档的 Java 外围知识点总结!

正文完
 0