1.摘要
图书管理贯彻在我们生活当中,使用java语言中的异常处理,抽象类多态,继承,封装,接口等知识点,来综合性的设计与实现出一个简易的图书管理系统。
2.设计思想
1.首先我们需要区分出管理员与普通用户,不同的用户所展示的界面不同,利用继承和多态可以实现这一思路;
2.我们需要实现管理员能够添加书籍,查找书籍,删除书籍,显示书籍及退出系统。
3.普通用户实现查找书籍,借阅书籍,归还书籍及退出系统。
4.删除及添加书籍我们运用数组的思想,进行遍历。
5.将用户和管理员的每个操作分别封装成一个类,方便继承。
3.项目分布
图书类
Book类: 定义了书的一些属性(书名,作者,价格,分类,状态)并且写了属性的get、set方法
package 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)?"已经借出":"未被借出")+
//", isBorrowed=" + isBorrowed +
'}';
}
}
书架类
public class BookList {
private Book[] books;
private int userSize;
public BookList(){
this.books=new Book[10];
//放书
this.books[0]=new Book("三国演义","罗贯中",18,"小说");
this.books[1]=new Book("红楼梦","曹雪芹",19,"小说");
this.books[2]=new Book("西游记","吴承恩",22,"小说");
this.userSize=3;
}
public int getUserSize() {
return userSize;
}
public void setUserSize(int userSize) {
this.userSize = userSize;
}
public Book getBook(int pos){
return books[pos];
}
public void setBook(int pos,Book book){
books[pos]=book;
}
public Book[] getBooks() {
return books;
}
}
操作类
增加图书
public class AddOperation implements IOPeration{
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("请输入价格");
int price = scanner.nextInt();
Book book = new Book(name, author, price,type);
//检查数组当中有没有这本书
int currentSize=bookList.getUserSize();
for (int i = 0; i < currentSize; i++) {
Book book1=bookList.getBook(i);
if(book1.getName().equals(name)){
System.out.println("有这本书,不进行存放");
return;
}
}
if(currentSize==bookList.getBooks().length){
System.out.println("书架满了");
}else{
bookList.setBook(currentSize,book);
bookList.setUserSize(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.getUserSize();
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功!");
System.out.println(book);
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 pos=-1;
int currentSize=bookList.getUserSize();
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;
}
//开始删除
int j=pos;
for ( j = pos; j <currentSize-1 ; j++) {
Book book=bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setBook(j,null);
bookList.setUserSize(currentSize-1);
}
}
退出系统
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);
String name = scanner.nextLine();
int currentSize = bookList.getUserSize();
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("没有找到");
}}
接口
package operation;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
归还图书
public class ReturnOperation 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.getUserSize();
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("归还成功!");
System.out.println(book);
return;
}
}
System.out.println("归还的图书不存在");
}
}
打印图书
public class ShowOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("打印图书!");
int currentSize = bookList.getUserSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
管理员类
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()};
}
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.退出系统*********");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作");
int choice = scanner.nextInt();
return choice;
}
}
Main类
public class Main {
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 user = login();
while (true) {
int choice = user.menu();
System.out.println("choice:" + choice);
//根据choice 来决定调用哪个方法
user.doOperation(choice, bookList);
}
}
普通用户类
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations=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.退出系统*********");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的操作");
int choice = scanner.nextInt();
return choice;
}
}
抽象类
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
ioPerations[choice].work(bookList);
}
}
4.运行截图: