图书
BOOK类【书籍信息】
package book;
public class Book {
private String name;//书名
private String author;//作者
private double price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
/*", isBorrowed=" + isBorrowed +*/
(isBorrowed ?" 已被借出 ":" 未被借出 ")+
'}';
}
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 double getPrice() {
return price;
}
public void setPrice(double 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;
}
}
BookList类【通过数组来存入书籍】
package book;
import com.sun.org.apache.bcel.internal.generic.RETURN;
public class BookList {
private static final int DEFAULT_SIZE=10;//定义常量来给定数的数量
private Book[] books=new Book[DEFAULT_SIZE];//定义可以容下0~10本的书
private int usedSize;//记录当前 books数组当中 有多少本书?
public BookList() {
books[0]=new Book("三国演义","罗贯中",89,"小说");
books[1]=new Book("西游记","吴承恩",78,"小说");
books[2]=new Book("红楼梦","曹雪芹",49,"小说");
this.usedSize=3;
}
public Book getBook(int pos){
return this.books[pos];
}
public void setBook(Book book){
this.books[usedSize]=book;
}
public void setBook(int pos,Book book){
this.books[pos]=book;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
图书系统的操作
添加图书操作
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
//添加图书操作
public class AddOpertion 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("输入类型:");
String type=scanner.nextLine();
System.out.println("输入价格:");
Double price=scanner.nextDouble();
Book book=new Book(name,author,price,type);
int currentSize= bookList.getUsedSize();
for (int i = 0; i <currentSize ; i++) {
Book tmp=bookList.getBook(i);
if(tmp.getName().equals(name)){
System.out.println("这本书已经存再来,不能再放入了!");
return;
}
}
bookList.setBook(book);
//修改usedSize
bookList.setUsedSize(currentSize+1);
}
}
借阅图书操作
package opera;
import java.util.Scanner;
import book.Book;
import book.BookList;
//借阅图书操作
public class BrrowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("输入你要借阅的书籍:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book= bookList.getBook(i);
if(book.getName().equals(name)&& !book.isBorrowed()){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
}
}
删除图书操作
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
//删除图书操作
public class DelOpertion implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入你要删除书籍的名字:");
Scanner scanner =new Scanner(System.in);
System.out.println("输入书名:");
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
int index=-1;
for (int i = 0; i <currentSize ; i++) {
Book tmp=bookList.getBook(i);
if(tmp.getName().equals(name)){
index=i;
break;
}else{
System.out.println("你所要删除的书籍不存在!");
return;
}
}
//把要删除的书按下标往后移(挪动数据)
for (int j=index;j<currentSize-1 ;j++){
Book book=bookList.getBook(j+1);//把下标为j+1的书放在book中
bookList.setBook(j,book);//把book的书放在j下标中
}
//修改size
bookList.setUsedSize(currentSize-1);
//因为删除的是对象,所以把最后一个置为null
bookList.setBook(currentSize-1,null);
System.out.println("删除成功!");
}
}
退出系统操作
package opera;
import book.BookList;
//退出系统操作
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);//进行强制退出
}
}
查找图书操作
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
//查找图书操作
public class FindOpertion implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入书名:");
Scanner scanner= new Scanner(System.in);
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找了这本书:");
System.out.println(book);//重写了toString可以直接输出
return;
}
}
System.out.println("没有这本书!");
}
}
归还图书操作
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
//归还图书操作
public class ReturnOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("输入你要归还的书籍:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book= bookList.getBook(i);
if(book.getName().equals(name)&& book.isBorrowed()){
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
}
}
打印图书操作
package opera;
import book.Book;
import book.BookList;
//打印图书操作
public class ShowOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("打印所有图书!");
int currentSize= bookList.getUsedSize();
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBook(i);
System.out.println(book);//这里可以直接输出,因为在Book类中重写了toSring方法
}
}
}
IOPeration接口
package opera;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
用户及管理员
管理员
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(),
new FindOpertion(),
new AddOpertion(),
new DelOpertion(),
new ShowOperation()
};
}
public int menu() {
System.out.println("********************************");
System.out.println("hello " + 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 choice=scanner.nextInt();
return choice;
}
}
普通用户
package user;
import opera.*;
import java.util.Scanner;
//普通用户
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.ioPerations=new IOPeration[] {
new ExitOperation(),
new FindOpertion(),
new BrrowOperation(),
new ReturnOperation()
};
}
public int menu(){
System.out.println("********************************");
System.out.println("hello "+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 choice=scanner.nextInt();
return choice;
}
}
用户类
package user;
import book.BookList;
import opera.IOPeration;
//用户类
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu() ;
public void doWork(int choice, BookList bookList){
this.ioPerations[choice].work(bookList);
}
}
Main方法
import book.Book;
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
/**
* 返回值为User这个类因为AdminUser和NormalUser的父类是User
* @return
*/
public static User Login() {
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
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();
User user =Login();
while (true){
int choice=user.menu();
user.doWork(choice, bookList);
}
}
}
运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/b743e1e2c3daf7f20aacaea1061faccd.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b32aa29f70ada709a3bf0ce38ca04d9c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/38ad33b312cb6bd40d6a0df8f40588b6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4b7ee50e86c8ed654d63731e9e600eb6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/36afef88a5bd10d1312927c4895eb500.png)
下载地址