1.Book类代码
package com.manager.book;
import java.util.Date;
public class Book {
private int id; //图书编号
private String name; //图书名称
private int state; //1表示图书库,0表示被借走了
private Date borrowDate; //借走日期
private int borrowCount; //被借次数
public int getBorrowCount () {
return borrowCount;
}
public void setBorrowCount ( int borrowCount ) {
this.borrowCount = borrowCount;
}
//构造方法
public int getId () {
return id;
}
public void setId ( int id ) {
this.id = id;
}
public String getName () {
return name;
}
public void setName ( String name ) {
this.name = name;
}
public int getState () {
return state;
}
public void setState ( int state ) {
this.state = state;
}
public Date getBorrowDate () {
return borrowDate;
}
public void setBorrowDate ( Date borrowDate ) {
this.borrowDate = borrowDate;
}
}
2.BookManager类代码
package com.manager.book;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class BookManager {
private Book[] books;
private Scanner input = new Scanner ( System.in );
//图书初始化
public void init ( ) {
books = new Book[ 10 ];
Book book0 = new Book ( );
book0.setId ( 1000 );
book0.setName ( "云计算" );
book0.setState ( 1 );
// book0.setBorrowDate(null);
// book0.setBorrowDate(0);
Book book1 = new Book ( );
book1.setId ( 1001 );
book1.setName ( "虚拟化" );
book1.setState ( 1 );
// book1.setBorrowDate(null);
// book1.setBorrowDate(0);
Book book2 = new Book ( );
book2.setId ( 1002 );
book2.setName ( "大数据" );
book2.setState ( 1 );
// book2.setBorrowDate(null);
// book2.setBorrowDate(0);
books[ 0 ] = book0;
books[ 1 ] = book1;
books[ 2 ] = book2;
}
//图书菜单
public void menu ( ) {
System.out.println ( "=======欢迎进入图书管理系统======" );
System.out.println ( "请根据提示选择" );
System.out.println ( "1.增加图书" );
System.out.println ( "2.删除图书" );
System.out.println ( "3.借阅图书" );
System.out.println ( "4.归还图书" );
System.out.println ( "5.图书风云榜" );
System.out.println ( "6.查看图书" );
int choice = input.nextInt ( );
switch ( choice ) {
case 1:
addBook ( );
break;
case 2:
deleteBook ( );
break;
case 3:
borrowBook ( );
break;
case 4:
returnBook ( );
break;
case 5:
BookRank ( );
break;
case 6:
showBooks ( );
break;
default:
}
}
private void isContinueMenu ( ) {
System.out.println ( "是否回到主菜单其他操作(y/其他" );
String isContinue = input.next ( );
if ( "y".equals ( isContinue ) ) {
menu ( ); //递归调用
}
}
//增加图书
private void addBook ( ) {
System.out.println ( "增加图书..." );
Book book = new Book ( );
System.out.println ( "请输入书编号" );
int id = input.nextInt ( );
System.out.println ( "请输入书名字" );
String name = input.next ( );
int state = 1;
// Date bDate =null;
// int bCount = 0;
// isContinueMenu();
book.setId ( id );
book.setName ( name );
book.setState ( 1 );
int position = - 1;
for ( int i = 0 ; i < books.length ; i++ ) {
if ( books[ i ] == null ) {
position = i;
break;
}
}
System.out.println ( position );
books[ position ] = book; //放书
isContinueMenu ( );
}
//删除图书
private void deleteBook ( ) {
System.out.println ( "请输入需要删除图书名称" );
String name = input.next ( );
//定位图书位置
int position = - 1;
for ( int i = 0 ; i < books.length ; i++ ) {
if ( books[ i ].getName ( ) .equals ( name ) ) {
position = i;
break;
}
}
//删除图书位置=移动图书位置
int start = position + 1;
int firstNullPosition = - 1;
for ( int i = 0 ; i < books.length ; i++ ) {
if ( books[ i ] == null ) {
firstNullPosition = i;
break;
}
}
int end = firstNullPosition - 1;
if ( firstNullPosition == - 1 ) {//书架已满,不存在空元素
end = books.length - 1;
}
//移动 前->后
for ( int i = start ; i <=end ; i++ ) {
books[ i - 1 ] = books[ i ];
}
books[ end ] = null;
isContinueMenu ( );
}
//借阅图书
private void borrowBook ( ) {
System.out.println ( "借阅图书" );
System.out.println ( "请输入借阅图书的名称" );
//定位图书位置
String name=input.next ();
int position = - 1;
for ( int i = 0 ; i < books.length ; i++ ) {
if ( books[ i ].getName ( ) .equals ( name ) ) {
position = i;
break;
}
}
books[position].setState ( 0 ); //取书
Date date=new Date ( );
books[position].setBorrowDate ( date ); //当前时间
isContinueMenu ( );
}
//归还图书
private void returnBook ( ) {
System.out.println ( "归还图书" );
System.out.println ( "请输入归还图书的名称" );
//定位图书位置
String name=input.next ();
int position = - 1;
for ( int i = 0 ; i < books.length ; i++ ) {
if ( books[ i ].getName ( ) .equals ( name ) ) {
position = i;
break;
}
}
books[position].setState ( 1 );
books[position].setBorrowDate ( null );
int count=books[position].getBorrowCount ()+1;
books[position].setBorrowCount ( count );
isContinueMenu ( );
}
//风云榜图书 //根据被借次数降序
private void BookRank ( ) {
System.out.println ( "图书风云榜" );
Book[] newBooks=new Book[books.length];
for ( int i=0;i<books.length;i++ ){
if ( books[i]!=null ){
newBooks[i]=books[i];
}
}
for ( int i=0;i<newBooks.length-1;i++ ){
for ( int j=0;j<newBooks.length-1-i;j++ ){
if ( newBooks[j]!=null && newBooks[j+1]!=null){
if ( newBooks[j].getBorrowCount ()<newBooks[j+1].getBorrowCount () ){
Book temp=newBooks[j];
newBooks[j]=newBooks[j+1];
newBooks[j+1]=temp;
}
}
}
}
System.out.println ( "编号\t\t\t书名\t\t\t库存\t\t被借日期\t\t被借次数" );
for ( Book book : newBooks ) {
if ( book != null ) { //解决空指针
String borrowDateSrt=""; //显示日期
if ( book.getBorrowDate ()==null ){
borrowDateSrt="-";
}else {
Date bDate=book.getBorrowDate ();
SimpleDateFormat sdf=new SimpleDateFormat ( "yyyy-MM-dd" );
borrowDateSrt= sdf.format ( bDate );
}
String stateStr = book.getState ( ) == 0 ? "被借" : "在库";
System.out.println ( book.getId ( ) + "\t\t" + book.getName ( ) + "\t\t" + stateStr + "\t\t" + borrowDateSrt + "\t\t\t" + book.getBorrowCount ( ) );
}
}
isContinueMenu ( );
}
//查看所有图书
private void showBooks ( ) {
System.out.println ( "编号\t\t\t书名\t\t\t库存\t\t被借日期\t\t被借次数" );
for ( Book book : books ) {
if ( book != null ) { //解决空指针
// String borrowDateSrt = book.getBorrowDate ( ) == null ? "_" : book.getBorrowDate ( ) + "";
String borrowDateSrt=""; //显示日期
if ( book.getBorrowDate ()==null ){
borrowDateSrt="-";
}else {
// borrowDateSrt=book.getBorrowDate ()+"";
Date bDate=book.getBorrowDate ();
//Date <-> String
SimpleDateFormat sdf=new SimpleDateFormat ( "yyyy-MM-dd" );
//Date -> String
borrowDateSrt= sdf.format ( bDate );
}
String stateStr = book.getState ( ) == 0 ? "被借" : "在库";
System.out.println ( book.getId ( ) + "\t\t" + book.getName ( ) + "\t\t" + stateStr + "\t\t" + borrowDateSrt + "\t\t\t" + book.getBorrowCount ( ) );
}
}
isContinueMenu ( );
}
}
3.TestBookManager类代码
package com.manager.book;
public class TestBookManager {
public static void main ( String[] args ) {
BookManager manage=new BookManager ();
manage.init ();
manage.menu ();
}
}
4.运行效果显示