今天与大家分享的是一个图书管理系统,这里我们运用的是java基础的语法其中包括类和对象、继承、封装、多态、抽象类、接口还有数组等。
我们需要实现一个可以进行管理员操作和用户操作的图书管理系统,其中包括了管理员操作(查找,添加,删除,显示,退出系统);用户操作(查找,借阅,归还,退出系统).
简单示意图:
可以分为三步:
一.书的内容和存取书
二.用户登录,分为管理员和普通用户
三.书相关操作
在idea中新建一个Book包,用来存放图书相关的类。
使用Java的封装的特点,用私有化的权限定义一些书的属性。这些属性创建之后,需要进行初始化,可以使用构造方法,同时,重写Book类中的toString方法,方便打印。
package book;
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//书的类型
private boolean isBorrowed;//是否被借出 默认值是false
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 +
'}';
}
}
创建一个BookList类来定义书架,在BookList类里面定义一个Book类的数组,还应该定义一个变量表示有多少本书。因为一个书架上不一定是满书的状态。在构造方法里面定义BooK数组大小为10,初始化3本书。
BookList里面需要什么方法呢?获取书架上某一个位置的书、改变书架上某一个位置的书、获取书架上书的数量、当书减少或增加时改变书的数量。
package book;
public class BookList {
private Book[] books;
private int usedSize;//记录当前书架放了几本书
public BookList() {
this.books = new Book[10];
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",10,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(Book book,int pos) {
books[pos] = book;
}
}
管理员类和普通用户类都有一些共性:用户姓名、菜单等,所以可以把它们共有的抽取出来,形成继承关系。
包含基本属性:姓名,menu(菜单)方法的声明,doOperation(执行方法操作)方法的声明
package user;
import book.BookList;
import operation.FindOperation;
import operation.IOPeration;
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) {
//this.ioPerations[choice].work(bookList);
IOPeration ioPeration = this.ioPerations[choice];
ioPeration.work(bookList);
}
}
管理员类继承父类User类。
package user;
import operation.*;
import java.util.Scanner;
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.退出系统");
System.out.println("************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
普通用户类继承了父类User类
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(),
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("***************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
这个包是整个程序最为关键的部分,包含图书管理上基本操作的实现,涉及了大量JAVA语言的基本语法与运用,下面我将进行逐步讲解。
为了更好的组织管理员与普通用户的操作,我在这里创建了接口后面让其余操作实现这个接口,在用户里以IOPeration接口创建数组,区分开管理员与普通用户的操作
package operation;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}
添加图书的方法
ackage operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
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("请输入你要新增图书的价格:");
int price = scanner.nextInt();
System.out.println("请输入你要新增图书的类型:");
scanner.nextLine();
String type = scanner.nextLine();
//此时就可以构造出 一个书的对象
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
System.out.println("存在这本书,不能重复添加!");
return;
}
}
//没有重复的书 开始新增
bookList.setBook(book,currentSize);
bookList.setUsedSize(currentSize+1);
}
}
寻找图书的方法
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请写出你要查找的图书的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
//Book book = bookList[i];
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("存在这本书,信息如下:");
System.out.println(book);
return;
}
}
//代码没有return 出去 ,么有你要找的书
System.out.println("没有你要找的这本书,书名为:"+ name);
}
}
删除图书所使用的方法
package operation;
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.println("请输入你要删除图书的书名:");
int currentSize = bookList.getUsedSize();
int index = -1;
String name = scanner.nextLine();
int i = 0;
for (; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
index = i;
break;//记录下来了 要删除图书的姓名
}
}
//
if(i >= currentSize) {
System.out.println("没有你要删除的图书!");
return;
}
//可以删除了 怎么删 稍等
for (int j = index; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1]
Book book = bookList.getBook(j+1);
bookList.setBook(book,j);
}
bookList.setBook(null,currentSize-1);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
显示所有图书的方法
package operation;
import book.Book;
import book.BookList;
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[i];
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
借出图书的方法
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请写出你要借阅的图书的书名:");
Scanner scanner = new Scanner(System.in);
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(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有你要借阅的图书:"+name);
}
}
归还图书的方法
package operation;
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 scanner = new Scanner(System.in);
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("没有你要归还的图书:"+name);
}
}
退出系统所使用的方法。
ackage operation;
import book.BookList;
public class ExitOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
//应该要 对 bookList 资源 手动回收
System.exit(0);
}
}
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
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 = new AdminUser(name);
// user = new NormalUser(name);
User user = login();
while (true) {
int choice = user.menu();
//根据你菜单返回的choice来执行对应的操作
user.doOperation(choice, bookList);
}
}
}
来到这里,我们的图书管理系统也就算讲解完毕了,希望对大家的学习有所帮助,也希望大家多多支持~~