目录
一.book
1.在book包中的Book 类用来定义和引用书的名字,作者,价格,类型等。
2.在book包中的第二个类是BookList是用来构建书架,和书架上的初始书本,
二、ioperations
1.AddOperation (增加图书)
2.BorrowOperation (借阅图书)
3.DelOperation (删除图书)
4.ExitOperation (显示图书)
5.FindOperation (查找图书)
6.IOPeration(接口)
7.ReturnOperation(归还图书)
8.ShowOperation(显示图书)
三、User
1.AdminUser(管理员)
2.NormalUser(普通用户)
3.User
四、Main类
图书管理系统的作用:显而易见,图书管理系统的出现就是为了缓和图书管理员的管理任务。当然,这其中也蕴含了一定的抽象意义,就是实现了从计算机上的虚拟化在现实生活中的实际运用;实现了图书馆由传统走向信息化道路的愿望。可以说,这样的管理系统是极大地减轻了管理员的负担。这样的程序在将图书信息进行简明化处理后,就实现了图书信息的集中化管理,使得管理的成本也大幅度降低。在进行查找操作时,系统可以快速地进行综合查询,得到最准确的结果。
图书管理系统构建简单的流程图
选择身份进入后选择你要做的内容,通过这个架构来编写代码。
首先我们先创建三个包
一.book
1.在book包中的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;
}
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 == true) ? " 已借出" : " 未借出") +
'}';
}
}
2.在book包中的第二个类是BookList是用来构建书架,和书架上的初始书本,
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList() {
this.books[0] = new Book("狂人日记","鲁迅",10,"小说");
this.books[1] = new Book("淘气包马小跳","杨红樱",10,"小说");
this.books[2] = new Book("笑猫日记","杨红樱",16,"小说");
this.books[3] = new Book("阿Q正传","鲁迅",16,"小说");
this.books[4] = new Book("红楼梦","曹雪芹",30,"小说");
this.usedSize = 5; //初始书本个数
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
public Book[] getBooks() {
return books;
}
}
二、ioperations
1.AddOperation (增加图书)
package ioperations;
import book.Book;
import book.BookList;
public class AddOperation implements IOPeration {
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 newBook = new Book(name,author,price,type);
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("这本书已经存在");
return;
}
}
bookList.setBook(currentSize,newBook);
bookList.setUsedSize(currentSize+1);
System.out.println("添加成功");
}
}
2.BorrowOperation (借阅图书)
package ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
Scanner scanner = new Scanner(System.in);
System.out.printf("请输入你借阅的书名:");
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("不存在");
}
}
3.DelOperation (删除图书)
package ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你删除的书名:");
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(i == currentSize) {
System.out.println("该书不存在");
return ;
}
for (int j = pos; j < currentSize-1; j++) {
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setBook(currentSize-1,null);
bookList.setUsedSize(currentSize-1);
System.out.println("已经删除");
}
}
4.ExitOperation (显示图书)
package ioperations;
import book.BookList;
public class ExitOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
5.FindOperation (查找图书)
package ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("查找图书");
Scanner scanner = new Scanner(System.in);
System.out.printf("请输入你的书名:");
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("不存在");
}
}
6.IOPeration(接口)
定义一个标准(接口)
package ioperations;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
7.ReturnOperation(归还图书)
package ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.printf("请输入你归还的书名:");
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()) {
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
System.out.println("不存在");
}
}
8.ShowOperation(显示图书)
package ioperations;
import book.Book;
import book.BookList;
public class ShowOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
int currentSize = bookList.getUsedSize();//3
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
三、User
1.AdminUser(管理员)
管理员可以管理书架,编辑图书的增加与删除。
管理员对图书的数量,图书号,图书类型,图书名称等进行修改、删除管理,如果普通用户在线借书,图书总数会相应减少。
package user;
import ioperations.*;
import java.util.Scanner;
import ioperations.IOPeration;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
public int menu() {
System.out.println("欢迎"+this.name+"来到图书系统");
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("//");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
2.NormalUser(普通用户)
普通用户可以借阅书籍,在使用后进行归还书籍。
package user;
import ioperations.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.operations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
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("/");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作:");
int choice = scanner.nextInt();
return choice;
}
}
3.User
package user;
import book.BookList;
import ioperations.IOPeration;
public abstract class User {
protected String name;
public IOPeration[] operations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
operations[choice].work(bookList);
}
public void doIoperation(int choice, BookList bookList) {
}
}
四、Main类
用于将整个项目串联到一起,然后整合起来运行。
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();
System.out.println("你好");
User user = login();
while (true){
int choice =user.menu();
user.doIoperation(choice,bookList);
}
}
}