Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

240 Project 1 DATABASE PROGRAMMING USING VC++.NET

CButton *pAdd,*pMod,*pDel,*pUpd, *pReActivate; if(m_pSet->m_Valid_Acc==”CA”)

{

SetDlgItemText(IDC_VALID,”UnOperational Account”); pAdd=(CButton*)GetDlgItem(IDC_SAVE); pAdd->EnableWindow(false);

pUpd=(CButton*)GetDlgItem(IDC_UPD);

pUpd->EnableWindow(false);

 

 

pMod=(CButton*)GetDlgItem(IDC MOD);

 

 

 

Y

pMod->EnableWindow(false);

L

 

 

 

F

pDel=(CButton*)GetDlgItem(IDC_DELETE);

pDel-> nableWindow(false);

 

 

 

M

 

pReActivate=(CButton*)GetDlgItem(IDCA _REACTIVATE);

pReActivate->EnableWindow(true);

E

 

 

T

 

 

 

CButton *chk1; chk1=(CButton*)GetDlgItem(IDC_CHECK1); chk1->SetCheck(false);

}

else

{

SetDlgItemText(IDC_VALID,”Operational Account”);

pAdd=(CButton*)GetDlgItem(IDC_SAVE); pAdd->EnableWindow(true);

pUpd=(CButton*)GetDlgItem(IDC_UPD); pUpd->EnableWindow(false);

pMod=(CButton*)GetDlgItem(IDC_MOD); pMod->EnableWindow(true);

Team-Fly®

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

241

 

 

 

pDel=(CButton*)GetDlgItem(IDC_DELETE); pDel->EnableWindow(true);

pReActivate=(CButton*)GetDlgItem(IDC_REACTIVATE); pReActivate->EnableWindow(false);

CButton *chk1; chk1=(CButton*)GetDlgItem(IDC_CHECK1); chk1->SetCheck(false);

}

CButton *pSaving,*pCurrent,*pChecking; pSaving=(CButton*)GetDlgItem(IDC_RADIO1); pCurrent=(CButton*)GetDlgItem(IDC_RADIO2); pChecking=(CButton*)GetDlgItem(IDC_RADIO3);

if(m_pSet->m_Type_account==”S”)

{

pSaving->SetCheck(true); pCurrent->SetCheck(false); pChecking->SetCheck(false);

}

else if(m_pSet->m_Type_account==”C”)

{

pSaving->SetCheck(false); pCurrent->SetCheck(true); pChecking->SetCheck(false);

}

else if(m_pSet->m_Type_account==”K”)

{

pSaving->SetCheck(false); pCurrent->SetCheck(false); pChecking->SetCheck(true);

}

}

The next operation that I will discuss is the deactivating and reactivating of accounts.

242 Project 1 DATABASE PROGRAMMING USING VC++.NET

The OnDeActivate Method

Currently, no records are permanently deleted from the table. Instead, the account is marked unoperational. To mark an account “unoperational”, you need to add the following code:

void CBankOperationView::OnDeActivate()

{

// If the user clicks on No then the deactivation process is cancelled

if (AfxMessageBox(“Are you sure you want to deactivate this account number?”, MB_YESNO) == IDNO)

return;

//If the user clicks on Yes then proceed to trace the account number in the //recordset and place it in edit mode

m_pSet->Edit();

long lCurrentAccountNum = m_pSet->m_Account_Number;

//Set the m_Valid_Acc field to “CA” to mark the record unoperational m_pSet->m_Valid_Acc=”CA”;

UpdateData(true); if(!m_pSet->Update())

AfxMessageBox(“Not Able to deactivate this account”); if(m_pSet->Requery()==0)

return;

AfxMessageBox(“The Account has been marked as Unused Account!!!”);

if(!m_pSet->IsBOF() && m_pSet->IsEOF()) m_pSet->MoveFirst();

while( m_pSet->m_Account_Number == lCurrentAccountNum || m_pSet->IsEOF())

{

m_pSet->MoveNext();

}

//Enabling and disabling controls

CButton *pAdd, *pUpd, *pMod, *pDel, *pReActivate; pAdd=(CButton*)GetDlgItem(IDC_SAVE); pAdd->EnableWindow(false);

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

243

 

 

 

pUpd=(CButton*)GetDlgItem(IDC_UPD); pUpd->EnableWindow(false);

pMod=(CButton*)GetDlgItem(IDC_MOD); pMod->EnableWindow(false);

pDel=(CButton*)GetDlgItem(IDC_DELETE); pDel->EnableWindow(false);

pReActivate=(CButton*)GetDlgItem(IDC_REACTIVATE); pReActivate->EnableWindow(true);

SetDlgItemText(IDC_VALID,”UnOperational Account”);

}

For reactivating accounts, all you have to do is set the m_Valid_Acc field to VA, indicating that the account is now operational. Then disable and enable the controls accordingly.

The Issue CheckBook Option

To issue check books, you need to have access to the Check_Book table at the back end. Therefore, you need to add a CRecordSet-based class. Follow the steps that you performed for adding the recordset class for the BankLogin table. Name the new recordset class CChkBook. Then, you need to add the code for processing the issuance of checks. I have given the code snippet for the message handler that is invoked when you select the check box next to the label Issue Check Book. The name of the handler is OnCheck1.

void CBankOperationView::OnCheck1()

{

//Checking whether the check box is selected or not; If not checked then return //without doing anything

CButton* pButton = (CButton*)GetDlgItem(IDC_CHECK1); if ( !(pButton->GetState() & 0x0003) )

return;

//If checked, then use the recordset pointer, which is pointing to the //recordset associated with the Check_Book table, to open the recordset and

244 Project 1 DATABASE PROGRAMMING USING VC++.NET

//trace for the selected account number. m_pBk=new CChkBook; if(!m_pBk->IsOpen())

m_pBk->Open(); if(m_pBk->IsBOF() && m_pBk->IsEOF ())

AfxMessageBox(“No Records in the Database...”); m_pBk->MoveLast();

// If account number is found, then store the starting check number; Add a new //record to the recordset; then refresh the view; mark the Hand_Over field as //‘C’.

int st=m_pBk->m_Start_Check_Number; m_pBk->AddNew();

UpdateData(true); m_pBk->m_Account_Number=m_pSet->m_Account_Number; GetDlgItemText(IDC_CURRDT,m_pBk->m_Date_of_Issue); m_pBk->m_Hand_Over=”C”;

//Based on the type of account, decide the number of checks to be issued and //accordingly set the start and the end check number values.

if(m_pSet->m_Type_account==”S”) //saving account

{

m_pBk->m_Number_of_Checks=20; m_pBk->m_Start_Check_Number=st+1; m_pBk->m_End_Check_Number=m_pBk->m_Start_Check_Number + 20;

}

if(m_pSet->m_Type_account==”C”) //Current account

{

m_pBk->m_Number_of_Checks=40; m_pBk->m_Start_Check_Number=st+1; m_pBk->m_End_Check_Number=m_pBk->m_Start_Check_Number + 40;

}

if(m_pSet->m_Type_account==”K”) //Checking account

{

m_pBk->m_Number_of_Checks=10;

m_pBk->m_Start_Check_Number=st+1;

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

245

 

 

 

m_pBk->m_End_Check_Number=m_pBk->m_Start_Check_Number + 50;

}

if(!m_pBk->Update())

AfxMessageBox(“Updation of Record Failed!!!”);

UpdateData(false);

AfxMessageBox(“Check Book has been issued”); if(m_pBk->Requery()==0)

return;

}

The operations that remain to be discussed are deposits, withdrawals, transfers, and commits.

Coding the Deposit/Withdrawal Form

To process any deposit or withdrawal operations, the user needs to click on the Dep/Wdrl button. When the user clicks on this button, the Deposit/Withdrawal form is displayed. For all transactions, the Transactions table is used. Therefore, to handle deposits, withdrawals, and transfer operations, you need to create a recordset for this table. Follow the same method as you did to add the recordset classes earlier. Name the class Tran_DW. Also, you need to refer to the Account_Detail table. Therefore, you also need to add another recordset class,

named Ref_AccNo, based on the Account_Detail table.

The following is the control flow when the Dep/Wdrl button is clicked:

1.You need to first trap the Click event of the button and add an event handler to it. Add the method named OnDeposit that will be invoked when the user clicks on the Dep/Wdrl button.

void CBankOperationView::OnDeposit()

{

CDeposit d1;

//This function invokes the OnInitDialog method of the CDeposit

//class

d1.DoModal();

}

2.The OnInitDialog function does some initialization, as illustrated in the following code:

246 Project 1 DATABASE PROGRAMMING USING VC++.NET

BOOL CDeposit::OnInitDialog()

{

CComboBox *cmb; cmb=(CComboBox*)GetDlgItem(IDC_ACCNUM);

//Current Date set as the transaction date

CTime t1=CTime::GetCurrentTime(); int dd=t1.GetDay();

int mm=t1.GetMonth(); int yy=t1.GetYear(); char str[30];

sprintf(str,”%2d/%2d/%2d”,dd,mm,yy); SetDlgItemText(IDC_TRANDT,str);

//Checking the number of items in the combo box; cleared if any and then //the recordset is searched for records that are valid. The corresponding //account numbers are added to the combo box.

if(cmb->GetCount()>0) cmb->Clear();

Ref_AccNo *m_pGet=new Ref_AccNo; if(!m_pGet->IsOpen ())

m_pGet->Open(); if(!m_pGet->IsBOF() && m_pGet->IsEOF())

m_pGet->MoveFirst(); while(!m_pGet->IsEOF())

{

char str[30]; if(m_pGet->m_Valid_Acc.CompareNoCase(“VA”) == 0)

{

sprintf(str,”%d”,m_pGet->m_Account_Number); cmb->AddString(str);

}

m_pGet->MoveNext();

}

return FALSE;

}

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

247

 

 

 

3.Once the Deposit/Withdrawal form is displayed, the user will select Deposit or Withdrawal. Corresponding methods are to be trapped and coded. The following code snippet shows how to do this:

void CDeposit::OnRdep()

//Function invoked when the user clicks on ‘Deposit’

{

CButton *pCash,*pCheck; pCash=(CButton*)GetDlgItem(IDC_CASH); pCheck=(CButton*)GetDlgItem(IDC_CHECK); pCash->EnableWindow(true); pCheck->EnableWindow(true);

}

void CDeposit::OnRwithdraw() //Invoked when the user clicks on ‘Withdraw’

{

CButton *pCash,*pCheck; pCash=(CButton*)GetDlgItem(IDC_CASH); pCheck=(CButton*)GetDlgItem(IDC_CHECK); pCash->EnableWindow(true); pCheck->EnableWindow(true);

}

4.After specifying what transaction is to happen, the user will have to select the mode of transaction, whether cash or check. Correspondingly, you will have to trap the Click event and add the required code. The following code shows how to accomplish this:

void CDeposit::OnCash()

//Invoked when the user chooses the Cash option

{

((CButton*)GetDlgItem(IDSAVE))->EnableWindow(true); ((CEdit*)GetDlgItem(IDC_AMTDEPW))->EnableWindow(true); ((CEdit*)GetDlgItem(IDC_CHECKNO))->EnableWindow(false);

}

When the Cash option is chosen, the Amount field is enabled.

void CDeposit::OnCheck()

//Invoked when the user chooses the Check option

248 Project 1 DATABASE PROGRAMMING USING VC++.NET

{

((CButton*)GetDlgItem(IDSAVE))->EnableWindow(true);

((CEdit*)GetDlgItem(IDC_AMTDEPW))->EnableWindow(true);

((CEdit*)GetDlgItem(IDC_CHECKNO))->EnableWindow(true);

}

When the Check option is chosen, the Amount and CheckNo fields are enabled.

5.After entering the required amount, the user will click on the Save button. Then, the transaction has to be processed and the corresponding table has to be updated. The following code snippet illustrates this implementation:

void CDeposit::OnSave()

{

Tran_DW *ptr;

Ref_AccNo *m_pGet=new Ref_AccNo; ptr=new Tran_DW;

if(!ptr->IsOpen()) ptr->Open();

if(!ptr->IsBOF() && ptr->IsEOF ()) ptr->MoveLast();

ptr->AddNew(); ptr->m_Account_Number=GetDlgItemInt(IDC_ACCNUM,NULL,true);

CButton *pradioDep,*pradioWdrl; pradioDep=(CButton*)GetDlgItem(IDC_RDEP); pradioWdrl=(CButton*)GetDlgItem(IDC_RWITHDRAW);

if(pradioDep->GetCheck()==BST_CHECKED) ptr->m_Transaction_Type=”DEP”;

if(pradioWdrl->GetCheck()==BST_CHECKED) ptr->m_Transaction_Type=”WTH”;

CButton *pCash,*pCheck; pCash=(CButton*)GetDlgItem(IDC_CASH); pCheck=(CButton*)GetDlgItem(IDC_CHECK);

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

249

 

 

 

if(pCash->GetCheck()==BST_CHECKED)

{

ptr->m_Transaction_Mode=”CAS”; ptr-

>m_Transaction_Amount=GetDlgItemInt(IDC_AMTDEPW,NULL,true); if(ptr->m_Transaction_Type==”WTH”)

{

if(!m_pGet->IsOpen ()) m_pGet->Open();

if(!m_pGet->IsBOF() && m_pGet->IsEOF()) m_pGet->MoveFirst();

while(!m_pGet->IsEOF())

{

if(m_pGet->m_Account_Number==ptr-

>m_Account_Number)

{

double WAmt=m_pGet->m_Balance-ptr-

>m_Transaction_Amount;

if(WAmt<500)

{

AfxMessageBox(“Insufficient Balance Amount -

Should

Deposit Money”); return;

}

else

{

ptr->m_Transaction_Status=”C”; m_pGet->Edit(); m_pGet->m_Balance=m_pGet->m_Balance-

ptr-

>m_Transaction_Amount; m_pGet->Update(); m_pGet->Requery(); AfxMessageBox(“Balance has been

Updated”);

break;

}