因为文件过多每个文件之间的关系如下(每个文件中都只有一个类):
因为JAVA属于面向对象编程的语言,所以我们想要实现图书管理系统就得分以下几步:
- 找出其中的所有的对象
- 实现所有的对象
- 完成对象之间的交互
在图书管理系统中我们可以想到这几个对象:书,用户(用户还可以分为普通用户和管理员),还得有存放书的书架。
找出对象之后我们将它们分别放在两个包中(一个放书和书架,另一个放普通用户和管理员)。
我们可以在书架类中创建一个书的数组,这样就相当于把书放在了书架中。
package book;
public class BookList {
public Book[] books;//书架
public int numBooks;//书架实际存放的书的本书
}
此时你还可以将书类中的属性都用private修饰因为后面我们的所有操作都是针对书架(如果用private修饰就需要对每个属性都实现get 和 set 方法)。
书类包含以下属性:
String name;
String author;
int money;
String type;
boolean isBorrow;
以下方法:
public Book(){}; public Book(String name, String author, int money, String kind) {}public String getName() {} public void setName(String name) {} public String getAuthor() {} public void setAuthor(String author) {} public int getMoney() {} public void setMoney(int money) {} public String getType() {} public void setType(String kind) {} public boolean getBorrow() {} public void setBorrow(boolean borrow) {} @Override public String toString() { return "name='" + name + '\'' + ", author='" + author + '\'' + ", money=" + money + ", kind='" + type + '\'' + ", 状态:"+(isBorrow == false ? "未被借出" : "已被借出"); }此处重写toString是为了可以直接使用System.out.println();输出book
因为普通用户和管理员都属于用户所以可与使用继承让 普通用户和管理员都继承用户这个父类。
管理员权限: 普通用户权限:
这样我们就可以利用多态的思想,创建一个用户类,由于引用的对象不同调用相同的方法结果也不相同。
可是现在又有一个新问题就是管理员和普通用户所打印出来的菜单是不同的,所以就不能在父类中实现,所以父类就不能是一个普通的父类,它必须是一个抽象类。
因为用户的操作太多而且有重复方法所以这里推荐新建一个包将其全部放入新的包中,并将所有的操作都单独封装成一个类。
操作属于用户所以写成如下格式:
//用户类
abstract public class User {
protected String name;
protected Work[] work;//存放用户所有的操作
public int counst; //work中的元素个数
}
//管理员
public class AdvancedUser extends User{
public AdvancedUser(String name){
this.name = name;
this.work = new Work[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
this.counst = 5;
}
}
//普通用户
public class NormalUser extends User {
public NormalUser(String name){
this.name = name;
this.work = new Work[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new RetOperation(),
};
this.counst = 4;
}
}
这样就可以用数组来调用方法
public void operation(int choice, BookList bookList){
work[choice].work(bookList);
}
完整代码:
package main;
import book.BookList;
import user.AdvancedUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner in = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = in.nextLine();
System.out.println("请选择你的身份:1.管理员 0.普通用户");
if (in.nextInt() == 1){
return new AdvancedUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while(true){
int choice;
do{
choice = user.menu();
}while(choice < 0 || choice >= user.counst);
user.operation(choice, bookList);
}
}
}
package book;
public class Book {
private String name;
private String author;
private int money;
private String type;
private boolean isBorrow;
public Book(){};
public Book(String name, String author, int money, String kind) {
this.name = name;
this.author = author;
this.money = money;
this.type = kind;
}
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 getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getType() {
return type;
}
public void setType(String kind) {
this.type = kind;
}
public boolean getBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
@Override
public String toString() {
return "name='" + name + '\'' +
", author='" + author + '\'' +
", money=" + money +
", kind='" + type + '\'' +
", 状态:"+(isBorrow == false ? "未被借出" : "已被借出");
}
}
package book;
public class BookList {
public Book[] books;//书架
public int numBooks;//书架实际存放的书的本书
public BookList() {
this.books = new Book[10];
//默认书架存放两本书
this.books[0] = new Book("西游记","吴承恩",39,"小说");
this.books[1] = new Book("红楼梦","曹雪芹",39,"小说");
this.numBooks = 2;
}
}
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation extends Work{
@Override
public void work(BookList bookList) {
Scanner in = new Scanner(System.in);
bookList.books[bookList.numBooks] = new Book();
System.out.print("请输入书名:");
bookList.books[bookList.numBooks].setName(in.nextLine());
System.out.print("请输入作者:");
bookList.books[bookList.numBooks].setAuthor(in.nextLine());
System.out.print("请输入所属书类:");
bookList.books[bookList.numBooks].setType(in.nextLine());
System.out.print("请输入书价:");
bookList.books[bookList.numBooks].setMoney(in.nextInt());
bookList.numBooks++;
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation extends Work{
@Override
public void work(BookList bookList) {
int numBooks = bookList.numBooks;
Scanner in = new Scanner(System.in);
System.out.print("请输入书名:");
String name = in.nextLine();
for (int i = 0; i < numBooks; i++) {
if (name.equals(bookList.books[i].getName())) {
if (bookList.books[i].getBorrow() == false){
System.out.println("借阅成功");
bookList.books[i].setBorrow(true);
return;
}else{
System.out.println("该书已被借出");
return;
}
}
}
System.out.println("未收录该书");
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class DelOperation extends Work{
@Override
public void work(BookList bookList) {
int numBooks = bookList.numBooks;
Scanner in = new Scanner(System.in);
System.out.print("请输入书名:");
String name = in.nextLine();
for (int i = 0; i < numBooks; i++) {
if (name.equals(bookList.books[i].getName())){
int j = 0;
for (j = i; j < numBooks-1; j++) {
bookList.books[j] = bookList.books[j + 1];
}
bookList.books[j] = null;
bookList.numBooks--;
return;
}
}
}
}
package operation;
import book.BookList;
public class ExitOperation extends Work{
@Override
public void work(BookList bookList) {
System.exit(0);
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class FindOperation extends Work{
@Override
public void work(BookList bookList) {
int numBooks = bookList.numBooks;
Scanner in = new Scanner(System.in);
System.out.print("请输入书名:");
String name = in.nextLine();
for (int i = 0; i < numBooks; i++) {
if (name.equals(bookList.books[i].getName())) {
System.out.println(bookList.books[i]);
return;
}
}
System.out.println("未收录该书");
}
}
package operation;
import book.BookList;
import java.util.Scanner;
public class RetOperation extends Work{
@Override
public void work(BookList bookList) {
int numBooks = bookList.numBooks;
Scanner in = new Scanner(System.in);
System.out.print("请输入书名:");
String name = in.nextLine();
for (int i = 0; i < numBooks; i++) {
if (name.equals(bookList.books[i].getName())) {
if (bookList.books[i].getBorrow()){
System.out.println("归还成功");
bookList.books[i].setBorrow(false);
return;
}else{
System.out.println("该书未被借出");
return;
}
}
}
System.out.println("未收录该书");
}
}
package operation;
import book.BookList;
public class ShowOperation extends Work{
@Override
public void work(BookList bookList) {
for (int i = 0; i < bookList.numBooks; i++) {
System.out.println(bookList.books[i]);
}
}
}
package operation;
import book.BookList;
abstract public class Work {
abstract public void work(BookList bookList);
}
package user;
import operation.*;
import java.util.Scanner;
public class AdvancedUser extends User{
public AdvancedUser(String name){
this.name = name;
this.work = new Work[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
this.counst = 5;
}
@Override
public int menu() {
System.out.println("###########################");
System.out.println(" Hello " + "AdvancedUser:" + 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 in = new Scanner(System.in);
return in.nextInt();
}
}
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name){
this.name = name;
this.work = new Work[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new RetOperation(),
};
this.counst = 4;
}
@Override
public int menu() {
System.out.println("###########################");
System.out.println(" Hello " + "NormalUser:" + 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 in = new Scanner(System.in);
return in.nextInt();
}
}
package user;
import book.BookList;
import operation.Work;
abstract public class User {
protected String name;
protected Work[] work;//存放用户所有的操作
public int counst; //work中的元素个数
abstract public int menu();
public void operation(int choice, BookList bookList){
work[choice].work(bookList);
}
}
运行结果: