package src;
import java.io.*;
import exception.*;
public class Library implements Serializable{
private static String library = "CUIT_Library";
private static int booksAmount = 0;
private static int studentsAmount = 0;
private static Book [] books = new Book[160];
private static Student [] students = new Student[160];
private static FileOutputStream oips;
public static void addBook (Book newBook){
try{
int index;
File file = new File ("books.txt");
if (!file.exists ()) {
file.createNewFile ();
}
FileInputStream fips = new FileInputStream (file);
if (fips.read() == -1){
ObjectOutputStream objos=new ObjectOutputStream(new FileOutputStream (file, true));
objos.writeObject(newBook);
objos.writeObject (new String ()); // 如果文件为空,写入String,作为下次判断读到文件尾标志
objos.close();
} else{
getBooks ();
File file2 = new File ("books.txt"); // 将文件内容清空,以便重新写入
FileWriter fw = new FileWriter(file2);
fw.write("");
fw.close();
for (index = 0; books[index] != null; ++ index);
books[index] = newBook; // 新添加书籍作为末尾
booksAmount = index + 1;
File file3 = new File ("books.txt"); // 关联文件
FileOutputStream f = new FileOutputStream (file3, true); // 创建文件输出流,以追加的方式写文件
ObjectOutputStream oips=new ObjectOutputStream(f);
for (index = 0; books[index] != null; ++ index){
oips.writeObject(books[index]);
}
oips.writeObject (new String ()); // 写入null,作为下次判断读到文件尾标志
oips.close();
}
} catch (FileNotFoundException e1){
System.out.println ("文件未找到");
e1.printStackTrace();
} catch (IOException e2){
System.out.println ("添加图书信息是I/O异常");
e2.printStackTrace();
} catch (Exception e3){
System.out.println ("未知错误");
e3.printStackTrace();
}
}
public static void deleteBook (String title){
int i;
getBooks();
for (i = 0; books[i] != null; ++ i){
if (title.equalsIgnoreCase(books[i].getTitle()))
break;
}
try{
if (books[i] == null)
throw new CanNotSearchBookException();
if (books[i].getAmount() < books[i].getInitAmount()) // 如果还有书籍被读者借去,则不能删除书籍,避免造成数据混乱
throw new CanNotDeleteBookException ();
for (int j = i; books[j] != null; ++ j){
books[j] = books[j + 1];
}
Library.saveAllBooks(books);
} catch (CanNotSearchBookException e1){
System.out.println("所删除图书不存在异常");
} catch (CanNotDeleteBookException e1){
System.out.println("不可删除图书异常,(由于有部分书籍借出未还)");
}
}
public static void saveAllBooks (Book [] newBooks){
try{
int index;
File file = new File ("books.txt"); // 将文件内容清空,以便重新写入
FileWriter fw = new FileWriter(file);
fw.write("");
fw.close();
FileOutputStream f = new FileOutputStream (file, true); // 创建文件输出流,以追加的方式写文件
ObjectOutputStream o=new ObjectOutputStream(f);
for (index = 0; newBooks[index] != null; ++ index){
o.writeObject(newBooks[index]);
}
o.writeObject ((Book) null); // 写入null,作为下次判断读到文件尾标志
o.close();
} catch (FileNotFoundException e1){
System.out.println ("文件未找到");
e1.printStackTrace();
} catch (IOException e2){
System.out.println ("保存全部图书信息是I/O异常");
e2.printStackTrace();
} catch (Exception e3){
System.out.println ("未知错误");
e3.printStackTrace();
}
}
public static Book[] getBooks () {
try{
int start = 0;
booksAmount = 0; // 调用此方法前先将字段清空
Object a;
for (int j = 0; j != books.length; ++ j){ // 防止多次调用此方法时数据错误
books[j] = null;
}
File file = new File ("books.txt"); // 关联文件
FileInputStream fips = new FileInputStream (file); // 打开文件,创建一个 FileInputStream
ObjectInputStream oips = new ObjectInputStream(fips);
while ((a = oips.readObject()) instanceof Book) { // 由之前写入的null,判断是否读到文件尾
books [start] = (Book) a;
++ start;
}
oips.close();
} catch (IOException e){
System.out.println ("获取图书信息是I/O异常");
e.printStackTrace();
} catch (Exception e2){
System.out.println ("未知错误");
e2.printStackTrace();
}
return books;
}
public static void addStudent (Student newstudent){
try{
int index;
File file = new File ("readers.txt");
if (!file.exists ()) {
file.createNewFile ();
}
FileInputStream fips = new FileInputStream (file);
if (fips.read() == -1){
ObjectOutputStream objos=new ObjectOutputStream(new FileOutputStream (file, true));
objos.writeObject(newstudent);
objos.writeObject (new String ()); // 如果文件为空,写入String,作为下次判断读到文件尾标志
objos.close();
} else{
getStudents ();
File file2 = new File ("readers.txt"); // 将文件内容清空,以便重新写入
FileWriter fw = new FileWriter(file2);
fw.write("");
fw.close();
for (index = 0; students[index] != null; ++ index);
students[index] = newstudent; // 新添加书籍作为末尾
studentsAmount = index + 1;
File file3 = new File ("readers.txt"); // 关联文件
FileOutputStream f = new FileOutputStream (file3, true); // 创建文件输出流,以追加的方式写文件
ObjectOutputStream oips=new ObjectOutputStream(f);
for (index = 0; students[index] != null; ++ index){
oips.writeObject(students[index]);
}
oips.writeObject (new String ()); // 写入null,作为下次判断读到文件尾标志
oips.close();
}
} catch (FileNotFoundException e1){
System.out.println ("文件未找到");
e1.printStackTrace();
} catch (IOException e2){
System.out.println ("添加学生信息是I/O异常");
e2.printStackTrace();
} catch (Exception e3){
System.out.println ("未知错误");
e3.printStackTrace();
}
}
public static void deleteStudent (String number){
int i;
getStudents();
for (i = 0; students[i] != null; ++ i){
if (number.equalsIgnoreCase(students[i].getStudentNum()))
break;
}
try{
if (students[i] == null)
throw new CanNotSearchStudentException();
if (students[i].getBorrowedBooks() != 0) // 如果还有书籍被读者借去,则不能删除书籍,避免造成数据混乱
throw new CanNotDeleteStudentException ();
for (int j = i; students[j] != null; ++ j){
students[j] = students[j + 1];
}
Library.saveAllStudents(students);
} catch (CanNotSearchStudentException e1){
System.out.println("所删除学生不存在异常");
} catch (CanNotDeleteStudentException e1){
System.out.println("不可删除学生异常,(由于该学生有借出书为还)");
}
}
public static void saveAllStudents (Student [] newStudents){
try{
int index;
File file = new File ("readers.txt"); // 将文件内容清空,以便重新写入
FileWriter fw = new FileWriter(file);
fw.write("");
fw.close();
if (!file.exists ()) {
file.createNewFile ();
}
FileOutputStream f = new FileOutputStream (file, true); // 创建文件输出流,以追加的方式写文件
ObjectOutputStream o=new ObjectOutputStream(f);
for (index = 0; newStudents[index] != null; ++ index){
o.writeObject(newStudents[index]);
}
o.writeObject (new Book ()); // 写入null,作为下次判断读到文件尾标志
o.close();
} catch (FileNotFoundException e1){
System.out.println ("文件未找到");
e1.printStackTrace();
} catch (IOException e2){
System.out.println ("保存全部学生信息时I/O异常");
e2.printStackTrace(
没有合适的资源?快使用搜索试试~ 我知道了~
基于Java的图书管理系统代码(包含UI代码)
共98个文件
class:61个
java:33个
applet:1个
5星 · 超过95%的资源 需积分: 10 27 下载量 142 浏览量
2013-04-11
12:59:14
上传
评论 1
收藏 68KB RAR 举报
温馨提示
基于Java的图书管理系统代码(包含UI代码) 以JFrame作为程序框架
资源推荐
资源详情
资源评论
收起资源包目录
Library.rar (98个子文件)
Library
exception
BorrowedBooksOutOfLimitException.java 92B
LoadFaultException.java 76B
NoBookCanBorrowException.java 82B
CanNotDeleteBookException.class 323B
BorrowedBooksOutOfLimitException.class 344B
NotBorrowException.java 76B
CanNotDeleteBookException.java 85B
CanNotDeleteStudentException.java 88B
CanNotSearchBookException.class 323B
NoBookCanBorrowException.class 320B
CanNotDeleteStudentException.class 332B
CanNotSearchBookException.java 83B
CanNotSearchStudentException.class 332B
NotBorrowException.class 302B
LoadFaultException.class 302B
CanNotSearchStudentException.java 88B
java.policy.applet 141B
.settings
org.eclipse.jdt.core.prefs 598B
src
Test.class 529B
Library.class 7KB
Library.java 9KB
ReturnThread.java 370B
Person.java 778B
Student.java 5KB
BorrowThread.class 930B
ReturnThread.class 929B
Person.class 1KB
Student.class 6KB
BorrowThread.java 376B
Book.java 2KB
Test.java 235B
Book.class 3KB
.project 383B
ui
AddBookDialog.java 2KB
MainMenu.java 2KB
DeleteBookDialog.class 1KB
MultithreadTestMenu.java 2KB
SearchBookErrDialog.class 1KB
ReturnBookDialog.class 1KB
AddStudentDialog.java 2KB
DeleteStudentDialog.java 978B
ReturnBookErrDialog$1.class 694B
StudentMenu$2.class 852B
MainMenu$1.class 1KB
StaticUI.java 390B
MainMenu.class 2KB
StaticUI.class 690B
StudentMenu.java 3KB
LibraryMenu$5.class 842B
loadDialog.java 1KB
LoadErrDialog.class 1KB
SearchBookDialog$1.class 1KB
MainMenu$2.class 863B
BookInfoDialog.java 1KB
ReturnBookDialog$1.class 1KB
DeleteStudentDialog$1.class 988B
LibraryMenu$3.class 730B
StudentMenu$1.class 851B
MainMenu$3.class 887B
MultithreadTestMenu$1.class 1KB
MultithreadTestMenu.class 3KB
DeleteBookDialog.java 959B
LibraryMenu.class 2KB
ChangeStuInfoDialog.java 2KB
SearchBookDialog.java 1KB
LoadErrDialog$1.class 658B
ChangeStuInfoDialog$1.class 2KB
MultithreadTestMenu$2.class 890B
LoadErrDialog.java 688B
AddStudentDialog.class 2KB
LibraryMenu$2.class 730B
DeleteBookDialog$1.class 965B
ReturnBookDialog.java 1KB
ReturnBookErrDialog.class 1KB
MainMenu$4.class 709B
ReturnBookErrDialog.java 686B
SearchBookErrDialog$1.class 694B
StudentMenu$4.class 866B
LibraryMenu.java 2KB
ChangeStuInfoDialog.class 2KB
SearchBookErrDialog.java 706B
AddBookDialog$1.class 1KB
loadDialog.class 2KB
StudentMenu.class 3KB
BookInfoDialog.class 2KB
AddBookDialog.class 2KB
DeleteStudentDialog.class 1KB
BookInfoDialog$2.class 1KB
SearchBookDialog.class 2KB
loadDialog$1.class 1KB
BookInfoDialog$1.class 698B
LibraryMenu$4.class 736B
AddStudentDialog$1.class 1KB
StudentMenu$3.class 859B
LibraryMenu$1.class 724B
.classpath 295B
interfac
Reader.class 225B
Reader.java 158B
共 98 条
- 1
资源评论
- nfkbjdifge2013-04-29正好学习一下谢谢
u010208675
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用 C 语言实现的计算非负整数的阶乘
- 2011-2021最新版本北京大学数字普惠金融指数(PKU-DFIIC).xlsx
- 县域数字乡村指数2018-2020(1).xlsx
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- 李慧琴C语言基础部分.zip
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功