银行管理系统

来源:互联网 发布:阿里云备案订单查询 编辑:程序博客网 时间:2024/06/10 00:31

# include <iostream.h>
# include <string.h>

class Account
{
 private:
  int ID;
  char * Name;
  char * accPWD;
  double Balance;
 public:
  Account();
  Account(int,const char * ,const char *,double);
  ~Account(){}
  void Deposit();
  void Withdraw();
  void ShowMe();
  char* getPWD();
  int getID();
 
};

class Bank
{
 private:
  Account * account[50];//数组存放Account类型指针,最多帐户数目50
  int maxID;
  int accNum;
 public:
  Bank();
  ~Bank(){}
  void Append();//开户
  void Delete();//销户
  void Query();//帐户的综合操作
};

Account::Account()
{
 ID = 0;
 strcpy(Name,"");
 strcpy(accPWD,"");
 Balance = 0;
}

Account::Account(int id,const char * name,const char * accpwd,double balance)//构造函数重载
{
 ID = id;
 Name = new char[20];
 strcpy(Name,name);
 accPWD=new char[20];
 strcpy(accPWD,accpwd);
 Balance = balance;
}

void Account::Deposit()//Deposit 存
{
 double number;

    cout<<"Please input depositing  number:";
    cin>>number;
    Balance = Balance + number;
    ShowMe();
    return;
}

void Account::Withdraw()//Withdraw取
{
    double number;
    cout<<"Please input withdraw number:";
    cin>>number;

    if(Balance<number)
 {
        cout<<"Sorry,now the balance of your accout is less than "<<number<<"."<<"So you can not withdraw!"<<endl;
         return ;
 }
    Balance = Balance - number;
    ShowMe();
     return  ;
}
void Account::ShowMe()
{
 cout<<"Account ID:"<<ID<<endl;
    cout<<"Name:"<<Name<<endl;
    cout<<"Balance:"<<Balance<<endl;
    cout<<endl;
}

int Account::getID()
{
 return ID;
}
char* Account::getPWD()
{
 return accPWD;
}

Bank::Bank()//构造函数
{
 for(int i=0;i<50;i++)
 {
      account[i] = NULL;
 }
    maxID = 0;
    accNum = 0;
}

void Bank::Append()
{
 if(accNum == 50)
 {
       cout<<"Sorry,the bank is full,so can not add new account!"<<endl;
       return;
 }
    int id = maxID;
    char * name = new char[20];
    char *accpwd=new char[20];
    double balance=0;

    cout<<"Please input the name of the account:";
    cin>>name;
    cout<<"Please input the password of the account:";
    cin>>accpwd;

    Account * acc = new Account(id,name,accpwd,balance);
    account[accNum] = acc;

    cout<<"Append successful!"<<endl<<endl;
    account[accNum]->ShowMe();

    maxID++;
    accNum++;

    return;
}

void Bank::Delete()
{
 int ID;
 char accPWD[20];
    cout<<"Please input the accout ID that you want to delete:";//操作检测,需要帐户和密码
    cin>>ID;

    int flag = 1;
    int i = 0;
    while((i<accNum)&&(flag))
 {
       if(ID == account[i]->getID())
    {
     int j=0;
       cout<<"Please input the account password that you want to query:";
    while(j!=3)
    {
       cin>>accPWD;
          if(strcmp(account[i]->getPWD(),accPWD)!=0)
    {
       cout<<"input the account password again";
         j++;
    }
       else
    {
       break;
    }
    }
    if(j==3)
    {
      cout<<"you have input the password 3 times,you can not query the account"<<endl;
      return;
    }
    flag = 0;
    }
       else
    {
          i++;
    }
   
 }

    if(flag)//flag ==1时
 {
       cout<<"The account does not exists!"<<endl<<endl;
       return; 
 }
    for(int j=i;j<accNum;j++)//将后面的帐户向前移
 {
        account[j] = account[j+1];
 }
    delete account[accNum-1];//释先前最后一个帐户的空间
    accNum--;//帐户数量减一
    cout<<"Delete successful!"<<endl<<endl;
     return;
}

void Bank::Query()
{
    int ID;
    char accPWD[20];
    cout<<"Please input the account ID that you want to query:";//操作检测,需要帐户和密码
    cin>>ID;
 
    int flag = 1;
    int i=0;
    while((i<accNum)&&(flag))
 {
       if(ID == account[i]->getID())
    {
       int j=0;
       cout<<"Please input the account password that you want to query:";
       while(j!=3)
    {
       cin>>accPWD;
          if(strcmp(account[i]->getPWD(),accPWD)!=0)
    {
       cout<<"input the account password again";
         j++;
    }
       else
    {
       break;
    }

    }
       if(j==3)
    {
        cout<<"you have input the password 3 times,you can not query the account"<<endl;
         return;
    }
       flag = 0;
   
    }
       else
    {
       i++;
    }
 }
    if(flag)
 {
       cout<<"The account does not exist!"<<endl<<endl;
       return;
 }

     account[i]->ShowMe();
     int choice = 0;
     while(choice!=3)
  {
        cout<<"   1:Deposit money"<<endl;
        cout<<"   2:Withdraw money"<<endl;
        cout<<"   3:Return"<<endl;
        cout<<"   Please input the choice:";
        cin>>choice;
        cout<<endl;
        switch(choice)
  {
           case 1:
           account[i]->Deposit();
           break;
           case 2:
           account[i]->Withdraw();
           break;
           case 3:
           break;
  }
  }
      return;
}

void main()
{
   Bank bank;
   int choice = 0;
   cout<<"Welcome to bank system"<<endl;
   while(choice != 4)
   {
      cout<<"1: Add an account"<<endl;
      cout<<"2: Delete an account"<<endl;
      cout<<"3: Query an account"<<endl;
      cout<<"4: Exit"<<endl<<endl;
      cout<<"Please input your choice:";
      cout<<endl;
      cin>>choice;
      switch(choice)
   {
         case 1:
          bank.Append();
          break;
         case 2:
          bank.Delete();
          break;
         case 3:
           bank.Query();
           break;
         case 4:
           break;
   }
      cout<<endl;
 }
 return;

原创粉丝点击