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

📄 seatnext.cpp

📁 机票座位预定系统(练习用)
💻 CPP
字号:
// SEATNEXT.cpp : implementation file
//

#include "stdafx.h"
#include "SeatRes.h"
#include "SEATNEXT.h"
#include "CONFIRM.h"
#include "Name.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSEATNEXT dialog


CSEATNEXT::CSEATNEXT(CWnd* pParent /*=NULL*/)
	: CDialog(CSEATNEXT::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSEATNEXT)
	m_EmpCount = 0;
	//}}AFX_DATA_INIT
	m_FlightNum = _T("");
}


void CSEATNEXT::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSEATNEXT)
	DDX_Control(pDX, IDC_EMP_LIST, m_empList);
	DDX_Text(pDX, IDC_EDIT_NUMBER, m_EmpCount);
	DDV_MinMaxInt(pDX, m_EmpCount, 0, 150);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSEATNEXT, CDialog)
	//{{AFX_MSG_MAP(CSEATNEXT)
	ON_BN_CLICKED(IDC_BTN_BACK, OnBtnBack)
	ON_BN_CLICKED(IDC_BTN_CONFIRM, OnBtnConfirm)
	ON_BN_CLICKED(IDC_QUIT, OnQuit)
	ON_BN_CLICKED(IDC_ASSIGN, OnAssign)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_LBN_SELCHANGE(IDC_EMP_LIST, OnSelchangeEmpList)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSEATNEXT message handlers


/********************************************************************
* NAME         :OnBtnConfirm
* FUNCTION     :Initialize the dialog class. 
				Initialize the empty seats list.
				Display the total count of the empty seats.
* INPUT        : none
* RETURN       : true
* CALL         :CSEATNEXT::LoadEmptyList()	//Load the empty seats list from "log.dat" file.
				CSEATNEXT::SetEmptyCount()	//Display the total count of the empty seats in the view.
				CSEATNEXT::SetTime()		//Display the system time on the view.
* DATE(ORG)    : 2005/01/05
********************************************************************/
BOOL CSEATNEXT::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	/*Load the new infomation from file. Add to the list box.*/
	SetDlgItemText(IDC_STATIC_FLIGHT,"Flight number : "+m_FlightNum+" ");
	LoadEmptyList();											
	SetEmptyCount();										

	/*Set timer to display "IDC_TIME" on time.*/
	SetTime();
	int iInstallResult;
	iInstallResult=SetTimer(1,1000,NULL);
	if(iInstallResult==0)
	{
		MessageBox("Can not install timer!");
	}
	return TRUE;
}

/********************************************************************
* NAME         :OnBtnBack
* FUNCTION     :Return to the top-level menu. 
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/05
********************************************************************/
void CSEATNEXT::OnBtnBack() 
{
	CDialog::OnOK();
}

/********************************************************************
* NAME         :OnQuit
* FUNCTION     :Quit from the system. 
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/05
********************************************************************/
void CSEATNEXT::OnQuit() 
{
	MessageBox("Thank you for using this system, good-bye!");
	CDialog::OnCancel();	
}

/********************************************************************
* NAME         :OnBtnConfirm
* FUNCTION     :Confirm all the changes about the seat assignment.
				Save all the actions on the view to " log.dat" file. 
* INPUT        : none
* RETURN       : none
* CALL         :CCONFIRM::DoModal()			//Ensure user's operation.
				CSEATNEXT::SaveAssignment()	//Save all the actions on the view to file.
				CSEATNEXT::LoadEmptyList()	//Reload the infomation of empty seats to list.
				CSEATNEXT::SetEmptyCount()	//Display the total count of the empty seats.
* DATE(ORG)    : 2005/01/11
********************************************************************/
void CSEATNEXT::OnBtnConfirm() 
{
	CCONFIRM confirm;
	
	/*Ensure user's operation.*/
	if( confirm.DoModal()== IDOK)
	{
		/*Save to file.*/
		SaveAssignment();

		/*Clear up the list box.*/
		m_empList.ResetContent();

		/*Reload the infomation of empty seats to the list box.*/
		LoadEmptyList();
		SetEmptyCount();
		MessageBox("Your manipulation succeeds!");
	}
}

/********************************************************************
* NAME         :OnAssign
* FUNCTION     :Change a seat's assignment from "unassigned" to "assigned".
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/13
********************************************************************/
void CSEATNEXT::OnAssign() 
{
	CString str;
	int Row = m_empList.GetCaretIndex();
	m_empList.GetText(Row,str);

	/*Assign a customer to the selected seat unassigned.*/
	if((str.Right(str.GetLength()-4-3-4-18)).Left(10) == "UNASSIGNED")
	{
		CName name;

		/*Enter user's name.*/
		if(name.DoModal() == IDOK)
		{
			m_empList.DeleteString(Row);
			str = str.Left(4+3+4) + "                    ASSIGNED" 
					+ "                  " + name.m_First +"."+ name.m_Last;
			m_empList.InsertString(Row,str);
		}
	}
	else
	{
		MessageBox("The seat you selected have been assigned!\nPlease select another one!");
	}
	return;
}

/********************************************************************
* NAME         :OnDelete
* FUNCTION     :Change a seat's assignment from "assigned" to "unassigned".
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/13
********************************************************************/
void CSEATNEXT::OnDelete() 
{
	CString str;
	int Row = m_empList.GetCaretIndex();
	m_empList.GetText(Row,str);
	/*Delete the selected assignment in the list.*/
	if((str.Right(str.GetLength()-4-3-4-18)).Left(10) == "UNASSIGNED")
	{
		
		MessageBox("The seat you selected haven't been assigned!\nPlease select another one!");	
	}
	else
	{
		m_empList.DeleteString(Row);
		str = str.Left(4+3+4+18) + "UNASSIGNED";
		m_empList.InsertString(Row,str);
	}
	return;
}

/********************************************************************
* NAME         :OnSelchangeEmpList
* FUNCTION     :When select the first row,set two button can't be used.
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/07
********************************************************************/
void CSEATNEXT::OnSelchangeEmpList() 
{
	int Row = m_empList.GetCaretIndex();
	if(Row == 0){
		GetDlgItem(IDC_ASSIGN)->EnableWindow(FALSE);
		GetDlgItem(IDC_DELETE)->EnableWindow(FALSE);
	}
	else{
		GetDlgItem(IDC_ASSIGN)->EnableWindow(TRUE);
		GetDlgItem(IDC_DELETE)->EnableWindow(TRUE);
	}
}

/********************************************************************
* NAME         :SetEmptyCount
* FUNCTION     :Display the total count of the empty seats in the view.
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/06
********************************************************************/
void CSEATNEXT::SetEmptyCount()
{
	m_EmpCount = m_empList.GetCount()-1;
	UpdateData(false);
}

/********************************************************************
* NAME         :LoadEmptyList
* FUNCTION     :Load empty seats list from "log.dat" file.
* INPUT        : none
* RETURN       : ture
* CALL         :
* DATE(ORG)    : 2005/01/09
********************************************************************/
BOOL CSEATNEXT::LoadEmptyList()
{
	/*Initialize the first row in the list box.*/
	m_empList.AddString("Seat Number          Assignment                    Name");

	/*Open the recorder file.*/
	char * pFileName = "log.dat";
	CFile file(pFileName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite);

	/*if frist use this system, initialize the file.*/
	DWORD len = file.GetLength();
	if(len == 0)
	{
		InitFile(file);
		len = file.GetLength();
	}
	
	/*Obtain the infomation of empty seats from file.*/
	char *info;
	info = (char *)malloc(len*sizeof(char));
	file.SeekToBegin();
	file.Read(info,len);
	CString str = _T(info);
	int nStart = str.Find(m_FlightNum);
	str = str.Right(str.GetLength()-nStart-4);
	nStart = str.Find("#");
	if(nStart != -1)
	{
		str = str.Left(nStart);
	}

	/*print to the list box.*/
	char sz[3];
	for(int i = 0;i<150 && str.GetLength() != 0;i++)
	{
		if(str.GetAt(0) == '*')
		{
			_itoa(150-i,sz,10);
			CString szStr = _T(sz);
			if((150-i)<100)
			{
				szStr = "0"+szStr;
				if((150-i)<10)
				{
					szStr = "0"+szStr;
				}
			}
			szStr = "    "+szStr+"    "+"                  UNASSIGNED";

			nStart = str.Find("%");
			str = str.Right(str.GetLength()-nStart-1);
			nStart = str.Find(",");
			if(str.GetAt(0) != '*')
			{
				szStr += "                  "+str.Left(nStart);
			}
			m_empList.InsertString(1,szStr);	//Print one row
			str = str.Right(str.GetLength()-nStart-2);
		}
		else
		{
			nStart = str.Find(",");
			str = str.Right(str.GetLength()-nStart-2);
		}
	}
	file.Close();
	free(info);
	return true;
}

/********************************************************************
* NAME         :InitFile
* FUNCTION     :When the first time to use this system, 
				you must initialize the "log.dat" file.
* INPUT        : CFile &file
* RETURN       : ture
* CALL         :
* DATE(ORG)    : 2005/01/08
********************************************************************/
BOOL CSEATNEXT::InitFile(CFile &file)
{
	CString info;
	int i,nCount = 0;
	file.SeekToBegin();
	
	/*Initialize the infomation of all the flights.*/
	info = "#012 ";
	nCount = 5;
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#123 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#456 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#292 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#444 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#008 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#126 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	info = "#631 ";
	file.Write(info,nCount);
	for(i = 0;i<150;i++)
	{
		info = "*%*, ";
		file.Write(info,nCount);
	}

	/*Add the flag of file-end to the file.*/
	info = "$$";
	file.Write(info,nCount);
	return true;
}

/********************************************************************
* NAME         :SaveAssignment
* FUNCTION     :Save all the actions on the view to " log.dat" file.
* INPUT        : none
* RETURN       : ture
* CALL         :
* DATE(ORG)    : 2005/01/15
********************************************************************/
BOOL CSEATNEXT::SaveAssignment()
{
	char * pFileName = "log.dat";
	CFile file(pFileName,CFile::modeReadWrite);
	DWORD len = file.GetLength();
	char *info;
	info = (char *)malloc(len*sizeof(char));

	/*Obtain the old assignment infomation from the file.*/
	file.SeekToBegin();
	file.Read(info,len);
	CString str = _T(info);
	CString strLeft,strRight;
	int nStart = str.Find(m_FlightNum);
	strLeft = str.Left(nStart+4);
	str = str.Right(str.GetLength()-nStart-4);
	strRight = _T(str);

	/*Replace the old infomation with the new one.*/
	int j = 150;
	for(int i=0 ; i < (m_empList.GetCount()-1) ; i++)
	{
		m_empList.GetText((m_empList.GetCount()-i-1),str);
		str = str.Right(str.GetLength()-4);
		j = j - atoi(str.Left(3));
		for(;j>0;j--)
		{
			nStart = strRight.Find(",");
			strLeft += strRight.Left(nStart+2);
			strRight = strRight.Right(strRight.GetLength()-nStart-2);
		}
		j = atoi(str.Left(3))-1;

		str = str.Right(str.GetLength()-3-4-18);
		if(str.Left(10) == "UNASSIGNED")
		{
			strLeft += strRight.Left(5);
		}else
		{
			str = str.Right(str.GetLength()-10-18);
			strLeft += "&%" + str +", ";
		}
		nStart = strRight.Find(",");
		strRight = strRight.Right(strRight.GetLength()-nStart-2);
	}
	str = strLeft + strRight;

	/*Delete redundant bytes in the string.*/
	nStart = str.Find("$$");
	str = str.Left(nStart+2);

	/*Save to the file.*/
	file.SetLength(0);
	file.Write(str,str.GetLength());
	file.Close();
	free(info);
	return true;
}

/********************************************************************
* NAME         :OnTimer
* FUNCTION     :When on time,Display the system time on the view.
* INPUT        : none
* RETURN       : none
* CALL         : CSEATNEXT::SetTime()
* DATE(ORG)    : 2005/01/4
********************************************************************/
void CSEATNEXT::OnTimer(UINT nIDEvent) 
{
	/*Display the system time on the view.*/
	SetTime();

	CDialog::OnTimer(nIDEvent);
}

/********************************************************************
* NAME         :SetTime
* FUNCTION     :Display the system time on the view.
* INPUT        : none
* RETURN       : none
* CALL         :
* DATE(ORG)    : 2005/01/4
********************************************************************/
void CSEATNEXT::SetTime()
{
	/*Display the system time on the view.*/
	CTime timeWrite;
	timeWrite = CTime::GetCurrentTime();
	CString str = timeWrite.Format("%d - %b - %y %H:%M:%S");
	SetDlgItemText(IDC_STATIC_TIME,str);
}

⌨️ 快捷键说明

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