?? choosefood.cpp
字號:
// ChooseFood.cpp : implementation file
//
#include "stdafx.h"
#include "qq.h"
#include "ChooseFood.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChooseFood dialog
CChooseFood::CChooseFood(CWnd* pParent /*=NULL*/)
: CDialog(CChooseFood::IDD, pParent)
{
//{{AFX_DATA_INIT(CChooseFood)
m_PaidType = -1;
//}}AFX_DATA_INIT
m_DefaultSum=0.00f;
bStriked=false;
}
void CChooseFood::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChooseFood)
DDX_Control(pDX, IDC_PAID, Paid);
DDX_Control(pDX, IDC_SUM, Sum);
DDX_Control(pDX, IDC_REBATE, Rebate);
DDX_Control(pDX, IDC_FOODBILLLIST, FoodBillList);
DDX_Control(pDX, IDC_FOODNUM, FoodNum);
DDX_Control(pDX, IDC_FOODLIST, FoodList);
DDX_Control(pDX, IDC_BILLCODE, BillCode);
DDX_Control(pDX, IDC_HANDCODE, HandCode);
DDX_Radio(pDX, IDC_RADIO1, m_PaidType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChooseFood, CDialog)
//{{AFX_MSG_MAP(CChooseFood)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_UPDATEDATA, OnUpdatedata)
ON_BN_CLICKED(IDC_RADIO1, OnRadio)
ON_BN_CLICKED(IDC_RADIO2, OnRadio)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChooseFood message handlers
BOOL CChooseFood::OnInitDialog()
{
CDialog::OnInitDialog();
HICON m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME2);
this->SetIcon(m_hIcon,true);//設置對話框圖標
// TODO: Add extra initialization here
if(!m_FoodInfoSet.Open())
{
MessageBox("打開數據庫失敗!","數據庫錯誤",MB_OK);
return false;
}
m_FoodInfoSet.MoveFirst();
while (!m_FoodInfoSet.IsEOF())
{
CString strFoodList="",strFoodPrice="";
strFoodList=m_FoodInfoSet.m_FoodName;
strFoodList=strFoodList+" ";
strFoodPrice.Format("%5.0f",m_FoodInfoSet.m_FoodPrice);
strFoodList=strFoodList+strFoodPrice;
FoodList.AddString(strFoodList);
m_FoodInfoSet.MoveNext();
}
m_FoodInfoSet.Close();
//產生帳單號碼
CCreateBillCode billcode;
BillCode.SetWindowText(billcode.GenereBillCode());
//將錢的總數設置為0
Sum.SetWindowText("0");
///Paid無效話
Paid.SetWindowText("0");
Paid.EnableWindow(FALSE);
//設置缺省折扣率
Rebate.SetWindowText("1");
bStriked=false;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CChooseFood::OnAdd()
{
// TODO: Add your control notification handler code here
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
///////////////////////添加一個項目模塊
int nCurSel=FoodList.GetCurSel();
if(nCurSel==-1)
{
MessageBox("請選擇至少一種菜!","選擇錯誤",MB_OK);
return;
}
CString strFoodNum;
FoodNum.GetWindowText(strFoodNum);
if(strFoodNum==""||strFoodNum=="0")
{
MessageBox("數量至少要為一","選擇錯誤",MB_OK);
return;
}
CString strFoodName="";
FoodList.GetText(nCurSel,strFoodName);//得到文本
char* s=strFoodName.GetBuffer(strFoodName.GetLength());
char* str=strtok(s," ");//得到菜的名字
bool bSame=false;
for(int i=0;i<FoodBillList.GetCount();i++)
{
CString strTemp="";
FoodBillList.GetText(i,strTemp);//得到文本
char* temp=strTemp.GetBuffer(strTemp.GetLength());//得到緩沖區字符
char* bSameText=strtok(temp," ");
if(*bSameText==*str)//找到一個相同的項目
{
bSame=true;
MessageBox("你已經選了這個食品!","選擇錯誤",MB_OK);
return;
}
// strTemp.ReleaseBuffer(-1);
}
if(!bSame)//若沒有相同的項目則添加新的記錄
{
strFoodName=str;//構造顯示文本
strFoodName=strFoodName+" "+strFoodNum;
FoodBillList.AddString(strFoodName);//添加字符竄
}
/////////////////////添加一個項目完成
////////////////////////////////////////////////////////////////////
/*########################計算當前應付的總款#########################
####################################################################*/
float MoneySum=0;//總錢數目
for( i=0;i<FoodBillList.GetCount();i++)
{
//取得當前文本
CString BillListText="";
FoodBillList.GetText(i,BillListText);//取得文本
//將取得的文本進行轉換(char*)
char* billlistText=BillListText.GetBuffer(BillListText.GetLength());
char* FoodNameInBillList,*FoodNumInBillList;
//分別取得對應的數據
FoodNameInBillList=strtok(billlistText," ");//食物名字
FoodNumInBillList=strtok(NULL," "); //食物數量
//根據菜單的名字查找相應的價錢
CString SQLstr;
SQLstr="select * from FoodInfo where FoodName='";
SQLstr=SQLstr+FoodNameInBillList;
SQLstr=SQLstr+"'";
if(!m_FoodInfoSet.Open(AFX_DB_USE_DEFAULT_TYPE,SQLstr))
{
MessageBox("打開數據庫失敗!","數據庫錯誤",MB_OK);
return;
}
float nMoney=m_FoodInfoSet.m_FoodPrice;//得到價錢
m_FoodInfoSet.Close();//關閉數據庫
//////////////////////////////////////////////////////////////////////////
///計算總數
long FoodNum=0;
FoodNum=atoi(FoodNumInBillList);//食物數量
MoneySum=MoneySum+nMoney*FoodNum;
}
////將計算得到的總數再乘以折扣率
m_DefaultSum=MoneySum;//設置缺省的總數
float rebate=0.00f;
CString strText;
Rebate.UpdateData(true);
Rebate.GetWindowText(strText);
if(strText=="")
{
MessageBox("請輸入折扣率!","數據為空錯誤",MB_OK);
return;
}
char* sTemp=strText.GetBuffer(strText.GetLength());
rebate=atof(sTemp);
MoneySum=m_DefaultSum*rebate;//乘以折扣率
CString strSum="";
strSum.Format("%6.00f",MoneySum);
Sum.SetWindowText(strSum);//顯示總數
//####################################################################
}
void CChooseFood::OnDelete()
{
// TODO: Add your control notification handler code here
int nCount=FoodBillList.GetCount();
if(nCount==0)
{//當前記錄為空
MessageBox("當前沒有刪除的項目!","記錄為空錯誤",MB_OK);
Sum.SetWindowText("0");
return;
}
int nCurSel=FoodBillList.GetCurSel();
if(nCurSel==-1)
{//沒有選擇一種菜
MessageBox("你應該選擇一種要刪除的菜!","數據為空",MB_OK);
return;
}
else
{
FoodBillList.DeleteString(nCurSel);
}
/////刪除以后從新計算帳總數
int nNum=FoodBillList.GetCount();
float nMoney=0.00f;
for(int i=0;i<nNum;i++)
{
CString strItemText="";
FoodBillList.GetText(i,strItemText);//得到項目文本
char* str=strItemText.GetBuffer(strItemText.GetLength());
char* strFoodName,*strFoodNum;
strFoodName=strtok(str," ");//得到菜的名字
strFoodNum=strtok(NULL," ");//得到菜的數目
CString SQLstr;
SQLstr="select * from FoodInfo where FoodName='";
SQLstr=SQLstr+strFoodName;
SQLstr=SQLstr+"'";
if(!m_FoodInfoSet.Open(AFX_DB_USE_DEFAULT_TYPE,SQLstr))
{
MessageBox("打開數據庫失敗!","數據庫錯誤",MB_OK);
return;
}
float nPrice=m_FoodInfoSet.m_FoodPrice;//得到價錢
m_FoodInfoSet.Close();//關閉數據庫
//////計算總數
long nFoodNum=atoi(strFoodNum);
nMoney=nMoney+nPrice*nFoodNum;
}
//顯示更新后的總數
m_DefaultSum=nMoney;//設置缺省的總數
CString strShowText,strReabte;
Rebate.GetWindowText(strReabte);
char* s=strReabte.GetBuffer(strReabte.GetLength());
double n=atof(s);
strShowText.Format("%6.00f",m_DefaultSum*n);
Sum.SetWindowText(strShowText);
}
void CChooseFood::OnUpdatedata()
{
// TODO: Add your control notification handler code here
////將計算得到的總數再乘以折扣率
float MoneySum=0.00f;
float rebate=0.00f;
CString strText;
Rebate.GetWindowText(strText);//得到折扣率文本
if(strText=="")
{
MessageBox("請輸入折扣率!","數據為空錯誤",MB_OK);
return;
}
char* sRebateTemp=strText.GetBuffer(strText.GetLength());
rebate=atof(sRebateTemp);//轉變折扣率
MoneySum=m_DefaultSum*rebate;
CString strSum="";
strSum.Format("%6.00f",MoneySum);
Sum.SetWindowText(strSum);//顯示總數
}
void CChooseFood::OnRadio()
{
// TODO: Add your control notification handler code here
this->UpdateData(true);
if(m_PaidType==0)
{
bStriked=true;
Paid.EnableWindow(true);
}
if(m_PaidType==1)
{
bStriked=false;
Paid.EnableWindow(false);
}
}
void CChooseFood::OnOK()
{//保存數據到數據庫中
// TODO: Add extra validation here
OnUpdatedata();//自動更新數據,防止因為忘記更新或者其他的原因產生誤操作
CString strHandCode;
HandCode.GetWindowText(strHandCode);
if(strHandCode=="")
{
MessageBox("請輸入手牌號!","數據為空錯誤",MB_OK);
return;
}
/////如果選擇的是"現結"方式,而已經付的金額為<0則提示輸入錯誤
CString strPaidTemp="";
Paid.GetWindowText(strPaidTemp);//得到已經付的款
if(bStriked)
{
if(strPaidTemp==""||strPaidTemp=="0")
{
MessageBox("你可能輸入錯誤,客人已經付的款應該大于0!!","輸入錯誤",MB_OK);
return;
}
}
//查找客人數據庫中是否存在這個手牌號
if(!m_GuestInfoSet.Open(AFX_DB_USE_DEFAULT_TYPE))
{
MessageBox("打開數據庫失敗!","數據庫錯誤",MB_OK);
return;
}
m_GuestInfoSet.MoveFirst();
bool bhas=false;
while(!m_GuestInfoSet.IsEOF())
{
if(m_GuestInfoSet.m_HandCode==strHandCode)
{
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
bhas=true;//存在這條記錄
break;
}
m_GuestInfoSet.MoveNext();//移動到下一條記錄
}
if(!bhas)
{
MessageBox("你輸入的手牌號不正確!","輸入消息錯誤",MB_OK);
m_GuestInfoSet.Close();//關閉數據庫
return;
}
m_GuestInfoSet.Close();//關閉數據庫
////////查詢完畢
//////////////////////////////////////////////////////////////////////////
/////將數據保存到相應的數據庫中
/////寫BillInfoSet表
if(!m_BillInfoSet.Open())
{//打開數據庫失敗處理異常
MessageBox("打開數據庫失敗!","數據庫錯誤",MB_OK);
return;
}
/////新增加一條帳單的記錄
m_BillInfoSet.AddNew();
/////數據填充
CString strBillCode,strSum,strPaid,strRebate;
BillCode.GetWindowText(strBillCode);
m_BillInfoSet.m_BillCode=strBillCode;//帳單號碼
m_BillInfoSet.m_HandCode=strHandCode;//客人手牌
Sum.GetWindowText(strSum);
char* s=strSum.GetBuffer(strSum.GetLength());
long nSum=atol(s);
m_BillInfoSet.m_Sum=nSum;//應付總數
Paid.GetWindowText(strPaid);
s=strPaid.GetBuffer(strPaid.GetLength());
long nPaid=atol(s);
m_BillInfoSet.m_Paid=nPaid;//實付總數
Rebate.GetWindowText(strRebate);
m_BillInfoSet.m_Rebate=strRebate;//折扣率
m_BillInfoSet.m_Striked=bStriked;//是否已經付款
m_BillInfoSet.Update();//數據更新
m_BillInfoSet.Close(); //關閉數據庫
//////////////////////////////////////////////////////////////////////////
// 開始寫BillContentInfo表,記錄這個帳單的具體消費情況
if(!m_BillContentInfoSet.Open())
{
MessageBox("打開數據庫失敗!","數據庫錯誤",MB_OK);
return;
}
m_BillContentInfoSet.AddNew();//新增一條記錄
CString strBillContent="";
for(int i=0;i<FoodBillList.GetCount();i++)
{
CString strTemp="";
FoodBillList.GetText(i,strTemp);
strBillContent=strBillContent+strTemp+",";
}
CString strBillcode;
BillCode.GetWindowText(strBillcode);
m_BillContentInfoSet.m_BillCode=strBillcode;
m_BillContentInfoSet.m_BillContent=strBillContent;
m_BillContentInfoSet.Update();//更新數據庫,將緩沖區的數據寫入數據庫中
m_BillContentInfoSet.Close();//關閉數據庫
CDialog::OnOK();
}
void CChooseFood::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -