文章目录
- 前言
- 一、设计书类、书架类
- 二、设计用户类
- 1.管理员
- 2.普通用户
- 三、操作包
前言
对于图书管理系统我想大家都不会陌生,在C语言的学习中相信大家都写过这个系统,那么今天我们就用Java来实现一下图书管理系统,看看和C语言又有什么不一样的地方呢。
一、设计书类、书架类
图书管理系统最重要的就是书,首先我们需要设计一个书,然后又书架来存放书籍,所以就需要设计一个Book类和一个BookShelf类。
Book里面有各种图书的信息,并提供带参构造方法,同时为了代码的安全性更高,我们将Book中的数据全部用private修饰,然后利用get方法来获取图书信息。
public class Book {
private String name;//图书名
private String author;//作者
private double price;//价格
private String category;//图书分类
private boolean isBorrow;//是否借出
public Book(String name, String author, double price, String category) {
this.name = name;
this.author = author;
this.price = price;
this.category = category;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public boolean isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
@Override
public String toString() {
return "Book{" +
"书名:'" + name + '\'' +
", 作者:'" + author + '\'' +
", 价格:" + price +
", 类别:'" + category + '\'' +
", 借阅情况:" + (isBorrow?"已被借出":"未被借出") +
'}';
}
}
然后再写BookShelf类,存放书籍。首先里面需要定义一个Book类型的数组,我们在调用书架类的构造方法时在书中初始化三本书。
public class Bookshelf {
private Book[] books = new Book[10];
private int num;
public Bookshelf() {
books[0] = new Book("三国演义","罗贯中",18,"名著");
books[1] = new Book("红楼梦","曹雪芹",19,"名著");
books[2] = new Book("西游记","吴承恩",20,"名著");
setNum(3);
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
二、设计用户类
我们的用户一共有两种,为管理员和普通用户,他们的很多信息都是相同的,所以我们可以定义一个抽象类User,然后让管理员和普通用户继承它的属性,并分别重写其中的抽象方法。
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,Bookshelf bookshelf){//操作方法
this.iOperations[choice].work(bookshelf);
//根据选择的操作,调用操作接口数组中对应下标的方法。
}
}
1.管理员
public class Admin extends User{
public Admin(String name) {//构造方法
super(name);//调用父类的构造方法
this.iOperations = new IOperation[]{//给操作接口数组赋值
new ExitOperation(),//退出系统
new FindOperation(),//查找操作
new AddOperation(),//增加操作
new DelOperation(),//删除操作
new ShowOperation()//显示所有图书操作
};
}
@Override
public int menu() {//重写menu方法
Scanner sc = new Scanner(System.in);
int choice = -1;
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("输入操作:");
choice = sc.nextInt();
return choice;//返回选择的操作,调用对应下标操作数组中方法。
}
}
2.普通用户
public class Reader extends User{
public Reader(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowBook(),
new ReturnBook()
};
}
public int menu() {
Scanner sc = new Scanner(System.in);
int choice = -1;
System.out.println("借阅系统");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("输入操作:");
choice = sc.nextInt();
return choice;
}
}
三、操作包
为了实现这些不同的操作方法,我们创建了一个包,在包里面创建了一个操作接口,可以通过它来实现不同的操作类。
public interface IOperation {
void work(Bookshelf bookshelf);
}
新增图书
public class AddOperation implements IOperation {
@Override
public void work(Bookshelf bookshelf) {
System.out.println("增加图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
System.out.println("请输入图书的作者:");
String author = sc.nextLine();
System.out.println("请输入图书的类别:");
String category = sc.nextLine();
System.out.println("请输入图书的价格:");
double price = sc.nextDouble();
Book book = new Book(name,author,price,category);
int num = bookshelf.getNum();
bookshelf.setBooks(num,book);
bookshelf.setNum(num+1);
System.out.println("新增图书成功");
}
}
借阅图书
public class BorrowBook implements IOperation{
@Override
public void work(Bookshelf bookshelf) {
System.out.println("借书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
int num = bookshelf.getNum();
for (int i = 0; i < num; i++) {
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
book.setBorrow(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有这本书");
}
}
查找图书
public class FindOperation implements IOperation{
@Override
public void work(Bookshelf bookshelf) {
System.out.println("查找图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
int num = bookshelf.getNum();
for (int i = 0; i < num; i++) {
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有这本书");
}
}
删除图书
public class DelOperation implements IOperation{
@Override
public void work(Bookshelf bookshelf) {
System.out.println("删除图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
int num = bookshelf.getNum();
for (int i = 0; i < num; i++) {
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
for(int j = i; j < num; j++){
bookshelf.setBooks(j,bookshelf.getBooks(j+1));
}
bookshelf.setNum(bookshelf.getNum()-1);
System.out.println("删除成功");
return;
}
}
System.out.println("没有这本书");
}
}
归还图书
public class ReturnBook implements IOperation{
@Override
public void work(Bookshelf bookshelf) {
System.out.println("归还图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
int num = bookshelf.getNum();
for (int i = 0; i < num; i++) {
Book book = bookshelf.getBooks(i);
if(book.getName().equals(name)){
book.setBorrow(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有此书");
}
}
退出系统
public class ExitOperation implements IOperation{
@Override
public void work(Bookshelf bookshelf) {
System.out.println("退出程序");
System.exit(0);
}
}
显示全部图书
public class ShowOperation implements IOperation{
@Override
public void work(Bookshelf bookshelf) {
System.out.println("显示全部图书信息");
int num = bookshelf.getNum();
for (int i = 0; i < num; i++) {
Book book = bookshelf.getBooks(i);
System.out.println(book);
}
}
}
以上就是我们操作类包里面的所有类,下面就看一下我们主函数如何使用这些类来实现图书管理系统:
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 Reader(name);
}
}
public static void main(String[] args) {
Bookshelf bookshelf = new Bookshelf();
User user = login();
while (true){
while(true){
int choice = user.menu();
user.doOperation(choice,bookshelf);
}
}
}
}