📄 flightdaofromfile.java
字号:
package com.tarena.abs.dao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JOptionPane;
import com.tarena.abs.model.Agent;
import com.tarena.abs.model.CabinClass;
import com.tarena.abs.model.Flight;
import com.tarena.abs.model.FlightSchedular;
import com.tarena.abs.model.MyDate;
import com.tarena.abs.model.Order;
import com.tarena.abs.model.OrderItem;
public class FlightDAOFromFile implements FlightDAO {
private File file;//文件中存放具体的航班
public FlightDAOFromFile(File file) {
this.file = file;
}
public Set getAllFlights(String fromAddr, String toAddr, MyDate date) {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs2=new HashSet();
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
HashSet hs=(HashSet)ois.readObject();
//System.out.println(hs);
if(ois!=null) ois.close();
if(fis!=null) fis.close();
Iterator it=hs.iterator();
while(it.hasNext()){
Flight fl=(Flight)it.next();
if(fl!=null &&fl.getSch().getFromAddress().equals(fromAddr) && fl.getSch().getToAddress().equals(toAddr) && fl.getDate().equals(date)){
System.out.println(fl.getSch().getFromAddress().toString()+fl.getSch().getToAddress().toString()+fl.getDate().toString());
hs2.add(fl);
}
}
} catch (Exception e){
e.printStackTrace();
}
return hs2;
}
public boolean order(Order ord) {
HashSet hs = new HashSet();//存放所有航班的信息
boolean isSuccess = false;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
hs = (HashSet)ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
ArrayList al = ord.getItems();
for (Iterator iter = hs.iterator(); iter.hasNext();) {
Flight f = (Flight) iter.next();
if(f==null)
break;
for (Iterator iterator = al.iterator(); iterator.hasNext();) {
OrderItem oi = (OrderItem) iterator.next();
Flight flight = oi.getFlight();
if(f.equals(flight)){
int[] count = {f.getFCSRemain(),f.getBCSRemain(),f.getECSRemain()};
if(oi.getF_class().toString().equals("头等舱") && f.getFCSRemain()>0){
f.setFCSRemain(f.getFCSRemain()-1);
isSuccess = true;
continue;
}
if(oi.getF_class().toString().equals("公务舱") && f.getBCSRemain()>0){
f.setBCSRemain(f.getBCSRemain()-1);
isSuccess = true;
continue;
}
if(oi.getF_class().toString().equals("经济舱") && f.getECSRemain()>0){
f.setECSRemain(f.getECSRemain()-1);
isSuccess = true;
continue;
}
f.setFCSRemain(count[0]);
f.setBCSRemain(count[1]);
f.setECSRemain(count[2]);
return false;
}
}
}
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(hs);
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
return isSuccess;
}
public boolean addFlightSchedular(FlightSchedular fs) {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
return false;
}
hs.add(fs);
try {
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
oos.close();
fis.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
public boolean removeFlightSchedular(String flightNumber) {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e){
return false;
}
Iterator it=hs.iterator();
while(it.hasNext()){
FlightSchedular h=(FlightSchedular)it.next();
if(h.getFlightNumber().equals(flightNumber)){
hs.remove(h);
try {
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
return false;
}
public void removeOverDateFlights() {
// TODO Auto-generated method stub
}
public Set getAllFlightSchedulars() {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
ois.close();
fis.close();
} catch (Exception e){
return null;
}
return hs;
}
public boolean addFlight(Flight fl) {
// TODO Auto-generated method stub
FileInputStream fis=null;
ObjectInputStream ois=null;
HashSet hs=null;
try {
fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
hs=(HashSet)ois.readObject();
ois.close();
fis.close();
hs.add(fl);
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(hs);
oos.flush();
oos.close();
fos.close();
}catch (Exception e) {
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -