📄 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 + -