/*
Name: Program for Alibaba bank account, this is a short CPP program, aiming to build a simple bank system without using class and objects.
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<iomanip>
#include<ctype.h>
using namespace std;
int main()
{
int acctnum=0;
double balance=0.0;
void initAccount(int &, double &);
void print_acct(int,double);
void calBalance(int,double);
void menu(int,double);
void withdrawal(int, double &);
void deposit(int, double&);
void delete_acct(int&, double&);
initAccount(acctnum,balance);//initialize the account
print_acct(acctnum,balance);//print the account before operation
menu(acctnum,balance);//operation menu
return 0;
}
//Function to initialize account
void initAccount(int &acctnum, double &balance)
{
//prompt the user
cout<<"Please input the account number:"<<endl;
cin>>acctnum;
cout<<"Please input the balance:"<<endl;
cin>>balance;
}
//Function for M withdraw
void withdrawal(int acctnum, double &balance)
{
int num=0;
double amount=0.0;
cout<<"Please input your account number:"<<endl;
//Structure input here
cin>>num;
while(num!=acctnum) {
cout<<"Error: account not exist, please re-enter!"<<endl;
cout<<"Please input your account number:"<<endl;
cin>>num;
}
cout<<"Please input the amount you want to withdraw:"<<endl;
cin>>amount;
if(amount>balance)
cout<<"Error: insufficient fund."<<endl;
else
balance=balance-amount;
}
//Function to deposit funds
void deposit(int acctnum, double &balance)
{
int num=0;
double amount=0.0;
cout<<"Please input your account number:"<<endl;
//Structure input here
cin>>num;
while(num!=acctnum) {
cout<<"Error: account not exist, please re-enter!"<<endl;
cout<<"Please input your account number:"<<endl;
cin>>num;
}
cout<<"Please input the amount you want to deposit:"<<endl;
cin>>amount;
if(amount<=0)
cout<<"Error: you should add money."<<endl;
else
balance=balance+amount;
}
//Function to delete account
void delete_acct(int &acctnum, double &balance)
{
int num=0;
cout<<"Please input your account number you want to delete:"<<endl;
//Structure input here
cin>>num;
while(num!=acctnum) {
cout<<"Error: account not exist, please re-enter!"<<endl;
cout<<"Please input your account number:"<<endl;
cin>>num;
}
//set account # and balance to zero to delete account
balance=0.0;
acctnum=0;
}
//Function to print the balance
void calBalance(int acctnum,double balance)
{
int num;
cout<<"Please input your account number:"<<endl;
cin>>num;
//If account number=0, it means account deleted
if (acctnum==num) {
cout<<"\n*********Bank of Alibaba: balance sheet*****<<******"<<endl;
cout<<"*********All information of the account*******"<<endl;
cout<<"Account number: "<<acctnum<<endl;
cout<<"Account balance: "<<setiosflags(ios::fixed)
<<setprecision(2)<<balance<<endl;
cout<<"**************************************************"<<endl;
} else
cout<<"\n*********Account deleted or not exist!***********"<<endl;
}
//Function to print acct info
void print_acct(int acctnum,double balance)
{
//If account number=0, it means account deleted
if (acctnum>0) {
cout<<"\n*********Bank of Alibaba: balance sheet***********"<<endl;
cout<<"*********All information of the account*******"<<endl;
cout<<"Account number: "<<acctnum<<endl;
cout<<"Account balance: "<<setiosflags(ios::fixed)
<<setprecision(2)<<balance<<endl;
cout<<"**************************************************"<<endl;
} else
cout<<"\n*********Account deleted or not exist!***********"<<endl;
}
//Function for the menu
void menu(int acctnum,double balance)
{
char input;
cout<<"\n*********Account operation**********************"<<endl;
cout<<"Please select your operation:"<<endl;
cout<<"W-Withdrawal"<<endl;
cout<<"D-Deposit"<<endl;
cout<<"B-Balance"<<endl;
cout<<"Q-Quit"<<endl;
cout<<"X-Delete Account"<<endl;
cin>>input;
//In case people input lower case letter
//if(input=='w'||input='d'||input=='b'||input=='q'||input=='x')
input=toupper(input);
while(input!='W' && input!='D' && input!='B'&& input!='Q'
&& input!='X') {
cout<<"\nError: please re-enter:"<<endl;
cout<<"\n*********Account operation**********************"<<endl;
cout<<"Please select your operation:"<<endl;
cout<<"W-Withdrawal"<<endl;
cout<<"D-Deposit"<<endl;
cout<<"B-Balance"<<endl;
cout<<"Q-Quit"<<endl;
cout<<"X-Delete Account"<<endl;
cin>>input;
input=toupper(input);
}
while(input!='Q') { //If not quit, continue with operation
switch(input) {
case 'W':
withdrawal(acctnum,balance);
break;
case 'D':
deposit(acctnum,balance);
break;
case 'B':
calBalance(acctnum,balance);
break;
case 'X':
delete_acct(acctnum,balance);
break;
case 'Q':
print_acct(acctnum,balance);
break;
default:
;
}
cout<<"\n*********Account operation**********************"<<endl;
cout<<"Please select your operation:"<<endl;
cout<<"W-Withdrawal"<<endl;
cout<<"D-Deposit"<<endl;
cout<<"B-Balance"<<endl;
cout<<"Q-Quit"<<endl;
cout<<"X-Delete Account"<<endl;
cin>>input;
input=toupper(input);
while(input!='W' && input!='D' && input!='B'&& input!='Q'
&& input!='X') {
cout<<"\nError: please re-enter:"<<endl;
cout<<"\n*********Account operation**********************"<<endl;
cout<<"Please select your operation:"<<endl;
cout<<"W-Withdrawal"<<endl;
cout<<"D-Deposit"<<endl;
cout<<"B-Balance"<<endl;
cout<<"Q-Quit"<<endl;
cout<<"X-Delete Account"<<endl;
cin>>input;
input=toupper(input);
}
}
//out of the loop,means=='Q',print and done.
print_acct(acctnum,balance);
}
回到主页
回到目录