图书管理系统
图书管理系统的概要
该图书管理系统其用了java中类,抽象类,封装,继承,多态,接口等知识,适合初学java者的一个练习,能让初学者更加熟悉了解Java。此管理系统有两个端:一为客户端,二为管理端;管理端可以实现查阅书籍,增加书籍,删除书籍,打印书籍列表,退出系统等功能其是管理员才能使用,而普通用户只能使用客户端,其客户端只能实现查询书籍借阅书籍,归还书籍,退出系统等功能。
类的设计
有关图书的类
Book 类
创建一个有关图书的类将图书有关的信息集中在一个类里面便于以后的查找和修改,一般来说,图书管理系统的图书含有以下信息:书的名称,书的作者,书的类型,书的价格,该书是否已经被借阅:其使用布尔类型进行判断:fasle为未借出,true为已经借出,此外,图书类里面的成员变量都应该使用private将其封装,这样能保护或者防止代码(数据)被我们无意中破坏。并进行该类的构造方法,并重写toString方法
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBoroowed;
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 isBoroowed() {
return isBoroowed;
}
public void setBoroowed(boolean boroowed) {
isBoroowed = boroowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBoroowed == true) ? " 已经借出 ":" 未借出" )+
'}';
}
}
BookList 类
Book 类建立好后,就需要将其保存在一个数组里面,因此就创建一个BookList类用于储存Book类 其该类包括书的数量和一个储存Book类的数组其容量为10,并将它们进行封装,该构造方法中可以先储存3个数,并将数量设为3;
package book;
public class BookList {
private int usedSize;
private Book[] books=new Book[10];
public BookList() {
books[0]=new Book("三国演义","罗贯中",20,"小说");
books[1]=new Book("红楼梦","曹雪芹",25,"小说");
books[2]=new Book("西游记","吴承恩",30,"小说");
usedSize=3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBooks(int i) {
return books[i];
}
public void setBooks(int pos,Book book) {
this.books[pos]=book;
}
}
创建操作相关的类
创建一个接口IOperation
创建一个接口,并让其他与操作相关的类实现该该接口,这样可以提高工作效率
package opeartion;
import book.BookList;
public interface IOPeration {
public void work(BookList bookList);
}
创建AddOperation类
该类为增加图书的操作。
package opeartion;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
@Override
public void work(BookList bookList) {
Scanner in=new Scanner(System.in);
System.out.println("新增图书!");
System.out.println("请输入图书的名字:");
String name=in.nextLine();
System.out.println("请输入图书的作者:");
String author=in.nextLine();
System.out.println("请输入图书的价格:");
int price= in.nextInt();
System.out.println("请输入图书的类型:");
String type=in.nextLine();
Book book=new Book(name,author,price,type);
int size=bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book1=bookList.getBooks(i);
if (book1.getName().equals(book.getName())){
return;
}
}
bookList.setBooks(size,book);
bookList.setUsedSize(size+1);
System.out.println("新增图书成功!");
}
}
创建BorrowOperation类
该类为借阅图书的操作。
package opeartion;
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 in=new Scanner(System.in);
System.out.println("请输入想借图书的名字:");
String name=in.nextLine();
int size=bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book= bookList.getBooks(i);
if (book.getName().equals(name)){
if(book.isBoroowed()==true){
System.out.println("借取失败!");
System.out.println("图书:"+name+"已经被借出");
}
else{
System.out.println("借取成功");
book.setBoroowed(true);
System.out.println(book);
}
return;
}
}
System.out.println("借阅失败!");
}
}
创建DelOperation类
该类为删除图书的操作
package opeartion;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner in=new Scanner(System.in);
System.out.println("请输入需要删除的图书:");
String name=in.nextLine();
int size=bookList.getUsedSize(),i=-1,j;
for (j = 0; j < size; j++) {
Book book=bookList.getBooks(j);
if(book.getName().equals(name)){
i=j;
break;
}
}
if(i!=-1){
for (j = i; j < size-1; j++) {
Book book2=bookList.getBooks(i+1);
bookList.setBooks(j,book2);
}
bookList.setUsedSize(size-1);
System.out.println("删除成功!");
return;
}
System.out.println("删除失败!");
}
}
创建DisplayOperation类
该类为打印所有图书的操作
package opeartion;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOPeration{
@Override
public void work(BookList bookList) {
int size= bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book= bookList.getBooks(i);
System.out.println(book.toString());
}
}
}
创建ExitOperation类
该类为退出图书系统的操作
package opeartion;
import book.BookList;
public class ExitOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.exit(0);
System.out.println("退出系统!");
}
}
创建FindOperation类
该类为查找图书的操作
package opeartion;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
Scanner in=new Scanner(System.in);
System.out.println("请输入你想要查找的图书的名字:");
String name=in.nextLine();
int size=bookList.getUsedSize(),i;
for (i = 0; i < size; i++) {
Book book= bookList.getBooks(i);
if (name.equals(book.getName())){
System.out.println("查找成功");
System.out.println(book.toString());
return;
}
}
System.out.println("当前图书馆中没有你想要的图书");
}
}
创建ReturnOperation类
该类为归还图书的操作
package opeartion;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入归还图书的名字:");
Scanner in=new Scanner(System.in);
String name=in.nextLine();
int size= bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book= bookList.getBooks(i);
if(name.equals(book.getName())){
book.setBoroowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("没有借阅过这本书或者根本没有这本书!");
}
}
创建用户相关的类
User 类
创建 User 类, 这是一个抽象类,其里面有菜单的抽象方法,有用户名的名字和操作功能的数组,其用户选项执行操作可以使用doOperation方法
package user;
import book.BookList;
import opeartion.IOPeration;
abstract public class User {
protected String name;
protected IOPeration[] operations;
public User(String name){
this.name=name;
}
abstract public int menu();
public void doOperation(int choice, BookList bookList) {
operations[choice].work(bookList);
}
}
NormalUser类
创建普通用户类, 是 User 的子类,其该类的构造方法先使用调用父类的构造方法,命名名字,再创建 该类的operations数组。并将抽象类的方法进行重写。
package user;
import book.BookList;
import opeartion.*;
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()};
}
@Override
public int menu() {
Scanner in=new Scanner(System.in);
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("=============================");
System.out.println("请输入你的选择");
int choice=in.nextInt();
return choice;
}
}
Admin类
创建管理员用户类,是 User 的子类,其该类的构造方法先使用调用父类的构造方法,命名名字,再创建该类的operations数组。并将抽象类的方法进行重写。
package user;
import book.BookList;
import opeartion.*;
import java.util.Scanner;
public class Admin extends User{
public Admin(String name){
super(name);
this.operations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
Scanner in=new Scanner(System.in);
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("=============================");
System.out.println("请输入你的选择");
int choice=in.nextInt();
return choice;
}
}
测试
创建一个Main类进行测试,该类创建一个方法login用于创建一个用户类是普通用户还是管理员
package main;
import book.BookList;
import user.Admin;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner in=new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name=in.nextLine();
System.out.println("请选择自己的身份:0.管理员 1.普通用户");
int choice=in.nextInt();
if(choice==0){
return new Admin(name);
}
else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
User user=login();
BookList bookList=new BookList();
while (true){
int choice=user.menu();
user.doOperation(choice,bookList);
}
}
}
测试结果
管理员
用户端
|