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

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

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;

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

#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++;
}
}
} 

相关推荐

  1. C++面对对象编程

    2024-07-16 06:12:03       46 阅读
  2. C++面向对象:智能指针讲解

    2024-07-16 06:12:03       31 阅读
  3. C#知识|上位面向对象编程时如何确定类?

    2024-07-16 06:12:03       25 阅读
  4. C++ 结构体(面向对象编程

    2024-07-16 06:12:03       58 阅读
  5. CC++ 编程面向对象编程

    2024-07-16 06:12:03       44 阅读
  6. 三、C#面向对象编程(类与对象

    2024-07-16 06:12:03       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 06:12:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 06:12:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 06:12:03       58 阅读
  4. Python语言-面向对象

    2024-07-16 06:12:03       69 阅读

热门阅读

  1. Python应用—车辆统计(Opencv)

    2024-07-16 06:12:03       23 阅读
  2. 浅谈为什么需要树链剖分

    2024-07-16 06:12:03       20 阅读
  3. 轨迹简化算法

    2024-07-16 06:12:03       22 阅读
  4. VisualTreeHelper.GetChildrenCount

    2024-07-16 06:12:03       20 阅读
  5. 使用Docker Compose进行多容器应用部署

    2024-07-16 06:12:03       23 阅读
  6. leetcode-22. 括号生成

    2024-07-16 06:12:03       24 阅读
  7. docker使用教学

    2024-07-16 06:12:03       22 阅读
  8. docker build 建立镜像,多出很多 none 的中间层镜像

    2024-07-16 06:12:03       29 阅读
  9. React Native: 构建原生级移动应用的跨平台框架

    2024-07-16 06:12:03       28 阅读
  10. 克隆上游仓库后想切换远程仓库为派生仓库

    2024-07-16 06:12:03       25 阅读
  11. Redis的哨兵和集群实现高可用

    2024-07-16 06:12:03       23 阅读