码字不易,对你有帮助 点赞/转发/关注 支持一下作者
微信搜公众号:不会编程的程序圆
看更多干货,获取第一时间更新
【数据结构轻松学】系列 Github :https://github.com/hairrrrr/EasyJava
本文的代码已上传至 Github
快期末了,如果各位对自己的课设没有头绪,不妨借鉴一下这个简易的图书管理系统。
如果你有问题,欢迎与我交流!
图解实现难点
实现
book 包
Book.java
package book;
/*
* Description: 图书类
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 18:52
*/
public class Book{
private String name;
private String author;
private String type;
private boolean isBorrowed;
public Book(String name, String author, String type) {
this.name = name;
this.author = author;
this.type = type;
}
// 注:alt + insert 建可以快速生成
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 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 + '\'' +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
BookList.java
package book;
/*
* Description: 书单
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 18:57
*/
public class BookList {
private Book[] books = new Book[100];
private int usedSize = 0;
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
// 初始化三本书
public BookList(){
books[0] = new Book("挪威的森林", "村上春树", "小说");
books[1] = new Book("海边的卡夫卡", "村上春树", "小说");
books[2] = new Book("不能承受的生命之轻", "米兰·昆德拉", "小说");
this.usedSize = 3;
}
}
operation 包
AddBook.java
package operation;
/*
* Description: 新增图书
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:15
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddBook implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("========= 新增图书 =========");
Scanner scan = new Scanner(System.in);
System.out.println("请输入:书名");
String name = scan.nextLine();
System.out.println("请输入:作者");
String author = scan.nextLine();
System.out.println("请输入:书籍类型");
String type = scan.nextLine();
// 书单是否借出就不用单独的初始化了,不初始化默认为 false
int size = bookList.getUsedSize();
bookList.getBooks()[size] = new Book(name, author, type);
bookList.setUsedSize(size + 1); // 不能写 size++ !
System.out.println("========= 成功! =========");
}
}
BorrowBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:15
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 借阅图书 =========");
System.out.println("请输入:书名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int i;
Book book = null;
for(i = 0; i < bookList.getUsedSize(); i++){
book = bookList.getBooks()[i];
if(book.getName().equals(name)){
break;
}
}
if(i == bookList.getUsedSize()) {
System.out.println("抱歉,未能找到 " + "《" + name + "》");
return;
}
if(book.isBorrowed()){
System.out.println("抱歉," + "《" + name + "》" + "已经借出,过两天再来看看吧");
}
else {
book.setBorrowed(true);
System.out.println("========= 借书成功! =========");
}
}
}
DeleteBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:15
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DeleteBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 删除图书 =========");
System.out.println("请输入:书名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int i;
Book book = null;
int usedSize = bookList.getUsedSize();
for(i = 0; i < usedSize; i++){
book = bookList.getBooks()[i];
if(book.getName().equals(name)){
break;
}
}
if(i == usedSize) {
System.out.println("抱歉,未能找到 " + "《" + name + "》");
return;
}
for(; i < usedSize - 1; i++){
bookList.getBooks()[i] = bookList.getBooks()[i + 1];
}
// 如果是尾删,我们只是简单的将数组已经使用的大小减一;对象依然被数组引用,会造成内存泄漏
bookList.getBooks()[usedSize - 1] = null;
bookList.setUsedSize(usedSize - 1);
System.out.println("========= 删除成功 =========");
}
}
DisplayBooks.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:16
*/
import book.Book;
import book.BookList;
public class DisplayBooks implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("========= 展示图书 =========");
for(int i = 0; i < bookList.getUsedSize(); i++){
System.out.println(bookList.getBooks()[i].toString());
/*上面的语句等价于下面的:
Book[] books = bookList.getBooks();
Book book = books[i];
System.out.println(book.toString());*/
}
}
}
Exit.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:17
*/
import book.BookList;
public class Exit implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("========= 退出系统 =========");
System.exit(0);
}
}
FindBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:16
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 查询图书 =========");
System.out.println("请输入:书名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++){
Book book = bookList.getBooks()[i];
if(book.getName().equals(name)){
System.out.println(book.toString());
return;
}
}
System.out.println("抱歉,未能找到 " + "《" + name + "》");
}
}
ReturnBook.java
package operation;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:16
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("========= 归还图书 =========");
System.out.println("请输入:书名");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
for(int i = 0; i < bookList.getUsedSize(); i++){
Book book = bookList.getBooks()[i];
if(book.getName().equals(name)){
bookList.getBooks()[i].setBorrowed(false);
System.out.println("========= 归还归还 =========");
return;
}
}
System.out.println("抱歉,未能找到 " + "《" + name + "》");
}
}
IOperation.java
package operation;
import book.BookList;
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:17
*/
public interface IOperation {
void work(BookList bookList);
}
user 包
Admin.java
package user;
/*
* Description: 管理员
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:18
*/
import book.BookList;
import operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name){
super(name);
this.operations = new IOperation[]{
null,
new FindBook(),
new AddBook(),
new DeleteBook(),
new DisplayBooks(),
new Exit(),
};
}
@Override
public int menu() {
System.out.println("Hello, " + this.name);
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("***********************************");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
@Override
public void doOperation(BookList bookList, int choice) {
this.operations[choice].work(bookList);
}
}
NormalUser.java
package user;
/*
* Description: 普通用户
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:19
*/
import book.BookList;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.operations = new IOperation[]{
null,
new FindBook(),
new BorrowBook(),
new ReturnBook(),
new DisplayBooks(),
new Exit(),
};
}
@Override
public int menu() {
System.out.println("Hello, " + this.name);
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("***********************************");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
@Override
public void doOperation(BookList bookList, int choice) {
this.operations[choice].work(bookList);
}
}
User.java
package user;
/*
* Description: 用户的父类
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:28
*/
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] operations;
public User(String name){
this.name = name;
}
public abstract int menu();
public abstract void doOperation(BookList bookList, int choice);
}
Main.java
/*
* Description:
* User: Shepard Wang
* Date: 2020 -06 -10
* Time: 19:19
*/
import book.BookList;
import user.Admin;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scan = new Scanner(System.in);
System.out.println("请输入用户姓名");
String name = scan.nextLine();
System.out.println("请输入选择:0 -> 管理员 1 -> 用户");
int choice = scan.nextInt();
if(choice == 0){
// 向上转型
return new Admin(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
// 书单
BookList bookList = new BookList();
while(true) {
// 用户:
User user = login();
// 应用运行时绑定,父类引用会使用子类的 menu 方法
int choice = user.menu();
user.doOperation(bookList, choice);
}
}
}
查看【0 基础学 Java】更详细的目录: https://github.com/hairrrrr/EasyJava
不要忘记 star 呦~
欢迎大家在 评论区/私信 提出问题/指正错误,谢谢观看。
我是程序圆,我们下次再见。