一.题目:
结合Java的面向对象的三大特征:封装、继承、多态等,以及抽象类、接口等相关知识实现图书管理系统。
二.具体设计:
1.登录功能;
2.图书:拥有书名,作者,价格,类型,借阅状态等属性;
3.用户:管理员和普通用户;
4.用户的对书籍可进行的相关操作:
(1)管理员的功能:
-
查阅书籍;
-
增加书籍;
-
删除书籍;
-
显示书籍;
-
退出系统
(2)普通用户的功能:
-
查询书籍;
-
借阅书籍;
-
归还书籍;
-
退出系统
三.具体实现:
1.创建图书相关的类:
在book包中创建了两个类:
(1)Book类:表示一本书;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean status;//false
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 isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
( (status == true) ? " 借出 " : " 未借出 ")+
'}';
}
}
(2)BookList类:用于保存图书;
public class BookList {
private Book[] books;
private int usedSize;
public BookList() {
this.books = new Book[10];
books[0] = new Book("三国演义","罗贯中",52,"小说");
books[1] = new Book("西游记","吴承恩",62,"小说");
books[2] = new Book("水浒传","施耐庵",65,"小说");
this.usedSize = 3;
}
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book getBook(int pos) {
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
2. 创建用户相关的类:
在user包中创建了3个类:
(1)用户类:User类,即一个抽象类;
public abstract class User {
public String name;
public IOperation[] operations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
this.operations[choice].work(bookList);
}
}
(2)管理员:AdminUser类继承于User类;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
/**
* 返回 所执行的对应的操作
* @return
*/
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
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("===============================");
int choice = scanner.nextInt();
return choice;
}
}
(3)普通用户类:NormalUser类继承于User类;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
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("===============================");
int choice = scanner.nextInt();
return choice;
}
}
3.对相关类的操作的具体实现:先睡了。。。