目标:
创建项目的基本结构;
对基本包进行功能模块的实例化;
实现图书管理系统的基本功能。
一、思路
- 该系统的对象实体主要分为图书实体和用户实体。
- 对于图书实体,可以编写一个图书类,其中包含图书的各项属性(书名,书号,作者等);除此之外,还需要一个书架类,其中包含一个图书数组,和记录图书书目的变量。
- 对于用户实体,包含两类用户:管理员用户和普通用户。因此,可以先写一个用户父类,由管理员类和普通用户类继承该父类,然后再分别实现各自不同的方法。
- 此外还要实现图书操作类,先写一个操作接口,然后由其他操作类都实现这个接口,为方便只要引用该接口就可以实例化所有操作类对象。
二、具体实现效果
进行简易图书管理系统的实现模块:
对于图书包来讲,需要拥有图书类以及图书架。在图书类中具体需要实现的是图书自身的信息,在图书架上存放的主要为各类图书。对于用户类而言,主要有两种,一种是图书管理员,一种是普通用户,他们都继承抽象用户类,在自己内部进行相应的实例化。
- 图书类的主要信息:
- 书架的主要信息:
- 管理员的实现效果:
用户的实现效果:
对于管理员和用户所有的图书操作,在操作类中具体实现,包括查找、删除、显示、借阅、归还等对于书的操作,以及退出系统操作的实现。再次尤其注意,有些操作只能对管理人员,有些操作只能对用户。通过操作接口来实现对书架上图书的操作。
三、具体实现代码段
book包
- 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=" + isBorrowed +
(isBorrowed ? " 已经被借出" : " 未被借出") +
'}';
}
}
- bookList类:
package book;
public class BookList {
private static final int DEFAULT_SIZE = 10;
private Book[] books = new Book[DEFAULT_SIZE];
private int usedSize;//记录当前books数组当中 有多少本书?
public BookList() {
books[0] = new Book("三国演义","罗贯中",89,"小说");
books[1] = new Book("西游记","吴承恩",78,"小说");
books[2] = new Book("红楼梦","曹雪芹",49,"小说");
this.usedSize = 3;
}
public Book getBook(int pos) {
return this.books[pos];
}
public void setBook(Book book) {
this.books[usedSize] = book;
}
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
user包
- User抽象类
package user;
import book.BookList;
import opera.IOperation;
public abstract class User {
protected String name;
protected IOperation[] iOperation;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doWork(int choice, BookList bookList) {
this.iOperation[choice].work(bookList);
}
}
- AdminUser类
package user;
import opera.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperation = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu() {
System.out.println("****************************");
System.out.println("hello "+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("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
- NormalUser类
package user;
import opera.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperation = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu() {
System.out.println("****************************");
System.out.println("hello "+name+" 欢迎来到图书小练习");
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;
}
}
opear包
- IOperation接口
package opera;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
- AddOperation类
package opera;
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("输入类型:");
String type = scanner.nextLine();
System.out.println("输入价格:");
int price = scanner.nextInt();
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);
//修改usedSize
bookList.setUsedSize(currentSize+1);
System.out.println("fssafafdsafa");
}
}
- BorrowOperation类
package opera;
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.isBorrowed()) {
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
}
}
- DelOperation类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation 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();
int index = -1;
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
index = i;
break;
}
}
//挪动数据
for (int j = index; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1];
Book book = bookList.getBook(j+1);
bookList.setBook(j, book);
}
//修改size
bookList.setUsedSize(currentSize-1);
//因为删除的是对象 所以 把最后一个置为null
bookList.setBook(currentSize-1,null);
System.out.println("删除成功!");
}
}
- ExitOperation类
package opera;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
- FindOperation类
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation 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++) {
//System.out.println(bookList[i]);
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("找了这本书:");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
- ReturnOperation类
package opera;
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.isBorrowed()) {
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
}
- ShowOperation类
package opera;
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++) {
//System.out.println(bookList[i]);
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}