目录
一、package book
1.1. Book
1.2. BookList
二、package user
2.1. User
2.2. NormalUser与AdminiUser
三、Main
四、NormalUser与AdminiUser的菜单界面
五、package operation
5.1. 设计管理员菜单
六、业务逻辑
七、完整代码
今天博主来带大家实现一个图书管理系统。今天的这期博客我们需要用的知识点有:java的基础语法、类和对象、继承与多态、抽象类接口。
在这个系统里面,我们需要创建的对象有书、书架、管理员、普通用户。 这么多对象,我们就要使用包来进行分类。我们先创建三个包:book、user、operation。
一、package book
1.1. Book
package book;public class Book {private String title;//书籍名称private String author;//作者private int price;//价格private String type;//类型private boolean BeBorrowed;//是否被借出public Book(String title, String author, int price, String type, boolean beBorrowed) {this.title = title;this.author = author;this.price = price;this.type = type;BeBorrowed = beBorrowed;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}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 isBeBorrowed() {return BeBorrowed;}public void setBeBorrowed(boolean beBorrowed) {BeBorrowed = beBorrowed;}@Overridepublic String toString() {return "Book{" +"title='" + title + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +", BeBorrowed=" + BeBorrowed +'}';}
}
我们定义的成员变量如上代码所示,我们只需要创建成员变量,剩下的都可以用编译器帮我们生成。
1.2. BookList
package book;public class BooList {private Book[] books;//存放书籍private static final int DEFAULT_SIZE = 10;private int usedSize; // 有效书籍的个数public BooList(Book[] books) {this.books = books;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}
}
我们的书架要存储很多的书,那么我们就用一个数组来存储。我们利用SIZE来确定数组的长度。如果说,这个书架最多能放下10本书,而我们手上需要有三本书需要存放,那我们就定义一个有效书籍这个变量。
二、package user
2.1. User
package user;public abstract class User {public String name;public User(String name){this.name = name;}public abstract void menu();
}
用户分为普通用户与管理员,后两者需要继承用户,我们就可以对用户这个类进行抽象化。我们在User类里面同时在写一个menu方法(且这个方法无具体表现)。
2.2. NormalUser与AdminiUser
package user;public class NormalUser extends User{public NormalUser(String name) {super(name);}@Overridepublic void menu() {System.out.println("普通用户菜单");}
}package user;public class AdminiUser extends User{public AdminiUser(String name) {super(name);}@Overridepublic void menu() {System.out.println("管理员菜单");}
}
三、Main
通过Main这个类,我们可以来进行一些操作,比如输入姓名,选择身份,查找图书等。我们在main方法之前,先写一个登录方法,因为我们的返回值是User的子类,所以把返回值类型void改成User。我们在这里可以先运行一下。
import user.AdminiUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {public static User Login() {Scanner sca = new Scanner(System.in);System.out.println("请输入你的姓名:");String name = sca.nextLine();System.out.println("请输入你的身份:1.管理员 2.普通用户");int choice = sca.nextInt();if(choice == 1){return new AdminiUser(name);}else{return new NormalUser(name);}}public static void main(String[] args) {User user = Login();user.menu();}
}
四、NormalUser与AdminiUser的菜单界面
@Overridepublic int menu() {System.out.println("欢迎"+this.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 sca = new Scanner(System.in);int choice = sca.nextInt();return choice;}
我们需要根据用户的选择来确定对应的菜单,并返回我们的choice,此时我们要对重写的menu方法的返回值也要进行修改,把void也要改成int。同样地,User类里面的抽象方法的返回值也要改成int。普通用户的菜单也同理。
@Overridepublic int menu() {System.out.println("欢迎"+this.name+"图书管理系统");System.out.println("**********普通用户菜单**********");System.out.println("1.查找图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("0.退出系统");System.out.println("****************************");Scanner sca = new Scanner(System.in);int choice = sca.nextInt();return choice;}
五、package operation
下面就是要根据用户对菜单的功能进行分类。分类要按照我们Main里面的选择来确定这个人是管理员还是普通用户。也就是通过choice的值来调用那个菜单。
5.1. 设计管理员菜单
首先我们先来设计查找或者是图书,就要在书架里面遍历数组来查找我们的图书。 那我们如何对这些功能进行分类呢?那么这些功能就得有一个统一的类型,所以说我们下一步就是要设计统一的类型。我们可以使用一个数组,并且这个数组还得是最高的类型。那我们就可以新建一个接口IOperation。我们利用数组的下标来进行对功能的选择。普通用户的菜单也同理。
public AdminiUser(String name) {super(name);this.iOperations = new IOperation[] {new ExitSystem(),new FindBook(),new AddBook(),new DeleteBook(),new DisplayBook()};}
当我们选择用户并进入菜单界面之后,我们就需要调用方法来对我们的功能进行实现。下面的方法通过调用就可以帮助我们实现时调用管理员还是普通用户。因为我们已经在BooList类里面实现了一个数组,所以我们直接可以把Book类换成BooList类,当然因为两个对象在不同包里面,我们就需要在每个功能里面导入"import book.BooList;"。
public interface IOperation {void work(BooList books);
}public void doOperation(int choice, BooList books){IOperation iOperation = this.iOperations[choice];iOperation.work(books);}
下面我们就可以给成员变量赋值了。
public BooList() {this.books = new Book[DEFAULT_SIZE];this.books[0] = new Book("骆驼祥子","老舍",26,"小说",false);this.books[1] = new Book("雷雨","曹禺",24,"戏剧",true);this.books[2] = new Book("再别康桥","徐志摩",19,"诗歌",false);this.books[3] = new Book("五猖会","鲁迅",21,"散文",false);
}
六、业务逻辑
我们基本的框架已经搭建好了,下面我们该实现我们的业务逻辑。下面我们分别实现查找、借阅、归还的业务。
public class FindBook implements IOperation{@Overridepublic void work(BooList booList) {System.out.println("查找图书...");Scanner sca = new Scanner(System.in);String name = sca.nextLine();1.先确定 当前数组当中 有效的书籍个数int currentSize = booList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = booList.getBook(i);if(book.getTitle().equals(name)) {System.out.println("找到这本书籍了,书籍内容如下:");System.out.println(book);return;}}System.out.println("没有你要找的书籍。");}
}
public class ExitSystem implements IOperation{@Overridepublic void work(BooList books) {System.out.println("退出系统...");int currentSize = books.getUsedSize();for (int i = 0; i < currentSize; i++) {
// books[i] = null;books.setBooks(i,null);}System.exit(0);}
}
public class BorrowBook implements IOperation{@Overridepublic void work(BooList books) {System.out.println("借阅图书...");System.out.println("请输入你要借阅的图书:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();//1.先遍历数组 查找是否存在要借阅的图书int currentSize = books.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = books.getBook(i);if(book.getTitle().equals(name)) {//2.如果存在 检查 是否已经被借出if (book.isBeBorrowed()) {System.out.println("这本书已经被借出了!");} else {//3. 如果没有借出 可以借book.setBeBorrowed(true);System.out.println(book);System.out.println("借阅成功!!");}return;}}//4.如果不存在 则不能借阅System.out.println("没有你要找的这本书,无法借阅!!");}
}
七、完整代码
import book.BooList;
import user.AdminiUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {public static User Login() {Scanner sca = new Scanner(System.in);System.out.println("请输入你的姓名:");String name = sca.nextLine();System.out.println("请输入你的身份:1.管理员 2.普通用户");int choice = sca.nextInt();if(choice == 1){return new AdminiUser(name);}else{return new NormalUser(name);}}public static void main(String[] args) {BooList booList = new BooList();User user = Login();int choice = user.menu();user.doOperation(choice,booList);}
}
package book;public class Book {private String title;//书籍名称private String author;//作者private int price;//价格private String type;//类型private boolean BeBorrowed;//是否被借出public Book(String title, String author, int price, String type, boolean beBorrowed) {this.title = title;this.author = author;this.price = price;this.type = type;BeBorrowed = beBorrowed;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}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 isBeBorrowed() {return BeBorrowed;}public void setBeBorrowed(boolean beBorrowed) {BeBorrowed = beBorrowed;}@Overridepublic String toString() {return "Book{" +"title='" + title + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +", BeBorrowed=" + BeBorrowed +'}';}public boolean beBorrowed(){return isBeBorrowed();}
}
package book;public class BooList {private Book[] books;//存放书籍private static final int DEFAULT_SIZE = 10;private int usedSize; // 有效书籍的个数public BooList() {this.books = new Book[DEFAULT_SIZE];this.books[0] = new Book("骆驼祥子","老舍",26,"小说",false);this.books[1] = new Book("雷雨","曹禺",24,"戏剧",true);this.books[2] = new Book("再别康桥","徐志摩",19,"诗歌",false);this.books[3] = new Book("五猖会","鲁迅",21,"散文",false);this.usedSize = 3;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}public Book getBook(int pos){return books[pos];}public void setBooks(int pos,Book book) {books[pos] = book;}
}
package user;import book.BooList;
import book.Book;
import operation.IOperation;public abstract class User {public String name;public IOperation[] iOperations;public User(String name){this.name = name;}public abstract int menu();public void doOperation(int choice, BooList books){
// return this.iOperations[choice];IOperation iOperation = this.iOperations[choice];iOperation.work(books);}
}
package user;import operation.*;import java.util.Scanner;public class NormalUser extends User{public NormalUser(String name) {super(name);this.iOperations = new IOperation[]{new ExitSystem(),new FindBook(),new BorrowBook(),new ReturnBook()};}@Overridepublic int menu() {System.out.println("欢迎"+this.name+"图书管理系统");System.out.println("**********普通用户菜单**********");System.out.println("1.查找图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("0.退出系统");System.out.println("****************************");Scanner sca = new Scanner(System.in);int choice = sca.nextInt();return choice;}
}
package user;import book.Book;
import operation.*;import java.util.Scanner;public class AdminiUser extends User{public AdminiUser(String name) {super(name);this.iOperations = new IOperation[] {new ExitSystem(),new FindBook(),new AddBook(),new DeleteBook(),new DisplayBook()};}@Overridepublic int menu() {System.out.println("欢迎"+this.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 sca = new Scanner(System.in);int choice = sca.nextInt();return choice;}
}
package operation;import book.BooList;
import book.Book;public interface IOperation {void work(BooList books);
}
package operation;
import book.BooList;
import book.Book;public class AddBook implements IOperation{@Overridepublic void work(BooList books){System.out.println("新增图书...");}
}
package operation;
import book.BooList;
import book.Book;import java.util.Scanner;public class BorrowBook implements IOperation{@Overridepublic void work(BooList books) {System.out.println("借阅图书...");System.out.println("请输入你要借阅的图书:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();//1.先遍历数组 查找是否存在要借阅的图书int currentSize = books.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = books.getBook(i);if(book.getTitle().equals(name)) {//2.如果存在 检查 是否已经被借出if (book.isBeBorrowed()) {System.out.println("这本书已经被借出了!");} else {//3. 如果没有借出 可以借book.setBeBorrowed(true);System.out.println(book);System.out.println("借阅成功!!");}return;}}//4.如果不存在 则不能借阅System.out.println("没有你要找的这本书,无法借阅!!");}
}
package operation;
import book.BooList;
import book.Book;public class ExitSystem implements IOperation{@Overridepublic void work(BooList books) {System.out.println("退出系统...");int currentSize = books.getUsedSize();for (int i = 0; i < currentSize; i++) {
// books[i] = null;books.setBooks(i,null);}System.exit(0);}
}