1、简单的登录
2、管理端
整理书籍(该功能为可扩展功能)
查阅书籍
增加书籍
删除书籍
打印书籍列表
退出
3、用户端
查询书籍
借阅书籍
归还书籍
退出
通过登录确认自己的身份,如果是管理员,对图书进行增,删,查,显示等,如果是学生,则实现借阅,归还,查找等功能
package book;
public class Book {
/*一本书的属性包括书名,作者,价格,类型,编号,以及状态*/
private String name;
private String author;
private int price;
private String type;
private int num;
private boolean status;
//有参数的构造方法
public Book(String name, String author, int price, String type, int num) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
this.num = num;
}
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=" + status +
((status==true)?"借出":"未借出")+
'}';
}
}
package book;
public class Booklist {
private int usedSize;
private Book[] books;
public Booklist() {
this.books = new Book[10];
books[0]=new Book("三国演义","罗贯中",11,"战国");
books[1]=new Book("西游记","吴承恩",21,"玄幻");
books[2]=new Book("红楼梦","曹雪芹",31,"言情");
books[3]=new Book("水浒传","施耐庵",41,"侠义");
books[4]=new Book("三体","刘慈欣",51,"科幻");
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;
}
}
package operation;
import book.Booklist;
public interface IOperation {
void work(Booklist booklist);
}
package operation;
import book.Book;
import book.Booklist;
import java.util.Scanner;
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);
int currentSize=booklist.getUsedSize();
booklist.setBooks(currentSize,book);
booklist.setUsedSize(currentSize+1);
}
}
借阅图书
package operation;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class BorrowOpreation implements IOperation {
@Override
public void work(Booklist booklist) {
System.out.println("借阅书籍!");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要借阅的书名:");
String name=scanner.nextLine();
for (int i = 0; i <booklist.getUsedSize() ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
//if成立,说明存在这本书
//修改借阅状态为true
book.setStatus(true);
System.out.println(book);
return;
}
}
System.out.println("没有你要借的书!!");
}
}
删除图书
package operation;
import book.Book;
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);
System.out.println("请输入所要删除的书名:");
String name=scanner.nextLine();
int i=0;
for (; i <booklist.getUsedSize() ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
//说明找到了这本书
break;
}
}
/**
* 退出有两种情况:
* 1.不满足循环条件
* 2.break
*/
if(i>booklist.getUsedSize()){
System.out.println("不存在要删除的书籍");
return;
}
//开始删
int pos=i;
//把后一个给前一个
//
for(int j=pos;j<booklist.getUsedSize()-1;j++){
Book book=booklist.getBook(j+1);
booklist.setBooks(j,book);
}
int currentSize=booklist.getUsedSize();
booklist.setUsedSize(currentSize-1);
System.out.println("删除成功!!!");
}
}
展示图书
package operation;
import book.Book;
import book.Booklist;
public class DisplayOpreation implements IOperation{
@Override
public void work(Booklist booklist) {
System.out.println("展示图书!");
for (int i = 0; i <booklist.getUsedSize() ; i++) {
Book book=booklist.getBook(i);
System.out.println(book);
}
}
}
查找图书
package operation;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(Booklist booklist) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要查找书籍的名字:");
String name=scanner.nextLine();
//开始遍历
for (int i = 0; i <booklist.getUsedSize() ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了!");
System.out.println(book);
return;
}
}
System.out.println("找不到!");
}
}
归还图书
package operation;
import book.Book;
import book.Booklist;
import java.util.Scanner;
public class ReturnOpreation implements IOperation{
@Override
public void work(Booklist booklist) {
System.out.println("归还图书!");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要归还的书名");
String name=scanner.nextLine();
for (int i = 0; i <booklist.getUsedSize() ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
//修改本书借阅状态
book.setStatus(false);
System.out.println(book);
return;
}
System.out.println("没有你要归还的书!!");
}
}
}
package user;
import operation.*;
import java.util.Scanner;
//普通用户
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOpreation(),
new FindOperation(),
new BorrowOpreation(),
new ReturnOpreation(),
};
}
@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;
}
}
package user;
import operation.*;
import java.util.Scanner;
//管理员
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOpreation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOpreation()
};
}
@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;
}
}
package operation;
import book.Booklist;
public class ExitOpreation implements IOperation{
@Override
public void work(Booklist booklist) {
System.out.println("退出系统!");
System.exit(1);
}
}
import book.Booklist;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name=scanner.nextLine();
System.out.println("1->管理员----2->普通用户");
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();
/**
* 1.登录:user引用一个对象,这个对象就是login()方法的返回值
* 不是AdminUser就是NormalUser
*/
User user=login();
while(true){
/**
* user来调用菜单函数
* choice来接收菜单函数的返回值
*/
int choice=user.menu();
user.doOperations(choice,booklist);
}
}
}