写在前面:
因为笔者刚刚入门没多久,各种知识储备还不足,图书馆管理系统是借鉴了许多前辈的文章写出来的,没有实现登陆,管理员账户等功能,且日期类实现的略有不足,并且还有很多隐藏的bug,笔者暂时没有足够的精力修复,毕竟要期末了(),等以后有机会会再翻新一遍代码。
目前已知的bug:很多地方的输入,如果用户不按照规范输入,就会直接报错退出,没有进行相应的规范,主要是我在这方面还有所不足。下次一定改()
一、项目功能
为图书管理人员编写一个图书管理系统,图书管理系统的设计主要是实现对图书的管理和相关操作,包括3个表:
图书信息表——存储图书的基本信息,包括书号、书名、作者、出版社、出版日期、存馆数量、定价等。
读者信息表——存储读者的基本信息,包括学号、姓名、学院、专业班级等。
图书借阅表——存储读者借书的相关信息,包括学号、姓名、书号、书名、借阅日期、应还日期、归还日期等。
用菜单选择方式完成下列功能:
1.图书信息添加功能:包括书号、书名、作者、出版社、存馆数量、定价等。
2.图书信息查询:分别按书名,按作者名,按出版社等进行查询。
3.图书信息排序:按书号、书名等按升序进行排序。
4.图书信息的修改、删除:按书号或书名进行图书的修改和删除。
5.读者信息添加功能:包括学号、姓名、学院、专业班级等。
6.读者信息查询:分别按学号、姓名、专业班级等进行查询。
7.读者信息排序:按学号、学院等按升序进行排序。
8.读者信息的修改、删除:按学号+姓名进行读者信息的修改和删除。
9.图书借阅:输入学号+书号,如果该书图书信息表中的存馆数量大于0,则可以借出,借出相应数量后修改图书信息表中的存馆数量,在图书借阅表添加该同学的借阅。
10.图书归还:输入学号+书号,修改图书信息表中的存馆数量,在图书借阅表中记录该同学的归还时间。
11.图书借阅查询:分别按学号、书名、学院等进行查询。
二、结构
写完具体结构之后才发现,很多大佬的结构都更加精简,封装的更好,这是我需要改进的。
三、代码
1、定义的类
(1)图书类
package EntityClass;
public class bookInformation{
public static int count;
private String name;//书名
private String author;//作者
private String price;//价格
private int num;//书号
private String press;//出版社
private String date;//出版日期
private int quantity;//存馆数量
//构造方法,set方法,get方法
public bookInformation() {
}
public bookInformation(String name, String author, String price, int num, String press, String date, int quantity) {
this.name = name;
this.author = author;
this.price = price;
this.num = num;
this.press = press;
this.date = date;
this.quantity = quantity;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPrice() {
return price;
}
public int getNum() {
return num;
}
public String getPress() {
return press;
}
public String getDate() {
return date;
}
public int getQuantity() {
return quantity;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(String price) {
this.price = price;
}
public void setNum(int num) {
this.num = num;
}
public void setPress(String press) {
this.press = press;
}
public void setDate(String date) {
this.date = date;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
//重写输出类
public String toString() {
return "书名"+getName()+"/"+"作者"+getAuthor()+"价格"+getPrice()+"书号"+getNum()+"出版社"+getPress()+"出版日期"+getDate()+"存馆数量"+getQuantity();
}
}
(2)读者类
//读者类
package EntityClass;
public class borrowinformation {
//存储读者借书的相关信息,包括学号、姓名、书号、书名、借阅日期、应还日期、归还日期等。
//姓名
private String name;
//学号
private int num1;
//书号
private int num2;
//书名
private String bookname;
//借阅日期
private String borrowdate;
//应还日期
private String Shouldreturndate;
//归还日期
private String returndate;
public borrowinformation() {
}
public borrowinformation(String name, int num1, int num2, String bookname, String borrowdate, String shouldreturndate, String returndate) {
this.name = name;
this.num1 = num1;
this.num2 = num2;
this.bookname = bookname;
this.borrowdate = borrowdate;
Shouldreturndate = shouldreturndate;
this.returndate = returndate;
}
public String getName() {
return name;
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
public String getBookname() {
return bookname;
}
public String getBorrowdate() {
return borrowdate;
}
public String getShouldreturndate() {
return Shouldreturndate;
}
public String getReturndate() {
return returndate;
}
public void setName(String name) {
this.name = name;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void setBorrowdate(String borrowdate) {
this.borrowdate = borrowdate;
}
public void setShouldreturndate(String shouldreturndate) {
Shouldreturndate = shouldreturndate;
}
public void setReturndate(String returndate) {
this.returndate = returndate;
}
}
//借阅类
package EntityClass;
public class readerinformation {
private String name;//姓名
private int num;//学号
private String college;//学院
private String Classs;//专业班级
//构造方法,set方法,get方法
public readerinformation(String name, int num, String college, String aClass) {
this.name = name;
this.num = num;
this.college = college;
Classs = aClass;
}
public readerinformation() {
}
public String getName() {
return name;
}
public int getNum() {
return num;
}
public String getCollege() {
return college;
}
public String getClasss() {
return Classs;
}
public void setName(String name) {
this.name = name;
}
public void setNum(int num) {
this.num = num;
}
public void setCollege(String college) {
this.college = college;
}
public void setClass(String aClass) {
Classs = aClass;
}
}
方法
add方法
package functions;
import EntityClass.bookInformation;
import EntityClass.readerinformation;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
//在这里实现图书信息的添加,读者信息的添加
public class Add {
//图书信息添加
public static void AddBookInformation(ArrayList<bookInformation>arrayList) throws IOException,ClassNotFoundException{
ArrayList<bookInformation> array1=new ArrayList<bookInformation>();
File file =new File("myFile");
file.mkdirs();
FileWriter fw=new FileWriter("myFile\\book.txt",true);
Scanner sc=new Scanner(System.in);
System.out.println("请输入书名:");
//String name1=tools.readDate();
String name=sc.nextLine();
//fw.write(name);
System.out.println("请输入书号(仅能输入数字):");
String n = sc.nextLine();
int num=Integer.parseInt(n);
//fw.write(num);
System.out.println("请输入作者:");
//String autho=sc.nextLine();
String author=sc.nextLine();
//fw.write(author);
System.out.println("请输入出版社:");
String press=sc.nextLine();
//fw.write(press);
System.out.println("请输入出版时间:");
String date=sc.nextLine();
//fw.write(quantity);
System.out.println("请输入数量:");
int quantity=sc.nextInt();
//fw.write(date);
System.out.println("请输入价格:");
sc.nextLine();
String price=sc.nextLine();
//fw.write(price);
bookInformation s=new bookInformation();
s.setName(name);
s.setAuthor(author);
s.setDate(date);
s.setNum(num);
s.setPress(press);
s.setPrice(price);
s.setQuantity(quantity);
array1.add(s);
for(int i=0;i<array1.size();i++) {
bookInformation a=new bookInformation();
a=array1.get(i);
fw.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("添加图书成功!");
fw.close();
}
//读者信息添加
public static void AddReaderInformation(ArrayList<readerinformation>arrayList) throws IOException,ClassNotFoundException{
ArrayList<readerinformation> array1=new ArrayList<readerinformation>();
File file =new File("myFile");
file.mkdirs();
FileWriter fw=new FileWriter("myFile\\reader.txt",true);
Scanner sc=new Scanner(System.in);
System.out.println("请输入姓名:");
String name=sc.nextLine();
System.out.println("请输入学号(仅能输入数字):");
String n = sc.nextLine();
int num=Integer.parseInt(n);
System.out.println("请输入学院:");
String college=sc.nextLine();
System.out.println("请输入专业班级:");
String Classs=sc.nextLine();
readerinformation s=new readerinformation();
s.setName(name);
s.setNum(num);
s.setCollege(college);
s.setClass(Classs);
array1.add(s);
for(int i=0;i<array1.size();i++) {
readerinformation a=new readerinformation();
a=array1.get(i);
fw.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
}
System.out.println("添加读者信息成功!");
fw.close();
}
}
change方法
package functions;
import EntityClass.bookInformation;
import EntityClass.readerinformation;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Change {
//这里是根据xx修改书籍信息的总方法
public static void sentakuchange1() throws IOException{
System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用书名\n2、使用书号");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
change1();
break;
case 2:
change2();
break;
}
}
public static void change1() throws IOException{
//按照书籍名修改书籍
System.out.println("请输入要修改的书名:");
ArrayList<bookInformation> array1=Save.inbook();
//ArrayList<bookInformation> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
//array2.add(a);
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
System.out.println("请输入修改后的书名:");
String name=sc.nextLine();
System.out.println("请输入修改后的书号(仅能输入数字):");
String h = sc.nextLine();
int num=Integer.parseInt(h);
System.out.println("请输入修改后的作者:");
String author=sc.nextLine();
System.out.println("请输入修改后的出版社:");
String press=sc.nextLine();
System.out.println("请输入修改后的出版时间:");
String date=sc.nextLine();
System.out.println("请输入修改后的数量:");
int quantity=sc.nextInt();
System.out.println("请输入修改后的价格:");
String pric=sc.nextLine();
String price=sc.nextLine();
bookInformation s=new bookInformation();
s.setName(name);
s.setAuthor(author);
s.setDate(date);
s.setNum(num);
s.setPress(press);
s.setPrice(price);
s.setQuantity(quantity);
array1.set(n,s);
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
bookInformation a=new bookInformation();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
public static void change2() throws IOException{
//按照书号修改书籍
System.out.println("请输入要修改的书号(仅能输入数字):");
ArrayList<bookInformation> array1=Save.inbook();
//ArrayList<bookInformation> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
int line=sc.nextInt();
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getNum()==line){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
//array2.add(a);
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
System.out.println("请输入修改后的书名:");
String name=sc.nextLine();
System.out.println("请输入修改后的书号(仅能输入数字):");
int num=sc.nextInt();
System.out.println("请输入修改后的作者:");
String author=sc.nextLine();
System.out.println("请输入修改后的出版社:");
String press=sc.nextLine();
System.out.println("请输入修改后的出版时间:");
int quantity=sc.nextInt();
System.out.println("请输入修改后的数量:");
String date=sc.nextLine();
System.out.println("请输入修改后的价格:");
String price=sc.nextLine();
bookInformation s=new bookInformation();
s.setName(name);
s.setAuthor(author);
s.setDate(date);
s.setNum(num);
s.setPress(press);
s.setPrice(price);
s.setQuantity(quantity);
array1.set(n,s);
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
bookInformation a=new bookInformation();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
//这里是根据学号+姓名修改读者信息的方法
public static void change3() throws IOException{
//按照学号+姓名修改信息
ArrayList<readerinformation> array1=Save.inreader();
//ArrayList<bookInformation> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入要修改的学号(仅能输入数字):");
int nu=sc.nextInt();
System.out.println("请输入要修改的姓名:");
String l=sc.nextLine();
String line=sc.nextLine();
for(int i=0;i<array1.size();i++)
{
readerinformation a=new readerinformation();
a=array1.get(i);
if(a.getName().equals(line)&&a.getNum()==nu){
System.out.println("您的信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
//array2.add(a);
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
return;
}
System.out.println("请输入修改后的姓名:");
String name=sc.nextLine();
System.out.println("请输入修改后的学号(仅能输入数字):");
String h = sc.nextLine();
int num=Integer.parseInt(h);
System.out.println("请输入修改后的学院:");
String College=sc.nextLine();
System.out.println("请输入修改后的专业班级:");
String Classs=sc.nextLine();
readerinformation s=new readerinformation();
s.setName(name);
s.setNum(num);
s.setCollege(College);
s.setClass(Classs);
array1.set(n,s);
FileWriter fr=new FileWriter("myFile\\reader.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
readerinformation a=new readerinformation();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
}
System.out.println("修改读者信息成功!");
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
}
delete方法
package functions;
import EntityClass.bookInformation;
import EntityClass.readerinformation;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Delete {
//根据xx删除书籍信息
public static void sentakudelete1() throws IOException{
System.out.println("您要选择哪种方法进行删除?:"+"\n"+"1、使用书名\n2、使用书号");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
detelebook1();
break;
case 2:
detelebook2();
break;
}
}
public static void detelebook1() throws IOException{
//本方法用书名删除书籍
System.out.println("请输入书名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<bookInformation> array1=Save.inbook();
boolean bool=false;
int n=0;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
array1.remove(n);
System.out.println("删除成功!");
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
bookInformation a=new bookInformation();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
//下面是根据书号删除书籍
public static void detelebook2() throws IOException{
//本方法用书号删除书籍
System.out.println("请输入书号(仅能输入数字):");
Scanner sc=new Scanner(System.in);
int line=sc.nextInt();
ArrayList<bookInformation> array1=Save.inbook();
boolean bool=false;
int n=0;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getNum()==line){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
/*ArrayList<bookInformation> array2=new ArrayList<>();
for (int i = 0; i < array1.size() - 1; i++) {
if (i < n) {
array2.set(i, array1.get(i));
} else {
array2.set(i, array1.get(i + 1));
}
}*/
array1.remove(n);
System.out.println("删除成功!");
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
bookInformation a=new bookInformation();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
public static void detelebook3() throws IOException{
//按照学号+姓名删除信息
ArrayList<readerinformation> array1=Save.inreader();
//ArrayList<bookInformation> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入学号(仅能输入数字):");
int nu=sc.nextInt();
System.out.println("请输入姓名:");
String l=sc.nextLine();
String line=sc.nextLine();
for(int i=0;i<array1.size();i++)
{
readerinformation a=new readerinformation();
a=array1.get(i);
if(a.getName().equals(line)&&a.getNum()==nu){
System.out.println("您的信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
//array2.add(a);
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
return;
}
array1.remove(n);
System.out.println("删除成功!");
FileWriter fr=new FileWriter("myFile\\reader.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
readerinformation a=new readerinformation();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
}
System.out.println("修改读者信息成功!");
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
}
Find方法
package functions;
import EntityClass.bookInformation;
import EntityClass.borrowinformation;
import EntityClass.readerinformation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Find {
public static void findbook(ArrayList<bookInformation> arrayList) throws IOException {
System.out.println("1、按书名查询");
System.out.println("2、按作者名查询");
System.out.println("3、按出版社查询");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s) {
case 1:
findbook1();
break;
case 2:
findbook2();
break;
case 3:
findbook3();
break;
}
}
//利用Save里读取文件到集合的方法,下面开始写查询方法
public static void findbook1() throws IOException{
//本方法用书名查找书籍
System.out.println("请输入书名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<bookInformation> array1=Save.inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}
//接下来是按照作者名查询方法
public static void findbook2() throws IOException{
//本方法用书名查找书籍
System.out.println("请输入作者名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<bookInformation> array1=Save.inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getAuthor().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}
//接下来是按照出版社查询方法
public static void findbook3() throws IOException{
//本方法用书名查找书籍
System.out.println("请输入出版社:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<bookInformation> array1=Save.inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getPress().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}
public static void findreader(ArrayList<readerinformation> arrayList) throws IOException {
System.out.println("1、按学号查询(仅能输入数字)");
System.out.println("2、按姓名查询");
System.out.println("3、按专业班级查询");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s) {
case 1:
findreader1();
break;
case 2:
findreader2();
break;
case 3:
findreader3();
break;
}
}
//利用Save里读取文件到集合的方法,下面开始写查询方法
public static void findreader1() throws IOException{
//本方法用书名查找书籍
System.out.println("请输入学号(仅能输入数字):");
Scanner sc=new Scanner(System.in);
String lin=sc.nextLine();
int line=Integer.parseInt(lin);
ArrayList<readerinformation> array1=Save.inreader();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
readerinformation a=new readerinformation();
a=array1.get(i);
if(a.getNum()==line){
System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
}
}
public static void findreader2() throws IOException{
//本方法用书名查找书籍
System.out.println("请输入姓名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<readerinformation> array1=Save.inreader();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
readerinformation a=new readerinformation();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
}
}
public static void findreader3() throws IOException{
//本方法用书名查找书籍
System.out.println("请输入专业班级:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<readerinformation> array1=Save.inreader();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
readerinformation a=new readerinformation();
a=array1.get(i);
if(a.getClasss().equals(line)){
System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
}
}
public static void findborrow(ArrayList<borrowinformation> arrayList) throws IOException {
System.out.println("1、按学号查询");
System.out.println("2、按书名查询");
System.out.println("3、按书号查询");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s) {
case 1:
findborrow1();
break;
case 2:
findborrow2();
break;
case 3:
findborrow2();
break;
}
}
//利用Save里读取文件到集合的方法,下面开始写查询方法
public static void findborrow1() throws IOException{
//本方法用学号查找借阅信息
System.out.println("请输入学号:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<borrowinformation> array1=Save.inborrow();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
borrowinformation a=new borrowinformation();
a=array1.get(i);
if(a.getNum1()==Integer.parseInt(line)){
System.out.println("您要找的书籍是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的借阅信息不存在!");
}
}
public static void findborrow2() throws IOException{
//本方法用书名查找借阅信息
System.out.println("请输入书名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<borrowinformation> array1=Save.inborrow();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
borrowinformation a=new borrowinformation();
a=array1.get(i);
if(a.getBookname().equals(line)){
System.out.println("您要查询的借阅信息是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的借阅信息不存在!");
}
}
public static void findborrow3() throws IOException{
//本方法用书号查找借阅信息
System.out.println("请输入书号:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<borrowinformation> array1=Save.inborrow();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
borrowinformation a=new borrowinformation();
a=array1.get(i);
if(a.getNum2()==Integer.parseInt(line)){
System.out.println("您要查询的借阅信息是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的借阅信息不存在!");
}
}
}
借阅方法
package functions;
import EntityClass.bookInformation;
import EntityClass.borrowinformation;
import EntityClass.readerinformation;
import Tool.tools;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class Libraryborrow {
//图书借阅
public static void sentaku() throws IOException, ParseException {
ArrayList<borrowinformation> array=new ArrayList<>();
//System.out.println("请输入学号(仅能输入数字):\n");
Scanner sc=new Scanner(System.in);
//String nu=sc.nextLine();
//int num=Integer.parseInt(nu);
//System.out.println("请输入姓名");
//String name=sc.nextLine();
System.out.println("请输入您要进行的操作:\n");
System.out.println("1、图书借阅");
System.out.println("2、图书归还");
System.out.println("3、图书借阅查询");
System.out.println("4、返回上一级");
char c;
while(true) {
String str;
str = sc.next();
c=str.charAt(0);
if (c != '1' && c != '2' &&
c != '3') {
System.out.print("选择错误,请重新输入:");
} else break;
}
switch (c)
{
case '1':
borrow();
break;
case '2':
returnbook();
break;
case '3':
Find.findborrow(array);
break;
case '4':
return;
}
}
public static void borrow() throws IOException, ParseException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入学号(仅能输入数字):\n");
String nu=sc.nextLine();
int num=Integer.parseInt(nu);
System.out.println("请输入姓名");
String name=sc.nextLine();
//array3
ArrayList<readerinformation> array3=Save.inreader();
boolean booll=false;
for(int i=0;i<array3.size();i++)
{
readerinformation a=new readerinformation();
a=array3.get(i);
if(a.getName().equals(name)&&a.getNum()==num){
booll=true;
}
}
if(booll==false){
System.out.println("输入的学号或姓名有误!");
return;
}
System.out.println("请输入要借阅的图书");
String line=sc.nextLine();
//array1
ArrayList<bookInformation> array1=Save.inbook();
//array2
ArrayList<borrowinformation> array2=new ArrayList<borrowinformation>();
int n = 0;
boolean bool=false;
int quantity = 0;
int D = 0;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
n=i;
quantity=a.getQuantity();
D=a.getNum();
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
if(quantity>0)
{
System.out.println("该书剩余存量"+quantity+"本,可以借阅");
//Date date =new Date(0);
borrowinformation borrow =new borrowinformation();
//学号
borrow.setNum1(num);
//姓名
borrow.setName(name);
//书名
borrow.setBookname(line);
//书号
borrow.setNum2(D);
//借阅时间(现在的时间)
Date date = new Date(0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time1 = simpleDateFormat.format(date);
//Date类型转String类型
//Date date = simpleDateFormat.parse(time);
borrow.setBorrowdate(time1);
//应还时间,15天之内
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//String time2 = simpleDateFormat.format(tools.DateUtils.getReturnDate());
//String string = “2003-10-14 10:10:20”;
borrow.setShouldreturndate(tools.DateUtils.getReturnDate());
//归还时间
borrow.setReturndate("0000-00-00");
File file =new File("myFile");
file.mkdirs();
FileWriter fw=new FileWriter("myFile\\borrow.txt",true);
array2.add(borrow);
for(int i=0;i<array2.size();i++) {
borrowinformation a=new borrowinformation();
a=array2.get(i);
fw.write(a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate()+"\r\n");
}
System.out.println("添加借阅信息成功!");
fw.close();
//array4
ArrayList<bookInformation> array4=Save.inbook();
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array4.size();i++) {
bookInformation a=new bookInformation();
//a.setQuantity(a.getQuantity()-1);
a=array4.get(i);
if(i==n){
a.setQuantity(a.getQuantity()-1);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
System.out.println("该书目前还有"+a.getQuantity()+"本");
}
else {
br.write(a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress() + "\r\n");
}
}
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
else {
System.out.println("该书存量不足,不可借阅,自动返回");
return;
}
}
//归还图书
public static void returnbook() throws IOException{
Scanner sc=new Scanner(System.in);
System.out.println("请输入学号(仅能输入数字):\n");
String nu=sc.nextLine();
int num=Integer.parseInt(nu);
System.out.println("请输入姓名");
String name=sc.nextLine();
//array3
ArrayList<readerinformation> array3=Save.inreader();
boolean booll=false;
for(int i=0;i<array3.size();i++)
{
readerinformation a=new readerinformation();
a=array3.get(i);
if(a.getName().equals(name)&&a.getNum()==num){
booll=true;
}
}
if(booll==false){
System.out.println("输入的学号或姓名有误!");
return;
}
System.out.println("请输入要归还的图书");
ArrayList<borrowinformation> array1=Save.inborrow();
//Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
int n = 0;
boolean bool=false;
int quantity = 0;
int D = 0;
for(int i=0;i<array1.size();i++)
{
borrowinformation a=new borrowinformation();
a=array1.get(i);
if(a.getBookname().equals(line)){
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
long timemillis=System.currentTimeMillis();
Date date=new Date(timemillis);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time1 = simpleDateFormat.format(date.getTime());
borrowinformation q=new borrowinformation();
q=array1.get(n);
q.setReturndate(time1);
System.out.println("归还书籍成功!");
FileWriter fr=new FileWriter("myFile\\borrow.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
borrowinformation a=new borrowinformation();
a=array1.get(i);
if(i==n){
a.setReturndate(q.getReturndate());
br.write(a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate()+"\r\n");
System.out.println("已写入归还时间");
}
else {
br.write(a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate() + "\r\n");
}
}
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
ArrayList<bookInformation> array4=Save.inbook();
FileWriter frr=new FileWriter("myFile\\book.txt");
BufferedWriter brr=new BufferedWriter(frr);
for(int i=0;i<array4.size();i++) {
bookInformation a=new bookInformation();
//a.setQuantity(a.getQuantity()-1);
a=array4.get(i);
if(i==n){
a.setQuantity(a.getQuantity()+1);
brr.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
System.out.println("该书目前还有"+a.getQuantity()+"本");
}
else {
brr.write(a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress() + "\r\n");
}
}
//System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
brr.close();
frr.close();
}
}
其他方法
package functions;
//这里是二级页面,图书管理系统
import EntityClass.bookInformation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import static functions.Add.AddBookInformation;
import static functions.Find.findbook;
public class LibraryinformationManagement {
public static void bookmew() throws IOException, ClassNotFoundException {
ArrayList<bookInformation> array=new ArrayList<bookInformation>();
while (true) {
System.out.println("1.添加图书");
System.out.println("2.查询图书");
System.out.println("3.图书排序");
System.out.println("4.修改或删除图书");
System.out.println("5.返回上一级");
char c = Tool.tools.readMenuSelection();
switch (c) {
case '1':
AddBookInformation(array);
break;
case '2':
findbook(array);
break;
case '3':
sort.sentakusort();
break;
case '4':
//Change.change1();
sentaku1();
break;
case '5':
return;
}
}
}
public static void sentaku1() throws IOException{
System.out.println("您要进行修改还是删除?:"+"\n"+"1、修改\n2、删除");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
Change.sentakuchange1();
break;
case 2:
Delete.sentakudelete1();
break;
}
}
}
package functions;
import EntityClass.readerinformation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class LibraryreaderManagement {
public static void readermew() throws IOException, ClassNotFoundException {
ArrayList<readerinformation> array=new ArrayList<readerinformation>();
while (true) {
System.out.println("1.添加读者信息");
System.out.println("2.查询读者信息");
System.out.println("3.读者信息排序");
System.out.println("4.修改或删除读者信息");
System.out.println("5.返回上一级");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
switch (line) {
case "1":
Add.AddReaderInformation(array);
break;
case "2":
Find.findreader(array);
break;
case "3":
sort.sentakusortreader();
break;
case "4":
sentaku1();
break;
case "5":
return;
}
}
}
public static void sentaku1() throws IOException{
System.out.println("您要进行修改还是删除?:"+"\n"+"1、修改\n2、删除");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
Change.change3();
break;
case 2:
Delete.detelebook3();
break;
}
}
}
读取文件io流方法
package functions;
import EntityClass.bookInformation;
import EntityClass.borrowinformation;
import EntityClass.readerinformation;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Save {
//先写一个读取文件进集合的方法
public static ArrayList inbook() throws IOException {
FileReader fr = new FileReader("myFile\\book.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<bookInformation> array = new ArrayList<bookInformation>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line = br.readLine()) != null) {
//把读取到的字符串数据用split()进行分割,得到一个字符串数组
String[] strArray = line.split(",");
bookInformation book = new bookInformation();
//把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
book.setNum(Integer.parseInt(strArray[0]));
book.setName(strArray[1]);
book.setAuthor(strArray[2]);
book.setPrice(strArray[3]);
book.setQuantity(Integer.parseInt(strArray[4]));
book.setDate(strArray[5]);
book.setPress(strArray[6]);
//把图书对象添加到集合
array.add(book);
}
br.close();
fr.close();
return array;
}
//利用上面这个读取文件到集合的方法,下面开始写查询方法
/*public static void findbook1() throws IOException{
//本方法用书名查找书籍
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<bookInformation> array1=inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
bookInformation a=new bookInformation();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}*/
//先写一个读取文件进集合的方法
public static ArrayList inreader() throws IOException {
FileReader fr = new FileReader("myFile\\reader.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<readerinformation> array = new ArrayList<readerinformation>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line = br.readLine()) != null) {
//把读取到的字符串数据用split()进行分割,得到一个字符串数组
String[] strArray = line.split(",");
readerinformation reader = new readerinformation();
//把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
reader.setNum(Integer.parseInt(strArray[0]));
reader.setName(strArray[1]);
reader.setCollege(strArray[2]);
reader.setClass(strArray[3]);
//把读者对象添加到集合
array.add(reader);
}
br.close();
fr.close();
return array;
}
//先写一个读取文件进集合的方法
public static ArrayList inborrow() throws IOException {
FileReader fr = new FileReader("myFile\\borrow.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<borrowinformation> array = new ArrayList<borrowinformation>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line = br.readLine()) != null) {
//把读取到的字符串数据用split()进行分割,得到一个字符串数组
String[] strArray = line.split(",");
borrowinformation borrow = new borrowinformation();
//把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
borrow.setNum1(Integer.parseInt(strArray[0]));
borrow.setName(strArray[1]);
borrow.setNum2(Integer.parseInt(strArray[2]));
borrow.setBookname(strArray[3]);
borrow.setBorrowdate(strArray[4]);
borrow.setShouldreturndate(strArray[5]);
borrow.setReturndate(strArray[6]);
//把图书对象添加到集合
array.add(borrow);
}
br.close();
fr.close();
return array;
}
}
sort方法
package functions;
import EntityClass.bookInformation;
import EntityClass.readerinformation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class sort {
//对书籍或读者信息进行排序
//定义图书排序(冒泡排序)
public static void sentakusort() throws IOException {
System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用书号\n2、使用书名");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
sortingBook1(Save.inbook());
break;
case 2:
sortingBook2(Save.inbook());
break;
}
}
//使用书号排列输出
public static void sortingBook1(ArrayList<bookInformation> array) {
// int[] arr = new int[100];
// for (int i = 0; i < array.size(); i++) {
// Book b = array.get(i);
// arr[i] = b.getBookid();
// }
//冒泡
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
bookInformation book1 = array.get(j);
bookInformation book2 = array.get(j+1);
if (book1.getNum()>book2.getNum()) {
// int temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序+\n排序后的列表:\n");
//查看排序后的图书
for (int i = 0; i < array.size(); i++) {
bookInformation a = array.get(i);
//排序后输出
System.out.println(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress());
}
}
//使用书名进行排序
public static void sortingBook2(ArrayList<bookInformation> array) {
// int[] arr = new int[100];
// for (int i = 0; i < array.size(); i++) {
// Book b = array.get(i);
// arr[i] = b.getBookid();
// }
//冒泡
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
bookInformation book1 = array.get(j);
bookInformation book2 = array.get(j+1);
int bool= book1.getName().compareTo(book2.getName());
if (bool>0) {
// int temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序\n排序后的列表:\n");
//查看排序后的图书
for (int i = 0; i < array.size(); i++) {
bookInformation a = array.get(i);
//排序后输出
System.out.println(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress());
}
}
public static void sentakusortreader() throws IOException {
System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用学号\n2、使用学院");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
sortingreader1(Save.inreader());
break;
case 2:
sortingreader2(Save.inreader());
break;
}
}
//使用学号排列输出
public static void sortingreader1(ArrayList<readerinformation> array) {
// int[] arr = new int[100];
// for (int i = 0; i < array.size(); i++) {
// Book b = array.get(i);
// arr[i] = b.getBookid();
// }
//冒泡
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
readerinformation book1 = array.get(j);
readerinformation book2 = array.get(j+1);
if (book1.getNum()>book2.getNum()) {
// int temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序\n排序后的列表:\n");
//查看排序后的图书
for (int i = 0; i < array.size(); i++) {
readerinformation a = array.get(i);
//排序后输出
System.out.println(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getCollege());
}
}
//使用学院进行排序
public static void sortingreader2(ArrayList<readerinformation> array) {
// int[] arr = new int[100];
// for (int i = 0; i < array.size(); i++) {
// Book b = array.get(i);
// arr[i] = b.getBookid();
// }
//冒泡
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
readerinformation book1 = array.get(j);
readerinformation book2 = array.get(j+1);
int bool= book1.getName().compareTo(book2.getName());
if (bool>0) {
// int temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序\n排序后的列表:\n");
//查看排序后的图书
for (int i = 0; i < array.size(); i++) {
readerinformation a = array.get(i);
//排序后输出
System.out.println(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getCollege());
}
}
}
工具类
package Tool;
//这里会封装一些常用方法
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//工具类,把不同的功能封装成方法,直接进行调用
public class tools {
public static Scanner scanner=new Scanner(System.in);
//用于主界面的选择,读取键盘,用户输入“1-5”中的任意数字,返回用户输入的数字,否则重新输入
public static char readMenuSelection(){
char c;
while(true) {
String str;
str = scanner.next();
c=str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static String readDate(){
String date;
date=scanner.nextLine();
return date;
}
//日期类
public static final class DateUtils { // 防止被其他类继承
// 定义图书馆规定的标准书籍借阅归还日期常量
public static final int STANDARD_RET_DATE = 15;
private DateUtils() { // 防止被实例化
super();
}
// 根据自定义借阅天数计算书籍归还日期
public static String getReturnDate(int divDate) {
if (divDate > STANDARD_RET_DATE)
return null;
else {
//借阅日期
Date date = new Date();
SimpleDateFormat returnDate = new SimpleDateFormat("yyyy-MM-dd");
return returnDate.format(date.getTime() + divDate * 24 * 60 * 60 * 1000);
}
}
// 根据图书馆规定的标准借阅天数计算书籍归还日期
public static String getReturnDate() {
return getReturnDate(STANDARD_RET_DATE);
}
}
/*public static void main(String[] args) {
System.out.println("图书馆规定最晚归还日期:" + DateUtils.getReturnDate());
if (DateUtils.getReturnDate(10) != null) // 测试借阅天数10
System.out.println("书籍实际归还日期:" + DateUtils.getReturnDate(10));
else
System.out.println("借阅时间过长");
}*/
}
Mew类
package view;
//这里是界面显示
import functions.Libraryborrow;
import functions.LibraryinformationManagement;
import functions.LibraryreaderManagement;
public class Mew {
public static void MainMew() throws Exception{
boolean b=true;
while(b){
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("");
char c=Tool.tools.readMenuSelection();
switch (c){
case '1':
LibraryinformationManagement.bookmew();
break;
case '2':
LibraryreaderManagement.readermew();
break;
case '3':
//Libraryborrow.
Libraryborrow.sentaku();
break;
case '4':
System.out.println("谢谢使用");
System.exit(0);
break;
case '5':
System.out.println("谢谢使用");
System.exit(0);
break;
}
}
}
}
package view;
//这里是启动
public class run {
public static void main(String[] args) throws Exception {
Mew.MainMew();
}
}
三、小结
以上代码基本实现了所有要求,但存在不够精简,代码冗余,注释不清等等问题,主要还是我第一次写比较大的代码,写到后面神志模糊有些放飞自我了,很多地方都不是很合理,随便一测就有bug,希望大家多多包涵,今后如果有机会,还会再重写一遍的。
没了。