目录
1.功能分类
为了更好的实现该系统,可以将不同的功能模块放在不同的包中。
BOOK:该包用于存放一个类,这个类是描述一个图书的基本信息。
比如:书的名字,价格,作者,类型,是否被借出等等
BookList:这个包用于存放一个书架类,这个类有一个BOOK[ ]用于存放各式各样的书,
同时该类可以得到书架的信息,有多少本书,增添书籍,查看书籍等。
Operation:这个包用于实现对于书籍的跟中操作方法,增加,查看,展示,借阅,删除,归还,退出系统等等方法
User:这个包用于实现不同的使用者身份,有了不同的使用者就可以针对他的身份提供不同操作方法。
Main:java文件包含主要流程的代码实现
2.BOOK包中书籍的实现
public class Book {
public String name;
public String auther;
public int price;
public String type;
public Boolean lend;
//一本书的基本信息,构造方法
public Book(String name, String auther, int price, String type, Boolean lend) {
this.name = name;
this.auther = auther;
this.price = price;
this.type = type;
this.lend = lend;
}
public void setName(String name) {
this.name = name;
}
public void setAuther(String auther) {
this.auther = auther;
}
public void setPrice(int price) {
this.price = price;
}
public void setType(String type) {
this.type = type;
}
public void setLend(Boolean lend) {
this.lend = lend;
}
public String getName() {
return name;
}
public String getAuther() {
return auther;
}
public int getPrice() {
return price;
}
public String getType() {
return type;
}
public Boolean isLend() {
return lend;
}
//重写构造方法,打印书籍,,注意lend打印时需要转换成借出未借出的状态
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", auther='" + auther + '\'' +
", price=" + price +
", type='" + type + '\'' +
", lend=" + (lend==true?("借出"):("未借出") )+
'}';
}
}
3.BookList包中代码的实现
public class BookList {
Book[] books = new Book[10];//表示书架的初始大小
public int usedsize = 0;//表示初始书架的被实际使用的空间
//先在书架上放三本书,初始被使用长度为3
public BookList(){
books[0] = new Book("西游记","吴承恩",10,"小说",false);
books[1] = new Book("红楼梦","曹雪芹",12,"小说",false);
books[2] = new Book("水浒传","施耐庵",13,"小说",false);
this.usedsize = 3;
}
public Book[] getBooks() {
return books;
}
public int getUsedsize() {
return usedsize;
}
//在书架上放书,具体到某个位置
public void setBooks(Book book,int pos) {
books[pos] = book;
}
public void setUsedsize(int usedsize) {
this.usedsize = usedsize;
}
//判断书架是否满了
public Boolean isfull(){
if(this.usedsize==books.length){
return true;
}
return false;
}
}
4.Operation中各类方法的实现
1.首先要实现一个接口,这个接口所有操作方法都可以重写使用
Ioperation接口:
public interface Iopertion {
public void work( BookList bookList);
}
2.AddbookOperation接上接口增添书籍的代码实现:
public class AddbookOpertion implements Iopertion{
@Override
public void work(BookList books) {
//首先判断书架是否已满,书架已满就不能再添加,直接退出
System.out.println("增添图书》》》");
if(books.isfull()){
System.out.println("图书已满!");
return;
}
//输入书籍信息
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("请输入本书的类型");
String type = scanner.nextLine();
//创建一本书,通过构造方法创建
Book book = new Book(name,author,price,type,false);
//得到书架的使用长度
int currentsize = books.usedsize;
//在下表为currentsize的位置插入书籍
books.setBooks(book, currentsize);
//插入完成,使用长度加一
books.setUsedsize(currentsize+1);
System.out.println("增添图书成功!");
}
}
3.BorrowedOperation借阅书籍
public class BorrowedOperation implements Iopertion{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书》》");
System.out.println("请输入你要借阅的图书!");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
//遍历数组,如果找到与名字相同的书籍,就将该书籍的状态置为true表示已经被借出
for (int i = 0; i < bookList.getUsedsize(); i++) {
Book book =bookList.getBooks()[i];
if(name.equals(book.getName())){
book.setLend(true);
return;
}
}
System.out.println("要借阅的图书不存在!");
}
}
4.DalabookOperation删除书籍
public class DelebookOpertion implements Iopertion{
@Override
public void work(BookList bookList) {
System.out.println("删除图书》》");
//删除书籍首先书架不能为空
int currentsize = bookList.getUsedsize();
if(currentsize==0){
System.out.println("书架已空,不能删除!");
return;
}
System.out.println("请输入你要删除的图书书名》》");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int i = 0;
//外层循环找到要删除的书籍
for (; i < currentsize; i++) {
Book book1 = bookList.getBooks()[i];
if(book1.name.equals(name)){
//内层循环将该书籍后一位赋给前一位的书,循环将该书后面的书籍全部向前移动一位
for (int j = i; j < currentsize-1; j++) {
Book book = bookList.getBooks()[j+1];/
Book book = bookList.getBooks()[j+1];
bookList.setBooks(book,j);
}
//暑假长度减一
bookList.usedsize-=1;
System.out.println("删除成功!");
return;
}
}
//如果i已经大于了书架长度,说明没有找到要删除的书籍
if(i>bookList.usedsize){
System.out.println("删除失败!");
}
}
}
5.查找书籍FindBookOperation
public class FindbookListOperation implements Iopertion{
@Override
public void work(BookList bookList) {
System.out.println("查找图书》》");
System.out.println("请输入要查找的书名!");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedsize(); i++) {
Book book = bookList.getBooks()[i];
if(name.equals(book.getName())){
System.out.println(book.toString());
return;
}
}
System.out.println("没有改书籍!");
}
}
6.展示书架ShowBookOperation
public class ShowbookListOperation implements Iopertion{
@Override
public void work(BookList bookList) {
System.out.println("展示书架》》");
if(bookList.usedsize==0){
System.out.println("书架为空!");
return;
}
int currentsize = bookList.getUsedsize();
for (int i = 0; i < bookList.getUsedsize(); i++) {
Book book = bookList.getBooks()[i];
// sout(toString(book))
System.out.println(book.toString());
}
}
}
7.归还图书ReturnBookOperation
public class ReturnbookListOperation implements Iopertion{
@Override
public void work(BookList bookList) {
System.out.println("归还图书》》");
System.out.println("请输入要归还的图书的名字!");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//直接遍历书架找到该书籍,将状态置为false 表示未借出
for (int i = 0; i < bookList.getUsedsize(); i++) {
Book book = bookList.getBooks()[i];
if(name.equals(book.getName())){
book.setLend(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("归还失败!");
}
}
8.ExitOperation推出图书系统
public class ExitOperation implements Iopertion{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
//退出时将书架每一个位置置为空
for (int i = 0; i < bookList.getUsedsize(); i++) {
bookList.setBooks(null,i);
}
System.exit(0);
}
}
5.User使用者身份
public abstract class user {
protected String name;
//定义一个数组接口,用来一次性new多种不同的方法,用于操作者使用。
protected Iopertion[] iopertions;
public user(String name){
this.name = name;
}
//一个抽象方法,来打印菜单方法
public abstract int menu();
//一个方法用来接收操作者选择身份的返回值,从而调用相应的方法
public void doIoperation(int chioce, BookList bookList) {
this.iopertions[chioce].work(bookList);
}
}
5.1Normaluser正常普通用户
public class Normaluser extends user{
public Normaluser(String name){
super(name);
//普通用户可以选择的方法
this.iopertions = new Iopertion[]{
new BorrowedOperation(),
new ShowbookListOperation(),
new FindbookListOperation(),
new ReturnbookListOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
while (true){
System.out.println("*********用户菜单********");
System.out.println("0.借阅图书");
System.out.println("1查看图书");
System.out.println("2.查找图书");
System.out.println("3.归还图书");
System.out.println("4.退出系统");
System.out.println("***********************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int chioce = scanner.nextInt();
//如果操作小于0,或者大于4,就错误
if(chioce<0 || chioce>4){
System.out.println("输入操作错误,请重新输入!");
continue;
}
//返回选择值
return chioce;
}
}
}
5.2图书管理者
public class AdminUser extends user{
public AdminUser(String name){
super(name);
this.iopertions = new Iopertion[]{
new FindbookListOperation(),
new AddbookOpertion(),
new DelebookOpertion(),
new ShowbookListOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
while (true){
System.out.println("**********管理员菜单********");
System.out.println("0.查找图书");
System.out.println("1.新增图书");
System.out.println("2.删除图书");
System.out.println("3.显示图书");
System.out.println("4.退出系统");
System.out.println("***************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if(choice<0 || choice>4){
System.out.println("输入错请重新输入");
continue;
}
return choice;
}
}
}
5.Main主要代码的流程
public class Main {
public static user login(){
//创建一个方法,返回User类型的操作者的对象
System.out.println("图书馆信息管理系统》》");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的名字:");
String name = scanner.nextLine();
System.out.println("请选择身份:1.管理员 2.普通用户");
int chioce = scanner.nextInt();
if(chioce==1){
AdminUser adminUser = new AdminUser(name);
return adminUser;
}
if (chioce==2){
Normaluser normaluser = new Normaluser(name);
return normaluser;
}
return null;
}
public static void main(String[] args) {】
//创建书架
BookList bookList = new BookList();
user user = login();
通过操作者的方法的选择,用choice来接受
while (true){
int choice = user.menu();
//将chioce传递给doIoperation,来调用相应的方法
user.doIoperation(choice,bookList);
}
}
}