我们要实现的功能:
1. 我们的用户可以分为==》管理员和用户。
2. 我们要实现的内容====》
查找图书==新增图书==删除图书==显示图书==退出系统==借阅图书==归还图书
1. 首先,我们先建一个Book包。
1.1 我们可以在包下,先定义一个Book类。
在Book类里面,我们把所有的对象定义为一个私有对象,那么接下来我们就要写Getter和Setter,当然构造方法和toString方法也不能忘记。
package book;
public class Book {
//首先先输入书的所有属性
private String name;
private String auter;
private int price;
private String type;
private boolean isBorrowed;//isBorrowed默认是false
//构造方法也必须是要有的;因为数存进来默认都是未借出的,所以构造方法里没必要写入isBorrowed.
public Book(String name, String auter, int price, String type) {
this.name = name;
this.auter = auter;
this.price = price;
this.type = type;
}
//因为所有属性都是私有的,现在来写get和set方法。
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuter() {
return auter;
}
public void setAuter(String auter) {
this.auter = auter;
}
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;
}
//当然,toString方法也不能忘了
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", auter='" + auter + '\'' +
", price=" + price +
", type='" + type + '\'' +
(isBorrowed == true ? " 已借出 ":" 未借出 ") +
'}';
}
}
1.2 然后我们可以建立一个书架,把我们的图书存放在里面。
package book;
public class BookList {
//我们可以定义一个Book数组来充当BookList。
private Book[] bookList = new Book[10];
private int usedSize;//这代表此前书架上有几本书
public BookList() {
//先定义三本书。
this.bookList[0] = new Book("三国演义","罗贯中",18,"小说");
this.bookList[1] = new Book("西游记","吴承恩",15,"小说");
this.bookList[2] = new Book("水浒传","施耐庵",19,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//把数放到pos位置。
public void setBookList(int pos,Book book) {
bookList[pos] = book;
}
//获取pos位置的书。
public Book getBook(int pos) {
return bookList[pos];
}
//这里本质是可以写 借阅书籍 等操作的;
}
2. 我们现在需要需要编写用户内容。
2.1 不管是管理员还是普通用户,首先,他是一个用户。
package user;
import book.BookList;
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 doOpreation(int choice, BookList bookList) {
this.iOperations[choice].work(bookList);
}
}
2.2 由于管理者和用户的界面不一样,我们现在要来分开写。
2.2.1 AdminUser
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 DisplayOperation()
};
}
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;
}
}
2.2.2 NormalUser
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("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;
}
}
3. 我们现在要来实现内容。
3.1 新增书籍
package operation;
import book.Book;
import book.BookList;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.Scanner;
public class AddOperation implements IOperation{
public void work(BookList bookList) {
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 type = sc.nextLine();
System.out.println("请输入你要新增的书籍的价钱:");
int price = sc.nextInt();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBookList(currentSize,book);
bookList.setUsedSize(currentSize+1);//成功放入一本书!
System.out.println("新增成功!");
}
}
3.2 借阅图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
public void work(BookList bookList) {
System.out.println("借阅图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("输入你要借阅的书籍:");
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("借阅成功,借阅书籍信息如下:");
System.out.println(book);
return;
}
}
System.out.println("借阅失败,没有这本书!");
}
}
3.3 删除图书
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 sc = new Scanner(System.in);
System.out.println("请输入你要删除的图书:");
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
index = i;
break;
}
}
//如果index == -1 说明没有这本书
if (index == -1) {
System.out.println("没有你要删除的图书!");
return;
}
//这里开始删除
for (int i = index; i < currentSize - 1; i++) {
//bookList[i] = bookList[i+1];
Book book = bookList.getBook(i + 1);
bookList.setBookList(i, book);
}
bookList.setUsedSize(currentSize - 1);
}
}
3.4 显示图书
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation {
public void work(BookList bookList) {
System.out.println("显示图书!");
int currenSize = bookList.getUsedSize();
for (int i = 0; i < currenSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
3.5 查找图书
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("查找图书!");
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要查找的书籍:");
String name = sc.nextLine();
int size = bookList.getUsedSize();
for (int i = 0; i < size; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
3.6 归还图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
public void work(BookList bookList) {
System.out.println("归还图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("输入你要归还的书籍:");
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("归还成功,归还书籍信息如下:");
System.out.println(book);
return;
}
}
System.out.println("归还失败,没有这本书!");
}
}
3.7 退出系统
package operation;
import book.Book;
import book.BookList;
public class ExitOperation implements IOperation {
public void work(BookList bookList) {
//这里可以手动对书架进行清空一次
System.out.println("退出系统!");
System.exit(0);
}
}
3.8 我们发现,所有内容的work的返回值都是(BookList bookList),那么我们是否可以用接口来实现对work的定义呢?如下:
package operation;
import book.Book;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
以上就是图书管理系统的过程了,只是大致方法,具体还得细细体会。
结果: