Java : 图书管理系统
图书管理系统的作用:
高效的图书管理 图书管理系统通过自动化管理,实现了图书的采编、编目、流通管理等操作的自动化处理,大大提高了图书管理的效率和准确性。 工作人员可以通过系统快速查找图书信息,实时掌握图书的借还情况,避免了繁琐的手工操作和人为错误。
主要功能:
- 图书管理:添加、删除、修改图书信息。
- 用户管理:用户注册、登录功能。
- 借阅与归还:用户可以借阅和归还书籍。
- 查询功能:可以根据书名或作者查询书籍信息。
通过不同包的串通和引用,来实现一个简单的图书管理系统
代码实现:
//定义一个book的包,包括Book和BookList两个类
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;
}
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 +
'}';
}
}
public class BookList {
private Book[] books=new Book[100];
private int usedSize;
public BookList(){
books[0]=new Book("三国演义","罗贯中",10,"小说");
books[1]=new Book("西游记","吴承恩",18,"小说");
books[2]=new Book("红楼梦","曹雪芹",14,"小说");
this.usedSize=3;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
}
//再定义一个user包,包括管理员的类和普通用户的类
public abstract class User {
public String name;
public IOperation[] iOperations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doIOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
public class AdminUser extends User{
public AdminUser(String name){
super(name);
this.iOperations=new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
@Override
public int menu() {
System.out.println("****管理员菜单****");
System.out.println("****************");
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);
System.out.println("请输入你的操作");
int choice=scanner.nextInt();
return choice;
}
}
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations=new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
@Override
public int menu() {
System.out.println("****普通用户菜单****");
System.out.println("******************");
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);
System.out.println("请输入你的操作");
int choice=scanner.nextInt();
return choice;
}
}
在定义一个operation包,包括一个接口和各种操作的类
public interface IOperation {
void work(BookList bookList);
}
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("添加图书");
int currentSize= bookList.getUsedSize();
if(currentSize==bookList.getBooks().length){
System.out.println("放满了,放不下了");
return;
}
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("请输入价格");
int price=scanner.nextInt();
Book book=new Book(name,author,price,type);
for (int i = 0; i < currentSize; i++) {
Book book1=bookList.getBook(i);
if(book1.getName().equals(name)){
System.out.println("有这本书不能插入");
return;
}
}
bookList.setBook(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
}
public class BorrowOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("借阅图书");
Scanner scanner=new Scanner(System.in);
System.out.println("输入你需要借阅的书本");
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)){
if (book.isBorrowed()){
System.out.println("该书已被借出");
return;
}
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有你要借阅的书本");
}
}
public class DelOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要删除的书籍");
String name=scanner.nextLine();
int currentSize= bookList.getUsedSize();
int pos=-1;
int i = 0;
for (; i <currentSize; i++) {
Book book=bookList.getBook(i);
if (book.getName().equals(name)){
pos=i;
break;
}
}
if (pos==currentSize){
System.out.println("没有你要删除的书籍");
return;
}
for (int j = 0; j < currentSize; j++) {
Book book=bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功");
}
}
public class ExitOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
public class FindOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("查找图书");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要查找的图书");
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);
return;
}
}
System.out.println("没有找到这本书");
}
}
public class ReturnOperation 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 currentSize= bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有你要归还的书本");
}
}
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);
}
}
}
最后一个类来串通整个操作过程
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();
User user=login();
while (true) {
int choice = user.menu();
user.doIOperation(choice, bookList);
}
}
}
希望能对大家有所帮助!!!!!