系统以菜单方式,具体要求如下:
1) 图书信息的录入.图书信息包括:登录号、书名、作者名、分类号、出版单位、出版时间、价格和库存量等。
2)图书信息的浏览。按照分类,显示图书信息与列表中。
3)查询和排序功能
a) 按书名查询
b) 按作者名查询
c) 按出版社查询
4)图书信息的删除与修改
5) 图书的借阅与归还。能根据借阅与归还情况修改库存量
6)保存数据的功能。保存所有数据到文件中,并且能够再次打开,显示到列表中。为各项操作功能设计一个菜单,程序运行后,用户通过菜单项选择要进行的操作项目。
数据结构
struct msg
{
int id; //登录号
string name;//书名
string author;//作者名
string classify;//分类号
string unit;//出版单位
int time;//出版时间
int price;//价格
int num;//库存量
};
图书信息功能录入
添加一个Dialog命名为DIALOG_INPUT,实现如下:
void CInputDlg::OnBnClickedButtonAdd()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE); //获取控件内容
if (m_id <= 0 || m_price <= 0 || m_num<= 0 || m_name.IsEmpty() || m_auther.IsEmpty() || m_classify.IsEmpty() || m_unit.IsEmpty())
{
MessageBox(TEXT("输入信息有误"));
return;
}
//需要包含#include "InfoFile.h"
CInfoFile file;
file.ReadDocline(); //读取商品信息
file.Addline(m_id,m_name,m_auther,m_classify,m_unit,m_time,m_num, m_price); //添加商品
file.WirteDocline(); //写文件
file.ls.clear(); //清空list的内容
MessageBox(_T("添加成功"));
m_id = 0;
m_name.Empty();
m_auther.Empty();
m_classify.Empty();
m_unit.Empty();
m_time = 0;
m_num = 0;
m_price = 0;
UpdateData(FALSE);
}
图书信息查询
void CSearchDlg::OnBnClickedButtonSearch()
{
int nType = -1;
UpdateData(TRUE);
// TODO: 在此添加控件通知处理程序代码
CInfoFile file;
//读取所有图书信息
file.ReadDocline();
CString str_content;
//获取查询条件
GetDlgItem(IDC_EDIT1)->GetWindowText(str_content);
//查询
if (IsDlgButtonChecked(IDC_RADIONAME))//被选中的单选框 书名
{
if(str_content == _T("请输入要查询的内容"))
{
AfxMessageBox(_T("请输入你要查询的的书名"));
return ;
}
nType = 1;
file.Search(str_content,1);
}
if (IsDlgButtonChecked(IDC_RADIOAUTHOR)) //作者
{
if(str_content == _T("请输入要查询的内容"))
{
AfxMessageBox(_T("请输入你要查询的的作者名"));
return ;
}
nType = 2;
file.Search(str_content,2);
}
if (IsDlgButtonChecked(IDC_RADIOUNIT)) //出版社
{
if(str_content == _T("请输入要查询的内容"))
{
AfxMessageBox(_T("请输入你要查询的的出版社"));
return ;
}
nType = 3;
file.Search(str_content,3);
}
UpdateInfo();
}
图书信息删除功能
//删除
void CSearchDlg::OnBnClickedButtonDelete()
{
int nItem = m_list3.GetNextItem (-1, LVNI_SELECTED); ; //取得当前行号
if(nItem == -1)
{
AfxMessageBox("请先选中你要删除的图书信息");
return ;
}
//m_list3.DeleteItem(nItem);//删除某行
CInfoFile file;
file.ReadDocSearchResult(0,nItem+1); //读取信息
ofstream ofs(_S_STOCK);//输出方式打开文件
////添加数据
int i = 0;
CString str;
ofs << "登录号|书名|作者名|类号|出版单位|出版时间|价格|库存量" << endl; //写入表头
for (list<SearchResult>::iterator it = file.search_ls.begin(); it != file.search_ls.end(); it++)
{
i++;
}
ofs.close();//关闭文件
UpdateInfo();
file.ReadDocline();
file.WirteAfterline(1, m_index);
}
图书信息归还
void CSearchDlg::OnBnClickedButtonReturn()
{
// TODO: 在此添加控件通知处理程序代码
int nItem = m_list3.GetNextItem (-1, LVNI_SELECTED); ; //获取选中行号
if(nItem == -1)
{
AfxMessageBox("请先选中你要的归还的图书信息");
return ;
}
CInfoFile file;
file.ReadDocSearchResult(2,nItem+1); //读取信息
ofstream ofs(_S_STOCK);//输出方式打开文件
////添加数据
int i = 0;
CString str;
int id = 0;
int num = 0;
ofs << "登录号|书名|作者名|类号|出版单位|出版时间|价格|库存量" << endl; //写入表头
for (list<SearchResult>::iterator it = file.search_ls.begin(); it != file.search_ls.end(); it++)
{
}
ofs.close();//关闭文件
//更新列表数据
UpdateInfo();
file.ReadDocline();
file.ReadDocSearchResult(0,0);
file.WirteAfterline(0, id,num);
}
图书信息借阅
void CSearchDlg::OnBnClickedButtonReturn()
{
// TODO: 在此添加控件通知处理程序代码
int nItem = m_list3.GetNextItem (-1, LVNI_SELECTED); ; //获取选中行号
if(nItem == -1)
{
AfxMessageBox("请先选中你要的归还的图书信息");
return ;
}
CInfoFile file;
file.ReadDocSearchResult(2,nItem+1); //读取信息
ofstream ofs(_S_STOCK);//输出方式打开文件
////添加数据
int i = 0;
CString str;
int id = 0;
int num = 0;
ofs << "登录号|书名|作者名|类号|出版单位|出版时间|价格|库存量" << endl; //写入表头
for (list<SearchResult>::iterator it = file.search_ls.begin(); it != file.search_ls.end(); it++)
{
}
ofs.close();//关闭文件
//更新列表数据
UpdateInfo();
file.ReadDocline();
file.ReadDocSearchResult(0,0);
file.WirteAfterline(0, id,num);
}
图书信息修改
void CSearchDlg::OnBnClickedButtonModify()
{
// TODO: 在此添加控件通知处理程序代码
int nItem = m_list3.GetNextItem (-1, LVNI_SELECTED); ; //取得当前行号
if(nItem == -1)
{
AfxMessageBox("请先选中你要的修改的图书信息");
return ;
}
m_list3.SetItemState(nItem,LVIS_SELECTED,LVIS_SELECTED);
CInfoFile file;
file.ReadDocSearchResult(3,0); //读取信息
////添加数据
int i = 0;
CString str;
for (list<SearchResult>::iterator it = file.search_ls.begin(); it != file.search_ls.end(); it++)
{
i++;
}
CString name;
CModifyDlg dlg;
dlg.DoModal();
UpdateInfo();
file.ReadDocline();
file.ReadDocSearchResult(0, 0);
for (list<SearchResult>::iterator it = file.search_ls.begin(); it != file.search_ls.end(); it++)
{
i++;
}
//更新所有图书信息
file.WirteAfterline(2, m_index,0, name);
}
显示并排序
void CSearchDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
//先把列表数据全部清除
m_list3.DeleteAllItems();
CInfoFile file;
//读取图书信息
file.ReadDocline();
int i = 0;
CString str;
for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
{
str.Format(_T("%d"), it->id);
m_list3.InsertItem(i, str);
int column = 1;
m_list3.SetItemText(i, column++, (CString)it->name.c_str());
m_list3.SetItemText(i, column++, (CString)it->author.c_str());
m_list3.SetItemText(i, column++, (CString)it->classify.c_str());
m_list3.SetItemText(i, column++, (CString)it->unit.c_str());
str.Format(_T("%d"), it->time);
m_list3.SetItemText(i, column++, str);
str.Format(_T("%d"), it->price);
m_list3.SetItemText(i, column++, str);
str.Format(_T("%d"), it->num);
m_list3.SetItemText(i, column++, str);
i++;
}
}
整体效果
录入信息
查询
1) 按书名查询
2)按作者名查询
3)按出版社名查询
如需完整的工程源代码可以私信获取。