C++编程逻辑讲解step by step:面向对象ATM机,网上购书

题目一、编写ATM机类,包括账号密码验证,存钱取钱、改密码等功能。

cpp 复制代码
class AccountItem
{ public:
    AccountItem();
    AccountItem(string Anumber, string Password, string Owener,
const double Balance); 
    void Display();
    void Read(ifstream& s);
    // the following function was added for inventory update
    void Write(ofstream& s);
    short CheckNumber(string Anumber);
    string GetNumber();
    string GetPassword();
    void UpdatePassword(string pass);          //修改密码
    void DeductBalance(double pay);
    void SaveAccount(double count);            //存钱
    double GetBalance();
    string GetName();
    short IsNull(); 
private:
    string m_Anumber;
    string m_Password;
    string m_Name;
    double m_Balance;
    };
增加的成员函数为:
void AccountItem::UpdatePassword(string pass)   //修改密码
{ m_Password=pass;
};
void AccountItem::SaveAccount(double count)     //存钱count到帐户中
{ m_Balance += count;
};
    主函数修改为:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include "strclass.h"
#include "accitem.h"
#include "Accbook.h"
int main()
{ ifstream InputStream("Account.in");
  string AccountNo;
string AccPassword;
string newPassword1;
string newPassword2; 
double AccountCount;
  string ItemName;
  double OldAccountbook;
  Accountbook MyAccountbook;
  AccountItem FoundItem;
  string TransactionCode;
  string Count;
  double SaveCount;
  MyAccountbook.LoadAccountbook(InputStream);
  cout << "请输入帐号: ";
  cin >> AccountNo;
  FoundItem = MyAccountbook.FindItem(AccountNo);
  if (FoundItem.IsNull() == true)
  { cout << "帐号不存在." << endl;
    return 0;
  }
  cout << "请输入口令: ";
  cin >> AccPassword;
if (FoundItem.GetPassword()!=AccPassword)
  { cout << "口令错误!" << endl;
    return 0;
   }
   OldAccountbook=FoundItem.GetBalance();
   cout << "请选择交易代码:" << endl;
   cout << "G (取钱)" << endl;
   cout << "C (查询余额)" << endl;
   cout << "S (存钱)" << endl;
   cout << "U (修改密码)" << endl;
   cin >> TransactionCode;
   if (TransactionCode == "C" || TransactionCode == "c")
   { cout << "余额是: " << FoundItem.GetBalance()<<endl;
   }
   else if (TransactionCode == "G" || TransactionCode == "g")
   { cout << "请选择取钱的数量: "<<endl;
     cout << "1 (取100)" << endl;
     cout << "2 (取200)" << endl;
     cout << "5 (取500)" << endl;
     cout << "A (取1000)" << endl;
     cout << "B (取2000)" << endl;
  cin >> Count;
	 if (Count=="1") AccountCount=100.;
     else if (Count=="2") AccountCount=200.;
     else if (Count=="5") AccountCount=500.;
     else if (Count=="A") AccountCount=1000.;
     else if (Count=="B") AccountCount=2000.;
     else { cout<<"选择错误"<<endl; return 0;}           
  if (OldAccountbook<AccountCount)
  { cout<<"存款余额不够!"<<endl;   }   
	 else
	 { FoundItem.DeductBalance(AccountCount);
       MyAccountbook.UpdateItem(FoundItem);
       cout << "请取钱!" << endl;
       ofstream OutputStream("Account.in");
       MyAccountbook.StoreAccountbook(OutputStream);
  }
	}
	else if (TransactionCode == "S"|| TransactionCode == "s")
	{ cout<<"请输入存钱数量:";
	  cin>>SaveCount;
      FoundItem.SaveAccount(SaveCount);
      MyAccountbook.UpdateItem(FoundItem);
      ofstream OutputStream("Account.in");
      MyAccountbook.StoreAccountbook(OutputStream);
}
	else if (TransactionCode == "U" || TransactionCode == "u")
    { cout<<"请输入修改后的密码:";
	  cin>>newPassword1;
      cout<<"请再次输入修改后的密码:";
	  cin>>newPassword2;
      if (newPassword1==newPassword2)
	  { FoundItem.UpdatePassword(newPassword1);
        MyAccountbook.UpdateItem(FoundItem);
        ofstream OutputStream("Account.in");
        MyAccountbook.StoreAccountbook(OutputStream);
	    cout<<"密码修改成功!";
	   }
	 else 
	 cout<<"密码修改失败!";
}    
  return 0;

题目二、网上购书,可以下订单。

cpp 复制代码
#include <iostream.h>
#include "strclass.h"
#include "buy.h"
#include "book.h"
class order
{ public:
	order()
    { buyerID=0;
      ordercount++;
	  orderID=ordercount;           //定单编号自动加1
listcount=0;
	}	
	void setbuyid(int b_id)
	{ buyerID=b_id;
    }
    void buy_one_book(string b_id)
	{ orderlist[listcount]=b_id;
	  listcount++;
    }
    void display();
private:
static int ordercount;                //自动增加定单编号
int orderID;                          //订单编号
int buyerID;                          //购书人编号
string orderlist[20];                 //书号数组
int listcount;                        //购书数量 
};
void order::display()
{ int i;
cout<<"\n定单信息\n\n订单号:"<<orderID<<"\t"; 
  cout<<"购书人编号:"<<buyerID<<"\n";
cout<<" 所购图书书号:";
for (i=0;i<listcount;i++)
	cout<<orderlist[i]<<"\t";
    cout<<endl;
}
int order::ordercount=0;
void main()
{   int i=0,j=0;
int buyerid,flag;
	book *c[2];
    layfolk b1("林小茶",1,"北京",0);
    honoured_guest b2("王遥遥",2,.6,"上海",0);
    member b3("赵红艳",3,5,"广州",0);
    order o1[20];               //订单数组
    buyer *b[3]= {&b1,&b2,&b3};
book c1("","C++ programing","谭浩强","清华",25);
book c2("A2","data structure","许天风","北大",20);
c[0]=&c1;
c[1]=&c2;
cout<<"购书人信息:\n\n";
    for (i=0;i<3;i++)
b[i]->display();
cout<<"\n图书信息:\n\n";
for (i=0;i<2;i++)
    c[i]->display();
while (j<2)
    { cout<<"\n\n请输入购书人编号:";
      cin>>buyerid;
      flag=0;
  for (i=0;i<3;i++)
if (b[i]->getid()==buyerid) { flag=1;break;}
	  if (!flag) { cout<<"编号不存在"<<endl;}
      else 
{ b[i]->setpay(c[0]->getprice());
        b[i]->setpay(c[1]->getprice());
        cout<<endl<<"购书人需要付费:"<<b[i]->getpay()<<"\n\n";
        o1[j].setbuyid(b[i]->getid()); 
        o1[j].buy_one_book(c[0]->getbook_ID());
o1[j].buy_one_book(c[1]->getbook_ID());
        o1[j].display(); j++;
}
}
} 
相关推荐
用余生去守护19 分钟前
python报错系列(16)--pyinstaller ????????
开发语言·python
yuanbenshidiaos22 分钟前
c++---------数据类型
java·jvm·c++
数据小爬虫@24 分钟前
利用Python爬虫快速获取商品历史价格信息
开发语言·爬虫·python
向宇it26 分钟前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
莫名其妙小饼干42 分钟前
网上球鞋竞拍系统|Java|SSM|VUE| 前后端分离
java·开发语言·maven·mssql
十年一梦实验室1 小时前
【C++】sophus : sim_details.hpp 实现了矩阵函数 W、其导数,以及其逆 (十七)
开发语言·c++·线性代数·矩阵
taoyong0011 小时前
代码随想录算法训练营第十一天-239.滑动窗口最大值
c++·算法
最爱番茄味1 小时前
Python实例之函数基础打卡篇
开发语言·python
这是我581 小时前
C++打小怪游戏
c++·其他·游戏·visual studio·小怪·大型·怪物
Uu_05kkq1 小时前
【C语言1】C语言常见概念(总结复习篇)——库函数、ASCII码、转义字符
c语言·数据结构·算法