首先我们要定义一个book类,里面是一本书信息。
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;
}//由于boolean默认是false,即默认未借出
/**
* 公开接口
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
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;
}
/**
* 打印一本书
* @return
*/
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true)?"已经借出":"未借出")+//三位操作符
'}';
}
}
接下来在定义一个booklist书架,里面放书book.
public class Booklist {
Book[]books=new Book[100];//可以放多少本书
private int useSize;//实时记录书本数
//书架的初始书本数
public Booklist() {
books[0]=new Book("三国演义","罗贯中",50,"小说");
books[1]=new Book("西游记","吴承恩",60,"小说");
books[2]=new Book("红楼梦","曹雪芹",80,"小说");
useSize=3;
}
//从书架拿到一本书
public Book getBook(int pos){
return books[pos];
}
//放一本书
public Book setBook(int pos,Book book){
return books[pos]=book;
}
//实时获取当前的书的个数
public int getUseSize() {
return useSize;
}
//实时的修改当前书架上的书的个数
public void setUseSize(int useSize) {
this.useSize = useSize;
}
}
图书管理系统的用户有2种,一个是管理员能够添加图书,删除图书,查找图书,显示图书,退出系统。普通用户能够查找书,借书,归还书,退出系统。
上面的操作写成接口IOperation()
public interface IOperation { void work(Booklist booklist); }
在让具体操作去继承这个接口
添加书
public class AddOperation implements IOperation {
@Override
public void work(Booklist booklist) {
System.out.println("添加图书");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入图书名");
String name=scanner.nextLine();
System.out.println("请输入作者");
String author=scanner.nextLine();
System.out.println("请输入价格");
int price=scanner.nextInt();
System.out.println("请输入类型");
String type=scanner.nextLine();
Book book=new Book(name,author,price,type);
booklist.setBook(booklist.getUseSize(), book);
booklist.setUseSize(booklist.getUseSize()+1);
System.out.println("增加成功");
return;
}
}
删除书
public class DelOpertion 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.getUseSize(); i++) {
if(booklist.getBook(i).getName().equals(name)){
for (int j = i; j < booklist.getUseSize()-1 ; j++) {
booklist.setBook(j,booklist.getBook(j+1));
}
booklist.setUseSize(booklist.getUseSize()-1);
System.out.println("删除成功");
return;
}
}
}
}
查找书
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.getUseSize(); i++) {
if(booklist.getBook(i).getName().equals(name)){
System.out.println(booklist.getBook(i));
return;
}
}
System.out.println("没有这本书");
}
}
显示书
public class DisOpertion implements IOperation{
@Override
public void work(Booklist booklist) {
for (int i = 0; i < booklist.getUseSize(); i++) {
System.out.println(booklist.getBook(i));
}
}
}
借书
public class BorrowOperation 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.getUseSize(); i++) {
if(booklist.getBook(i).equals(name)){
booklist.getBook(i).setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
}
}
归还书
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.getUseSize(); i++) {
if(booklist.getBook(i).getName().equals(name)){
booklist.getBook(i).setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有这本书");
}
}
退出系统
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.getUseSize(); i++) {
if(booklist.getBook(i).getName().equals(name)){
booklist.getBook(i).setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有这本书");
}
}
接下来就是定义管理员类和普通用户类。由于2者都用共同点,使用可以先定义一个user类,再去继承他。
abstract public class User {
protected String name;
protected 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);
}//调用操作函数
}
管理人员
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations=new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOpertion(),
new DisOpertion()
};
}
@Override
public int menu() {
System.out.println("hello"+name+"欢迎来到图书管理系统");
System.out.println("0.退出系统");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("请选择:");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
普通用户
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() {
System.out.println("hello"+name+"欢迎来到图书管理系统");
System.out.println("0.退出系统");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("请选择:");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
程序入口
public class Test {
public static User login(){
System.out.println("请输入你的名字");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请选择你的身份:1.管理员,0.普通用户");
int choice=scanner.nextInt();
if(choice==1){
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 choice=user.menu();
user.doOperation(choice,booklist);//整个程序框架搭建的重点
}
}
}
上面是整个程序框架的搭建过程