该系统是基于Java面向对象的特性来写的,体现其封装、继承、多态等思想。
package book;
public class Book {
// book的一些基本属性,比如书名,作者,价格,类型,是否被借出
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed; //这里isBorrowed初始化就是false,所以不需要在构造方法里初始化了
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
//提高一些get、set方法,方便以后进行查看和修改(因为上面都是用private进行修饰的属性,只能在该类里面访问)
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 isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
//重写toString,以后打印book就会调用该方法
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
(isBorrowed ? " 已被借出" : " 未被借出") +
'}'; //注意三目运算符
}
}
package book;
public class BookList {
private Book[] books= new Book[10]; //创建一个Book数组,以后存放的书就在这里
private int size; //size是记录当前存放书的数目的
public BookList() { //初始存放三本书
books[0] = new Book("三国演义","罗贯中",33,"小说");
books[1] = new Book("西游记","吴承恩",46,"小说");
books[2] = new Book("红楼梦","曹雪芹",64,"小说");
this.size = 3; //并将size初始化为3
}
//提供get与set方法,来查看和改变数组中存放的书
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(int pos, Book book) {
books[pos] = book;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
该类包含了普通用户与管理员的共同点,也就是名字还有一些方法,将来是给AdminUser类和NormalUser类继承的,由此我们可以写出它的代码:
package user;
import book.BookList;
import opera.IOPeration;
public class User {
protected String name; //体现封装性,用protected,不同包中的子类也能访问
protected IOPeration[] ioPerations; //这个接口数组将来存放的是 实现了该接口的操作方法,根据菜单需求,在两个子类中分别初始化。
public User(String name) {
this.name = name;
}
public int menu() { //因为两种用户的菜单不同,所以提供一个方法,供子类根据需求进行重写
return 0;
}
public void work(int m, BookList bookList) {
this.ioPerations[m].work(bookList);
} //这里通过输入m,来控制调用的操作方法
}
package user;
import opera.*;
import java.util.Scanner;
public class AdminUser extends User{
//通过构造方法来初始化姓名及接口数组(普通用户和管理员的菜单不同,所以要根据需求初始化数组)
public AdminUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(), //把0号位置对应退出系统操作
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
//重写父类的menu方法
public int menu() {
System.out.println("************************************");
System.out.println("欢迎" + 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 scanner = new Scanner(System.in);
int m = scanner.nextInt();
return m; //返回操作数
}
}
import opera.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BrrowOperation(),
new ReturnOperation()
};
}
public int menu() {
System.out.println("************************************");
System.out.println("欢迎" + 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 scanner = new Scanner(System.in);
int m = scanner.nextInt();
return m;
}
}
package opera;
import book.BookList;
public interface IOPeration { //该接口提供了一个work方法
void work(BookList bookList); //因为是对BookList进行操作,所以传入该类型参数
}
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
@Override //重写work方法
public void work(BookList bookList) {
System.out.println("请输入你要添加的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入类型:");
String type = scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
//将这些信息存入变量book中
Book book = new Book(name,author,price,type);
for(int i = 0; i < bookList.getSize(); i++) { //遍历数组,判断是否存在该书
Book tmp = bookList.getBooks(i);
if(tmp.getName().equals(name)) {
System.out.println("这本书已存在");
return;
}
} //该书不存在则将该书加入数组中
bookList.setBooks(bookList.getSize(),book);
bookList.setSize(bookList.getSize()+1);
}
}
package opera;
import book.BookList;
import java.util.Scanner;
public class BrrowOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for(int i = 0; i < bookList.getSize(); i++) {
// 借阅图书,不仅得有该书,还得该书处于未被借阅状态
if(bookList.getBooks(i).getName().equals(name) && !bookList.getBooks(i).isBorrowed()) {
System.out.println("借阅成功!");
bookList.getBooks(i).setBorrowed(true);
return;
}
}
System.out.println("没有该书,请重新选择!");
}
}
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int i = 0;
for(i = 0; i < bookList.getSize(); i++) {
if(bookList.getBooks(i).getName().equals(name)) {
break; //找到该书后,i即是该书在数组中的下标位置
}
}
if(i < bookList.getSize()) {
for( ; i < bookList.getSize()-1; i++) {
//将后面的书往前覆盖,再改变一下size的值即可
bookList.setBooks(i,bookList.getBooks(i+1));
}
bookList.setSize(i);
System.out.println("删除成功!");
}else { //如果i走到了最后,则该书不存在
System.out.println("没有该书!");
}
}
}
package opera;
import book.BookList;
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.exit(0); //退出程序
}
}
package opera;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for(int i = 0; i < bookList.getSize(); i++) { //遍历查找图书
if(bookList.getBooks(i).getName().equals(name)) {
System.out.println("找到了!");
System.out.println(bookList.getBooks(i));
return;
}
}
System.out.println("没有该书!");
}
}
package opera;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
@Override //归还图书,类似于借阅图书
public void work(BookList bookList) {
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for(int i = 0; i < bookList.getSize(); i++) {
if(bookList.getBooks(i).getName().equals(name) && bookList.getBooks(i).isBorrowed()) {
System.out.println("归还成功!");
bookList.getBooks(i).setBorrowed(false);
return;
}
}
System.out.println("没有该书,请重新选择!");
}
}
package opera;
import book.BookList;
public class ShowOperation implements IOPeration{
@Override //显示图书
public void work(BookList bookList) {
for(int i = 0; i < bookList.getSize(); i++) {
System.out.println(bookList.getBooks(i)); //这里调用了我们重写的toString方法
}
}
}
import book.BookList;
import opera.IOPeration;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份: 1 --> 管理员 2 --> 普通用户");
int a = scanner.nextInt();
if(a == 1) { //这里通过a,来判断身份,并返回一个该身份的对象
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login(); //父类接收身份对象,发生了向上转型和动态绑定
while(true) { //选择是循环的,即操作完一次继续操作,直到退出系统
int m = user.menu();
user.work(m, bookList);
}
}
}