文章目录
前言
【项目说明】
长期以来,人们使用传统的人工方式管理图书馆的日常业务,其操作流程比较烦琐。所以,我们需求设计一个图书管理系统来方便学生的借书和图书馆管理书籍。
【项目内容】
项目功能结构图:
一、工程文件
文件目录:二、Book.java
图书的基本信息
代码如下:
private static String ISBN;//图书编号ISBN
private String bookname;//图书名称
private String author;//作者
private String publish;//出版社
private String pubilishdate;//出版日期
private String printtime;//印刷次数
private String unitprice;//单价
public Book() {
}
public Book(String ISBN, String bookname, String author, String publish, String pubilishdate, String printtime, String unitprice) {
this.ISBN = ISBN;
this.bookname = bookname;
this.author = author;
this.publish = publish;
this.pubilishdate = pubilishdate;
this.printtime = printtime;
this.unitprice = unitprice;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public String getPubilishdate() {
return pubilishdate;
}
public void setPubilishdate(String pubilishdate) {
this.pubilishdate = pubilishdate;
}
public String getPrinttime() {
return printtime;
}
public void setPrinttime(String printtime) {
this.printtime = printtime;
}
public String getUnitprice() {
return unitprice;
}
public void setUnitprice(String unitprice) {
this.unitprice = unitprice;
}
@Override
public String toString() {
return ISBN +
"," + bookname +
"," + author +
"," + publish +
"," + pubilishdate +
"," + printtime +
"," + unitprice
;
}
图书的增删改查方法
代码如下:
//删除图书信息
public static void deleteBook() {
try {
FileReader f2 = new FileReader("book.txt");
int x=0;
char[] chars = new char[50];
if (( x= f2.read(chars))==-1){
System.out.println("图书信息为空请先添加!!!");
return;
}
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<String> arrayBook = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的图书编号:");
String s = scanner.next();
try {
BufferedReader b1 = new BufferedReader(new FileReader("book.txt"));
String x;
while ((x=b1.readLine())!=null){
arrayBook.add(x);
}
// System.out.println(arrayBook);
b1.close();
} catch (IOException e) {
e.printStackTrace();
}
String[] chars = new String[9];
boolean flag = false;
for (String s1:arrayBook){
chars=s1.split(",");
if (chars[0].equals(s)){
flag=true;
}
}
if(!flag) {
System.out.println("输入的图书编号不存在!!!");
return;
}
ArrayList<String> borrowBook = new ArrayList<>();
try {
BufferedReader b2 = new BufferedReader(new FileReader("borrowbook.txt"));
String x;
while ((x=b2.readLine())!=null){
borrowBook.add(x);
}
b2.close();
} catch (IOException e) {
e.printStackTrace();
}
String[] chars1 = new String[5];
for (String a:borrowBook){
chars1=a.split(",");
if (chars1[1].equals(s)) {
System.out.println("该图书已被读者借去,还未归还,不能删除!!!");
return;
}
}
String[] b = new String[9];
Iterator iterator = arrayBook.iterator();
while (iterator.hasNext()){
String s1 = (String) iterator.next();
b = s1.split(",");
if (b[0].equals(s)) {
iterator.remove();
}
}
try {
FileWriter fileWriter = new FileWriter("book.txt", false);
StringBuffer stringBuffer = new StringBuffer();
for(String s2 : arrayBook){
stringBuffer.append(s2);
stringBuffer.append("\n");
}
fileWriter.write(stringBuffer.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
//提示信息
System.out.println("删除图书信息成功");
}
//修改图书信息
public static void updateReader() {
System.out.println("请删除这本书,再添加一本书!!!");
}
//查看图书信息
public static void fandAllBook() {
try {
FileReader f2 = new FileReader("book.txt");
int x=0;
char[] chars = new char[50];
if (( x= f2.read(chars))==-1){
System.out.println("图书信息为空请先添加!!!");
return;
}else {
do {
System.out.print(new String(chars, 0, x));
// f2.read(chars);
} while ((x = f2.read(chars)) != -1);
}
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//添加图书信息
public static void addBook() {
ArrayList<String> arrayBook = new ArrayList<>();
try {
BufferedReader b1 = new BufferedReader(new FileReader("book.txt"));
String x;
while ((x=b1.readLine())!=null){
arrayBook.add(x);
}
b1.close();
} catch (IOException e) {
e.printStackTrace();
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入图书编号:");
String ISBN=scanner.next();
String[] strings = new String[9];
for (String b:arrayBook){
strings=b.split(",");
if (strings[0].equals(ISBN)){
System.out.println("该图书已存在!!!");
return;
}
}
System.out.println("请输入图书名称:");
String bookname=scanner.next();
System.out.println("请输入图书作者:");
String author=scanner.next();
System.out.println("请输入图书出版社:");
String publish=scanner.next();
System.out.println("请输入图书出版日期:");
String pubilishdate=scanner.next();
System.out.println("请输入图书印刷次数:");
String printtime=scanner.next();
System.out.println("请输入图书单价:");
String unitprice=scanner.next();
//创建读者对象
Book book = new Book();
book.setISBN(ISBN);
book.setBookname(bookname);
book.setAuthor(author);
book.setPublish(publish);
book.setPubilishdate(pubilishdate);
book.setPrinttime(printtime);
book.setUnitprice(unitprice);
//提示信息
System.out.println("添加图书信息成功");
try {
FileWriter fileWriter = new FileWriter("book.txt",true);
fileWriter.write(book+"\n");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
三、BookType.java
图书类别的基本信息
代码如下:
public class BookType extends Book{
private String typeid;//图书类型编号
private String typename;//图书类型名称
public BookType() {
}
public BookType(String typeid, String typename) {
this.typeid = typeid;
this.typename = typename;
}
@Override
public String toString() {
return typeid +
"," + typename +
"," + getISBN()
;
}
public String getTypeid() {
return typeid;
}
public void setTypeid(String typeid) {
this.typeid = typeid;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
}
图书类别的增删改查方法
代码如下:
public static void selectBookType() {
ArrayList<String> bookType = new ArrayList<>();
try {
BufferedReader b1 = new BufferedReader(new FileReader("booktype.txt"));
String x;
while ((x=b1.readLine())!=null){
bookType.add(x);
}
b1.close();
} catch (IOException e) {
e.printStackTrace();
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要添加的图书类别编号:");
String n=scanner.next();
String[] chars = new String[3];
// for (String s:bookType){
// chars=s.split(",");
// if (chars[0].equals(n)){
// System.out.println("输入的图书类别编号已存在!!!");
// return;
// }
// }
System.out.println("请输入要添加的图书类别名称:");
String m=scanner.next();
boolean flag=true;
for (String s:bookType){
chars=s.split(",");
if (chars[0].equals(n)&&chars[1].equals(m)){
break;
}
if (chars[0].equals(n)){
System.out.println("输入的图书类别编号已存在!!!");
return;
}
if (chars[1].equals(m)){
flag=false;
break;
}
}
if (!flag){
System.out.println("输入的图书类别名称已存在!!!");
return;
}
System.out.println("请输入要添加的图书类别的图书编号:");
String b=scanner.next();
ArrayList<String> book = new ArrayList<>();
try {
BufferedReader b1 = new BufferedReader(new FileReader("book.txt"));
String x;
while ((x=b1.readLine())!=null){
book.add(x);
}
b1.close();
} catch (IOException e) {
e.printStackTrace();
}
boolean falg=false;
String[] chars1 = new String[9];
for (String s:book){
chars1=s.split(",");
if (chars1[0].equals(b)){
falg=true;
}
}
if (!falg) {
System.out.println("输入的图书编号不存在!!!");
return;
}
for (String s:bookType){
chars=s.split(",");
if (chars[2].equals(b)){
System.out.println("输入的图书已存在类别!!!");
return;
}
}
BookType type = new BookType();
type.setTypeid(n);
type.setTypename(m);
type.setISBN(b);
//提示信息
System.out.println("添加图书类别成功!!!");
try {
FileWriter f1 = new FileWriter("booktype.txt",true);
f1.write(type+"\n");
f1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//删除图书类别
public static void deleteBookType() {
ArrayList<String> bookType = new ArrayList<>();
try {
FileReader f = new FileReader("booktype.txt");
char[] chars1 = new char[50];
if ((f.read(chars1))==-1){
System.out.println("图书类型为空请先添加!!!");
return;
}else {
BufferedReader b1 = new BufferedReader(new FileReader("booktype.txt"));
String x;
while ((x=b1.readLine())!=null){
bookType.add(x);
}
b1.close();
}
f.close();
} catch (IOException e) {
e.printStackTrace();
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的图书类别编号:");
String n=scanner.next();
boolean flag=false;
String[] chars = new String[3];
Iterator<String> iterator = bookType.iterator();
while (iterator.hasNext()){
String s=iterator.next();
chars=s.split(",");
if (chars[0].equals(n)){
iterator.remove();
flag=true;
}
}
if (!flag){
System.out.println("输入的类别编号不存在!!!");
return;
}
System.out.println("恭喜你,输入的图书类别编号删除成功!!!");
try {
FileWriter f1 = new FileWriter("booktype.txt",false);
StringBuffer stringBuffer = new StringBuffer();
for (String b:bookType){
stringBuffer.append(b);
stringBuffer.append("\n");
}
f1.write(stringBuffer.toString());
f1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//修改图书类别
public static void updateBookType() {
System.out.println("请删除这个类型,再添加一个新类型!!!");
}
//查询图书类别
public static void insertBookType() {
try {
FileReader f1 = new FileReader("booktype.txt");
int i = 0;
char[] chars = new char[3];
if ((i = f1.read(chars)) == -1) {
System.out.println("图书类型为空请先添加!!!");
return;
}
do {
System.out.print(new String(chars, 0, i));
} while ((i = f1.read(chars)) != -1);
f1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
四、BorrowBook.java
借阅图书的基本信息
代码如下:
public class BorrowBook extends Book{
private String readerid;//读者编号
private String ISBN;//图书编号
private String borrowdate;//借书日期
private String returndate;//还书日期
private double fine;//违约罚款
public BorrowBook() {
}
public BorrowBook(String readerid, String ISBN, String borrowdate, String returndate, double fine) {
this.readerid = readerid;
this.ISBN = ISBN;
this.borrowdate = borrowdate;
this.returndate = returndate;
this.fine = fine;
}
@Override
public String toString()</