用java做一个简单的图书管理系统且不用数据库

a472789753 2011-12-18 07:14:12
用java做一个简单的图书管理系统,且不用数据库,能够实现一人(多人)能够借多本书,还书,读者管理等功能,且不要做界面,由于是初学,只能做了一个人能借多本书,不能借多人借书,读者管理也不懂做,望哪位高手帮帮忙,谢谢了
...全文
1048 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cwmwss 2011-12-20
  • 打赏
  • 举报
回复
不用数据库,那写xml文件里比较合适
Lifehopeness 2011-12-20
  • 打赏
  • 举报
回复
package csdn;



import java.io.Serializable;

public class Product implements Serializable {
String productNo;// 产品编号
String name;// 产品名称

Product() {

}

Product(String aNo, String aName) {
productNo = aNo;
name = aName;

}

public String getNo() {
return productNo;
}

public String getName() {
return name;
}

public void setNo(String aNo) {
productNo = aNo;
}

public void setName(String aName) {
name = aName;
}

}


package csdn;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class ProductManageSystem {
ArrayList<Product> al = new ArrayList<Product>();
BufferedReader bin;
File file;
boolean continue_e = true;
boolean bFlag;
String tmpID;
ObjectInputStream fin;
ObjectOutputStream fout;
int position;
Product firstProduct, tmpProduct;

ProductManageSystem() {
try {
file = new File("src/文件/Product.txt");
fout = new ObjectOutputStream(new FileOutputStream(file, true));
firstProduct = new Product("2000", "hello");
al.add(firstProduct);
fout.writeObject(al);
fout.close();
bin = new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e) {

}
}

public boolean searchID(String proID) {
// 检查系统中是否已经存在编号为stuID的记录
try {
fin = new ObjectInputStream(new FileInputStream(file));
al = (ArrayList) fin.readObject();

fin.close();
} catch (Exception e) {

}

for (position = 0; position < al.size(); position++) {
tmpProduct = (Product) al.get(position);
if (tmpProduct.getNo().equalsIgnoreCase(proID)) {
break;
}
}// for
if (al.size() != 0 && position != al.size()) {// 找到了满足条件的记录

return true;
}
else
return false;// 未找到满足条件的记录
}

public int searchStudByID(String idStr) {
//
try {
fin = new ObjectInputStream(new FileInputStream(file));
al = (ArrayList) fin.readObject();

fin.close();
} catch (Exception e) {

}
for (position = 0; position < al.size(); position++) {
tmpProduct = (Product) al.get(position);
if (tmpProduct.getNo().equalsIgnoreCase(idStr)) {
break;
}
}// for
return position;
}

public void studentOutput(Product pro) {
System.out.println("产品编号:" + pro.getNo());
System.out.println("产品名称:" + pro.getName());

}

public void mainOperateToCommunication() {
int choice = 0;
while (continue_e) {
System.out.println();
System.out.println("请选择您要进行的操作(请输入1-5中的任一数字)" + "\n1.添加产品信息"
+ "\n2.显示所有产品信息" + "\n3.删除某条产品信息" + "\n4.查找某条产品信息"
+ "\n5.退出");
bFlag = true;
try {
while (bFlag) {
try {
choice = Integer.parseInt(bin.readLine());
if (choice < 1 || choice > 5) {
System.out.println("输入错误!请输入数字1-5选择你想要进行的操作!");
continue;
}
bFlag = false;
} catch (NumberFormatException e) {
System.out.println("输入错误!请输入数字1-5选择你想要进行的操作!");
continue;
}
}// while(bFlag)
switch (choice) {
case 1:// 添加新产品信息
System.out.print("产品编号:");
tmpID = bin.readLine().trim();
while (tmpID.length() == 0) {
System.out.println("产品编号不能为空,请重试!");
System.out.print("产品编号:");

tmpID = bin.readLine().trim();
}
try {

if (searchID(tmpID)) {
System.out.println("此记录已经存在,请不要重复输入!");
} else {
fout = new ObjectOutputStream(new FileOutputStream(
file));
Product newStu = new Product();
newStu.setNo(tmpID);
System.out.print("产品名称:");
newStu.setName(bin.readLine().trim());

al.add(newStu);
fout.writeObject(al);
System.out.println("记录添加成功!");
fout.close();
}

} catch (Exception e) {

}
break;

case 2:// 显示所有产品信息
System.out.println("下面是系统中的所有产品信息:");
try {
fin = new ObjectInputStream(new FileInputStream(file));
al = (ArrayList) fin.readObject();
fin.close();
} catch (Exception e) {

}
for (int i = 0; i < al.size(); i++) {
System.out.println("---------第" + i
+ "条记录:-------------");
tmpProduct = (Product) al.get(i);
studentOutput(tmpProduct);
}
break;

case 3:// 删除某条产品信息
System.out.print("请输入产品编号:");
tmpID = bin.readLine().trim();
while (tmpID.length() == 0) {
System.out.println("产品编号不能为空,请重试!");
System.out.print("产品编号:");

tmpID = bin.readLine().trim();
}
int delPosition = searchStudByID(tmpID);
try {
fout = new ObjectOutputStream(
new FileOutputStream(file));

if (al.size() != 0 && delPosition != al.size()) {
al.remove(delPosition);
fout.writeObject(al);
System.out.println("产品编号为:" + tmpID + "的记录删除成功!");
} else {
System.out.println("系统不存在产品编号为:" + tmpID + "的记录!");
}
fout.close();
} catch (IOException e) {

}
break;
case 4:// 查找某条产品信息
System.out.print("请输入产品编号:");
tmpID = bin.readLine().trim();
while (tmpID.length() == 0) {
System.out.println("产品编号不能为空,请重试!");
System.out.print("产品编号:");

tmpID = bin.readLine().trim();
}
int findPosition = searchStudByID(tmpID);
if (al.size() != 0 && findPosition != al.size()) {
tmpProduct = (Product) al.get(findPosition);
studentOutput(tmpProduct);
} else {
System.out.println("产品编号为" + tmpID + "的记录不存在!");
}
break;
case 5:
System.out.println("运行结束,再见!");
System.exit(0);
break;

}
} catch (IOException e) {

}
}// while
}

public static void main(String[] args) {

new ProductManageSystem().mainOperateToCommunication();
}
}
这是产品的一个例子
langkebai 2011-12-20
  • 打赏
  • 举报
回复
用集合就可以了,前几天做过一个。
a472789753 2011-12-19
  • 打赏
  • 举报
回复
好的,首先谢谢了,能不能给出具体的代码参考一下。
Lifehopeness 2011-12-18
  • 打赏
  • 举报
回复
那输入输出流可以的,把对象放在arraylist里边,在把arraylist写入文件,在程序寻运行开始的时候先把这个arraylist读出来,就可以操作啦。。。。此方法行得通,因为做过例子

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧