提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 基本结构
- 一、图书信息管理
- 1.book类
- 2.booklist类
- (1)add()
- (2)query()
- (3)sort()
- (4)change()
- (5)delete()
- (6)write()和read()
- 二、读者信息管理
- 1.Reader类
- 2.Readerlist类
- 三、图书借阅管理
- 1.Bookborrower类
- 2.Bookborrowlist类
- 四、菜单
- 五、BookMangementSystem类
- 总结
为图书管理人员编写一个图书管理系统,图书管理系统的设计主要是实现对图书的管理和相关操作,通过IO流实现对文件的读写操作,使得程序在再次打开时,能够记录到上次使用对数据的操作
基本结构
图书信息表——存储图书的基本信息,包括书号、书名、作者、出版社、出版日期、存馆数量、定价等。
读者信息表——存储读者的基本信息,包括学号、姓名、学院、专业班级等。
图书借阅表——存储读者借书的相关信息,包括学号、姓名、书号、书名、借阅日期、应还日期、归还日期等。
一、图书信息管理
1.book类
该类的功能是将每一本书都当作Book类的对象,该类中包含书籍的属性和一些set、get函数以及toString函数,能够对图书进行一些操作,实现了Serializable接口,是对象可以序列化。
注意:这里将书号设置为int类型,便于后面的排序,不过在输入也需要特别注意,所以在后边的一些函数中通过异常处理来确保输入的准确。
import java.io.Serializable;
@SuppressWarnings("all")
public class Book implements Serializable {
protected String name,author,publisher,publicationdate;
protected int amount,booknumber;
protected double price;
public Book()
{
this.name = null;
this.author = null;
this.publisher = null;
this.publicationdate = null;
this.amount = 0;
this.price = 0;
this.booknumber = 0;
}
public void setBooknumber(int booknumber) {
this.booknumber = booknumber;
}
public void setName(String name) {
this.name = name;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setPublicationdate(String publicationdate) {
this.publicationdate = publicationdate;
}
public void setPrice(double price) {
this.price = price;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getAuthor() {
return author;
}
public String getName() {
return name;
}
public String getPublisher() {
return publisher;
}
public int getBooknumber() {
return booknumber;
}
@Override
public String toString() {
return "书号="+booknumber+" 书名="+ name+" 作者="+author+" 出版社=" +publisher+" 出版日期="+publicationdate+" 存馆数量="+amount+" 定价="+price;
}
}
2.booklist类
该类的对象相当于一张图书信息表,所有图书的信息都存储在booklist属性中的Arraylist集合中,并通过函数对这个图书表进行操作
(1)add()
该函数负责对图书信息表的添加工作,依次输入要添加的图书的书号、书名、作者、出版社、出版日期、存馆数量和定价。当输入书号、存馆数量、定价这些非String类型的数据时,通过try catch语句,来确保输入的是数字。该函数还能够防止书号的重复,如果输入的书号已经存在,就会提醒用户该书已存在。
(2)query()
该函数负责对图书信息表的查询工作,通过输入图书的书名、作者、或出版社来进行查询,里面有一个迭代器来实现对图书表的遍历,如果找不到,就提示给用户,没有找到该图书的信息。
(3)sort()
该函数负责对图书信息表的排序工作,并在排序后将图书信息表的所有图书打印到屏幕上。这里让用户选择是通过书号来排序还是通过书名来排序。
当通过书号排序时,通过比较器实现升序排序。当通过书名排序时,比较书名的长度升序排序。
(4)change()
该函数负责对图书信息表的更改操作,通过输入图书的书号或书名来操作,由于书号是int类型,书名是String类型,要防止输入书号时不是数字,也要防止修改后的书号不是数字或者重复,这里先遍历书名,再遍历书号,并且在输入修改后的书号时,遍历已有的图书表防止重复。
(5)delete()
该函数负责对图书信息表的删除操作,通过输入要删除的图书的书号或书名,获取该书在图书表中的位置,调用Arraylist中的remove()函数进行删除
(6)write()和read()
这两个函数负责文件的处理,在读和写中,通过对象流来实现,不过这样会导致该文件打开时发生乱码,所以在写入时,在定义一个字符流将图书信息在文本文件中可以直接显示出来。为了防止出现异常,在读文件时,先判断目标文件的长度不为0时再读取。
import java.util.*;
import java.io.*;
import java.io.File;
@SuppressWarnings("all")
public class Booklist extends Book {
protected List bookArr;
public Booklist() {
this.bookArr = new ArrayList();
}
public List getBookArr() {
return bookArr;
}
public void add() {
boolean flag = true;
Book b1 = new Book();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要添加的书号:");
while (true) {
String a = scanner.next();
try {
Iterator iterator = bookArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
Book b = (Book) o;
if(b.booknumber == Integer.parseInt(a))//防止图书表中有重复书号的图书
{
flag = false;
break;
}
}
b1.setBooknumber(Integer.parseInt(a));
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
};
if(flag)
{
System.out.println("请输入要添加的书名:");
String b = scanner.next();
b1.setName(b);
System.out.println("请输入要添加的书的作者:");
String c = scanner.next();
b1.setAuthor(c);
System.out.println("请输入要添加的书的出版社:");
String d = scanner.next();
b1.setPublisher(d);
System.out.println("请输入要添加的书的出版日期:");
String e = scanner.next();
b1.setPublicationdate(e);
System.out.println("请输入要添加的书的存馆数量:");
while (true) {
String a1 = scanner.next();
try {
b1.setAmount(Integer.parseInt(a1));
break;
} catch (RuntimeException e1) {
System.out.println("您输入的不是数字,请重新输入。"); //确保用户输入的是数字
}
}
;
System.out.println("请输入要添加的书的定价:");
while (true) {
String a11 = scanner.next();
try {
b1.setPrice(Double.parseDouble(a11));
break;
} catch (RuntimeException e11) {
System.out.println("您输入的不是数字,请重新输入。"); //确保用户输入的是数字
}
}
;
bookArr.add(b1);//在booklist中添加这本图书
System.out.println("添加成功!");
}
else System.out.println("该书号已存在,无法再次添加");
}
public void query() {
System.out.println("请输入要查询的书籍的书名、作者、或出版社");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int j = 0, k = 0;
Iterator iterator = bookArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
Book b = (Book) o;
if (b.getName().equals(a) || b.getAuthor().equals(a) || b.getPublisher().equals(a)) {
System.out.println(o);
k++;
}
j++;
}
if ( k == 0) {
System.out.println("未能查询到该书的信息");
}
}
public void sort() {
System.out.println("请选择要排序的方式");
System.out.println("1.按书号排序,2.按书名排序");
int s;
Scanner scanner = new Scanner(System.in);
s = scanner.nextInt();
if (s == 1) {
Collections.sort(bookArr, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
return o1.booknumber - o2.booknumber; // 按书号升序排列
}
});
Iterator iterator = bookArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
System.out.println(o);
}
} else if (s == 2) {
Collections.sort(bookArr, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
if (o1.name.length() - o2.name.length() == 0)
return o1.booknumber - o2.booknumber;
else return o1.name.length() - o2.name.length(); // 按照书名的长度排序,如果等长,则按书号升序排列
}
});
Iterator iterator = bookArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
System.out.println(o);
}
}
if(bookArr.size() == 0)
{
System.out.println("空!");
}
}
public void change() {
boolean flag1 = true, flag2 = true;
System.out.println("请输入要修改的图书的书号或书名");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
Iterator iterator = bookArr.iterator();
int k = 0;
while (iterator.hasNext()) {
Object o = iterator.next();
Book b = (Book) o;
if (b.getName().equals(a)) {
int key;
k = 1;
Boolean flag = true;
flag2 = false;
while (flag) {
System.out.println("请输入要修改的信息:1.书名,2.书号,3.作者,4.出版社,5.出版日期,6.存馆数量,7.定价,0.退出修改");
key = scanner.nextInt();
switch (key) {
case 0:
flag = false;
break;
case 1:
System.out.println("请输入修改后的书名");
String s1 = scanner.next();
b.setName(s1);
System.out.println("修改成功");
break;
case 2:
System.out.println("请输入修改后的书号:");
while (true) {
String s2 = scanner.next();
try {
Iterator iterator1 = bookArr.iterator();
while (iterator1.hasNext()) {
Object o1 = iterator1.next();
Book b1 = (Book) o1;
if (b1.booknumber == Integer.parseInt(s2))//防止图书表中有重复书号的图书
{
flag1 = false;
System.out.println("该书号已存在,无法修改成输入的书号");
break;
}
}
if (flag1) {
b.setBooknumber(Integer.parseInt(s2));
System.out.println("修改成功");
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
break;
}
break;
case 3:
System.out.println("请输入修改后的作者");
String s3 = scanner.next();
b.setAuthor(s3);
System.out.println("修改成功");
break;
case 4:
System.out.println("请输入修改后的出版社");
String s4 = scanner.next();
b.setPublisher(s4);
System.out.println("修改成功");
break;
case 5:
System.out.println("请输入修改后的出版日期");
String s5 = scanner.next();
b.setPublicationdate(s5);
System.out.println("修改成功");
break;
case 6:
System.out.println("请输入修改后的存馆数量:");
while (true) {
String s6 = scanner.next();
try {
b.setAmount(Integer.parseInt(s6));
System.out.println("修改成功");
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
}
break;
case 7:
System.out.println("请输入修改后的定价:");
while (true) {
String s7 = scanner.next();
try {
b.setPrice(Double.parseDouble(s7));
System.out.println("修改成功");
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
}
break;
}
}
break;
}
}
if(flag2) {
Iterator iterator2 = bookArr.iterator();
while (iterator2.hasNext()) {
Object o = iterator2.next();
Book b = (Book)o;
while (true) {
try {
int a1 = Integer.parseInt(a);
if (b.getBooknumber() == a1) {
int key;
Boolean flag = true;
k = 1;
while (flag) {
System.out.println("请输入要修改的信息:1.书名,2.书号,3.作者,4.出版社,5.出版日期,6.存馆数量,7.定价,0.退出修改");
key = scanner.nextInt();
switch (key) {
case 0:
flag = false;
break;
case 1:
System.out.println("请输入要修改的书名");
String s1 = scanner.next();
b.setName(s1);
System.out.println("修改成功");
break;
case 2:
System.out.println("请输入修改后的书号:");
while (true) {
String s2 = scanner.next();
try {
Iterator iterator1 = bookArr.iterator();
while (iterator1.hasNext()) {
Object o1 = iterator1.next();
Book b1 = (Book) o1;
if (b1.booknumber == Integer.parseInt(s2))//防止图书表中有重复书号的图书
{
flag1 = false;
System.out.println("该书号已存在,无法修改成输入的书号");
break;
}
}
if (flag1) {
b.setBooknumber(Integer.parseInt(s2));
System.out.println("修改成功");
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
break;
}
break;
case 3:
System.out.println("请输入修改后的作者");
String s3 = scanner.next();
b.setAuthor(s3);
System.out.println("修改成功");
break;
case 4:
System.out.println("请输入修改后的出版社");
String s4 = scanner.next();
b.setPublisher(s4);
System.out.println("修改成功");
break;
case 5:
System.out.println("请输入修改后的出版日期");
String s5 = scanner.next();
b.setPublicationdate(s5);
System.out.println("修改成功");
break;
case 6:
System.out.println("请输入修改后的存馆数量:");
while (true) {
String s6 = scanner.next();
try {
b.setAmount(Integer.parseInt(s6));
System.out.println("修改成功");
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
}
break;
case 7:
System.out.println("请输入修改后的定价:");
while (true) {
String s7 = scanner.next();
try {
b.setPrice(Double.parseDouble(s7));
System.out.println("修改成功");
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
}
break;
}
break;
}
}
break;
} catch (RuntimeException e1) {
System.out.println("您输入的不是数字,请重新输入。"); //确保用户输入的是数字
}
}
}
}
if ( k == 0) {
System.out.println("未能查询到该书的信息,无法修改");//通过j和k确定是否查询到过图书
}
}
public void delete() {
System.out.println("请输入要删除的书籍的书号或书名");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int j = 0, k = 0,m = 0,p = 0,q = 0;
Iterator iterator = bookArr.iterator();
while (iterator.hasNext()) {
q=0;
Object o = iterator.next();
Book b = (Book) o;
if (b.getName().equals(a)) {
int n = bookArr.indexOf(b);
bookArr.remove(n);
k++;
q = 1;
System.out.println("删除成功");
break;
}
else if(q != 0) {
while(true)
{
try{
if(m != 0) {
a = scanner.next();
}
int a1 = Integer.parseInt(a);
if (b.getBooknumber() == a1)
{
int n = bookArr.indexOf(b);
bookArr.remove(n);
k++;
p = 1;
System.out.println("删除成功");
}
break;
}catch(RuntimeException e){
System.out.println("您输入的不是数字,且在图书表中未能查询到该书名,请重新输入");
m = 1;
}
}
if(p != 0)
{
break;
}
}
j++;
if (j == bookArr.size() && k == 0) {
System.out.println("未能查询到该书的信息,无法删除");
break;
}
}
}
public void write()throws IOException
{
String filePath = "D:\\bookdat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
Iterator iterator = bookArr.iterator();
while(iterator.hasNext())
{
Object o = iterator.next();
Book b = (Book) o;
oos.writeObject(b) ;
}
oos.close();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\booklist"));//将数据写入文本文件中,便于直接查看
Iterator iterator1 = bookArr.iterator();
while(iterator1.hasNext())
{
Object o = iterator1.next();
Book b = (Book) o;
bufferedWriter.write("书号="+b.booknumber+" 书名="+ b.name+" 作者="+b.author+" 出版社=" +b.publisher+" 出版日期="+b.publicationdate+" 存馆数量="+b.amount+" 定价="+b.price);
bufferedWriter.newLine();
}
bufferedWriter.close();
}
public void read()throws Exception
{
String filePath = "D:\\bookdat";
String s;
File file = new File("D:\\bookdat");
if(file.length() != 0) {//当文件内有内容时才读取,防止出现异常
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
Object o = null;
while(true){
try{
while (true) {
o = ois.readObject();
Book b = (Book) o;
if(o == null)
break;
bookArr.add(b);
}
}catch(EOFException e){
break;
}
}
}
}
}
二、读者信息管理
读者信息表与图书信息表大同小异,里面的函数操作与booklist中基本一样,就不再赘述了。
1.Reader类
import java.io.Serializable;
@SuppressWarnings("all")
public class Reader implements Serializable {
protected String name,institute,professionalclass;
protected int studentnumber;
public Reader()
{
this.name = null;
this.institute = null;
this.professionalclass = null;
this.studentnumber = 0;
}
public void setName(String name) {
this.name = name;
}
public void setStudentnumber(int studentnumber) {
this.studentnumber = studentnumber;
}
public void setInstitute(String institute) {
this.institute = institute;
}
public void setProfessionalclass(String professionalclass) {
this.professionalclass = professionalclass;
}
public String getName() {
return name;
}
public int getReadernumber() {
return studentnumber;
}
public String getProfessionalclass() {
return professionalclass;
}
@Override
public String toString() {
return "学号=" + studentnumber + " 姓名=" +name + " 学院=" + institute + " 专业班级=" + professionalclass;
}
}
2.Readerlist类
import java.util.*;
import java.io.*;
import java.io.File;
@SuppressWarnings("all")
public class Readerlist extends Reader{
protected List readerArr;
public Readerlist() {
this.readerArr = new ArrayList();
}
public List getReaderArr() {
return readerArr;
}
public void add() {
boolean flag = true;
Reader r1 = new Reader();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要添加的学号:");
while (true) {
String a = scanner.next();
try {
Iterator iterator = readerArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
Reader b = (Reader) o;
if(b.studentnumber == Integer.parseInt(a))//防止读者表中有重复学号的读者
{
flag = false;
break;
}
}
r1.setStudentnumber(Integer.parseInt(a));
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
};
if(flag) {
System.out.println("请输入要添加的姓名:");
String b = scanner.next();
r1.setName(b);
System.out.println("请输入要添加的学院:");
String c = scanner.next();
r1.setInstitute(c);
System.out.println("请输入要添加的专业班级:");
String d = scanner.next();
r1.setProfessionalclass(d);
readerArr.add(r1);//在Readerlist中添加这本图书
System.out.println("添加成功!");
}
else System.out.println("该学号已存在,无法再次添加");
}
public void query() {
System.out.println("请输入要查询的读者的学号、姓名或专业班级");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int k = 0, p = 1, m = 0;
Iterator iterator = readerArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
Reader r = (Reader) o;
if (r.getName().equals(a) || r.getProfessionalclass().equals(a)) {
System.out.println(o);
k++;
p = 0;
}
}
if (p != 0) {
Iterator iterator1 = readerArr.iterator();
while (iterator1.hasNext()) {
Object o = iterator1.next();
Reader r = (Reader) o;
while (true) {
try {
if (m != 0) {
a = scanner.next();
}
int a1 = Integer.parseInt(a);
if (r.getReadernumber() == a1) {
System.out.println(o);
k++;
p = 1;
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,且在读者表中未能查询到该姓名和专业班级,请重新输入");
m = 1;
}
}
break;
}
}
if (k == 0) {
System.out.println("未能查询到该读者的信息");
}
}
public void sort() {
System.out.println("请选择要排序的方式");
System.out.println("1.按学号排序,2.按姓名排序");
int s;
Scanner scanner = new Scanner(System.in);
s = scanner.nextInt();
if (s == 1) {
Collections.sort(readerArr, new Comparator<Reader>() {
@Override
public int compare(Reader o1, Reader o2) {
return o1.getReadernumber() - o2.getReadernumber(); // 按学号升序排列
}
});
Iterator iterator = readerArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
System.out.println(o);
}
} else if (s == 2) {
Collections.sort(readerArr, new Comparator<Reader>() {
@Override
public int compare(Reader o1, Reader o2) {
if (o1.name.length() - o2.name.length() == 0)
return o1.studentnumber - o2.studentnumber;
else return o1.name.length() - o2.name.length(); // 按照学生姓名的长度排序,如果等长,则按学号升序排列
}
});
Iterator iterator = readerArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
System.out.println(o);
}
}
if(readerArr.size() == 0)
{
System.out.println("空!");
}
}
public void change() {
boolean flag1 = true,flag2 = true;
System.out.println("请输入要修改的读者的学号或姓名");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
Iterator iterator = readerArr.iterator();
int k = 0;
while (iterator.hasNext()) {
Object o = iterator.next();
Reader r = (Reader) o;
if (r.getName().equals(a)) {
int key;
k = 1;
Boolean flag = true;
flag2 = false;
while (flag) {
System.out.println("请输入要修改的信息:1.姓名,2.学号,3.学院,4.专业班级,0.退出修改");
key = scanner.nextInt();
switch (key) {
case 0:
flag = false;
break;
case 1:
System.out.println("请输入修改后的姓名");
String s1 = scanner.next();
r.setName(s1);
break;
case 2:
System.out.println("请输入修改后的学号:");
while (true) {
String s2 = scanner.next();
try {
Iterator iterator1 = readerArr.iterator();
while (iterator1.hasNext()) {
Object o1 = iterator1.next();
Reader r1 = (Reader) o1;
if (r1.studentnumber == Integer.parseInt(s2))//防止图书表中有重复书号的图书
{
flag1 = false;
System.out.println("该学号已存在,无法修改成输入的学号");
break;
}
}
if (flag1) {
r.setStudentnumber(Integer.parseInt(s2));
System.out.println("修改成功");
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
}
break;
case 3:
System.out.println("请输入修改后的学院");
String s3 = scanner.next();
r.setInstitute(s3);
break;
case 4:
System.out.println("请输入修改后的专业班级");
String s4 = scanner.next();
r.setProfessionalclass(s4);
break;
}
break;
}
break;
}
}
if(flag2) {
Iterator iterator2 = readerArr.iterator();
while (iterator2.hasNext()) {
Object o = iterator2.next();
Reader r = (Reader) o;
while (true) {
try {
int a1 = Integer.parseInt(a);
if (r.getReadernumber() == a1) {
int key;
Boolean flag = true;
k = 1;
while (flag) {
System.out.println("请输入要修改的信息:1.姓名,2.学号,3.学院,4.专业班级,0.退出修改");
key = scanner.nextInt();
switch (key) {
case 0:
flag = false;
break;
case 1:
System.out.println("请输入修改后的姓名");
String s1 = scanner.next();
r.setName(s1);
break;
case 2:
System.out.println("请输入修改后的学号:");
while (true) {
String s2 = scanner.next();
try {
Iterator iterator1 = readerArr.iterator();
while (iterator1.hasNext()) {
Object o1 = iterator1.next();
Reader r1 = (Reader) o1;
if(r1.studentnumber == Integer.parseInt(s2))//防止图书表中有重复书号的图书
{
flag1 = false;
System.out.println("该学号已存在,无法修改成输入的学号");
break;
}
}
if(flag1)
{
r.setStudentnumber(Integer.parseInt(s2));
System.out.println("修改成功");
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
}
break;
case 3:
System.out.println("请输入修改后的学院");
String s3 = scanner.next();
r.setInstitute(s3);
break;
case 4:
System.out.println("请输入修改后的专业班级");
String s4 = scanner.next();
r.setProfessionalclass(s4);
break;
}
break;
}
}
break;
} catch (RuntimeException e1) {
System.out.println("您输入的不是数字,请重新输入。"); //确保用户输入的是数字
}
}
}
if (k == 0) {
System.out.println("未能查询到该书的信息,无法修改");//通过j和k确定是否查询到过图书
}
}
}
public void delete() {
System.out.println("请输入要删除的读者的书号或书名");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int j = 0, k = 0,m = 0,p = 0,q = 0;
Iterator iterator = readerArr.iterator();
while (iterator.hasNext()) {
q=0;
Object o = iterator.next();
Reader r = (Reader) o;
if (r.getName().equals(a)) {
int n = readerArr.indexOf(r);
readerArr.remove(n);
k++;
q = 1;
System.out.println("删除成功");
break;
}
else if(q != 0) {
while(true)
{
try{
if(m != 0) {
a = scanner.next();
}
int a1 = Integer.parseInt(a);
if (r.getReadernumber() == a1)
{
int n = readerArr.indexOf(r);
readerArr.remove(n);
k++;
p = 1;
System.out.println("删除成功");
}
break;
}catch(RuntimeException e){
System.out.println("您输入的不是数字,且在读者表中未能查询到该书名,请重新输入");
m = 1;
}
}
if(p != 0)
{
break;
}
}
j++;
if (j == readerArr.size() && k == 0) {
System.out.println("未能查询到该读者的信息,无法删除");
break;
}
}
}
public void write()throws IOException
{
String filePath = "D:\\readerdat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
Iterator iterator = readerArr.iterator();
while(iterator.hasNext())
{
Object o = iterator.next();
Reader r = (Reader) o;
oos.writeObject(r) ;
}
oos.close();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\readerlist"));//将数据写入文本文件中,便于直接查看
Iterator iterator1 = readerArr.iterator();
while(iterator1.hasNext())
{
Object o = iterator1.next();
Reader r = (Reader) o;
bufferedWriter.write("学号=" + r.studentnumber + " 姓名=" + r.name + " 学院=" + r.institute + " 专业班级=" + r.professionalclass);
bufferedWriter.newLine();
}
bufferedWriter.close();
}
public void read()throws Exception
{
String filePath = "D:\\readerdat";
String s;
File file = new File("D:\\readerdat");
if(file.length() != 0) {//当文件内有内容时才读取,防止出现异常
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
Object o = null;
while(true){
try{
while (true) {
o = ois.readObject();
Reader r = (Reader) o;
if(o == null)
break;
readerArr.add(r);
}
}catch(EOFException e){
break;
}
}
}
}
}
三、图书借阅管理
1.Bookborrower类
这个类与上述的book类和reader类一样
import java.io.Serializable;
public class Bookborrower implements Serializable {
int studentnumber,booknumber;
String studentname,bookname,borrowdate,shouldreturndate,returndate,institute;
public Bookborrower(){
this.studentnumber = 0;
this.booknumber = 0;
this.bookname = null;
this.studentname = null;
this.borrowdate = null;
this.shouldreturndate = null;
this.returndate = null;
}
public void setInstitute(String institute) {
this.institute = institute;
}
public void setStudentnumber(int studentnumber) {
this.studentnumber = studentnumber;
}
public void setBooknumber(int booknumber) {
this.booknumber = booknumber;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void setBorrowdate(String borrowdate) {
this.borrowdate = borrowdate;
}
public void setReturndate(String returndate) {
this.returndate = returndate;
}
public void setShouldreturndate(String shouldreturndate) {
this.shouldreturndate = shouldreturndate;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getBookname() {
return bookname;
}
public int getStudentnumber() {
return studentnumber;
}
public String getInstitute() {
return institute;
}
public String toString(){
return "书号=" + booknumber + " 书名=" + bookname + " 学号=" + studentnumber + " 学生姓名=" + studentname + " 借阅时间=" + borrowdate + " 应还时间=" + shouldreturndate + " 归还时间=" +returndate;
}
}
2.Bookborrowlist类
这个类本质和booklist和readerlist一样,不过在开始借阅时和归还图书时,定义LocalDateTime date = LocalDateTime.now();获得当前的时间,并通过DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);来将时间的格式定义为如:2008年12月12日。
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.io.*;
import java.io.File;
@SuppressWarnings("all")
public class Bookborrowlist extends Bookborrower{
protected List bookborrowArr;
public Bookborrowlist(){
this.bookborrowArr = new ArrayList();
}
public void bookborrow(List booklist,List readerlist){
boolean flag = false,flag1= false,flag2 = true;
int B = 0;
List bookArr = new ArrayList();
bookArr.addAll(booklist);
List readerArr = new ArrayList();
readerArr.addAll(readerlist);
Bookborrower b = new Bookborrower();
System.out.println("请输入您想要借阅的图书的书号");
Scanner scanner = new Scanner(System.in);
while (true) {
String a = scanner.next();
try {
b.setBooknumber(Integer.parseInt(a));
Iterator iterator = bookArr.iterator();
while(iterator.hasNext()){
Object o = iterator.next();
Book b1 = (Book) o;
if(b1.booknumber == b.booknumber) //开始借阅
{
if(b1.amount <= 0)
{
System.out.println("该书数量不足,无法借出");
flag2 = false;
break;
}
B = bookArr.indexOf(b1);
b.setBookname(b1.getName());
LocalDateTime date = LocalDateTime.now();//获得当前的时间
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String borrowdate = dateTimeFormatter.format(date);//格式化当前日期
b.setBorrowdate(borrowdate);
String shouldreturndate = dateTimeFormatter.format(date.plusMonths(1));//将应还日期定在一个月后
b.setShouldreturndate(shouldreturndate);
b.setReturndate("暂未还");
flag = true;
break;
}
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
};
if(flag == false && flag2)
{
System.out.println("未能找到该书信息,无法借阅");
}
if(flag)
{
System.out.println("请输入借阅人的学号");
while (true) {
String d = scanner.next();
try {
b.setStudentnumber(Integer.parseInt(d));
Iterator iterator1 = readerArr.iterator();
while(iterator1.hasNext()){
Object o1 = iterator1.next();
Reader r1 = (Reader) o1;
if(r1.studentnumber == b.studentnumber) //开始借阅
{
b.setStudentname(r1.getName());
Book b2 =(Book)bookArr.get(B);
b2.amount--;//借阅成功后,该书的存馆数量减1
b.setInstitute(r1.institute);
flag1 = true;
break;
}
}
break;
} catch (RuntimeException e1) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
};
if(flag1 == false && flag2)
{
System.out.println("未能在读者信息中找到该同学");
}
}
if(flag && flag1)
{
System.out.println("借阅成功");
bookborrowArr.add(b);
}
}
public void query() {
System.out.println("请输入要查询的图书借阅的学号、书名、学院");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int j = 0, k = 0, p = 0, m = 0;
Iterator iterator = bookborrowArr.iterator();
while (iterator.hasNext()) {
p = 1;
Object o = iterator.next();
Bookborrower b = (Bookborrower) o;
if (b.getBookname().equals(a) || b.getInstitute().equals(a)){
System.out.println(o);
k++;
p = 0;
} else if (p != 0){
while(true)
{
try{
int a1 = Integer.parseInt(a);
if (b.getStudentnumber() == a1)
{
System.out.println(o);
k++;
p = 1;
}
break;
}catch(RuntimeException e){
break;
}
}
}
j++;
}
if (j == bookborrowArr.size() && k == 0) {
System.out.println("未能查询到该条借阅信息");
}
}
public void bookreturn(List booklist){
List bookArr = new ArrayList();
bookArr.addAll(booklist);
System.out.println("请输入您想要归还的图书的书号");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int m = 0,B = 0;
Boolean flag1 = true,flag2 = true;
Iterator iterator = bookborrowArr.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
Bookborrower b = (Bookborrower) o;
while(true)
{
try{
if(m != 0) {
a = scanner.next();
}
int a1 = Integer.parseInt(a);
if (b.booknumber == a1)
{
Iterator iterator1 = bookArr.iterator();
while(iterator1.hasNext()){
Object o1 = iterator1.next();
Book b1 = (Book)o1;
if(b1.booknumber == a1){
B = bookArr.indexOf(b1);
break;
}
}
System.out.println("请输入借阅人的学号");
flag1 = false;
while (true) {
String c = scanner.next();
try {
int a2 = Integer.parseInt(c);
if(b.studentnumber == a2 && b.returndate.equals("暂未还")){
flag2 = false;
Book b2 =(Book)bookArr.get(B);
b2.amount++;//归还成功后,该书的存馆数量+1
LocalDateTime date = LocalDateTime.now();//获得当前的时间
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String returndate = dateTimeFormatter.format(date);//格式化当前日期
b.setReturndate(returndate);
}
break;
} catch (RuntimeException e) {
System.out.println("您输入的不是数字,请重新输入。");//确保用户输入的是整数
}
break;
};
}
break;
}catch(RuntimeException e){
System.out.println("您输入的不是数字,请重新输入");
m = 1;
}
}
}
if(flag1)
{
System.out.println("未能在借阅表找到该图书,无法归还");
}
else if(flag1 == false && flag2){
System.out.println("该图书不是该同学借阅的或者已归还,无法归还");
}
else if(flag1 == false && flag2 == false)
{
System.out.println("归还成功");
}
}
public void write()throws IOException
{
String filePath = "D:\\bookborrowdat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
Iterator iterator = bookborrowArr.iterator();
while(iterator.hasNext())
{
Object o = iterator.next();
Bookborrower b = (Bookborrower) o;
oos.writeObject(b) ;
}
oos.close();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\bookborrowlist"));//将数据写入文本文件中,便于直接查看
Iterator iterator1 = bookborrowArr.iterator();
while(iterator1.hasNext())
{
Object o = iterator1.next();
Bookborrower b = (Bookborrower) o;
bufferedWriter.write("书号=" + b.booknumber + " 书名=" + b.bookname + " 学号=" + b.studentnumber + " 学生姓名=" + b.studentname + " 借阅时间=" + b.borrowdate + " 应还时间=" + b.shouldreturndate + " 归还时间=" + b.returndate);
bufferedWriter.newLine();
}
bufferedWriter.close();
}
public void read()throws Exception
{
String filePath = "D:\\bookborrowdat";
String s;
File file = new File("D:\\bookborrowdat");
if(file.length() != 0) {//当文件内有内容时才读取,防止出现异常
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
Object o = null;
while(true){
try{
while (true) {
o = ois.readObject();
Bookborrower b = (Bookborrower) o;
if(o == null)
break;
bookborrowArr.add(b);
}
}catch(EOFException e){
break;
}
}
}
}
}
四、菜单
在菜单中把booklist、readerlist、bookborrowlist实例化,便于调用这些对象中方法
import java.util.*;
@SuppressWarnings("all")
public class Menu {
Booklist booklist = new Booklist();
Readerlist readerlist = new Readerlist();
Bookborrowlist bookborrowlist = new Bookborrowlist();
public void bookadd(){
booklist.add();//提供添加功能
}
public void bookquery()
{
booklist.query();//提供查询功能
}
public void booksort()
{
booklist.sort();//提供排序功能
}
public void bookchange()
{
booklist.change();
}
public void bookdelete()
{
booklist.delete();
}
public void readeradd(){
readerlist.add();
}
public void readerquery(){
readerlist.query();
}
public void readersort(){
readerlist.sort();
}
public void readerchange(){
readerlist.change();
}
public void readerdelete() {
readerlist.delete();
}
public void bookborrow(){
bookborrowlist.bookborrow(booklist.getBookArr(),readerlist.getReaderArr());
}
public void bookborrowquery()
{
bookborrowlist.query();
}
public void bookreturn(){
bookborrowlist.bookreturn(booklist.getBookArr());
}
}
五、BookMangementSystem类
,main函数就在该类中,确保在程序开始时读取文件,在程序退出时写入文件,通过循环和switch语句实现操作。
import javax.imageio.IIOException;
import java.util.Scanner;
import java.io.*;
@SuppressWarnings("all")
public class BookMangementSystem {
public static void main(String []args)throws Exception {
Menu menu = new Menu();
menu.booklist.read();
menu.bookborrowlist.read();
menu.readerlist.read();
int key;
System.out.println(" 欢迎使用图书管理系统");
while(true)
{
System.out.println(" ***请输入你要选择的操作***");
System.out.println(" ***系统操作:0.退出系统***");
System.out.println("***图书信息管理:1.图书信息添加,2.图书信息查询,3.图书信息排序,4.图书信息修改,5.图书信息删除***");
System.out.println("***读者信息管理:6.读者信息添加,7.读者信息查询,8.读者信息排序,9.读者信息修改,10.读者信息删除***");
System.out.println(" *** 图书借阅管理:11.图书借阅,12.图书借阅查询,13.图书归还***");
Scanner scanner = new Scanner(System.in);
key=scanner.nextInt();
switch(key){
case 0:
System.out.println("您已成功退出系统。");
menu.booklist.write();
menu.bookborrowlist.write();
menu.readerlist.write();
System.exit(0);
case 1:
menu.bookadd();
break;
case 2:
menu.bookquery();
break;
case 3:
menu.booksort();
break;
case 4:
menu.bookchange();
break;
case 5:
menu.bookdelete();
break;
case 6:
menu.readeradd();
break;
case 7:
menu.readerquery();
break;
case 8:
menu.readersort();
break;
case 9:
menu.readerchange();
break;
case 10:
menu.readerdelete();
break;
case 11:
menu.bookborrow();
break;
case 12:
menu.bookborrowquery();
break;
case 13:
menu.bookreturn();
break;
}
}
}
}
总结
其中的一个文件截图:
以上就是我的图书管理系统的作业,由于经验不足,水平有限,所以还有很多不足的地方,不过通过这次学习经历,让我对编程的理解浅进一步>_<。