图书馆管理系统
一.功能调查
查阅书籍
借书
还书
显示所有书籍
2.系统功能图及uml图
系统功能图
uml图
3.类的说明
Main类:主要用作于系统界面的展示和各类操作
Book类:用于储存书籍的各类信息和相关操作
LibraryDao接口:实现Dao模式
LibraryDaoImpI类:用于读取文件中的书籍信息,展示书籍,借书,还书,和查询书籍信息等操作
使用到List类,对于图书的存储较为方便
4.系统的包(package)的规划设计:
5.特点
使用了DAO模式,DAO接口可以使得可以在不同数据处理的时候处理更加方便,相关代码的更改等也变得更加轻松容易方便,且DAO使程序层次分明,逻辑结构清晰
6.系统演示
查找书籍
借书
还书
显示所有书籍及退出
7.代码展示
Main
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
LibraryDao book=new LibraryDaoImpI();
book.addBook();
while(true)
{
int choice;
System.out.println("*************************************************");
System.out.println("* *");
System.out.println("* *");
System.out.println("* 欢迎来到图书馆 *");
System.out.println("* *");
System.out.println("* *");
System.out.println("*************************************************");
System.out.println("请输入所要想要执行的功能编号:");
System.out.println("1.查找书籍 2.借书 3.还书 4.显示所有藏书 其他.退出菜单");
choice=sc.nextInt();
if(choice==1)
{
while(true)
{
int key;
System.out.println("1.查找编号 2.查找书名 3.查找作者相关书 4.退出查找");
System.out.println("请选择查找方式的编号:");
key = sc.nextInt();
if(key == 1)System.out.println("请输入查找的书的编号");
else if(key == 2)System.out.println("请输入需要查找的书名");
else if(key == 3)System.out.println("请输入需要查找的作家");
else if(key == 4)break;
else {
System.out.println("输入了错误的编号,请重新输入");
continue;
}
int x= book.Search(key);
if(x == 0)System.out.println("没有找到书籍");
}
}else if(choice==2)
{
String name;
while(true)
{
book.ShowBook();
System.out.println("请输入需要借的书名,或者输入0返回");
name=sc.next();
if(name.equals("0"))break;
if(book.Borrow(name)==0)System.out.println("没有找到该书籍");
}
}else if(choice==3)
{
String name;
while(true)
{
System.out.println("请输入需要归还的书籍的名字,或者输入0返回");
name=sc.next();
if(name.equals("0"))break;
if(book.Return(name)==0)System.out.println("没有找到该书籍");
}
}else if(choice==4)
{
book.ShowBook();
}else {
System.out.println("退出成功!");
break;
}
}
}
}
Book类
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Book {
private String id;//书本编号
private String bookName;//书本名称
private String price;//书本价格
private String writer;//作家
private boolean flag;//标记是否为可借阅状态,true为可借,false为不可借
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getOp() {
return price;
}
public void setOp(String op) {
this.price = price;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Book() {
}
public Book(String id,String bookName,String price,String writer,boolean flag)
{
this.id=id;
this.bookName=bookName;
this.price = price;
this.writer=writer;
this.flag=flag;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((bookName == null) ? 0 : bookName.hashCode());
result = prime * result + (flag ? 1231 : 1237);
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
result = prime * result + ((writer == null) ? 0 : writer.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (bookName == null) {
if (other.bookName != null)
return false;
} else if (!bookName.equals(other.bookName))
return false;
if (flag != other.flag)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
if (writer == null) {
if (other.writer != null)
return false;
} else if (!writer.equals(other.writer))
return false;
return true;
}
@Override
public String toString() {
return "Book [id=" + id + ", bookName=" + bookName + ", price=" + price + ", writer=" + writer + ", flag="
+ flag + "]";
}
}
LibraryDao接口
import java.io.IOException;
public interface LibraryDao {
public void addBook()throws IOException;//添加书籍
public void ShowBook();//展示书籍
public int Search(int key);//查找书籍
public int Borrow(String name);//借书
public int Return(String name);//还书
}
LibraryDaoImpI类
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class LibraryDaoImpI implements LibraryDao{
String f = "book.txt";
private ArrayList<Book> bookList = new ArrayList<>();
@Override
public void addBook()throws IOException {
try {
//BufferedReader input=new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8"));
BufferedReader input = new BufferedReader(new FileReader(f));
while (true) {
String str;
if ((str = input.readLine()) != null) {
String id;
String name;
String price;
String writer;
int seat1 = str.indexOf(" ");
id = str.substring(0,seat1);
int seat2 = str.indexOf(" ",seat1+1);
name = str.substring(seat1+1,seat2);
int seat3 = str.indexOf(" ",seat2+1);
price = str.substring(seat2+1,seat3);
writer = str.substring(seat3+1);
Book x = new Book(id,name,price,writer,true);
bookList.add(x);
} else {
break;
}
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void ShowBook() {
for(Book e:bookList)
{
System.out.println(e.toString());
}
}
@Override
public int Search(int key) {//查找
Scanner sc = new Scanner(System.in);
int x = 0;
if(key == 1) {
String id = sc.next();
for(Book e:bookList)
{
if(e.getId().equals(id)) {
System.out.println(e.toString());
x=1;
}
}
}else if(key == 2){
String name=sc.next();
for(Book e:bookList)
{
if(e.getBookName().equals(name)) {
System.out.println(e.toString());
x=1;
}
}
}else if(key == 3)
{
String writer=sc.next();
for(Book e:bookList)
{
if(e.getWriter().equals(writer)) {
System.out.println(e.toString());
x = 1;
}
}
}
return x;
}
@Override
public int Borrow(String name) {
int x = 0;
for(Book e:bookList)
{
if(e.getBookName().equals(name))
{
x=1;
if(e.isFlag()==true)
{
System.out.println(e.toString());
System.out.println("借阅成功!");
e.setFlag(false);
}else
{
System.out.println(e.toString());
System.out.println("不好意思,该书已被借走!");
}
}
}
return x;
}
@Override
public int Return(String name) {
int x = 0;
for(Book e:bookList)
{
if(e.getBookName().equals(name))
{
x=1;
if(e.isFlag()==true)
{
System.out.println("此书已在书架上,无需归还");
}else
{
e.setFlag(true);
System.out.println("已为您归还书籍");
}
}
}
return x;
}
}