📄 flightdaofromfile.java
字号:
package com.tarena.abs.dao;
import java.io.*;
import java.util.*;
import com.tarena.abs.model.*;
import com.tarena.abs.server.ServerMainClass;
public class FlightDaoFromFile implements FlightDAO {
private File file;
public FlightDaoFromFile(String fileName){
file=new File(fileName);
FileOutputStream fos=null;
ObjectOutputStream oos=null;
FileInputStream fis=null;
ObjectInputStream ois=null;
Object obj=null;
try {//试着读取文件中的对象
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
obj=ois.readObject();
} catch (Exception e) {
//e.printStackTrace();
System.out.println("创建了"+fileName+"文件");
}finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
//如果读到的对象为null,或者不是HashSet
if(obj==null || !(obj instanceof HashSet)){
try {//就向文件中写入一个空的HashSet
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(new HashSet());
oos.flush();
System.out.println("创建了"+fileName+"文件");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
}
}
public Set getAllFlights(String fromAddr, String toAddr, Calendar date) {
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
HashSet flights = new HashSet(); //切记 ,为了执行add操作无异常
boolean b=false;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
System.out.println("dao查到的所有航班"+hs);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
Iterator it=hs.iterator();
while(it.hasNext()){
Flight f=(Flight)it.next();
//System.out.println(f);
FlightSchedular fs = f.getSch();
//System.out.println("dao查到的发送每个航班的航班计划"+fs);
//System.out.println(f.getDate()+" "+fs.getFromAddress()+" "+fs.getToAddress());
//System.out.println(date+" "+fromAddr+" "+toAddr);
if(f.getCalendar().equals(date) && fs.getFromAddress().equals(fromAddr) && fs.getToAddress().equals(toAddr)){
b = flights.add(f); //把flights初始化,否则NullPointerException
//System.out.println("dao查到的航班"+flights);
}
}
if(b)return flights;
return null;
}
public boolean order(Order ord) {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
ArrayList Items = null;
boolean b=true; //默认成功,失败时则赋值。
// 从数据库读取航班信息
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
//遍历item和flight,把舱位数减少
Items = ord.getItems();
//System.out.println("Items");
//System.out.println(Items);
for(Object item:Items){
TicketOrder m = (TicketOrder)item;
//System.out.println("m");
//System.out.println(m);
for(Object flight:hs){
Flight f = (Flight)flight;
//System.out.println("f");
//System.out.println(m.getFlight()+":"+f);
if(m.getFlight().equals(f)){
//System.out.println(m.getFlight()+":"+f);
//不是婴儿票则减少座位
if(m.getT_type()!=TicketType.BabyTicket){
if(m.getF_class()==CabinClass.EconomyClass ){
if(f.getECSRemain()>0)
f.setECSRemain(f.getECSRemain()-1);
else
b=false;
}
//System.out.println("f.getECSRemain()"+f.getECSRemain());
if(m.getF_class()==CabinClass.FirstClass ){
if(f.getFCSRemain()>0)
f.setFCSRemain(f.getFCSRemain()-1);
else
b=false;
}
// System.out.println("f.getECSRemain()"+f.getECSRemain());
if(m.getF_class()==CabinClass.OfficialClass){
if(f.getBCSRemain()>0)
f.setBCSRemain(f.getBCSRemain()-1);
else
b=false;
}
//System.out.println("f.getECSRemain()"+f.getECSRemain());
}
//当前实现的是:有一张票出不来就算失败
}
}
}
//如果改变了舱位数,则存入航班
if(b==true){
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
}
return b;
}
public boolean addFlightSchedular(FlightSchedular fs) {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
boolean b=false;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
b = hs.add(fs);
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
return b;
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
return false;
}
public boolean removeFlightSchedular(String flightNumber) {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
boolean b=false;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
Iterator it=hs.iterator();
while(it.hasNext()){
FlightSchedular fs=(FlightSchedular)it.next();
if(fs.getFlightNumber().equals(flightNumber)){
b = hs.remove(fs);
}
}
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
return b;
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
return false;
}
public void removeOverDateFlights() {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
Iterator it=hs.iterator();
while(it.hasNext()){
Flight f=(Flight)it.next();
if(f.getCalendar().after(Calendar.getInstance())){
hs.remove(f);
}
}
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
}
public Set getAllFlightSchedulars() {
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
return hs;
}
public boolean addFlight(Flight fl) {
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
HashSet hs=null;
boolean b=false;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(ois!=null)try{ois.close();}catch(IOException e){}
if(fis!=null)try{fis.close();}catch(IOException e){}
}
b = hs.add(fl);
try {
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(oos!=null)try{oos.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -