⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flightdaofromfile.java

📁 一套基于JAVA开发的完整版航空订票系统,代码简洁,适合JAVA初学者研究
💻 JAVA
字号:
package com.tarena.abs.dao;

import java.io.*;
import java.util.*;

import com.tarena.abs.model.Flight;
import com.tarena.abs.model.FlightSchedular;
import com.tarena.abs.model.MyDate;
import com.tarena.abs.model.MyTime;
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;
		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) {
			System.out.println("flight.dat文件中目前还没有数据,将写入一个空数据!");
		}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();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(oos!=null)try{oos.close();}catch(IOException e){}
				if(fos!=null)try{fos.close();}catch(IOException e){}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public Set getAllFlights(String fromAddr, String toAddr, MyDate date) {
		HashSet hs = new HashSet();//存放所有航班的信息
		HashSet hb = new HashSet();//存放所有符合条件的航班
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
			hs = (HashSet)ois.readObject();
			ois.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Iterator it = hs.iterator();
		while(it.hasNext()){
			Flight flight = (Flight)it.next();
			FlightSchedular schedular = flight.getSch();
			if(schedular.getFromAddress().equals(fromAddr) && schedular.getToAddress().equals(toAddr) && flight.getDate().equals(date)){
				hb.add(flight);
			}
		}
		return hb;
	}

	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();
			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;
	}

	@SuppressWarnings("unchecked")
	public boolean addFlightSchedular(FlightSchedular fs) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		HashSet hs = new HashSet();
		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 {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(hs);
			oos.flush();
			oos.close();
		} catch (IOException e) {
			return false;
		}	
		return true;
	}

	public boolean removeFlightSchedular(String flightNumber) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		HashSet hs = new HashSet();
		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 a = (FlightSchedular)it.next();
			if(a.getFlightNumber().equals(flightNumber)){
				hs.remove(a);
				try {
					ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
					oos.writeObject(hs);
					oos.flush();
					oos.close();
				} catch (IOException e) {
					return false;
				}	
				return true;
			}
		}
		return false;
		
	}

	public void removeOverDateFlights() {
		new Thread(){
			public void run(){
				while(true){
					Calendar c = new GregorianCalendar();//得到当前系统时间
					if(Calendar.HOUR_OF_DAY == 0){
						c.setTimeInMillis(c.getTimeInMillis() - 3*60*60*1000);//得到当前时间3小时之前的时间
						FileInputStream fis = null;
						ObjectInputStream ois = null;
						HashSet hs = new HashSet();
						try {
							fis = new FileInputStream(file);
							ois = new ObjectInputStream(fis);
							hs = (HashSet)ois.readObject();//得到航班的集合
							ois.close();
							fis.close();
							
						} catch (Exception e) {
							System.out.println(e.getMessage());
						}
						
						Iterator it = hs.iterator();
						while(it.hasNext()){//得到每个航班的时间
							Flight flight = (Flight)it.next();
							FlightSchedular schedular = flight.getSch();
							MyDate date = schedular.getStartDate();
							MyTime time = schedular.getFromTime();
							Calendar c2 = new GregorianCalendar(date.getYear(),date.getMonth(),date.getDay(),time.getHour(),time.getMin());
							if(c2.after(c)){//如果得到的日期在当前日期之后,执行删除操作
								hs.remove(flight);
								try {
									ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
									oos.writeObject(hs);
									oos.flush();
									oos.close();
								}catch (IOException e) {
									e.printStackTrace();
								}
								
							}
						}
					}
				}
			}
		}.start();

		
		
	}

	public Set getAllFlightSchedulars() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		HashSet hs = new HashSet();
		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			hs = (HashSet)ois.readObject();
			ois.close();
			fis.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hs;
	}

	@SuppressWarnings("unchecked")
	public boolean addFlight(Flight fl) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		HashSet hs = new HashSet();
		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			hs = (HashSet)ois.readObject();//从文件中读取对象
			ois.close();//关闭文件输入流
			fis.close();
			hs.add(fl);//集合中添加一个对象
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(hs);//把集合对象从新写入文件中
			oos.flush();
			oos.close();
			
		} catch (Exception e) {
			return false;
		}
		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -