?? shouxiandialog.cpp
字號:
// ShouXianDialog.cpp : implementation file
//
#include "stdafx.h"
#include "Book.h"
#include "ShouXianDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CShouXianDialog dialog
CShouXianDialog::CShouXianDialog(CWnd* pParent /*=NULL*/)
: CDialog(CShouXianDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CShouXianDialog)
m_jsbookno = 0;
m_hsstno = 0;
m_jsstuno = 0;
//}}AFX_DATA_INIT
}
void CShouXianDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShouXianDialog)
DDX_Control(pDX, IDC_LISTSTUDENT, m_showstudent);
DDX_Control(pDX, IDC_BUTTONGETSTNO, m_getstuno);
DDX_Control(pDX, IDC_BUTTONBORROWBK, m_borrowbook);
DDX_Control(pDX, IDC_BUTTONBACKBOOK, m_backbook);
DDX_Text(pDX, IDC_EDITJSBOOKNO, m_jsbookno);
DDX_Text(pDX, IDC_EDITHSSTNO, m_hsstno);
DDX_Text(pDX, IDC_EDITJSSTUNO, m_jsstuno);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShouXianDialog, CDialog)
//{{AFX_MSG_MAP(CShouXianDialog)
ON_BN_CLICKED(IDC_BUTTONBORROWBK, OnButtonborrowbk)
ON_BN_CLICKED(IDC_BUTTONGETSTNO, OnButtongetstno)
ON_BN_CLICKED(IDC_BUTTONBACKBOOK, OnButtonbackbook)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShouXianDialog message handlers
BOOL CShouXianDialog::OnInitDialog()
{
CDialog::OnInitDialog();
borrowBooksno=0;
sxstudentsource=new CStdDataSource(&theApp.m_DB);
sxbooksource=new CBookDataSouce(&theApp.m_DB);
unsigned i;
CODBCFieldInfo info;
m_showstudent.SetBkColor(RGB(200,250,230));//設置列表框的背景顏色
m_showstudent.SetTextBkColor(RGB(200,250,230));//設置列表框中文字的背景顏色
m_showstudent.SetTextColor(RGB(0,0,255));
sxstudentsource->Open();
m_showstudent.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
for(i=0;i<sxstudentsource->m_nFields;i++)
{
sxstudentsource->GetODBCFieldInfo(i,info);
m_showstudent.InsertColumn(i,info.m_strName,LVCFMT_LEFT,84);
}
sxstudentsource->Close();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CShouXianDialog::OnButtonborrowbk()//借書按鈕
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(!BookLate(m_jsstuno))//有借的書過期就不能借書了,必須先還書
{
return;
}
if(!FindBooks(m_jsbookno))//判斷欲借的書是否存在或借滿
{
return;
}
if(!BorrowBooks(m_jsstuno,m_jsbookno))
{
return;
}
else
AfxMessageBox("借書成功!");
UpdateData(FALSE);
}
void CShouXianDialog::OnButtongetstno()//還書的顯示按鈕
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
borrowBooksno=0;
if(!ShowStuList(m_hsstno))
return;
BookLate(m_hsstno);
UpdateData(FALSE);
}
void CShouXianDialog::OnButtonbackbook()//還書按鈕
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
BOOL flag=true;
sxstudentsource->Open();
if(AfxMessageBox("確實要刪除此數據嗎?", MB_YESNO)==IDYES)
{
for(int i=0;i<sxstudentsource->GetRecordCount();i++)
{
if(m_showstudent.GetItemState(i,LVIS_SELECTED)==LVIS_SELECTED)
{
if(sxstudentsource->m_column4==0)//如果用戶沒有借書
{
AfxMessageBox("該用戶沒有借書!");
sxstudentsource->Close();
return;
}
else if(borrowBooksno==1)//如果用戶借了一本書
{
sxstudentsource->Edit();
sxstudentsource->m_column4=0;
sxstudentsource->m_column5="";
sxstudentsource->m_column6=NULL;
sxstudentsource->m_column7=NULL;
sxstudentsource->Update();
flag=false;
}
else
{
sxstudentsource->Delete();
sxstudentsource->Requery();
flag=false;
}
}
if(!sxstudentsource->IsEOF())
sxstudentsource->MoveNext();
}
}
sxstudentsource->Close();
if(flag)
{
AfxMessageBox("請在列表中選中要刪除的數據!");
return;
}
borrowBooksno=0;
if(!flag)
ShowStuList(m_hsstno);
UpdateData(FALSE);
}
BOOL CShouXianDialog::FindBooks(int bookno)//查找要借的書是否已經借滿或是否存在
{
BOOL flag=true;
sxbooksource->Open();
while(!sxbooksource->IsEOF())
{
if(sxbooksource->m_column1==bookno)//存在這本書
{
if(sxbooksource->m_column9==sxbooksource->m_column8)//如果此書已經借滿
{
AfxMessageBox("該書已經借滿!");
sxbooksource->Close();
return false;
}
sxbooksource->Edit();
sxbooksource->m_column9+=1;//借書的數量加一
sxbooksource->Update();
flag=false;
break;
}
sxbooksource->MoveNext();
}
if(flag)
{
AfxMessageBox("圖書庫中沒有這本書!");
sxbooksource->Close();
return false;
}
sxbooksource->Close();
return true;
}
BOOL CShouXianDialog::BorrowBooks(int stuno, int bookno)//借書函數
{
CString bookname;//保存要借的書名
CString stuname;//保存用戶的姓名
CString stuclass;//保存用戶的專業
int stuborrowno=0;//保存用戶借書的數量
BOOL flag=true;//判斷用戶是否存在的標志
sxbooksource->Open();
while(!sxbooksource->IsEOF())//查找要借的書名
{
if(sxbooksource->m_column1==bookno)
{
bookname=sxbooksource->m_column2;
break;
}
sxbooksource->MoveNext();
}
sxbooksource->Close();
sxstudentsource->Open();
while(!sxstudentsource->IsEOF())//在用戶的數據表中查找用戶
{
if(sxstudentsource->m_column1==stuno)//如果用戶存在
{
if(sxstudentsource->m_column4==0)//還沒有借書的情況
{
sxstudentsource->Edit();
sxstudentsource->m_column4=bookno;
sxstudentsource->m_column5=bookname;
sxstudentsource->m_column6=CTime::GetCurrentTime();
sxstudentsource->Update();
sxstudentsource->Close();
return true;
}
stuborrowno++;//借書數量加一
stuname=sxstudentsource->m_column2;
stuclass=sxstudentsource->m_column3;
flag=false;
}
sxstudentsource->MoveNext();
}
if(flag)
{
AfxMessageBox("該學號不存在!");
sxstudentsource->Close();
return false;
}
if(stuborrowno==5)
{
AfxMessageBox("您已經借滿5本書!");
sxstudentsource->Close();
return false;
}
else
{
sxstudentsource->AddNew();
sxstudentsource->m_column1=stuno;
sxstudentsource->m_column2=stuname;
sxstudentsource->m_column3=stuclass;
sxstudentsource->m_column4=bookno;
sxstudentsource->m_column5=bookname;
sxstudentsource->m_column6=CTime::GetCurrentTime();
sxstudentsource->Update();
sxstudentsource->Requery();
}
sxstudentsource->Close();
return true;
}
BOOL CShouXianDialog::ShowStuList(int stuno)//還書的列表控件顯示函數
{
int j=0;
CString str;
BOOL flag=true;
sxstudentsource->Open();
m_showstudent.DeleteAllItems();
while(!sxstudentsource->IsEOF())
{
if(sxstudentsource->m_column1==stuno)
{
str.Format("%d",sxstudentsource->m_column1);
m_showstudent.InsertItem(j,str,0);
m_showstudent.SetItemText(j,1,sxstudentsource->m_column2);
m_showstudent.SetItemText(j,2,sxstudentsource->m_column3);
str.Format("%d",sxstudentsource->m_column4);
m_showstudent.SetItemText(j,3,str);
m_showstudent.SetItemText(j,4,sxstudentsource->m_column5);
m_showstudent.SetItemText(j,5,sxstudentsource->m_column6.Format("%Y-%m-%d"));
str.Format("%d",sxstudentsource->m_column7);
m_showstudent.SetItemText(j,6,str);
flag=false;
borrowBooksno++;
j++;
}
sxstudentsource->MoveNext();
}
sxstudentsource->Close();
if(flag)
{
AfxMessageBox("沒有該學號!");
return false;
}
return true;
}
BOOL CShouXianDialog::BookLate(int stuno)//判斷用戶借的書是否過期
{
BOOL flag=false;
long lateday;//借書的天數
CTimeSpan latetime;
sxstudentsource->Open();
while(!sxstudentsource->IsEOF())
{
if(sxstudentsource->m_column1==stuno)
{
if(sxstudentsource->m_column4==0)//沒有借書,不存在過期的情況
{
sxstudentsource->Close();
return true;
}
latetime=CTime::GetCurrentTime()-sxstudentsource->m_column6;//當前時間減去借書時的時間
lateday=latetime.GetTotalHours()/24;//把小時轉化為天數
if(lateday>60)//借書時間超過60天
{
sxstudentsource->Edit();
sxstudentsource->m_column7=lateday-60;
sxstudentsource->Update();
flag=true;
}
}
sxstudentsource->MoveNext();
}
if(flag)
{
AfxMessageBox("該用戶有書籍借閱過期!");
sxstudentsource->Close();
return false;
}
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -