zoukankan      html  css  js  c++  java
  • 银行储蓄程序(C++,simple)

    */
     * Copyright (c) 2016,烟台大学计算机与控制工程学院
     * All rights reserved.
     * 文件名:text.cpp
     * 作者:常轩
     * 微信公众号:Worldhello
     * 完成日期:2016年6月1日
     * 版本号:V1.0
     * 问题描述: 	实现一个简单的银行储蓄系统,承担活期用户的存款和取款业务 (只是初步的写出)
    1. 要求如下:
    
    1) 实现描述银行的类Bank,记录系统中现有哪些储户(可用数组或vector实现),定义了生成储户的函数append,
    按照账户删除储户的函数的deleteUser,按账号查询储户的函数query,并显示结果。
    
    2) 定义储户基类Account,具有属性账号,存款人姓名和余额,操作saveMoney、getoutMoney和showAccountInfo。
    虚函数saveMoney存储业务,虚函数getoutMoney处理取款业务,虚showAccountInfo函数显示储户所有信息。
    
    3) 定义储户派生类普通储户NormalAccount,实现操作saveMoney、getoutMoney和showAccountInfo,
    函数getoutMoney处理取款业务时余额不足不予取并提示信息,函数showAccountInfo显示普通储户的所有信息。
    
    4) 定义储户派生类高级储户VIPAccount,包含普通账户的所有信息,同时包含透支上限,透支总额,
    函数getoutMoney处理取款业务时超过透支上限不予取并提示信息,函数showAccountInfo显示高级储户的所有信息。
    
    5) 编写main函数,测试上述所要求的各种功能,即可以根据菜单命令增加,删除,和查询储户,
    以及储户存款和取款操作:
    
    
    
    
       a   增加账户时可选择增加普通账户和高级账户,普通账户帐号格式为“N001”,高级账户帐号格式为“V001”;
       b   根据输入的帐号删除和查询账户;
    
       c   储户取款和存款时要求输入帐号,根据帐号来操作账户。
     * 程序输入:无
     * 程序输出:见运行结果
     */
    
    #include<iostream>
    #include<string>
    using namespace std;
    //储户基类
    class Account 
    {
    public:
    	Account(string i,string n,double m=0);
    	virtual void saveMoney(double s);     //存钱
    	virtual void getoutMoney(double g);   //取钱
    	virtual void showAccountInfo();         //查询
    	string getId();
    protected:
    	string id;                                    //用户账号
    	string name;                                  //用户姓名
        double money;                                  //用户余额
    };
    Account::Account(string i,string n,double m)
    {
    	id=i;
    	name=n;
    	money=m;
    }
    string Account::getId()
    {
      return id;
    }
    void Account::showAccountInfo()         //V查询函数定义
    {
    	
    }
    void Account::getoutMoney(double g)         //V查询函数定义
    {
    	
    }
    void Account::saveMoney(double s)         //V查询函数定义
    {
    	
    }
    //普通储户类
    class NormalAccount:public Account{
    public:
    	NormalAccount(string i,string n,double m):Account(i,n,m)
    	{
          
    	}
    	void saveMoney(double s);             //N存钱
    	void getoutMoney(double g);           //N取钱
        void showAccountInfo();                 //N查询
    private:
    	/*string name;
    	string Nid;
    	double money;*/
    };
    
    void NormalAccount::saveMoney(double s)  //N存钱函数定义  
    {
        this->money=this->money+s;
    }
    void NormalAccount::getoutMoney(double g)//N取钱函数定义
    {
    	if(this->money<g)
    	{
    		cout<<"您的余额已不足!"<<endl;
    	}
    	else
    	{
    		this->money=this->money-g;
    		cout<<"取款成功!"<<endl;
    	}
    }
    void NormalAccount::showAccountInfo()            //N查询函数定义
    {
    	cout<<"姓名:"<<name<<endl<<"账号:"<<this->id<<endl<<"余额:"<<this->money<<endl;
    	
    }
    
    //VIP储户类
    class VipAccount:public Account{
    public:
    		VipAccount(string i,string n,double m):Account(i,n,m)
    	{
        
        touzhilimit=20000;                     //V透支上限两万
    	touzhimoney=0;                          //V透支总额(初始化为零
    	}                               //V构造函数(初始化透支上限与总额)
    	void saveMoney(double s);            //V存钱
    	void getoutMoney(double g);          //V取钱
        void showAccountInfo();                //V查询
    private:
    /*	string name;
    	string Vid;
    	double money;*/
    	double touzhilimit;                         //V透支上限
    	double touzhimoney;                         //V透支总额
    };
    /*
    VipAccount::VipAccount()                   //VIP储户类构造函数定义
    {
    	touzhilimit=20000;                     //V透支上限两万
    	touzhimoney=0;                          //V透支总额(初始化为零)
    }*/
    void VipAccount::saveMoney(double s)     //V存钱函数定义
    {
    	if(this->money=0)                                                                           
        {
    		if(touzhimoney>0)
    			touzhimoney=touzhimoney-s;
    		else
    			this->money=this->money+s;
    	}
    	else
            this->money=this->money+s;
     
    }
    void VipAccount::getoutMoney(double g)   //V取钱函数定义
    {
    	if(g>(this->money+(touzhilimit-touzhimoney)))
    	{
    		cout<<"您的余额不足,已达到透支上限!"<<endl;
    	}
    	else
    	{
    		this->money=this->money-g;
    		if(this->money<0)
    		{
    			touzhimoney=this->money*(-1)+touzhimoney;
    			this->money=0;
    		}
    		cout<<"取款成功!"<<endl;
    	}
    }
    void VipAccount::showAccountInfo()         //V查询函数定义
    {
    	cout<<"姓名:"<<this->name<<endl<<"账号:"<<this->id<<endl<<"余额:"<<this->money<<endl<<"透支总额:"<<this->touzhimoney<<"透支上限:"<<this->touzhilimit<<endl;
    
    }
    
    //银行类
    class Bank{
    public:
    	Bank();
    	void deleteuser();
    	void query();
    	void append();
    	void del();
    public:
    	int Index;
    	
    	Account *user[1000];
    	
    };
    Bank::Bank()
    {
    	for(int i=0;i<1000;i++)
    	{
    	      user[i] =NULL;
    	      Index=0;
    	}
    }
    void Bank::append()
    {
    	int choose;
    	string ID;
    	string Name;
    	double Money;
        cout<<"*****        你好,选择开普通账户请按——1        *****"<<endl<<"*****              选择开高级账户请按——2        *****"<<endl;
        cin>>choose;
    	if(choose==1)
    	{   
    		
    		cout<<"开户中..."<<endl;
    		cout<<"请输入账号,例如:N001或者N080"<<endl<<"账号:";
    		cin>>ID;
    		int tmp =1;
    		for(int i=0;i<Index;i++)
    		{
    			
    			if(user[i]->getId()==ID)
    			{
    				cout<<"该账号已存在!"<<endl;
    				tmp =2;
    			}
    		}
    		if(tmp==2)
    			return;
    		else
    		{
    		cout<<"姓名:";
    		cin>>Name;
    		cout<<"存入:";
    		cin>>Money;
    	    
    		
    		Account *n=new NormalAccount(ID,Name,Money);
    		cout<<"开户成功!"<<endl;
    		
            user[Index]=n;
    		cout<<endl<<"当前账户状态:"<<endl;
    		user[Index]->showAccountInfo();
    		Index++;
    		
    		}
    	}
        else
    	{   
    		cout<<"开户中..."<<endl;
    		cout<<"请输入账号,例如:V001或者V080"<<endl<<"账号:";
    		cin>>ID;
    		int tmp =1;
    		for(int i=0;i++;i<1000)
    		{
    			if(user[i]->getId()==ID)
    			{
    				cout<<"该账号已存在!"<<endl;
    				tmp =2;
    			}
    		}
    		if(tmp==2)
    			return;
    		cout<<"姓名:";
    		cin>>Name;
    		cout<<"存入:";
    		cin>>Money;
    		Account *n;
    		n=new VipAccount(ID,Name,Money);
    		cout<<"开户成功!"<<endl;
            user[Index]=n;
    		cout<<endl<<"当前账户状态:"<<endl;
    		user[Index]->showAccountInfo();
    		Index++;
    		
    	}
    }
    void Bank::query()
    {
    	string n;
    	cout << "请输入您要查询的用户账号:" << endl;
    	cin>>n;
    	int tem=1;
    	for(int i=0;i<Index;i++)
    	{
    		if(user[i]->getId() == n)
    		{
    			tem=2;
    			break;
    		}
    	}
    	if(tem==2)
          user[i]->showAccountInfo();
    	else
          cout<<"账户不存在!"<<endl;
    }
    void Bank::del()
    {
    	string n;
    	cout << "请输入要注销的用户账号" << endl;
    	cin>>n;
    	int tem=1;
    	int j;                            //用来记录应删除账户的下标
    	for(int i=0;i<Index;i++)
    	{   
            if(user[i]->getId()==n)
    		{
    			tem=2;
    			j=i;
    	        break;
    		}
    		
    	}
    	if(tem==2)
    	{
    		delete user[j];
    		Index--;
    		cout<<"注销成功!"<<endl;
    	}
    	else
            cout<<"没有这个账号"<<endl;
    }
    int menu_select();
    
    int main()
    {
    	Bank bank;
        char choice;
       
    	for(;;)
    	{
    		choice=menu_select();
    		if(choice==1)
                   bank.append();
    			   
    		else if(choice==2)
                   bank.del();
    		else if(choice==3)
    		{
    			string n;
    	          cout<<"请输入您要存取款的账号"<<endl;
                  cin>>n;
    			  int tem=1;
                  for(int i=0;i<1000;i++)
    			  {
    				  if(bank.user[i]->getId()==n)
    				  {
    					  tem=2;
    					  break;
    				  }
    			  }
    		      if(tem==1)
    			  {
    			    cout<<"账号输入错误"<<endl;
    			  }
    	       	  if(tem==2)
    			  {
    			        int choice;
    			
    							cout<<"1.取款"<<endl;
    							cout<<"2.存款"<<endl;
    							cout<<"请选择"<<endl;
    						
    			               cin>>choice;
    		               	if(choice == 1)
    						{
    			 				double jine;
    							cout<<"请输入取款金额:"<<endl;
    							cin >> jine;
    							bank.user[i]->getoutMoney(jine);
    							bank.user[i]->showAccountInfo();
    											
    						}
    			            if(choice == 2)
    						{
    							double qkuan;
    							cout<<"请输入存款金额:"<<endl;
    							cin>>qkuan;
    							bank.user[i]->saveMoney(qkuan);
    							bank.user[i]->showAccountInfo();
    						}
    			  }
    		
    			  
    		}
            else if(choice==4)
                  bank.query();
    		
        	else
    			break;
    		}
    	
    	return 0;
    }
    
    int menu_select()
    {
       char c;
       cout<<"***************************************************************"<<endl;
       cout<<"***********************银行储蓄系统 v1.0***********************"<<endl;
       cout<<"*                                                             *"<<endl;
       cout<<"*                         1.开户                              *"<<endl;
       cout<<"*                         2.销户                              *"<<endl;
       cout<<"*                         3.取款或存款                        *"<<endl;
       cout<<"*                         4.查询账户                          *"<<endl;
       cout<<"*                         5.退出                              *"<<endl;
       cout<<"***************************************************************"<<endl;
       cout<<"                    请输入(1-5)进行操作:"<<endl;
       do{
    	   cin.get(c);
       }while(c<'1'||c>'5');
       return c-48;
    
    }


    下面有两张简单的运行图片:




    结语:

          或许运行起来还是有BUG的,希望大家可以给我指正!

  • 相关阅读:
    最短路-dij
    链式前向星
    B树、B+树
    C++类
    差分约束
    数位DP
    Markdown编辑器:表格
    git使用笔记
    leetcode 162. 寻找峰值(二分)
    python matplotlib包的安装注意事项:报错——No such file or dir : tmp/matplotlib-xxxxxx
  • 原文地址:https://www.cnblogs.com/chxuan/p/8232191.html
Copyright © 2011-2022 走看看