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

📄 aviationbsdlg.cpp

📁 航空订票系统 学校大作业开发的
💻 CPP
字号:
// AviationBSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AviationBS.h"
#include "AviationBSDlg.h"
#include "SetLine.h"
#include <fstream.h>
#include<Windows.h>
#include<stdio.h>
#include<stdlib.h>

#include "BookingDlg.h"
#include "Refund.h"
#include "QueueDlg.h"

#ifndef SYSLINE_H
#define SYSLINE_H
#include "SysLine.h"
#endif

#ifndef  USEDLINE_H
#define  USEDLINE_H
#include "UsedLine.h"
#endif

#include "Client.h"
#include "Queue.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern SysLine sysLine;
extern UsedLine usedLine;
extern CClient client;
extern Queue queue;
extern int Index[8];
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAviationBSDlg dialog

CAviationBSDlg::CAviationBSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAviationBSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAviationBSDlg)
	m_FlyDate = COleDateTime::GetCurrentTime();
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAviationBSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAviationBSDlg)
	DDX_Control(pDX, IDC_COMBO2, m_LineCtrl);
	DDX_Control(pDX, IDC_FLIGHINFOLIST, m_FlightListCtrl);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_FlyDate);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAviationBSDlg, CDialog)
	//{{AFX_MSG_MAP(CAviationBSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ABOUTBUTTON, OnAboutbutton)
	ON_BN_CLICKED(IDC_SETLINEBUTTON, OnSetlinebutton)
	ON_BN_CLICKED(IDC_FIND, OnFind)
	ON_BN_CLICKED(IDC_BOOKINGBUT, OnBooking)
	ON_BN_CLICKED(IDC_REFUND, OnRefund)
	ON_BN_CLICKED(IDC_ONQUEUE, OnQueue)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAviationBSDlg message handlers

BOOL CAviationBSDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
		//设置航班的列表框的属性
    m_FlightListCtrl.SetExtendedStyle(m_FlightListCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT |
		 LVS_EX_GRIDLINES | LVS_EX_SUBITEMIMAGES);
	//设置航班的列表框的第一行
    TCHAR ListHead[9][13] = {_T("目的地"),_T("航班号"), _T("飞机型号"),_T("起飞日期"),_T("起飞时间"),_T("头等舱剩余数"),
						    _T("头等舱票价"),_T("经济舱剩余数"),_T("经济舱票价")};
	 LV_COLUMN lvcolumn;
	 CRect rect;
	 m_FlightListCtrl.GetWindowRect(&rect);
	 for(int i=0;i<9;i++)
	 {
        lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER;
		lvcolumn.fmt = LVCFMT_LEFT;
		lvcolumn.pszText = ListHead[i];
		lvcolumn.iSubItem = i;
		lvcolumn.iOrder = i;
		if(i<3||i==4) lvcolumn.cx = rect.Width()*2/21;
		   else if(i==3)lvcolumn.cx = rect.Width()*2/20.5;
			   else lvcolumn.cx = rect.Width()*2/15.3;
		m_FlightListCtrl.InsertColumn(i, &lvcolumn);
	 }
/////////////////////////////////////////////////////////////////////////////////
//创建数据库文件夹
	 if(CreateDirectory("DataBase",NULL))
			CreateDirectory("DataBase",NULL);

//读取SystemData.dat文件,把保存在里面的sysLine的关于系统航线信息读出来
    fstream file("DataBase/SystemData.dat",ios::binary | ios::in);  
	if(!file){MessageBox("Read file failed!");return false;}
	file.seekp(0,ios::beg);
	long beg=file.tellp();
	file.seekp(0,ios::end);
	long end=file.tellp();
	  if(beg!=end)
	  {   
		  SysLine temp;
		  file.seekp(0,ios::beg);
		  file.read((char *)& temp,sizeof(SysLine));
		  sysLine=temp; 
	  }
	  file.close();	
	//在查询框中的"目的地"下拉列表中加入已安排航班的航线
	 CString str;
     for(i=0;i<sysLine.LineSz;i++)
	{
		str.Format(_T("%s"),sysLine.lineArray[sysLine.flightArray[i].lineindex].destination);
		str=str.Left(10);
		str.Remove(' ');
        m_LineCtrl.AddString(str);
	}

//用户航线usedLine信息的读取与创建,usedLine 包含了七天之内的所有航班息(所在航线,售票情况)
	fstream file1("DataBase/UsedLine.dat",ios::binary | ios::in);  
	if(!file1){MessageBox("Read file failed!");return false;}
	file1.seekp(0,ios::beg);
	beg=file1.tellp();
	file1.seekp(0,ios::end);
	end=file1.tellp();
	  if(beg!=end)
	  {   
		  UsedLine temp1;
		  file1.seekp(0,ios::beg);
		  file1.read((char *)& temp1,long(sizeof(UsedLine)));	
		  usedLine=temp1;
	  }
	  file1.close();
 usedLine.Create();  //第一次启动时的创建系统,以及更新航线信息
 client.OnCreate();  //创建及更新客户信息
 queue.OnCreate();
/////////////////////////////////////////////////////////////////////////////////	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAviationBSDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAviationBSDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAviationBSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAviationBSDlg::OnAboutbutton() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg adlg;
	adlg.DoModal();
}

void CAviationBSDlg::OnSetlinebutton() 
{
	// TODO: Add your control notification handler code here
	SetLine setLine;
	setLine.DoModal();
//在查询框中的"目的地"下拉列表中加入已安排航班的航线,更新航线
	 CString str;
	 int size=m_LineCtrl.GetCount();
	for (int i=0;i<size;i++)//删除旧的
	{
	   m_LineCtrl.DeleteString(0);
	}
     for(i=0;i<sysLine.LineSz;i++)
	{
		str.Format(_T("%s"),sysLine.lineArray[sysLine.flightArray[i].lineindex].destination);
		str=str.Left(10);
		str.Remove(' ');
        m_LineCtrl.AddString(str);
	}
}

void CAviationBSDlg::OnOK() 
{
	// TODO: Add extra validation here
	fstream file("DataBase/SystemData.dat",ios::out | ios::binary);  //SystemData.dat的保存
    if(!file)MessageBox("Write file failed!");
     else 
	 {
		 file.seekp(0,ios::beg);
		 file.write((char*)&sysLine,long(sizeof(SysLine)));	 
	 }
	 file.close();
	fstream file1("DataBase/UsedLine.dat",ios::out | ios::binary);  //UsedLine.dat的保存
    if(!file1)MessageBox("Write file failed!");
     else 
	 {
		 file1.seekp(0,ios::beg);
		 file1.write((char*)& usedLine,long(sizeof(UsedLine)));	
	 }
	 file1.close();
	 client.Save();
     queue.Save();
	CDialog::OnOK();
}

void CAviationBSDlg::OnFind() 
{

	UpdateData();
	if(m_FlightListCtrl.GetItemCount( )>0)m_FlightListCtrl.DeleteAllItems();
	int index=m_LineCtrl.GetCurSel();
	if(CB_ERR == index){MessageBox("请选择目的地!");return;}
	if(m_FlyDate>=usedLine.GetNextNDay(7))
		{MessageBox("对不起,本系统只能查询七天以内的航班信息!谢谢合作!");return;}
	else if(m_FlyDate<usedLine.GetNextNDay(0))
        {MessageBox("对不起,你查询的日期已过期!");return;}
    COleDateTime Time=COleDateTime::GetCurrentTime();
	//删除今天已过期的航班
for(int i=usedLine.flightArray[index].front;
        usedLine.flightArray[index].length()!=0&&usedLine.flightArray[index].flight[i].FlyTime<=Time;) 
	{
	  usedLine.flightArray[index].deQueue();
	 i=usedLine.flightArray[index].front;
	}
    //查询合适的航班
    int Findex=0; 
for( i=usedLine.flightArray[index].front;i!=(usedLine.flightArray[index].rear+1)%31;i=(i+1)%31)
	{
	  if(usedLine.CompareDate(m_FlyDate,usedLine.flightArray[index].flight[i].FlyTime)==0
		  &&usedLine.flightArray[index].flight[i].FlyTime>Time)
	  {	  
		Index[Findex]=i;         //用一个数组存放航班的下标
    	CString str;
		LV_ITEM ListItem={0};
		ListItem.mask=LVIF_TEXT;
		ListItem.iItem=Findex;
		ListItem.iSubItem=0;
		str.Format(_T("%s"),usedLine.lineArray[index].destination);
	    str=str.Left(10);
	    str.Remove(' ');
		ListItem.pszText=str.GetBuffer(str.GetLength());
		str.ReleaseBuffer();
		if (m_FlightListCtrl.InsertItem(&ListItem) != -1)
			 {
				LV_ITEM FlightInfo = {0};
				for(int j=1;j<9;j++)
				{ 
					FlightInfo.mask=LVIF_TEXT;
					FlightInfo.iItem=Findex;
					FlightInfo.iSubItem =j;
					switch(j)
					{
					case 1: {
							 str.Format(_T("%s"),usedLine.flightArray[index].flight[i].PlaneNum);
							 str=str.Left(6);
							 str.Remove(' ');
							 FlightInfo.pszText=str.GetBuffer(str.GetLength());
							 str.ReleaseBuffer();
							 break;
							}
					case 2: {
							 str.Format(_T("%s"),usedLine.flightArray[index].flight[i].PlaneType);
							 str=str.Left(6);
							 str.Remove(' ');
							 FlightInfo.pszText=str.GetBuffer(str.GetLength());
							 str.ReleaseBuffer();
							 break;
							}
                    case 3:{
						     str.Format("%d-%d-%d",
								usedLine.flightArray[index].flight[i].FlyTime.GetYear(),
								usedLine.flightArray[index].flight[i].FlyTime.GetMonth(),
								usedLine.flightArray[index].flight[i].FlyTime.GetDay());
							FlightInfo.pszText=str.GetBuffer(str.GetLength());
							str.ReleaseBuffer();
							break;
							}
					case 4: {
							str.Format("%d:%d:%d",
								usedLine.flightArray[index].flight[i].FlyTime.GetHour(),
								usedLine.flightArray[index].flight[i].FlyTime.GetMinute(),
								usedLine.flightArray[index].flight[i].FlyTime.GetSecond());
							FlightInfo.pszText=str.GetBuffer(str.GetLength());
							str.ReleaseBuffer();
							break;
							}
					case 5: {str.Format("%d",usedLine.flightArray[index].flight[i].FirCFreeNum);FlightInfo.pszText=str.GetBuffer(str.GetLength());str.ReleaseBuffer();break;}
					case 6: {str.Format("%.1f",usedLine.flightArray[index].flight[i].FirCP);FlightInfo.pszText=str.GetBuffer(str.GetLength());str.ReleaseBuffer();break;}
					case 7: {str.Format("%d",usedLine.flightArray[index].flight[i].TouCFreeNum);FlightInfo.pszText=str.GetBuffer(str.GetLength());str.ReleaseBuffer();break;}
					case 8: {str.Format("%.1f",usedLine.flightArray[index].flight[i].TouCP);FlightInfo.pszText=str.GetBuffer(str.GetLength());str.ReleaseBuffer();break;}
					}
					m_FlightListCtrl.SetItem(&FlightInfo);  
				}
			}
		Findex++;
	  }
	  if(usedLine.CompareDate(m_FlyDate,usedLine.flightArray[index].flight[i].FlyTime)<0)break;	
	}
 if(m_FlightListCtrl.GetItemCount()==0)MessageBox("对不起,今天的航班都已经起飞,请更换日期或航线查询,谢谢!");
}

void CAviationBSDlg::OnBooking() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	COleDateTime now=COleDateTime::GetCurrentTime();
	int LineIndex=m_LineCtrl.GetCurSel();     //选中航线的下标
	int index=m_FlightListCtrl.GetSelectionMark();
	if(-1==index){MessageBox("请选择你要订票的航班,谢谢!");return;}
	else 
	{
	  
      int FlightIndex=Index[index];			  
	  if(now>=usedLine.flightArray[LineIndex].flight[FlightIndex].FlyTime)
	  {MessageBox("抱歉,你所选的航班已经起飞!请选择其它航班,谢谢!");return;}
	  Index[6]=LineIndex;    //通过m_index数组将所选的航线及即航班传给订票对话框
	  Index[7]=FlightIndex;
	  if(usedLine.flightArray[LineIndex].flight[FlightIndex].FirCFreeNum==0&&
		 usedLine.flightArray[LineIndex].flight[FlightIndex].TouCFreeNum==0)
	
	     {
		  int temp=MessageBox("你所选择的航班已满员,是否愿意填写候补表格,一有空位将马上通知你!",
	                         NULL,MB_ICONQUESTION|MB_OKCANCEL);
		  if(IDOK==temp) //候补队列 
			{

			  CQueueDlg Dlg;
			  Dlg.DoModal();
			}
	     }
      else   //进行订票
	  {
		  CBookingDlg Dlg;
		  Dlg.DoModal();
      m_FlightListCtrl.DeleteAllItems();
	  }
	}
}

void CAviationBSDlg::OnRefund() 
{
	// TODO: Add your control notification handler code here
	CRefund Dlg;
	Dlg.DoModal();
    if(m_FlightListCtrl.GetItemCount( )>0)m_FlightListCtrl.DeleteAllItems();
}

void CAviationBSDlg::OnQueue() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	int LineIndex=m_LineCtrl.GetCurSel();     //选中航线的下标
	int index=m_FlightListCtrl.GetSelectionMark();
	if(-1==index){MessageBox("请选择你要订票的航班,谢谢!");return;}
	else 
	{
	    if(m_FlightListCtrl.GetItemCount( )>0)m_FlightListCtrl.DeleteAllItems();
		int FlightIndex=Index[index];
		Index[6]=LineIndex;    //通过m_index数组将所选的航线及即航班传给订票对话框
		Index[7]=FlightIndex;
		CQueueDlg Dlg;
		Dlg.DoModal();
	}
}

⌨️ 快捷键说明

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