此为 用IO流写入文件代替数据库,编写的DVD管理系统,实现了对DVD系统的增删改查,这个第一次运行的时候会出现错误,因为文件中什么都没有,所以靠IO流的读取是读不到东西的,系统就会抛出异常。所以运行之前,请自行加入一些数据,方能方便运行。
首先是DVD的属性类:
public class Dvd implements java.io.Serializable{
private String num;
private String name;
private double price;
private int count;
public Dvd(String num, String name, double price, int count) {
super();
this.num = num;
this.name = name;
this.price = price;
this.count = count;
}
public Dvd(){}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
再次就是方法类:方法类通过对文件里数据进行操作,实现数据库保存功能,一读一写就是方便保存,不然会刷新文件里的东西导致保存不住
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class DvdBiz implements java.io.Serializable{
List addList = new ArrayList();
private Dvd dvd = new Dvd();
// 增 删 改 查 四项功能
public void add(Dvd dvd){ //通过传来的Dvd对象加入到list集合中然后写入Dvd文件
Read();
addList.add(dvd);
write();
}
public boolean delete(String num){ // 删除选中编号的DV
Read();
for(int i =0;i
dvd = (Dvd) addList.get(i);
if(dvd.getNum().equals(num)){
addList.remove(i);
write();
return true;
}
}
return false;
}
public void show(){ // 光盘信息显示
Read();
for(int i =0;i
dvd = (Dvd) addList.get(i);
System.out.println("\t"+dvd.getNum()+"\t\t"+dvd.getName()
+"\t\t"+dvd.getPrice()+"\t\t"+dvd.getCount());
}
write();
}
public Dvd search(String num){//根据编号查询是否存在此书,找到则返回Dvd的对象找不到则返回null
Read();
for(int i= 0;i
dvd = (Dvd) addList.get(i);
if(dvd.getNum().equals(num)){
return dvd;
}
}
return null;
}
public Dvd change(String num,String name,double price,int count){ //根据编号修改图书信息。。
Read();
for(int i= 0;i
dvd = (Dvd) addList.get(i);
if(dvd.getNum().equals(num)){
dvd.setPrice(price);
dvd.setName(name);
dvd.setCount(count);
write();
return dvd;
}
}
return null;
}
public void Read(){ //读取文件中存在的信息
ObjectInputStream ois = null;
FileInputStream fis = null;
File file = new File("E:\\haha\\Dvd.txt");
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
addList = (List)ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void write(){ //往文件里写入获取到的信息
ObjectOutputStream oos = null;
FileOutputStream fos = null;
File file = new File("E:\\haha\\Dvd.txt");
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
if(file.exists()){
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(addList);
}else{
file.createNewFile();
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(addList);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
oos.flush();
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
再就是界面类:
import java.util.Scanner;
public class DvdJsp implements java.io.Serializable{
private DvdBiz dz = new DvdBiz();
private Dvd dvd = new Dvd();
Scanner input = new Scanner(System.in);
public void main() {
System.out.println("========================" +
"======================================");
System.out.println("\tDVD编号\t\tDVD名称\t\tDVD价格\t\t库存");
dz.show();
System.out
.println("=======================" +
"=======================================");
while (true) {
System.out.println("\t\t\t\t 1.DVD查询");
System.out.println("\t\t\t\t 2.DVD添加");
System.out.println("\t\t\t\t 3.DVD修改");
System.out.println("\t\t\t\t 4.DVD删除");
System.out.print("\t\t请输入您的将进行的操作:");
int in = input.nextInt();
String name;
String num;
double price;
int count;
boolean inFlog ;
switch(in){
case 1:
System.out.print("请输入您要查询的DVD编号:");
num = input.next();
dvd = dz.search(num);
if(dvd != null){
System.err.println("找到了,此光盘信息为:");
System.out.println("\tDVD编号\tDVD名称\tDVD价格\t库存");
System.out.println("\t"+dvd.getNum()+"\t"+dvd.getName()
+"\t"+dvd.getPrice()+"\t"+dvd.getCount());
}else
System.err.println("未找到您要查询的光盘信息!");
break;
case 2:
System.out.print("请输入要添加的光盘编号:");
num = input.next();
System.out.print("请输入要添加的光盘名称:");
name = input.next();
System.out.print("请输入要添加的光盘价格:");
price = input.nextDouble();
System.out.print("请输入要添加的光盘库存:");
count = input.nextInt();
dvd.setCount(count);
dvd.setName(name);
dvd.setNum(num);
dvd.setPrice(price);
dz.add(dvd);
break;
case 3:
System.out.print("请输入要修改的光盘编号:");
num = input.next();
System.out.print("请输入要修改的光盘名称:");
name = input.next();
System.out.print("请输入要修改的光盘价格:");
price = input.nextDouble();
System.out.print("请输入要修改的光盘库存:");
count = input.nextInt();
dvd = dz.change(num, name, price, count);
if(dvd!=null){
System.err.println("修改成功,修改后光盘的信息为:");
System.out.println("\tDVD编号\t\tDVD名称\t\tDVD价格\t\t库存");
System.out.println("\t"+dvd.getNum()+"\t\t"+dvd.getName()
+"\t\t"+dvd.getPrice()+"\t\t"+dvd.getCount());
}else
System.err.println("没有找到您要修改的光盘,请核对后再修改!");
break;
case 4:
System.out.println("请输入要删除的光盘编号:");
num = input.next();
inFlog = dz.delete(num);
if(inFlog){
System.err.println("删除成功!");
}else
System.err.println("没找到!");
break;
default:
System.out.println("您的输入有误请重新选择要进行的操作");
break;
}
}
}
}
最后就是测试类:
public class Test {
public static void main(String[] args){
DvdJsp dj = new DvdJsp();
dj.main();
}
}