VS2017/C++ 类class编写一个简单的图书借阅系统

C++ VS2017用类class编写一个简单的图书借阅系统
在这个程序中需要实现的目标:
1.显示当前图书情况,包括书名、价格、借阅次数、库存
2.具有借阅和归还功能
3.友好的界面
4.类似于命令行的控制方法

程序执行效果如下:
可以使用的命令有
display:显示当前图书状态
borrow:借出一本书
restore:归还一本书
end:程序结束(未列出)
以String类扫描输入的指令,将每个指令的功能封装为一个函数。
异常情况
错误输入:当输入无效命令时,提示指令错误
无库存:当无库存时,再使用borrow命令,提示无库存,无法借出
过多归还:当归还图书数量超过原本数量,提示无法归还,请联系管理员

在这里插入图片描述
程序代码:

#include<iostream>
#include<string>
using namespace std;
class Book
{
public:
 string bookname;
 double price;
 const int num = 5;	//设置图书数量
 int number=num, count=0;  //number-当前存书数量,count-借阅次数
 void display();
 void borrow();
 void restore();
};
void Book::display()
{
 cout << "Book name: " << bookname << endl;
 cout << "Price is:" << price << endl;
 cout << number << " in stock" << endl;
 cout << "The book is borrowed " << count << " time(s)" << endl;
}
void Book::borrow()
{
 if (number <= 0)
 {
  cout << "No Book left!"<<endl;
  return;
 }
 number -= 1;
 count += 1;
 cout << "This book is borrowed " << count << " time(s)" << endl;
 cout << number << " in stock" << endl;
}
void Book::restore()
{
 if (number >= num)
 {
  cout << "Can't handle! Please contact Librarian!";
  return;
 }
 else 
 {
  number += 1;
  cout << number << " in stock" << endl;
 }
}
int main()
{
 string opeartion="unknown";
 Book math;
 math.bookname = "Math is diffcult";
 math.price = 49.99;
 cout << "Command List:\ndisplay\tborrow\trestore" << endl;
 while (opeartion.compare("end") != 0)
 {
 
  cout << "Please command an opeartion: ";
  cin >> opeartion;
  cout << endl;
  if (opeartion.compare("display") == 0)
  {
   math.display();
   cout << endl;
   continue;
  }
  if (opeartion.compare("borrow") == 0)
  {
   math.borrow();
   cout << endl;
   continue;
  }
  if (opeartion.compare("restore") == 0)
  {
   math.restore();
   cout << endl;
   continue;
  }
  if (opeartion.compare("end") == 0)
  {
   cout << "Program end" << endl;
   continue;
  }
  cout << "Command Error!" << endl;
 }
 
}

目前功能较少,只是简单的基础结构的程序。后续更多功能以及更多命令,如管理员命令(对图书属性的操作等),读者类,密码登录等,陆续更新中。。。
内容为原创,如果想要实现其他有意思的功能,欢迎留言,软件代码持续修改中。奥里给!

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Blablabla...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值