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

📄 customer.cpp

📁 基于UML的饮料自动售货机系统设计该设计模拟自动售货机的工作原理和方式
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Customer.cpp : implementation file
//

#include "stdafx.h"
#include "Wumei.h"
#include "Customer.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCustomer dialog


CCustomer::CCustomer(CWnd* pParent /*=NULL*/)
	: CDialog(CCustomer::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCustomer)
	m_drink = -1;
	m_10 = _T("");
	m_5 = _T("");
	m_1 = _T("");
	m_05 = _T("");
	//}}AFX_DATA_INIT
	m_noDrink=FALSE;
}


void CCustomer::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCustomer)
	DDX_Radio(pDX, IDC_RADIO1, m_drink);
	DDX_Text(pDX, IDC_EDIT1, m_10);
	DDX_Text(pDX, IDC_EDIT3, m_5);
	DDX_Text(pDX, IDC_EDIT4, m_1);
	DDX_Text(pDX, IDC_EDIT5, m_05);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCustomer, CDialog)
	//{{AFX_MSG_MAP(CCustomer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCustomer message handlers

/*void CCustomer::OnCheck_pesi() 
{
	// TODO: Add your control notification handler code here
	
}

void CCustomer::OnCheck_cola() 
{
	// TODO: Add your control notification handler code here
	
}

void CCustomer::OnCheck_xuebi() 
{
	// TODO: Add your control notification handler code here
	
}

void CCustomer::OnCheck_tea() 
{
	// TODO: Add your control notification handler code here
	
}*/

void CCustomer::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CCustomer::OnOK() 
{
	// TODO: Add extra validation here
	    UpdateData();

		CoInitialize(NULL);
		_ConnectionPtr pCon(__uuidof(Connection));
		_RecordsetPtr pRed(__uuidof(Recordset));
		
		pCon->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=000;Initial Catalog=master;Data Source=.";
		pCon->Open("","","",adConnectUnspecified);

		if(m_drink!=-1 && (!m_10 && !m_5 && !m_1 && !m_05))
		{
			MessageBox("Please select money you want to pay...");
			m_drink=-1;
			UpdateData(FALSE);
		}//no money
		else
		{
			int money1=atoi(m_10);
			int money2=atoi(m_5);
			int money3=atoi(m_1);
			int money4=atoi(m_05);


			pRed=pCon->Execute("select moneyAmount from moneyInfo where moneyType='10元'",NULL,adCmdText);
			_variant_t num10=pRed->GetCollect("moneyAmount");

			pRed=pCon->Execute("select moneyAmount from moneyInfo where moneyType='5元'",NULL,adCmdText);
			_variant_t num5=pRed->GetCollect("moneyAmount");

			pRed=pCon->Execute("select moneyAmount from moneyInfo where moneyType='1元'",NULL,adCmdText);
			_variant_t num1=pRed->GetCollect("moneyAmount");

			pRed=pCon->Execute("select moneyAmount from moneyInfo where moneyType='0.5元'",NULL,adCmdText);
			_variant_t num05=pRed->GetCollect("moneyAmount");

			float totalMoney=10*money1 + 5*money2 + 1*money3 + 0.5*money4;

			if(m_drink==0)//choose pesi
			{	
				pRed=pCon->Execute("select dAmount from drinks where dName='百事可乐'",NULL,adCmdText);
				_variant_t dNum=pRed->GetCollect("dAmount");

				if(dNum.lVal>0)//have such drink
				{
					if(totalMoney<3.5)
					{
						MessageBox("You have not paid enough money!");
						
					}//not enough
					else
					{						
						if(money1==1 && money2==0 && money3==0 && money4==0 )//only  ¥10
						{
							if(num5.lVal>=1 && num1.lVal>=1 && num05.lVal>=1)
							{
								MessageBox("OK! Please take the drink and changes!");
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='1元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='0.5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+1 where moneyType='10元'",NULL,adCmdText);
								pCon->Execute("update drinks set dAmount=dAmount-1 where dName='百事可乐'",NULL,adCmdText);								
							}
							else if(num1.lVal>=6 && num05.lVal>=1)
							{
								MessageBox("OK! Please take the drink and changes!");
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-6 where moneyType='1元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='0.5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+1 where moneyType='10元'",NULL,adCmdText);
								pCon->Execute("update drinks set dAmount=dAmount-1 where dName='百事可乐'",NULL,adCmdText);								
							}
							else 
							{
								MessageBox("Sorry,there is no suitable changes...");
								MessageBox("Please take away your money!");								
							}
						
						}						
						
						else if(money1==0 && money2==1 && money3==0 && money4==0)
						{
							if(num1.lVal>=1 && num05.lVal>=1)//5-3.5=1+0.5
							{
								MessageBox("OK! Please take the drink and changes!");
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='1元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='0.5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+1 where moneyType='5元'",NULL,adCmdText);
								pCon->Execute("update drinks set dAmount=dAmount-1 where dName='百事可乐'",NULL,adCmdText);								
							}
							else 
							{
								MessageBox("Sorry,there is no suitable changes...");
								MessageBox("Please take away your money!");
							}
						}
						else if(money1==0 && money2==0 && money3==4 && money4==0)
						{
							if(num05.lVal>=1)
							{
								MessageBox("OK! Please take the drink and changes!");						
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='0.5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+4 where moneyType='1元'",NULL,adCmdText);
								pCon->Execute("update drinks set dAmount=dAmount-1 where dName='百事可乐'",NULL,adCmdText);								
							}
							else 
							{
								MessageBox("Sorry,there is no suitable changes...");
								MessageBox("Please take away your money!");
							}
						}
						else if(money1==0 && money2==0 && money3==3 && money4==1)
						{
							MessageBox("OK! Please take the drink!");						
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+1 where moneyType='0.5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+3 where moneyType='1元'",NULL,adCmdText);
								pCon->Execute("update drinks set dAmount=dAmount-1 where dName='百事可乐'",NULL,adCmdText);	
						}
						else
						{
							MessageBox("You should have smarter choice for money...");
							MessageBox("Please take away your money!");
						}
						CDialog::OnOK();
					}//enough money					
				}//have such drink
				else
				{
					m_noDrink=TRUE;
				}//no such drink
			}//choose pesi

			else if(m_drink==1)//choose cola
			{
				pRed=pCon->Execute("select dAmount from drinks where dName='可口可乐'",NULL,adCmdText);
				_variant_t dNum=pRed->GetCollect("dAmount");

				if(dNum.lVal>0)//have such drink
				{
					if(totalMoney<3)
					{
						MessageBox("You have not paid enough money!");
					}//not enough
					else
					{						
						if(money1==1 && money2==0 && money3==0 && money4==0 )//only  ¥10
						{
							if(num5.lVal>=1 && num1.lVal>=2 )/*10-3=5+1*2*/								
							{
								MessageBox("OK! Please take the drink and changes!");
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-1 where moneyType='5元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount-2 where moneyType='1元'",NULL,adCmdText);
								pCon->Execute("update moneyInfo set moneyAmount=moneyAmount+1 where moneyType='10元'",NULL,adCmdText);
								pCon->Execute("update drinks set dAmount=dAmount-1 where dName='可口可乐'",NULL,adCmdText);

⌨️ 快捷键说明

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