📄 hoteldlg1.cpp
字号:
// hoteldlg1.cpp : implementation file
//
#include "stdafx.h"
#include "dbexe.h"
#include "hoteldlg1.h"
#include "hotelset.h"
#include "hoteladddlg.h"
#include "reservedlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Choteldlg dialog
Choteldlg::Choteldlg(CWnd* pParent /*=NULL*/)
: CDialog(Choteldlg::IDD, pParent)
{
//{{AFX_DATA_INIT(Choteldlg)
m_field = _T("");
m_relation = _T("");
m_content = _T("");
//}}AFX_DATA_INIT
}
void Choteldlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Choteldlg)
DDX_Control(pDX, IDC_LIST2, m_basic);
DDX_CBString(pDX, IDC_COMBO1, m_field);
DDX_CBString(pDX, IDC_COMBO2, m_relation);
DDX_CBString(pDX, IDC_COMBO3, m_content);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Choteldlg, CDialog)
//{{AFX_MSG_MAP(Choteldlg)
ON_BN_CLICKED(IDC_INSERT, OnInsert)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_UPDATA, OnUpdata)
ON_BN_CLICKED(IDC_SELECT, OnSelect)
ON_BN_CLICKED(IDC_RESERVE, OnReserve)
ON_NOTIFY(NM_CLICK, IDC_LIST2, OnClickList2)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Choteldlg message handlers
void Choteldlg::OnInsert()
{
// TODO: Add your control notification handler code here
m_database.Close();
Choteladddlg m_flightadd;
m_flightadd.DoModal();
RefreshData();
}
void Choteldlg::OnDelete()
{
int i=m_basic.GetHotItem();
CString strSQL;
if(i==-1){
MessageBox("please choose a record first!");
}
else{
CString keyid=m_basic.GetItemText(i,0);
strSQL.Format("delete from hotels where location='"+keyid+"'");//删除元组
m_database.ExecuteSQL(strSQL);
m_database.Close();
RefreshData();
} // TODO: Add your control notification handler code here
}
void Choteldlg::OnUpdata()
{
int i=m_basic.GetHotItem();
if(i==-1){
MessageBox("please choose a record first!");
}
else{
CString strSQL;
Choteladddlg m_flightadd;
m_flightadd.m_location=m_basic.GetItemText(i,0);
m_flightadd.m_price=atoi(m_basic.GetItemText(i,1));
m_flightadd.m_roomnum=atoi(m_basic.GetItemText(i,2));
m_flightadd.m_roomavail=atoi(m_basic.GetItemText(i,3));
strSQL.Format("delete from hotels where location='"+m_flightadd.m_location+"'");
m_database.ExecuteSQL(strSQL);
m_flightadd.DoModal();
Sleep(500);
RefreshData();
}// TODO: Add your control notification handler code here
}
void Choteldlg::OnSelect()
{
UpdateData(TRUE);
CString m_realfield;
if(!m_field.Compare("地点")){
m_realfield.Format("location");
p_query.Format("select * from hotels where %s%s'%s'",m_realfield,m_relation,m_content);
}
if(!m_field.Compare("价格")){
m_realfield.Format("price");
p_query.Format("select * from hotels where %s%s%d",m_realfield,m_relation,atoi(m_content));
}
if(!m_field.Compare("总房间数")){
m_realfield.Format("numrooms");
p_query.Format("select * from hotels where %s%s%d",m_realfield,m_relation,atoi(m_content));
}
if(!m_field.Compare("剩余房间数")){
m_realfield.Format("numavail");
p_query.Format("select * from hotels where %s%s%d",m_realfield,m_relation,atoi(m_content));
}
RefreshData(); // TODO: Add your control notification handler code here
}
void Choteldlg::OnReserve()
{
Creservedlg m_reserve;
int i=m_basic.GetHotItem();
if(i==-1){
MessageBox("please choose a record first!");
}
else{
m_reserve.customname=customname;
m_reserve.reservetype=2;
m_reserve.reservekey=m_basic.GetItemText(i,0);
CString strsql;
strsql.Format("delete from hotels where location='"+m_reserve.reservekey+"'");
if(!m_database.IsOpen()){
m_database.Open(_T("database1"));
}
m_database.ExecuteSQL(strsql);
int newnumavail=atoi(m_basic.GetItemText(i,3))-1;
strsql.Format("insert into hotels values(\'%s\',%d,%d,%d)",m_basic.GetItemText(i,0),atoi(m_basic.GetItemText(i,1)),atoi(m_basic.GetItemText(i,2)),newnumavail);
m_database.ExecuteSQL(strsql);
RefreshData();
m_reserve.insert();
MessageBox("您已经预定了一个房间,若想取消您的预定内容,请到预定信息栏中取消");
} // TODO: Add your control notification handler code here
}
void Choteldlg::OnClickList2(NMHDR* pNMHDR, LRESULT* pResult)
{
int i=m_basic.GetSelectionMark();
CString strSQL;
if(i==-1){
MessageBox("please choose a record first!");
}
else{
hotel_id=m_basic.GetItemText(i,0);
m_basic.SetHotItem(i);
}// TODO: Add your control notification handler code here
// *pResult = 0;
}
BOOL Choteldlg::OnInitDialog(void){
CDialog::OnInitDialog();
m_basic.InsertColumn(0,"地点");
m_basic.InsertColumn(1,"价格");
m_basic.InsertColumn(2,"总房间数");
m_basic.InsertColumn(3,"剩余房间数");
RECT rectbasic;
m_basic.GetWindowRect(&rectbasic);
int widbasic=rectbasic.right-rectbasic.left;
m_basic.SetColumnWidth(0,widbasic/4);
m_basic.SetColumnWidth(1,widbasic/4);
m_basic.SetColumnWidth(2,widbasic/4);
m_basic.SetColumnWidth(3,widbasic/4);
m_basic.SetExtendedStyle(LVS_EX_FULLROWSELECT);
p_query.Format("select* from hotels");
RefreshData();
return true;
}
void Choteldlg::RefreshData()
{
if(!m_database.IsOpen()){
m_database.Open(_T("database1"));
}
m_basic.DeleteAllItems();
Chotelset m_basicset(&m_database);
CString strsql;
m_basicset.Open(AFX_DB_USE_DEFAULT_TYPE,p_query);
CDBVariant varValue;
if(m_basicset.GetRecordCount()!=0)
m_basicset.MoveFirst();
else MessageBox("没有您需要的内容");
char buf[20];
int i=0;
while(!m_basicset.IsEOF())
{int temp=0;
m_basicset.GetFieldValue(temp,varValue);
m_basic.InsertItem(i,varValue.m_pstring->GetBuffer(1));//插入元组
m_basicset.GetFieldValue(1,varValue);
sprintf(buf,"%d",varValue.m_lVal); m_basic.SetItemText(i,1,buf);
m_basicset.GetFieldValue(2,varValue);
sprintf(buf,"%d",varValue.m_lVal); m_basic.SetItemText(i,2,buf);
m_basicset.GetFieldValue(3,varValue);
sprintf(buf,"%d",varValue.m_lVal); m_basic.SetItemText(i,3,buf);
m_basicset.MoveNext();
i++;
}
}
void Choteldlg::unreserve()
{
if(!m_database.IsOpen()){
m_database.Open(_T("database1"));
}
//strsql.Format("update cars set numavail=%d where location='"+key+"'",numavail+1);
Chotelset m_basicset(&m_database);
CString strsql;
p_query.Format("select * from hotels where location='"+key+"'");
m_basicset.Open(AFX_DB_USE_DEFAULT_TYPE,p_query);
CDBVariant varValue;
m_basicset.MoveFirst();
int temp=0;
m_basicset.GetFieldValue(temp,varValue);
CString location=varValue.m_pstring->GetBuffer(1);//
m_basicset.GetFieldValue(1,varValue);
int price=varValue.m_lVal;
m_basicset.GetFieldValue(2,varValue);
int carnum=varValue.m_lVal;
m_basicset.GetFieldValue(3,varValue);
int numavail=varValue.m_lVal+1;
strsql.Format("delete from hotels where location='%s'",key);
m_database.ExecuteSQL(strsql);
strsql.Format("insert into hotels values(\'%s\',%d,%d,%d)",location,price,carnum,numavail);
m_database.ExecuteSQL(strsql);
}
void Choteldlg::OnButton1()
{
CDialog::OnCancel(); // TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -