/*
    Name: Program for Alibaba bank account, this is a short CPP program, aiming to build a simple bank system without using class and objects, 
    but with multiple users input.
    Author: muguangde
    Date:09/22/2014-09:21AM
    Description: This is a small program to simulate bank
    account management. User create an account first, then
    perform other operations.
*/

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <ctype.h>

using namespace std;
const int MAX_NUM=12;//Max account number

int main()
{
//To declare functions
    int read_accts(int [],double [],int,int &);
    void menu();
    int findacct(int [],int,int);
    void withdrawal(int [], double [], int);
    void deposit(int[], double[],int);
    int new_acct(int [],double[],int&);
    int delete_acct(int [],double [],int&);
    void balance(int [], double [], int);
    void print_accts(int [], double[], int&);

    int acctnum_array[12];
    double balance_array[12];
    int num_accts=0;
    char option;
//Initialize the arrays
    for(int i=0; i<12; i++) {
        acctnum_array[i]=0;
        balance_array[i]=0.0;
    }
//Read account info from file and print
    read_accts(acctnum_array,balance_array,MAX_NUM,num_accts);
    print_accts(acctnum_array,balance_array,num_accts);
    menu();//Print the menu

    //To set structure input below
    cin>>option;
    option=toupper(option);
    while(option!='W' && option!='D' && option!='N'&& option!='B'
            && option!='Q'&& option!='X'&& option!='P') {
        cout<<"Error: Please input the right option."<<endl;
        menu();//Print the menu
        cin>>option;
        option=toupper(option);
    }

    //To choose operation
    while(option!='Q') {
        switch(option) {
        case 'B':
            balance(acctnum_array,balance_array,num_accts);
            break;
        case 'W':
            withdrawal(acctnum_array,balance_array,num_accts);
            break;
        case 'D':
            deposit(acctnum_array,balance_array,num_accts);
            break;
        case 'N':
            new_acct(acctnum_array,balance_array,num_accts);
            break;
        case 'Q':
            print_accts(acctnum_array,balance_array,num_accts);
        case 'P':
            print_accts(acctnum_array,balance_array,num_accts);
            break;
        case 'X':
            delete_acct(acctnum_array,balance_array,num_accts);
            break;
        }

        menu();
        cin>>option;
        option=toupper(option);
        while(option!='W' && option!='D' && option!='B'&& option!='Q'
                && option!='X' && option!='P'&& option!='N') {
            menu();
            cin>>option;
            option=toupper(option);
        }
    }
    //out of the loop,means=='Q',print and done.
    print_accts(acctnum_array,balance_array,num_accts);

    system("PAUSE");
    return 0;
}

//Function to read in data
int read_accts(int acctnum_array[],double balance_array[],int max_accts,
               int &num_accts)
{
    ifstream infile;
    infile.open("input.txt");

    if(!infile.is_open()) {
        cout<<"Error: fail to open file."<<endl;
        system("PAUSE");
        exit(1);
    }
//To read in the data and determine the number of accounts
    while(!infile.eof() && max_accts>=num_accts) {
        infile>>acctnum_array[num_accts]>>balance_array[num_accts];
        num_accts++;
    }

    infile.close();
    return 0;
}

//Function to print data
void print_accts(int acctnum_array[],double balance_array[],
                 int &num_accts)
{
    cout<<"*******Alibaba bank account info*************"<<endl;
    cout<<"Account number      "<<"  Balance"<<endl;
    for(int i=0; i<num_accts; i++) {
        cout<<setw(9)<<acctnum_array[i]<<setw(20)<<setiosflags(ios::fixed)
            <<setprecision(2)<<balance_array[i]<<endl;
    }
}

//Function to print the menu
void menu()
{
    cout<<"\n*********Please select one of the following*******"<<endl;
    cout<<"W - Withdrawal"<<endl;
    cout<<"D - Deposit"<<endl;
    cout<<"N - New account"<<endl;
    cout<<"B - Balance"<<endl;
    cout<<"P - Print all the users"<<endl;
    cout<<"Q - Quit"<<endl;
    cout<<"X - Delete Account"<<endl;
}
//Function to find index
int findacct(int acctnum_array[], int num_accts, int requested_account)
{
    for(int i=0;i<num_accts;i++)
    {
        if(acctnum_array[i]==requested_account)
            return i;
    }
    return -1;
}
//Function to return balance
void balance(int acctnum_array[], double balance_array[],
             int num_accts)
{
    int requested_account;

    cout<<"Please input the account number:"<<endl;
    cin>>requested_account;

    if(findacct(acctnum_array,num_accts,requested_account)==-1)
        cout<<"Account not exist, please double check"<<endl;
    else {
        int i=findacct(acctnum_array,num_accts,requested_account);
        cout<<"*******Alibaba bank account info*************"<<endl;
        cout<<"Account number      "<<"  Balance"<<endl;
        cout<<setw(9)<<acctnum_array[i]<<setw(20)<<setiosflags(ios::fixed)
            <<setprecision(2)<<balance_array[i]<<endl;
    }
}

//Function to perform withdrawal
void withdrawal(int acctnum_array[],double balance_array[],
                int num_accts)
{
    int requested_account;
    int amount;

    cout<<"Please input the account number:"<<endl;
    cin>>requested_account;

    if(findacct(acctnum_array,num_accts,requested_account)==-1)
        cout<<"Account not exist, please double check"<<endl;
    else {
        int i=findacct(acctnum_array,num_accts,requested_account);
        cout<<"Please input the withdrawal amount"<<endl;
        cin>>amount;
        if(amount>balance_array[i])
            cout<<"Insufficient fund now"<<endl;
        else
            balance_array[i]=balance_array[i]-amount;
    }
}
//Function to perform deposit
void deposit(int acctnum_array[],double balance_array[],
             int num_accts)
{
    int requested_account;
    int amount;

    cout<<"Please input the account number:"<<endl;
    cin>>requested_account;

    if(findacct(acctnum_array,num_accts,requested_account)==-1)
        cout<<"Account not exist, please double check"<<endl;
    else {
        int i=findacct(acctnum_array,num_accts,requested_account);
        cout<<"Please input the deposit amount"<<endl;
        cin>>amount;
        if(amount<=0)
            cout<<"Don't play with ATM, dumb boy."<<endl;
        else
            balance_array[i]=balance_array[i]+amount;
    }
}

//Function to delete an account
int delete_acct(int acctnum_array[],double balance_array[],
                int &num_accts)
{
    int requested_account;

    cout<<"Please input the account number:"<<endl;
    cin>>requested_account;

    if(findacct(acctnum_array,num_accts,requested_account)==-1)
        cout<<"Account not exist, please double check"<<endl;
    else {
        int index=findacct(acctnum_array,num_accts,requested_account);
        num_accts--;
        for(int i=index; i<num_accts; i++) {
            acctnum_array[i]=acctnum_array[i+1];
            balance_array[i]=balance_array[i+1];
        }
    }
    return num_accts;
}

//Function to add a new account
int new_acct(int acctnum_array[],double balance_array[],
             int &num_accts)
{
    int requested_account;

    cout<<"Please input the account number:"<<endl;
    cin>>requested_account;

    if(findacct(acctnum_array,num_accts,requested_account)!=-1)
        cout<<"Account already exist, please double check"<<endl;
    else {
    int index;
       //The for loop return where to insert
       for(index=0;index<num_accts;index++)
        {
          if(requested_account<acctnum_array[index])
            break;
        }
    //acctnum_array[index]=requested_account;//insert account name
  //  balance_array[index]=0.0;//initialize new account
    for(int i=num_accts;i>index;i--)
        {
          acctnum_array[i]=acctnum_array[i-1];
          balance_array[i]=balance_array[i-1];
        }
        acctnum_array[index]=requested_account;//insert account name
        balance_array[index]=0.0;//initialize new account
        num_accts++;
    }
    return num_accts;
}




  
回到主页 回到目录