小型图书管理系统
主函数
public class Main {
public static User login(){//User返回值 向上转型
System.out.println(“请输入名字”);
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println(“请输入身份:1管理员,2用户”);
int choice = scanner.nextInt();
if(choice==1){
return new Admin(name);//向上转型返回
}else {
return new NormalUser(name);//向上转型返回
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true){
int choice = user.menu();//菜单选出操作
IOperation iOperation = user.doOperation(choice);
//根据choice 进行操作
iOperation.work(bookList);
}
}
}
使用者(类):管理员&用户
public abstract class User {
protected String name;
protected IOperation[] iOperations;//人有操作的权限
//iOperations 为数组名 存放IOperation 类型 实际存放为派生类,
public User(String name){
this.name = name;
}
public abstract int menu();//不同使用者有不同功能,
public IOperation doOperation(int choice){//接收所选操作选项
return this.iOperations[choice];
}
}
public class Admin extends User {
public Admin(String name) {
super(name);
//normaluser 的IOperation 有了操作功能
this.iOperations= new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
@Override
public int menu() {
//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、退出系统");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
//return 0;
}
}
管理员
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
//normaluser 的IOperation 有了操作功能
this.iOperations= new IOperation[] {
//iOperations 为父类的数组名,在此构造(初始化),
// new 一个数组 ,内存放IOperation类,实际存放为 new IOperation的子类
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
用户
@Override
public int menu() {
//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、退出系统”);
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
书籍:书数组、书
书
public class Book {
//书的信息
private String name;
private String author;
private int price;
private String type;//类型
private boolean isBorrowed;//状态:是否被借出
//构造方法
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
//私有的 提供接口 getter and setter
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;
}
//缺少打印书
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +", "+
//", isBorrowed=" + isBorrowed +
((isBorrowed == true)? "已经借出":"未借出")+
'}';
}
}
书数组:多本书
public class BookList {
//public int[] a={1,2,3,4};创建顺序表
private Book[] elem = new Book[10];//放10本书
private int usedSize;
public BookList(){
this.elem[0]=new Book(“三国演义”,“罗贯中”,15,“小说”);
this.elem[1]=new Book(“Java编程”,“比特”,15,“学习”);
this.elem[2]=new Book(“西游记”,“吴承恩”,15,“小说”);
this.usedSize=3;
//this.elem[3]=new Book(“三国演义”,“罗贯中”,15,“小说”);
}
public void setBook(int pos,Book book){//pos位置设置成某一本书 尾插
this.elem[pos]=book;
}
public Book getBook(int pos){ //
return this.elem[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
操作:只写个接口剩下的很简单,实现一下就可以了
public interface IOperation {
Scanner scan = new Scanner(System.in);
void work(BookList bookList);
}