?? bookborrow.~cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "BookBorrow.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfmBookBorrow *fmBookBorrow;
//---------------------------------------------------------------------------
__fastcall TfmBookBorrow::TfmBookBorrow(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::FormShow(TObject *Sender)
{
// 輸入讀者查詢條件獲取焦點
edReaderCon->SetFocus();
// 設置借閱圖書明細Grid的標題
StringGrid1->Cells[0][0] = "狀態";
StringGrid1->Cells[1][0] = "編號";
StringGrid1->Cells[2][0] = "書名";
StringGrid1->Cells[3][0] = "借出時間";
StringGrid1->Cells[4][0] = "應還時間";
StringGrid1->Cells[5][0] = "出版社";
StringGrid1->Cells[6][0] = "書價";
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::btOkClick(TObject *Sender)
{
// 保存 新借 的圖書
AnsiString sql;
for(int i=1; i<StringGrid1->RowCount; i++)
{
if(!(StringGrid1->Cells[0][i] == "新借" && StringGrid1->Cells[1][i].Length()>0))
{
continue;
}
sql = "insert into 圖書借閱(圖書編號, 讀者編號, 借閱時間, ";
sql += "應還時間, 續借次數, 操作員, 狀態) values ('";
sql += StringGrid1->Cells[1][i] + "','";
sql += edReaderCon->Text + "','";
sql += StringGrid1->Cells[3][i] + "','";
sql += StringGrid1->Cells[4][i] + "',0,'操作員','新借')";
Query1->SQL->Clear();
Query1->SQL->Add(sql);
Query1->ExecSQL();
}
// 調用存儲過程, 減少圖書庫存
TQuery* pQuery = new TQuery(NULL);
pQuery->DatabaseName = "db";
pQuery->SQL->Add("exec sf_圖書借閱");
pQuery->ExecSQL();
delete pQuery;
m_nBorrowNum = 0;
m_nNum = 0;
// 清空數據
edReaderCon->Text = "";
edBookCon->Text = "";
// 輸入讀者查詢條件獲取焦點
edReaderCon->SetFocus();
// 清空借閱列表
for(int i=1; i<StringGrid1->RowCount; i++)
{
StringGrid1->Cells[0][i] = "";
StringGrid1->Cells[1][i] = "";
StringGrid1->Cells[2][i] = "";
StringGrid1->Cells[3][i] = "";
StringGrid1->Cells[4][i] = "";
StringGrid1->Cells[5][i] = "";
StringGrid1->Cells[6][i] = "";
}
Panel2->Caption = "";
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::btCancelClick(TObject *Sender)
{
m_nBorrowNum = 0;
m_nNum = 0;
// 清空數據
edReaderCon->Text = "";
edBookCon->Text = "";
// 輸入讀者查詢條件獲取焦點
edReaderCon->SetFocus();
// 清空借閱列表
for(int i=1; i<StringGrid1->RowCount; i++)
{
StringGrid1->Cells[0][i] = "";
StringGrid1->Cells[1][i] = "";
StringGrid1->Cells[2][i] = "";
StringGrid1->Cells[3][i] = "";
StringGrid1->Cells[4][i] = "";
StringGrid1->Cells[5][i] = "";
StringGrid1->Cells[6][i] = "";
}
Panel2->Caption = "";
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::btExitClick(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::FormClose(TObject *Sender,
TCloseAction &Action)
{
// 刪除窗體并回收空間
Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::edReaderConKeyPress(TObject *Sender, char &Key)
{
// 回車選擇讀者
if(Key == 13)
{
// 檢查是否存在未保存的記錄
for(int i=1; i<StringGrid1->RowCount; i++)
{
if(StringGrid1->Cells[0][i] == "新借")
{
Application->MessageBox("存在未保存的記錄","提示",MB_OK);
return;
}
}
AnsiString szCon, sql, szReadCode;
// 按照編號查詢
if(rbReaderCode->Checked)
szCon = "編號";
else
szCon = "條形碼";
szCon += "='" + edReaderCon->Text + "'";
sql = "select 姓名,類型,編號 from 讀者信息 where " + szCon;
Query1->SQL->Clear();
Query1->SQL->Add(sql);
Query1->Open();
// 選擇讀者
edName->Text = Query1->FieldByName("姓名")->AsString;
edType->Text = Query1->FieldByName("類型")->AsString;
szReadCode = Query1->FieldByName("編號")->AsString;
// 如果查詢條件不正確,返回重新查詢
if(edName->Text == "")
{
return;
}
// 選擇該類型讀者可以借閱的冊書
Query1->SQL->Clear();
sql = "select 圖書冊書 from 讀者類型 where 類型='";
sql += edType->Text + "'";
Query1->SQL->Add(sql);
Query1->Open();
edNum->Text = Query1->FieldByName("圖書冊書")->AsString;
// 將焦點跳到選擇圖書
edBookCon->SetFocus();
// -------------------
// 在借書列表中顯示已經借出的圖書
Query1->SQL->Clear();
sql = "select 圖書編號,借閱時間,應還時間 from 圖書借閱 where 狀態='未還' and 讀者編號='";
sql += szReadCode + "'";
Query1->SQL->Add(sql);
Query1->Open();
Query1->First();
m_nBorrowNum = 0;
// 清空借閱列表
for(int i=1; i<StringGrid1->RowCount; i++)
{
StringGrid1->Cells[0][i] = "";
StringGrid1->Cells[1][i] = "";
StringGrid1->Cells[2][i] = "";
StringGrid1->Cells[3][i] = "";
StringGrid1->Cells[4][i] = "";
StringGrid1->Cells[5][i] = "";
StringGrid1->Cells[6][i] = "";
}
// 序號寫入已借圖書列表
while(!Query1->Eof)
{
// 借出冊書加一
m_nBorrowNum ++;
AnsiString szBookCode = Query1->FieldByName("圖書編號")->AsString;
Table1->Filtered = false;
Table1->Filter = "編號='" + szBookCode + "'";
Table1->Filtered = true;
StringGrid1->Cells[0][m_nBorrowNum] = "未還";
StringGrid1->Cells[1][m_nBorrowNum] =
Table1->FieldByName("編號")->AsString;
StringGrid1->Cells[2][m_nBorrowNum] =
Table1->FieldByName("書名")->AsString;
StringGrid1->Cells[3][m_nBorrowNum] =
Query1->FieldByName("借閱時間")->AsString;
StringGrid1->Cells[4][m_nBorrowNum] =
Query1->FieldByName("應還時間")->AsString;
StringGrid1->Cells[5][m_nBorrowNum] =
Table1->FieldByName("出版社")->AsString;
StringGrid1->Cells[6][m_nBorrowNum] =
Table1->FieldByName("價格")->AsString;
// 下一記錄
Query1->Next();
}
}
// 設置狀態條冊書
Panel2->Caption = "共 " + IntToStr(m_nBorrowNum) + " 冊 ";
Panel2->Caption = Panel2->Caption + "本次 0 冊";
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::edBookConKeyPress(TObject *Sender,
char &Key)
{
// 回車選擇圖書
if(Key == 13)
{
// 檢查數量是否達到限制了
if(m_nBorrowNum >= StrToInt(edNum->Text))
{
Application->MessageBox("借書數量達到上限","提示",MB_OK);
return;
}
AnsiString szCon, sql, szBookType;
// 按照編號查詢
if(rbBookCode->Checked)
szCon = "編號";
else
szCon = "條形碼";
szCon += "='" + edBookCon->Text + "'";
Table1->Filtered = false;
Table1->Filter = szCon;
Table1->Filtered = true;
// 先檢查選擇的圖書在列表中是否存在
for(int i=1; i<StringGrid1->RowCount; i++)
{
if(StringGrid1->Cells[1][i] == Table1->FieldByName("編號")->AsString)
{
Application->MessageBox("圖書已借給該讀者","提示",MB_OK);
return;
}
}
szBookType = Table1->FieldByName("類型")->AsString;
// 獲得該類別圖書的借期
sql = "select 可借天數 from 圖書類型 where 類型名稱='";
sql += szBookType + "'";
// 選擇該類型讀者可以借閱的冊書
Query1->SQL->Clear();
Query1->SQL->Add(sql);
Query1->Open();
// 借出冊書加1
m_nBorrowNum ++;
m_nNum ++;
TDateTime dt;
dt = dt.CurrentDate(); // 借閱時間取當前時間
StringGrid1->Cells[0][m_nBorrowNum] = "新借";
StringGrid1->Cells[1][m_nBorrowNum] =
Table1->FieldByName("編號")->AsString;
StringGrid1->Cells[2][m_nBorrowNum] =
Table1->FieldByName("書名")->AsString;
// 借閱時間取當前時間
StringGrid1->Cells[3][m_nBorrowNum] = dt.DateString();
// 應還時間當前時間+可借天數
dt = dt + Query1->FieldByName("可借天數")->AsInteger;
StringGrid1->Cells[4][m_nBorrowNum] = dt.DateString();
StringGrid1->Cells[5][m_nBorrowNum] =
Table1->FieldByName("出版社")->AsString;
StringGrid1->Cells[6][m_nBorrowNum] =
Table1->FieldByName("價格")->AsString;
// -------------------------
// 設置狀態條冊書
Panel2->Caption = "共 " + IntToStr(m_nBorrowNum) + " 冊 ";
Panel2->Caption = Panel2->Caption + "本次 ";
Panel2->Caption = Panel2->Caption + IntToStr(m_nNum) + " 冊";
// -------------------------
// 清空圖書查詢條件
edBookCon->Text = "";
}
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::FormCloseQuery(TObject *Sender,
bool &CanClose)
{
// 關閉檢查是否存在未保存的記錄
for(int i=1; i<StringGrid1->RowCount; i++)
{
if(StringGrid1->Cells[0][i] == "新借")
{
if(Application->MessageBox("未保存數據,是否關閉?",
"提示",MB_YESNO) == IDYES)
{
CanClose = true;
}
else
CanClose = false;
break;
}
}
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -