Book类 用于初始化图书并对其进行操作
BookList类 用于存放多本图书
package Book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrow;
//写一个构造方法 初始化图书内容
public Book(String name,String author,int price,String type) {
this.author = author;
this.name = name;
this.type = type;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
",isBorrow='"+isBorrow()
+ '}';
}
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 isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
}
package Book;
public class BookList {
private Book[] books;
private int usedSize;
//假设书架的容量大小为十本书 usedSize是此时书架上有几本书
//初始化几本书(可以自定义)
public BookList() {
this.books = new Book[10];
books[0] = new Book("西游记","施耐庵",10,"小说");
books[1] = new Book("红楼梦","曹雪芹",14,"小说");
books[2] = new Book("三国演义","罗贯中",15,"小说");
this.usedSize = 3;
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(Book book, int pos) {
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
package operation;
import Book.BookList;
public interface Operation {
public void work(BookList bookList);
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class AddOperation implements Operation {
@Override
public void work(BookList bookList) {
System.out.println("新增图书->");
Scanner scan = new Scanner(System.in);
System.out.println("请输入书名->");
String name = scan.next();
System.out.println("请输入作者->");
String author = scan.next();
System.out.println("请输入价格->");
int price = scan.nextInt();
System.out.println("请输入类型->");
String type = scan.next();
Book book = new Book(name,author,price,type);
bookList.setBooks(book,bookList.getUsedSize());
System.out.println("添加图书成功!");
bookList.setUsedSize(bookList.getUsedSize()+1);
}
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class BorrowOperation implements Operation {
@Override
public void work(BookList bookList) {
Scanner scan = new Scanner(System.in);
System.out.println("借阅图书->");
System.out.println("请输入要借阅的书名->");
String name = scan.next();
int tmp =bookList.getUsedSize();
for (int i = 0; i < tmp; i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())) {
bookList.getBooks(i).setBorrow(false);
System.out.println("借阅成功!");
System.out.println(bookList.getBooks(i).toString());
return;
}
}
System.out.println("找不到这本书");
}
}
package operation;
import Book.BookList;
public class ExitOperation implements Operation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统->");
System.exit(0);
}
}
package operation;
import Book.BookList;
import Book.Book;
import java.util.Scanner;
public class FindOperation implements Operation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书->");
Scanner scan = new Scanner(System.in);
System.out.println("请输入要查找的书名->");
String name = scan.next();
int tmp =bookList.getUsedSize();
for (int i = 0; i < tmp; i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())) {
System.out.println("找到这本书了,它的信息是->");
System.out.println(book.toString());
return;
}
}
System.out.println("找不到这本书");
}
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class RemoveOperation implements Operation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书->");
Scanner scan = new Scanner(System.in);
System.out.println("请输入要删除的书名->");
String name = scan.next();
int tmp = bookList.getUsedSize();
int flag = -1;
for (int i = 0; i < tmp; i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())) {
flag = i;
}
}
if(flag == -1) {
System.out.println("没有这本书,删除失败!");
return;
}
for (int i = flag; i < tmp-1; i++) {
bookList.setBooks(bookList.getBooks(i+1),i);
}
bookList.setBooks(null, tmp-1);
bookList.setUsedSize(tmp-1);
System.out.println("删除成功!");
}
}
package operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class ReturnOperation implements Operation {
@Override
public void work(BookList bookList) {
Scanner scan = new Scanner(System.in);
System.out.println("归还图书->");
System.out.println("请输入要归还的书名->");
String name = scan.next();
int tmp =bookList.getUsedSize();
for (int i = 0; i < tmp; i++) {
Book book = bookList.getBooks(i);
if(name.equals(book.getName())) {
bookList.getBooks(i).setBorrow(true);
System.out.println("归还成功!");
System.out.println(bookList.getBooks(i).toString());
return;
}
}
System.out.println("找不到这本书");
}
}
package operation;
import Book.BookList;
public class ShowOperation implements Operation {
@Override
public void work(BookList bookList) {
System.out.println("显示图书->");
int tmp = bookList.getUsedSize();
for (int i = 0; i < tmp; i++) {
System.out.println(bookList.getBooks(i).toString());
}
}
}
package User;
import Book.BookList;
import operation.*;
public abstract class User {
protected Operation[] operations;
protected String name;
public User(String name) {
this.name = name;
}
public abstract void doOperation(int choice, BookList bookList);
public abstract int menu();
}
package User;
import Book.BookList;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.operations = new Operation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new RemoveOperation(),
new ShowOperation(),
};
}
@Override
public void doOperation(int choice, BookList bookList) {
this.operations[choice].work(bookList);
}
@Override
public int menu() {
System.out.println("********管理人员菜单********");
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 scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
package User;
import Book.BookList;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.operations = new Operation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public void doOperation(int choice, BookList bookList) {
this.operations[choice].work(bookList);
}
public int menu() {
System.out.println("********读者菜单********");
System.out.println("1、查找图书");
System.out.println("2、借阅图书");
System.out.println("3、归还图书");
System.out.println("0、退出系统");
System.out.println("***********************");
System.out.println("请输入您要进行的操作->");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
{
public static User login() {
Scanner scan = new Scanner(System.in);
System.out.println("您的姓名是->");
String name = scan.next();
System.out.println("请选择你要登录的系统->");
System.out.println("1、管理员系统");
System.out.println("2、读者系统");
System.out.println("————————————————————");
int choice = scan.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);
}
}
}
以上就是 图书馆管理系统源码(Java) 的全部内容了,希望能对你有所帮助!