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

📄 carpropertypage1.cpp

📁 用VC实现的一个简单的旅行预订系统
💻 CPP
字号:
// CarPropertyPage1.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "CarPropertyPage1.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNCREATE(CCarPropertyPage1, CPropertyPage)
IMPLEMENT_DYNCREATE(CCarPropertyPage2, CPropertyPage)


/////////////////////////////////////////////////////////////////////////////
// CCarPropertyPage1 property page

CCarPropertyPage1::CCarPropertyPage1() : CPropertyPage(CCarPropertyPage1::IDD)
{
	//{{AFX_DATA_INIT(CCarPropertyPage1)
	m_strCarLoc = _T("");
	m_strNumAvail = _T("");
	m_strNum = _T("");
	m_strPrice = _T("");
	//}}AFX_DATA_INIT
}

CCarPropertyPage1::~CCarPropertyPage1()
{
}

void CCarPropertyPage1::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCarPropertyPage1)
	DDX_Control(pDX, IDC_LIST_CAR_AM, m_listCarAM);
	DDX_Text(pDX, IDC_EDIT_CAR_LOC, m_strCarLoc);
	DDX_Text(pDX, IDC_EDIT_CAR_NUMAV, m_strNumAvail);
	DDX_Text(pDX, IDC_EDIT_CAR_NUMR, m_strNum);
	DDX_Text(pDX, IDC_EDIT_CAR_PRICE, m_strPrice);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCarPropertyPage1, CPropertyPage)
	//{{AFX_MSG_MAP(CCarPropertyPage1)
	ON_BN_CLICKED(IDC_BTN_CARA, OnBtnCara)
	ON_BN_CLICKED(IDC_BTN_CAR_M, OnBtnCarM)
	ON_NOTIFY(NM_CLICK, IDC_LIST_CAR_AM, OnClickListCarAm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CCarPropertyPage2 property page

CCarPropertyPage2::CCarPropertyPage2() : CPropertyPage(CCarPropertyPage2::IDD)
{
	//{{AFX_DATA_INIT(CCarPropertyPage2)
	m_strCarLoc = _T("");
	m_strPrice = _T("");
	m_strCustName = _T("");
	m_strCarLocRes = _T("");
	m_strNumRes = _T("");
	//}}AFX_DATA_INIT
}

CCarPropertyPage2::~CCarPropertyPage2()
{
}

void CCarPropertyPage2::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCarPropertyPage2)
	DDX_Control(pDX, IDC_LIST_CAR_QR, m_listCarQR);
	DDX_Text(pDX, IDC_EDIT_CARQ_LOC, m_strCarLoc);
	DDX_Text(pDX, IDC_EDIT_CARQ_PRICE, m_strPrice);
	DDX_Text(pDX, IDC_EDIT_CARR_CUSTNAME, m_strCustName);
	DDX_Text(pDX, IDC_EDIT_CARR_LOC, m_strCarLocRes);
	DDX_Text(pDX, IDC_EDIT_CARR_NUMRES, m_strNumRes);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCarPropertyPage2, CPropertyPage)
	//{{AFX_MSG_MAP(CCarPropertyPage2)
	ON_BN_CLICKED(IDC_BTN_CARQ, OnBtnCarq)
	ON_BN_CLICKED(IDC_BTN_CAR_RES, OnBtnCarRes)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



void CCarPropertyPage1::OnBtnCara() 
{
	// TODO: Add your control notification handler code here
	CString strSQL;
	UpdateData();
	if(m_strCarLoc.IsEmpty())
	{
		MessageBox("地点不能为空!");
		return;
	}
	if(m_strPrice.IsEmpty())
	{
		m_strPrice = "0.00";
		return;
	}
	if(m_strNum.IsEmpty())
	{
		m_strNum = "0";
		return;
	}
	if(m_strNumAvail.IsEmpty())
	{
		m_strNumAvail = "0";
		return;
	}
	strSQL = "insert into CARS values \
		('" + m_strCarLoc + "','" + m_strPrice + "','" \
		+ m_strNum + "','" + m_strNumAvail + "')";
	((CTRSApp*)AfxGetApp())->db.BeginTrans();
	try{
		((CTRSApp*)AfxGetApp())->db.ExecuteSQL(strSQL);
	}
	catch(CDBException e)
	{
		e.ReportError();
		((CTRSApp*)AfxGetApp())->db.Rollback();
		return;
	}
	((CTRSApp*)AfxGetApp())->db.CommitTrans();
	RefreshData();
}

void CCarPropertyPage1::OnBtnCarM() 
{
	// TODO: Add your control notification handler code here
	int i = m_listCarAM.GetSelectionMark();
	CString strSQL;
	if( i == -1 )
	{
		MessageBox("请选择一条记录!");
		return;
	}
	else
	{
		UpdateData();
		strSQL = "update CARS set price = " + m_strPrice + ",\
					numCars = " + m_strNum + ",\
					numAvail = " + m_strNumAvail + "\
					where location = '" + m_strCarLoc +"'";
	}
	((CTRSApp*)AfxGetApp())->db.BeginTrans();
	try{
		((CTRSApp*)AfxGetApp())->db.ExecuteSQL(strSQL);
	}
	catch(CDBException e)
	{
		e.ReportError();
		((CTRSApp*)AfxGetApp())->db.Rollback();
		return;
	}
	((CTRSApp*)AfxGetApp())->db.CommitTrans();
	RefreshData();
}

void CCarPropertyPage2::OnBtnCarq() 
{
	// TODO: Add your control notification handler code here
	CString strSQL;
	int choice = CDialog::GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO2);
	UpdateData();
	if( choice == IDC_RADIO1 )
	{
		if(m_strCarLoc.IsEmpty())
		{
			MessageBox("地点不能为空!");
			return;
		}
		strSQL = "select * from CARS where location ='" + m_strCarLoc + "'";
	}
	else if( choice == IDC_RADIO2 )
	{
		if(m_strPrice.IsEmpty())
		{
			MessageBox("价格不能为空!");
			return;
		}
		strSQL = "select * from CARS where price =" + m_strPrice;
	}
	else
	{
		strSQL = "select * from CARS";
	}
	CString location,price,numCars,numAvail;
	m_listCarQR.DeleteAllItems();

	CRecordset rs(&(((CTRSApp*)AfxGetApp())->db));
	rs.Open(CRecordset::forwardOnly,strSQL);
	int i = 0;
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("location",location);
		m_listCarQR.InsertItem(i,location);
		rs.GetFieldValue("price",price);
		m_listCarQR.SetItemText(i,1,price);
		rs.GetFieldValue("numCars",numCars);
		m_listCarQR.SetItemText(i,2,numCars);
		rs.GetFieldValue("numAvail",numAvail);
		m_listCarQR.SetItemText(i,3,numAvail);
		rs.MoveNext();
		i++;
	}
	rs.Close();
}

void CCarPropertyPage2::OnBtnCarRes() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString strSQL = "select numAvail from CARS where location ='" + m_strCarLocRes +"'";
	
	if(m_strCustName.IsEmpty())
	{
		MessageBox("客户名不能为空!");
		return;
	}
	if(m_strCarLocRes.IsEmpty())
	{
		MessageBox("地点不能为空!");
		return;
	}
	if(m_strNumRes.IsEmpty())
	{
		MessageBox("预定数量不能为空!");
		return;
	}
	CRecordset rs(&(((CTRSApp*)AfxGetApp())->db));

	rs.Open(CRecordset::forwardOnly,strSQL);

	CDBVariant numAvail;
	if( ! rs.IsEOF() )
	{	
		rs.GetFieldValue("numAvail",numAvail);
		if(numAvail.m_lVal - atoi(m_strNumRes) >= 0)
		{//更新CARS表
			strSQL.Format("update CARS set numAvail = %d where\
				location='"+ m_strCarLocRes+"'",numAvail.m_lVal - atoi(m_strNumRes));
			((CTRSApp*)AfxGetApp())->db.BeginTrans();
			try{
				((CTRSApp*)AfxGetApp())->db.ExecuteSQL(strSQL);
			}
			catch(CDBException e)
			{
				e.ReportError();
				((CTRSApp*)AfxGetApp())->db.Rollback();
				return;
			}
			((CTRSApp*)AfxGetApp())->db.CommitTrans();
			//添加RESERVATIONS表
			strSQL="insert into RESERVATIONS(custName,resvType)\
				values('"+m_strCustName+"',3)";

			((CTRSApp*)AfxGetApp())->db.BeginTrans();
			try{
				((CTRSApp*)AfxGetApp())->db.ExecuteSQL(strSQL);
			}
			catch(CDBException e)
			{
				e.ReportError();
				((CTRSApp*)AfxGetApp())->db.Rollback();
				return;
			}
			((CTRSApp*)AfxGetApp())->db.CommitTrans();
			RefreshData();
		}
		else
		{
			MessageBox("没有足够的出租车!");
		}
	}
	else
	{
		MessageBox("没有符合条件的出租车!");
	}
	rs.Close();
}

BOOL CCarPropertyPage1::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_listCarAM.InsertColumn(0,"地点");
	m_listCarAM.InsertColumn(1,"价格");
	m_listCarAM.InsertColumn(2,"汽车数");
	m_listCarAM.InsertColumn(3,"剩余出租车");
	RECT rect;
	m_listCarAM.GetWindowRect(&rect);
	int rectWidth = rect.right - rect.left;
	m_listCarAM.SetColumnWidth(0,rectWidth/4);
	m_listCarAM.SetColumnWidth(1,rectWidth/4);
	m_listCarAM.SetColumnWidth(2,rectWidth/4);
	m_listCarAM.SetColumnWidth(3,rectWidth/4);
	m_listCarAM.SetExtendedStyle(LVS_EX_FULLROWSELECT);//单行选中
	RefreshData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CCarPropertyPage2::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	CWnd::GetDlgItem(IDC_EDIT_CARQ_LOC)->EnableWindow(false);
	CWnd::GetDlgItem(IDC_EDIT_CARQ_PRICE)->EnableWindow(false);
	m_listCarQR.InsertColumn(0,"地点");
	m_listCarQR.InsertColumn(1,"价格");
	m_listCarQR.InsertColumn(2,"汽车数");
	m_listCarQR.InsertColumn(3,"剩余出租车");
	RECT rect;
	m_listCarQR.GetWindowRect(&rect);
	int rectWidth = rect.right - rect.left;
	m_listCarQR.SetColumnWidth(0,rectWidth/4);
	m_listCarQR.SetColumnWidth(1,rectWidth/4);
	m_listCarQR.SetColumnWidth(2,rectWidth/4);
	m_listCarQR.SetColumnWidth(3,rectWidth/4);
	m_listCarQR.SetExtendedStyle(LVS_EX_FULLROWSELECT);//单行选中
	RefreshData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCarPropertyPage1::RefreshData()
{
	CString location,price,numCars,numAvail;
	m_listCarAM.DeleteAllItems();

	CRecordset rs(&(((CTRSApp*)AfxGetApp())->db));
	rs.Open(CRecordset::forwardOnly,"select * from CARS");
	int i = 0;
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("location",location);
		m_listCarAM.InsertItem(i,location);
		rs.GetFieldValue("price",price);
		m_listCarAM.SetItemText(i,1,price);
		rs.GetFieldValue("numCars",numCars);
		m_listCarAM.SetItemText(i,2,numCars);
		rs.GetFieldValue("numAvail",numAvail);
		m_listCarAM.SetItemText(i,3,numAvail);
		rs.MoveNext();
		i++;
	}
	rs.Close();
}

void CCarPropertyPage2::RefreshData()
{
	CString location,price,numCars,numAvail;
	m_listCarQR.DeleteAllItems();

	CRecordset rs(&(((CTRSApp*)AfxGetApp())->db));
	rs.Open(CRecordset::forwardOnly,"select * from CARS");
	int i = 0;
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("location",location);
		m_listCarQR.InsertItem(i,location);
		rs.GetFieldValue("price",price);
		m_listCarQR.SetItemText(i,1,price);
		rs.GetFieldValue("numCars",numCars);
		m_listCarQR.SetItemText(i,2,numCars);
		rs.GetFieldValue("numAvail",numAvail);
		m_listCarQR.SetItemText(i,3,numAvail);
		rs.MoveNext();
		i++;
	}
	rs.Close();
}

void CCarPropertyPage2::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	CWnd::GetDlgItem(IDC_EDIT_CARQ_LOC)->EnableWindow(true);
	CWnd::GetDlgItem(IDC_EDIT_CARQ_PRICE)->EnableWindow(false);
}

void CCarPropertyPage2::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	CWnd::GetDlgItem(IDC_EDIT_CARQ_LOC)->EnableWindow(false);
	CWnd::GetDlgItem(IDC_EDIT_CARQ_PRICE)->EnableWindow(true);
}

void CCarPropertyPage1::OnClickListCarAm(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int i = m_listCarAM.GetSelectionMark();

	if( i == -1 )
	{
		MessageBox("请选择一条记录!");
		return;
	}
	else		//选中的记录出现在编辑框中
	{
		m_listCarAM.SetHotItem(i);//选中的行高亮
		m_strCarLoc = m_listCarAM.GetItemText(i,0);
		m_strPrice = m_listCarAM.GetItemText(i,1);
		m_strNum = m_listCarAM.GetItemText(i,2);
		m_strNumAvail = m_listCarAM.GetItemText(i,3);
		
		UpdateData(false);
	}
	*pResult = 0;
}

⌨️ 快捷键说明

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