用Java实现 图书管理系统 使用类,抽象类,封装,继承,多态,接口等知识点
Posted wwzzzzzzzzzzzzz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Java实现 图书管理系统 使用类,抽象类,封装,继承,多态,接口等知识点相关的知识,希望对你有一定的参考价值。
目录
3.因为Operation包中各个操作都需要用到book,所以创建一个接口IOperation
图书管理系统
简单的示意图:
分析:
此时分为3部分:
一.书的内容和存取书
二.用户登录,分为管理员和普通用户
三.书相关操作
分别可以创建三个包 package book package operation package user
book包中可以用 Book类来表示一本书 BookList类来表示N本书
user包中可以使用 User类来表示用户,其中User又有 普通用户 可以用NomalUser类来表示 管理员用Admin类来表示
Operation包中分别用
AddOperation类来表示增添图书
DelOperation类来表示删除图书
FindOperation类来表示查找图书
DisplayOperation类来表示显示图书
BorrowOperation类来表示显示图书
ReturnOperation类来表示归还图书
ExitOperation类来表示退出图书
各个类的实现
1.Book类
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)?"被借出":"未被借出")+
'';
2.BookList类
package book;
public class BookList
private Book[] books = new Book[100];//用顺序表的方式存书
private int usedSize;
public BookList()
//给3本书在图书管理系统里
books[0] = new Book("Java SE","博哥",2000,"专业知识");
books[1] = new Book("Java EE","大博哥",3000,"专业知识");
books[2] = new Book("数据结构","博哥哥",4000,"理论知识");
usedSize=3;
public Book getBook(int pos)
return this.books[pos];
public void setBooks(int pos,Book book)
this.books[pos] = book;
public int getUsedSize()
return usedSize;
public void setUsedSize(int usedSize)
this.usedSize = usedSize;
3.因为Operation包中各个操作都需要用到book,所以创建一个接口IOperation
package Operation;
import book.BookList;
import java.util.Scanner;
public interface IOperation
public void work(BookList bookList);
Scanner sc = new Scanner(System.in);
4.User类
package user;
import Operation.IOperation;
import book.BookList;
abstract public class User
protected String name ;//用户名
protected IOperation[] iOperations;//对书的操作
public User(String name)
this.name = name;
abstract public int menu();
public void doOperation(int choice, BookList bookList)
this.iOperations[choice].work(bookList);
5.Admin类
package user;
import Operation.*;
import book.BookList;
import java.util.Scanner;
public class Admin extends User
public Admin(String name)
super(name);
this.iOperations = new IOperation[]
new ExitOperation(),//下标为0
new FindOperation(),//下标为1
new AddOperation(),//下标为2
new DelOperation(),//下标为3
new DisplayOperation()//下标为4
;
@Override
public int menu()
System.out.println("======================================");
System.out.println("hello "+this.name+" 欢迎来到图书馆里系统");
System.out.println("1.查阅图书");
System.out.println("2.添加图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("======================================");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
6.Normal类
package user;
import Operation.*;
import java.util.Scanner;
public class NormalUser extends User
public NormalUser(String name)
super(name);
this.iOperations = new IOperation[]
new ExitOperation(),//下标为0
new FindOperation(),//下标为1
new BorrowOperation(),//下标为2
new ReturnOperation()//下标为3
;
@Override
public int menu()
System.out.println("======================================");
System.out.println("hello "+this.name+" 欢迎来到图书馆里系统");
System.out.println("1.查阅图书");
System.out.println("2.借出图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("======================================");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
7.Main类
import book.BookList;
import user.Admin;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main
public static User login()
System.out.println("请输入你的姓名: ");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("请选择你的身份: 1. 管理员 2. 普通用户");
int choice = sc.nextInt();
if(choice==1)
return new Admin(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);
8.实现DisplayOperation
package Operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("显示图书");
for (int i = 0; i < bookList.getUsedSize(); i++)
Book book = bookList.getBook(i);
System.out.println(book);
9.实现FindOperation
package Operation;
import book.Book;
import book.BookList;
public class FindOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("查找图书");
System.out.println("请输入你要查找的图书的书名:");
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++)
Book book = bookList.getBook(i);
if(book.getName().equals(name))
System.out.println("找到了这本书!");
System.out.println(book);
//找到了这本书直接return
return;
//循环结束 没有找到书
System.out.println("没有这本书!");
10.实现AddOperation
package Operation;
import book.Book;
import book.BookList;
public class AddOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("添加图书! ");
System.out.println("请输入你要添加图书的书名:");
String name = sc.next();
System.out.println("请输入你要添加图书的作者:");
String author = sc.next();
System.out.println("请输入你要添加图书的价格:");
int price = sc.nextInt();
System.out.println("请输入你要添加图书的类型:");
String type = sc.next();
Book book = new Book(name,author,price,type);
bookList.setBooks(bookList.getUsedSize(), book);
bookList.setUsedSize(bookList.getUsedSize()+1);//usedSize要加1
System.out.println("添加成功! ");
11.实现BorrowOperation
package Operation;
import book.Book;
import book.BookList;
public class BorrowOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("借出图书");
System.out.println("请输入你要借的书名:");
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++)
Book book = bookList.getBook(i);
if(book.getName().equals(name))
book.setBorrowed(true);
System.out.println("借书成功");
return;
System.out.println("没有你要借的书!");
12.实现ReturnOperation
package Operation;
import book.Book;
import book.BookList;
public class ReturnOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("归还图书");
System.out.println("请输入你要归还的图书:");
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++)
Book book = bookList.getBook(i);
if(book.getName().equals(name))
book.setBorrowed(true);
System.out.println("归还成功!");
return;
System.out.println("没有你要归还的书籍");
13.实现DelOperation
package Operation;
import book.Book;
import book.BookList;
public class DelOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("删除图书");
System.out.println("请输入你要删除的图书的书名:");
String name = sc.nextLine();
int pos = -1;
for (int i = 0; i < bookList.getUsedSize(); i++)
Book book = bookList.getBook(i);
if (book.getName().equals(name))
pos = i;
break;//有相同的书就返回i的下标
if(pos == -1)
//当pos没有改变时,直接返回
System.out.println("没有你要删除的书籍");
return;
//将pos位置删除,从后往前交换
for (int i = pos; i < bookList.getUsedSize(); i++)
Book book = bookList.getBook(i+1);
bookList.setBooks(i,book);
bookList.setUsedSize(bookList.getUsedSize()-1);//usedSize要-1
System.out.println("删除成功!");
14.ExitOperation
package Operation;
import book.BookList;
public class ExitOperation implements IOperation
@Override
public void work(BookList bookList)
System.out.println("退出系统");
System.exit(0);
运行结果图:
以上是关于用Java实现 图书管理系统 使用类,抽象类,封装,继承,多态,接口等知识点的主要内容,如果未能解决你的问题,请参考以下文章