需求:
实现简单的用户管理,图书管理和借阅管理,字符界面,没有做GUI(偷懒了一点呵呵)
使用数组列表保存数据ArrayList, 退出后所有数据丢失,没有用文件系统和数据库,
主要是保留自己以前用C写的时候的样子,怀念一下
改进意见:
可以考虑使用XML来保存数据,我的blog里有一篇简单介绍Jdom的,数据库也没有问题
改进了以后请大家发一份copy给我,FaneAnn@yahoo.com.cn
程序代码如下:
/*
* @(#)BookManager.java
* Have classes:BookManager Book Borrower
* Copyright 2005 Fane. All rights reserved.
* @author Fane
* @version 1.0.0.0, 09/05/05
* @since JDK 1.4
*/
package person.fane.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
public class LibraryManager
{
private ArrayList bookList = new ArrayList();
private ArrayList userList = new ArrayList();
public final int maxBorrower = 100;
public final int maxBook = 100;
public static void main(String args[])
{
//System.out.println("欢迎来到图书管理系统!!");
LibraryManager newLibraryManager = new LibraryManager();
BookManagerThread myBookManagerThread = new BookManagerThread(newLibraryManager);
new Thread(myBookManagerThread).start();
}
/**
* @return 返回 bookList。
*/
public ArrayList getBookList() {
return bookList;
}
/**
* @return 返回 userList。
*/
public ArrayList getUserList() {
return userList;
}
/**
* @param userList 要设置的 userList。
*/
public void setUserList(ArrayList userList) {
this.userList = userList;
}
}
class Book
{
/*
* Book类包含的属性
*/
private String bookName;
private float bookPrice;
private int bookType;
private Date publishDate;
private boolean isBorrowed;
private Date borrowedDate;
/**
* @return 返回 bookName。
*/
public String getBookName() {
return bookName;
}
/**
* @param bookName 要设置的 bookName。
*/
public void setBookName(String bookName) {
this.bookName = bookName;
}
/**
* @return 返回 bookPrice。
*/
public float getBookPrice() {
return bookPrice;
}
/**
* @param bookPrice 要设置的 bookPrice。
*/
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
/**
* @return 返回 bookType。
*/
public int getBookType() {
return bookType;
}
/**
* @param bookType 要设置的 bookType。
*/
public void setBookType(int bookType) {
this.bookType = bookType;
}
/**
* @return 返回 borrowDate。
*/
public Date getBorrowedDate() {
return borrowedDate;
}
/**
* @param borrowDate 要设置的 borrowDate。
*/
public void setBorrowedDate(Date borrowDate) {
this.borrowedDate = borrowDate;
}
/**
* @return 返回 isBorrowed。
*/
public boolean isBorrowed() {
return isBorrowed;
}
/**
* @param isBorrowed 要设置的 isBorrowed。
*/
public void setBorrowed(boolean isBorrowed) {
this.isBorrowed = isBorrowed;
}
/**
* @return 返回 publishDate。
*/
public Date getPublishDate() {
return publishDate;
}
/**
* @param publishDate 要设置的 publishDate。
*/
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public String toString()
{
StringBuffer myStringBuffer = new StringBuffer();
myStringBuffer.append("书名:"+this.getBookName()+" ##");
myStringBuffer.append("价格:"+this.getBookPrice()+" ## ");
myStringBuffer.append("类型:"+this.getBookType()+" /n");
myStringBuffer.append("出版日期:"+this.getPublishDate()+" /n");
myStringBuffer.append("借阅日期:"+this.getBorrowedDate()+" /n");
return myStringBuffer.toString();
}
}
class Borrower
{
private String name = "user";
private int age = 1;
private int borrowerId;
private ArrayList borrowedBook = new ArrayList();
public Borrower()
{}
public Borrower(String name,int age,int borrowId)
{
this.setName(name);
this.setAge(age);
this.setBorrowerId(borrowId);
}
/**
* @return 返回 age。
*/
public int getAge() {
return age;
}
/**
* @param age 要设置的 age。
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return 返回 borrowBook。
*/
public Collection getBorrowedBook() {
return borrowedBook;
}
/**
* @return 返回 borrowerId。
*/
public int getBorrowerId() {
return borrowerId;
}
/**
* @param borrowerId 要设置的 borrowerId。
*/
public void setBorrowerId(int borrowerId) {
this.borrowerId = borrowerId;
}
/**
* @return 返回 name。
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name。
*/
public void setName(String name) {
this.name = name;
}
/*
* 输出用户基本信息和借阅信息
* (非 Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuffer myStringBuffer = new StringBuffer();
myStringBuffer.append("用户名:"+ name +" ### ");
myStringBuffer.append("年龄:"+ age +" ### ");
myStringBuffer.append("用户编号:"+ borrowerId +" ### /n");
myStringBuffer.append("借阅书籍如下: /n");
Iterator myIterator = this.getBorrowedBook().iterator();
while(myIterator.hasNext())
{
Book myBook = (Book)myIterator.next();
myStringBuffer.append("书名:"+myBook.getBookName()+" ");
myStringBuffer.append("价格:"+myBook.getBookPrice()+" /n");
myStringBuffer.append("出版日期:"+myBook.getPublishDate()+" /n");
myStringBuffer.append("借阅日期:"+myBook.getBorrowedDate()+" /n");
}
return myStringBuffer.toString();
}
}
class BookManagerThread implements Runnable
{
private boolean isOut = false;
private int mainFrame = 0;
private int subFrame = 0;
private BufferedReader keyboardInput;
private LibraryManager myLibraryManager;
private SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public BookManagerThread(LibraryManager myLibraryManager)
{
this.myLibraryManager = myLibraryManager;
}
/*
* 图书管理系统的运行进程
* (非 Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
while(!isOut)
{
logicProcess();
}
}
private void displayMain()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:用户管理 <输入U+回车> ");
System.out.println("2:图书管理 <输入B+回车> ");
System.out.println("3:借阅管理 <输入L+回车> ");
System.out.println("Exit:退出 <输入Exit+回车> ");
}
private void dispalyUserManager()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:增加用户 <输入A+回车> ");
System.out.println("2:修改用户 <输入M+回车> ");
System.out.println("3:删除用户 <输入D+回车> ");
System.out.println("Up:返回上一级 <输入up+回车> ");
}
private void dispalyBookManager()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:增加图书 <输入A+回车> ");
System.out.println("2:修改图书 <输入M+回车> ");
System.out.println("3:删除图书 <输入D+回车> ");
System.out.println("Up:返回上一级 <输入up+回车> ");
}
private void dispalyBorrowManager()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:借阅图书 <输入B+回车> ");
System.out.println("2:归还图书 <输入L+回车> ");
System.out.println("3:浏览用户借阅信息 <输入V+回车> ");
System.out.println("Up:返回上一级 <输入up+回车> ");
}
private void dispalyAllUsers()
{
Iterator myIterator = this.getMyLibraryManager().getUserList().iterator();
int i = 0;
while(myIterator.hasNext())
{
System.out.println("该用户序列号是:"+ i);
System.out.println(((Borrower)myIterator.next()).toString());
i++;
}
}
private void displayAllBooks()
{
Iterator myIterator = this.getMyLibraryManager().getBookList().iterator();
int i = 0;
while(myIterator.hasNext())
{
System.out.println("该书籍序列号是:"+ i);
System.out.println(((Book)myIterator.next()).toString());
i++;
}
}
public void logicProcess()
{
String operateCode = "";
/*
* 以下是菜单选择
*/
if(mainFrame == 0)
{
displayMain();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("U"))
mainFrame = 1;
if(operateCode.equalsIgnoreCase("B"))
mainFrame = 2;
if(operateCode.equalsIgnoreCase("L"))
mainFrame = 3;
if(operateCode.equalsIgnoreCase("Exit"))
isOut = true;
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
if((mainFrame == 1) && (subFrame == 0))
{
dispalyUserManager();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("A"))
subFrame = 1;
if(operateCode.equalsIgnoreCase("M"))
subFrame = 2;
if(operateCode.equalsIgnoreCase("D"))
subFrame = 3;
if(operateCode.equalsIgnoreCase("Up"))
{
mainFrame = 0;
subFrame = 0;
}
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 0))
{
dispalyBookManager();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("A"))
subFrame = 1;
if(operateCode.equalsIgnoreCase("M"))
subFrame = 2;
if(operateCode.equalsIgnoreCase("D"))
subFrame = 3;
if(operateCode.equalsIgnoreCase("Up"))
{
mainFrame = 0;
subFrame = 0;
}
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 0))
{
dispalyBorrowManager();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("B"))
subFrame = 1;
if(operateCode.equalsIgnoreCase("L"))
subFrame = 2;
if(operateCode.equalsIgnoreCase("V"))
subFrame = 3;
if(operateCode.equalsIgnoreCase("Up"))
{
mainFrame = 0;
subFrame = 0;
}
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
/*
* 以下是个小块功能实现
*/
if((mainFrame == 1) && (subFrame == 1))
{
try
{
Borrower newBorrower = new Borrower();
System.out.println("请输入用户名:(up返回上一级)");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String nameTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(nameTemp))
{
subFrame = 0;
}else
{
System.out.println("请输入年龄:");
String ageTemp = keyboardInput.readLine();
newBorrower.setAge(Integer.parseInt(ageTemp));
newBorrower.setName(nameTemp);
if(this.getMyLibraryManager().getUserList() == null)
{
newBorrower.setBorrowerId(1);
}else
{
newBorrower.setBorrowerId(this.getMyLibraryManager().getUserList().size()+1);
}
getMyLibraryManager().getUserList().add(newBorrower);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 1) && (subFrame == 2))
{
try
{
dispalyAllUsers();
System.out.println("请输入序列号来修改:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
Borrower modifiedBorrower = (Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(idTemp));
System.out.println("请输入用户名:");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String nameTemp = keyboardInput.readLine();
System.out.println("请输入年龄:");
String ageTemp = keyboardInput.readLine();
modifiedBorrower.setAge(Integer.parseInt(ageTemp));
modifiedBorrower.setName(nameTemp);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 1) && (subFrame == 3))
{
try
{
dispalyAllUsers();
System.out.println("请输入序列号来删除:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
if(((Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(idTemp))).getBorrowedBook().size() == 0)
this.getMyLibraryManager().getUserList().remove(Integer.parseInt(idTemp));
else
{
System.out.println("该用户仍有借书,不能删除!!");
}
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 1))
{
try
{
Book newBook = new Book();
System.out.println("请输入书名:(up返回上一级)");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String bookNameTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(bookNameTemp))
{
subFrame = 0;
}else
{
System.out.println("请输入价格:");
String bookPriceTemp = keyboardInput.readLine();
System.out.println("请输入类型:1-计算机 2-数学 3-自然科学");
String bookTypeTemp = keyboardInput.readLine();
System.out.println("请输入出版日期:(格式yyyy-MM-dd HH:mm:ss)");
String publishDateTemp = keyboardInput.readLine();
newBook.setBookName(bookNameTemp);
newBook.setBookPrice(Float.parseFloat(bookPriceTemp));
newBook.setBookType(Integer.parseInt(bookTypeTemp));
newBook.setPublishDate(mySimpleDateFormat.parse(publishDateTemp));
this.getMyLibraryManager().getBookList().add(newBook);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 2))
{
try
{
displayAllBooks();
System.out.println("请输入序列号来修改:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
Book modifiedBook = (Book)this.getMyLibraryManager().getBookList().get(Integer.parseInt(idTemp));
System.out.println("请输入书名:");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String bookNameTemp = keyboardInput.readLine();
System.out.println("请输入价格:");
String bookPriceTemp = keyboardInput.readLine();
System.out.println("请输入类型:1-计算机 2-数学 3-自然科学");
String bookTypeTemp = keyboardInput.readLine();
System.out.println("请输入出版日期:(格式yyyy-MM-dd HH:mm:ss)");
String publishDateTemp = keyboardInput.readLine();
modifiedBook.setBookName(bookNameTemp);
modifiedBook.setBookPrice(Float.parseFloat(bookPriceTemp));
modifiedBook.setBookType(Integer.parseInt(bookTypeTemp));
modifiedBook.setPublishDate(mySimpleDateFormat.parse(publishDateTemp));
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 3))
{
try
{
displayAllBooks();
System.out.println("请输入序列号来删除:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
this.getMyLibraryManager().getBookList().remove(Integer.parseInt(idTemp));
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 1))
{
try
{
displayAllBooks();
System.out.println("请输入序列号来选择要借阅的书籍:(up返回上一级)");
String idTemp = keyboardInput.readLine();
dispalyAllUsers();
System.out.println("请输入序列号来选择借阅者:(up返回上一级)");
String borrowerIdTemp = keyboardInput.readLine();
if(("up".equalsIgnoreCase(idTemp)) || ("up".equalsIgnoreCase(borrowerIdTemp)))
{
subFrame = 0;
}else
{
Book borrowedBook = (Book)this.getMyLibraryManager().getBookList().get(Integer.parseInt(idTemp));
Borrower thisBorrower = (Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(borrowerIdTemp));
borrowedBook.setBorrowed(true);
borrowedBook.setBorrowedDate(new Date());
thisBorrower.getBorrowedBook().add(borrowedBook);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 2))
{
try
{
this.displayAllBooks();
System.out.println("请输入序列号来选择要归还的书籍:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
Book returnBook = (Book)this.getMyLibraryManager().getBookList().get(Integer.parseInt(idTemp));
if(returnBook.isBorrowed())
{
Iterator myIterator = this.getMyLibraryManager().getUserList().iterator();
Borrower borrowerTemp = new Borrower();
while(myIterator.hasNext())
{
borrowerTemp = (Borrower)myIterator.next();
if(borrowerTemp.getBorrowedBook().contains(returnBook))
{
borrowerTemp.getBorrowedBook().remove(returnBook);
returnBook.setBorrowed(false);
break;
}
}
}else
{
System.out.println("该书未被借阅!!!");
}
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 3))
{
try
{
Iterator myIterator = this.getMyLibraryManager().getUserList().iterator();
int i = 0;
while(myIterator.hasNext())
{
System.out.println("该用户的序列号是:"+ i);
System.out.println(((Borrower)myIterator.next()).getName());
i++;
}
System.out.println("请输入序列号来查看:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
System.out.println(((Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(idTemp))).toString());
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
}
/**
* @return 返回 myLibraryManager。
*/
public LibraryManager getMyLibraryManager() {
return myLibraryManager;
}
}
实现简单的用户管理,图书管理和借阅管理,字符界面,没有做GUI(偷懒了一点呵呵)
使用数组列表保存数据ArrayList, 退出后所有数据丢失,没有用文件系统和数据库,
主要是保留自己以前用C写的时候的样子,怀念一下
改进意见:
可以考虑使用XML来保存数据,我的blog里有一篇简单介绍Jdom的,数据库也没有问题
改进了以后请大家发一份copy给我,FaneAnn@yahoo.com.cn
程序代码如下:
/*
* @(#)BookManager.java
* Have classes:BookManager Book Borrower
* Copyright 2005 Fane. All rights reserved.
* @author Fane
* @version 1.0.0.0, 09/05/05
* @since JDK 1.4
*/
package person.fane.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
public class LibraryManager
{
private ArrayList bookList = new ArrayList();
private ArrayList userList = new ArrayList();
public final int maxBorrower = 100;
public final int maxBook = 100;
public static void main(String args[])
{
//System.out.println("欢迎来到图书管理系统!!");
LibraryManager newLibraryManager = new LibraryManager();
BookManagerThread myBookManagerThread = new BookManagerThread(newLibraryManager);
new Thread(myBookManagerThread).start();
}
/**
* @return 返回 bookList。
*/
public ArrayList getBookList() {
return bookList;
}
/**
* @return 返回 userList。
*/
public ArrayList getUserList() {
return userList;
}
/**
* @param userList 要设置的 userList。
*/
public void setUserList(ArrayList userList) {
this.userList = userList;
}
}
class Book
{
/*
* Book类包含的属性
*/
private String bookName;
private float bookPrice;
private int bookType;
private Date publishDate;
private boolean isBorrowed;
private Date borrowedDate;
/**
* @return 返回 bookName。
*/
public String getBookName() {
return bookName;
}
/**
* @param bookName 要设置的 bookName。
*/
public void setBookName(String bookName) {
this.bookName = bookName;
}
/**
* @return 返回 bookPrice。
*/
public float getBookPrice() {
return bookPrice;
}
/**
* @param bookPrice 要设置的 bookPrice。
*/
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
/**
* @return 返回 bookType。
*/
public int getBookType() {
return bookType;
}
/**
* @param bookType 要设置的 bookType。
*/
public void setBookType(int bookType) {
this.bookType = bookType;
}
/**
* @return 返回 borrowDate。
*/
public Date getBorrowedDate() {
return borrowedDate;
}
/**
* @param borrowDate 要设置的 borrowDate。
*/
public void setBorrowedDate(Date borrowDate) {
this.borrowedDate = borrowDate;
}
/**
* @return 返回 isBorrowed。
*/
public boolean isBorrowed() {
return isBorrowed;
}
/**
* @param isBorrowed 要设置的 isBorrowed。
*/
public void setBorrowed(boolean isBorrowed) {
this.isBorrowed = isBorrowed;
}
/**
* @return 返回 publishDate。
*/
public Date getPublishDate() {
return publishDate;
}
/**
* @param publishDate 要设置的 publishDate。
*/
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public String toString()
{
StringBuffer myStringBuffer = new StringBuffer();
myStringBuffer.append("书名:"+this.getBookName()+" ##");
myStringBuffer.append("价格:"+this.getBookPrice()+" ## ");
myStringBuffer.append("类型:"+this.getBookType()+" /n");
myStringBuffer.append("出版日期:"+this.getPublishDate()+" /n");
myStringBuffer.append("借阅日期:"+this.getBorrowedDate()+" /n");
return myStringBuffer.toString();
}
}
class Borrower
{
private String name = "user";
private int age = 1;
private int borrowerId;
private ArrayList borrowedBook = new ArrayList();
public Borrower()
{}
public Borrower(String name,int age,int borrowId)
{
this.setName(name);
this.setAge(age);
this.setBorrowerId(borrowId);
}
/**
* @return 返回 age。
*/
public int getAge() {
return age;
}
/**
* @param age 要设置的 age。
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return 返回 borrowBook。
*/
public Collection getBorrowedBook() {
return borrowedBook;
}
/**
* @return 返回 borrowerId。
*/
public int getBorrowerId() {
return borrowerId;
}
/**
* @param borrowerId 要设置的 borrowerId。
*/
public void setBorrowerId(int borrowerId) {
this.borrowerId = borrowerId;
}
/**
* @return 返回 name。
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name。
*/
public void setName(String name) {
this.name = name;
}
/*
* 输出用户基本信息和借阅信息
* (非 Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuffer myStringBuffer = new StringBuffer();
myStringBuffer.append("用户名:"+ name +" ### ");
myStringBuffer.append("年龄:"+ age +" ### ");
myStringBuffer.append("用户编号:"+ borrowerId +" ### /n");
myStringBuffer.append("借阅书籍如下: /n");
Iterator myIterator = this.getBorrowedBook().iterator();
while(myIterator.hasNext())
{
Book myBook = (Book)myIterator.next();
myStringBuffer.append("书名:"+myBook.getBookName()+" ");
myStringBuffer.append("价格:"+myBook.getBookPrice()+" /n");
myStringBuffer.append("出版日期:"+myBook.getPublishDate()+" /n");
myStringBuffer.append("借阅日期:"+myBook.getBorrowedDate()+" /n");
}
return myStringBuffer.toString();
}
}
class BookManagerThread implements Runnable
{
private boolean isOut = false;
private int mainFrame = 0;
private int subFrame = 0;
private BufferedReader keyboardInput;
private LibraryManager myLibraryManager;
private SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public BookManagerThread(LibraryManager myLibraryManager)
{
this.myLibraryManager = myLibraryManager;
}
/*
* 图书管理系统的运行进程
* (非 Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
while(!isOut)
{
logicProcess();
}
}
private void displayMain()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:用户管理 <输入U+回车> ");
System.out.println("2:图书管理 <输入B+回车> ");
System.out.println("3:借阅管理 <输入L+回车> ");
System.out.println("Exit:退出 <输入Exit+回车> ");
}
private void dispalyUserManager()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:增加用户 <输入A+回车> ");
System.out.println("2:修改用户 <输入M+回车> ");
System.out.println("3:删除用户 <输入D+回车> ");
System.out.println("Up:返回上一级 <输入up+回车> ");
}
private void dispalyBookManager()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:增加图书 <输入A+回车> ");
System.out.println("2:修改图书 <输入M+回车> ");
System.out.println("3:删除图书 <输入D+回车> ");
System.out.println("Up:返回上一级 <输入up+回车> ");
}
private void dispalyBorrowManager()
{
System.out.println(" 欢迎来到图书管理系统! ");
System.out.println("1:借阅图书 <输入B+回车> ");
System.out.println("2:归还图书 <输入L+回车> ");
System.out.println("3:浏览用户借阅信息 <输入V+回车> ");
System.out.println("Up:返回上一级 <输入up+回车> ");
}
private void dispalyAllUsers()
{
Iterator myIterator = this.getMyLibraryManager().getUserList().iterator();
int i = 0;
while(myIterator.hasNext())
{
System.out.println("该用户序列号是:"+ i);
System.out.println(((Borrower)myIterator.next()).toString());
i++;
}
}
private void displayAllBooks()
{
Iterator myIterator = this.getMyLibraryManager().getBookList().iterator();
int i = 0;
while(myIterator.hasNext())
{
System.out.println("该书籍序列号是:"+ i);
System.out.println(((Book)myIterator.next()).toString());
i++;
}
}
public void logicProcess()
{
String operateCode = "";
/*
* 以下是菜单选择
*/
if(mainFrame == 0)
{
displayMain();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("U"))
mainFrame = 1;
if(operateCode.equalsIgnoreCase("B"))
mainFrame = 2;
if(operateCode.equalsIgnoreCase("L"))
mainFrame = 3;
if(operateCode.equalsIgnoreCase("Exit"))
isOut = true;
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
if((mainFrame == 1) && (subFrame == 0))
{
dispalyUserManager();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("A"))
subFrame = 1;
if(operateCode.equalsIgnoreCase("M"))
subFrame = 2;
if(operateCode.equalsIgnoreCase("D"))
subFrame = 3;
if(operateCode.equalsIgnoreCase("Up"))
{
mainFrame = 0;
subFrame = 0;
}
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 0))
{
dispalyBookManager();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("A"))
subFrame = 1;
if(operateCode.equalsIgnoreCase("M"))
subFrame = 2;
if(operateCode.equalsIgnoreCase("D"))
subFrame = 3;
if(operateCode.equalsIgnoreCase("Up"))
{
mainFrame = 0;
subFrame = 0;
}
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 0))
{
dispalyBorrowManager();
try
{
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
operateCode = keyboardInput.readLine();
if(operateCode.equalsIgnoreCase("B"))
subFrame = 1;
if(operateCode.equalsIgnoreCase("L"))
subFrame = 2;
if(operateCode.equalsIgnoreCase("V"))
subFrame = 3;
if(operateCode.equalsIgnoreCase("Up"))
{
mainFrame = 0;
subFrame = 0;
}
}catch(Exception ex)
{
System.out.println("输入错误!!!");
}
}
/*
* 以下是个小块功能实现
*/
if((mainFrame == 1) && (subFrame == 1))
{
try
{
Borrower newBorrower = new Borrower();
System.out.println("请输入用户名:(up返回上一级)");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String nameTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(nameTemp))
{
subFrame = 0;
}else
{
System.out.println("请输入年龄:");
String ageTemp = keyboardInput.readLine();
newBorrower.setAge(Integer.parseInt(ageTemp));
newBorrower.setName(nameTemp);
if(this.getMyLibraryManager().getUserList() == null)
{
newBorrower.setBorrowerId(1);
}else
{
newBorrower.setBorrowerId(this.getMyLibraryManager().getUserList().size()+1);
}
getMyLibraryManager().getUserList().add(newBorrower);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 1) && (subFrame == 2))
{
try
{
dispalyAllUsers();
System.out.println("请输入序列号来修改:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
Borrower modifiedBorrower = (Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(idTemp));
System.out.println("请输入用户名:");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String nameTemp = keyboardInput.readLine();
System.out.println("请输入年龄:");
String ageTemp = keyboardInput.readLine();
modifiedBorrower.setAge(Integer.parseInt(ageTemp));
modifiedBorrower.setName(nameTemp);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 1) && (subFrame == 3))
{
try
{
dispalyAllUsers();
System.out.println("请输入序列号来删除:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
if(((Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(idTemp))).getBorrowedBook().size() == 0)
this.getMyLibraryManager().getUserList().remove(Integer.parseInt(idTemp));
else
{
System.out.println("该用户仍有借书,不能删除!!");
}
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 1))
{
try
{
Book newBook = new Book();
System.out.println("请输入书名:(up返回上一级)");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String bookNameTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(bookNameTemp))
{
subFrame = 0;
}else
{
System.out.println("请输入价格:");
String bookPriceTemp = keyboardInput.readLine();
System.out.println("请输入类型:1-计算机 2-数学 3-自然科学");
String bookTypeTemp = keyboardInput.readLine();
System.out.println("请输入出版日期:(格式yyyy-MM-dd HH:mm:ss)");
String publishDateTemp = keyboardInput.readLine();
newBook.setBookName(bookNameTemp);
newBook.setBookPrice(Float.parseFloat(bookPriceTemp));
newBook.setBookType(Integer.parseInt(bookTypeTemp));
newBook.setPublishDate(mySimpleDateFormat.parse(publishDateTemp));
this.getMyLibraryManager().getBookList().add(newBook);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 2))
{
try
{
displayAllBooks();
System.out.println("请输入序列号来修改:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
Book modifiedBook = (Book)this.getMyLibraryManager().getBookList().get(Integer.parseInt(idTemp));
System.out.println("请输入书名:");
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String bookNameTemp = keyboardInput.readLine();
System.out.println("请输入价格:");
String bookPriceTemp = keyboardInput.readLine();
System.out.println("请输入类型:1-计算机 2-数学 3-自然科学");
String bookTypeTemp = keyboardInput.readLine();
System.out.println("请输入出版日期:(格式yyyy-MM-dd HH:mm:ss)");
String publishDateTemp = keyboardInput.readLine();
modifiedBook.setBookName(bookNameTemp);
modifiedBook.setBookPrice(Float.parseFloat(bookPriceTemp));
modifiedBook.setBookType(Integer.parseInt(bookTypeTemp));
modifiedBook.setPublishDate(mySimpleDateFormat.parse(publishDateTemp));
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 2) && (subFrame == 3))
{
try
{
displayAllBooks();
System.out.println("请输入序列号来删除:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
this.getMyLibraryManager().getBookList().remove(Integer.parseInt(idTemp));
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 1))
{
try
{
displayAllBooks();
System.out.println("请输入序列号来选择要借阅的书籍:(up返回上一级)");
String idTemp = keyboardInput.readLine();
dispalyAllUsers();
System.out.println("请输入序列号来选择借阅者:(up返回上一级)");
String borrowerIdTemp = keyboardInput.readLine();
if(("up".equalsIgnoreCase(idTemp)) || ("up".equalsIgnoreCase(borrowerIdTemp)))
{
subFrame = 0;
}else
{
Book borrowedBook = (Book)this.getMyLibraryManager().getBookList().get(Integer.parseInt(idTemp));
Borrower thisBorrower = (Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(borrowerIdTemp));
borrowedBook.setBorrowed(true);
borrowedBook.setBorrowedDate(new Date());
thisBorrower.getBorrowedBook().add(borrowedBook);
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 2))
{
try
{
this.displayAllBooks();
System.out.println("请输入序列号来选择要归还的书籍:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
Book returnBook = (Book)this.getMyLibraryManager().getBookList().get(Integer.parseInt(idTemp));
if(returnBook.isBorrowed())
{
Iterator myIterator = this.getMyLibraryManager().getUserList().iterator();
Borrower borrowerTemp = new Borrower();
while(myIterator.hasNext())
{
borrowerTemp = (Borrower)myIterator.next();
if(borrowerTemp.getBorrowedBook().contains(returnBook))
{
borrowerTemp.getBorrowedBook().remove(returnBook);
returnBook.setBorrowed(false);
break;
}
}
}else
{
System.out.println("该书未被借阅!!!");
}
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
if((mainFrame == 3) && (subFrame == 3))
{
try
{
Iterator myIterator = this.getMyLibraryManager().getUserList().iterator();
int i = 0;
while(myIterator.hasNext())
{
System.out.println("该用户的序列号是:"+ i);
System.out.println(((Borrower)myIterator.next()).getName());
i++;
}
System.out.println("请输入序列号来查看:(up返回上一级)");
String idTemp = keyboardInput.readLine();
if("up".equalsIgnoreCase(idTemp))
{
subFrame = 0;
}else
{
System.out.println(((Borrower)this.getMyLibraryManager().getUserList().get(Integer.parseInt(idTemp))).toString());
subFrame = 0;
}
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("输入错误!!!");
}
}
}
/**
* @return 返回 myLibraryManager。
*/
public LibraryManager getMyLibraryManager() {
return myLibraryManager;
}
}