//********************************* Banking by Liang Chen *****************************************
//This program works similarily to a bank's computer system to manage customers'accounts. 
//The types of accounts are mainly checking and savings accounts and customers are given 
//atm cards for more convenient access to their checking accounts.  Moreover, atm cards can also
//be used for purchases besides deposits and withdrawls. There are different amounts of service 
//charges imposed on atm cards and checking account, but none for savings accounts. Savings accounts
// bear interest just like CD accounts except the differences in interest calculation. 
//Therefore, this program is capable of maintaining customers' account balances for all kinds of 
//activities described above. 

# include <iostream.h>

class savings;
class account	{								//abstract class

public:
	virtual int find_depositor(int)=0;			//pure virtual functions
	virtual void make_deposit(int)=0;
	virtual make_withdrawl(int)=0; 
	virtual void output_data(int)=0;
	virtual void output_old_data()=0;
	virtual void open_account(int)=0;
 	virtual void set_service_charge()=0;
	virtual int get_id(int)=0;
				
		
class checking:public account	{				//derived class of account-checking
	int act_no;
	float balance;
	static float service_charge_amt;
public:
	checking& open_account(int);
	int find_depositor(int);				//member functions of checking
	void make_deposit(int);
	void make_withdrawl(int); 
	void output_data(int);
	void output_old_data();
	void set_service_charge();
	int get_id(int);
	void debit_check(int);	
	void transfer_funds(savings&);
	float operator+(float);
  friend float operator-(checking&);		  
	checking();
	~checking();
								};

class atm_card:public checking	{				//derived class of checking-atm_card
	float atm_balance;
	static float service_charge_amt;
public:											//member functions of atm_card								
	void output_data(int);
	void service_charge(int);
	void debit_purchase(int);
	atm_card();
	~atm_card();
								};						

class savings:public account	{				//derived class of account-savings
	float savings_balance;
	float interest_payment;						
	int act_no;
public:											//member functions of savings
	savings& open_account(int);
	void output_data(int);
	int find_depositor(int);					//member functions of checking
	void make_deposit(int);
	void make_withdrawl(int); 
	void output_old_data();
	int get_id(int);
	void set_interest_rate();
	void interest_payment(int);
 friend void checking::transfer_funds(savings&);
								};

class cd_account:public savings	{				//derived class of savings-cd_account
	float cd_balance;
	float cd_interest_rate;
public:								//member functions of cd_account
	void output_data(int);
	void early_withdrwal_penalty;
				};

//********************************* constructors and destructors *********************************************************
checking::checking()
{balance = 0.0;}
checking::~checking()	           	
{};

atm_card::atm_card()
{atm_balance = 0.0;}
atm_card::~atm_card()	           	
{};

savings::savings()
{saving_balance = 0.0;}
saving::~saving()
{};	             

cd_account::cd_account()
{cd_balance = 0.0;}
cd_account::~cd_account()	           	
{};

//*********** member function declarations for checking and it's derived class ********************************************

void checking::make_deposit(int acc_numb)			//adds amount to checking balance for deposit
{
	
	float amount;
	float balance;
	if(acc_numb == act_no)  {
		cout << "\nenter amount deposited\n";
		cin >> amount;
		balance += amount;
		cout << "\ndeposit processed thank you\n";                    
		cout << "\nchecking balance in US dollar and cent is" << balance;                  
				}
		else 
		{
		cout << "\nwrong customer\n";
		return;
		}
}	

void checking::make_withdrawl(int acc_numb)		//subtracts amount withdrew from checking balance for 
{
	
	float amount;
	float balance;
	if(acc_numb == act_no)  {
		cout << "\nenter amount withdrew\n";
		cin >> amount;
		balance -= amount;
		cout << "\nwithdrawl processed thank you\n";                    
		cout << "\nchecking balance in US dollar and cent is" << balance;                  
				}
		else 
		{
		cout << "\nwrong customer\n";
		return;
		}
}	

void checking::output_data(int acc_numb)		//displays current account information
{
	if (acc_numb == act_no)  {
		cout << "\naccount number" << act_no;
		cout << "\ncurrent balance in US dollar and cent is " << balance;
		                  }
	 else   {
		cout << "\nwrong customer\n";
		return;
			}
}

void checking::output_old_data()			//displays previous account information
{
	int act_no;
	float balance;
	cout<< "\naccount number" << act_no;
	cout<< "\nbalance" << balance;
	return;
}

checking* checking::open_account(int acc_numb)		//opens new checking accounts
{
	float amount;
	if (acc_numb != 0)
		{
	cout << "\ncheck account number\n";
	return this;
		}	
	
	else	{
	cout << "\nenter account number\n";
	cin >> acc_numb;
	act_no = acc_numb;
	cout << "\nenter initial deposit\n";
	cin >> amount;
	balance = amount;
	return this;
		}
}

void checking::set_service_charge()			//sets checking account service charge
{
	cout << "\nenter monthly service charge amount\n";
	cin  << service_charge;
}

void checking::service_charge(int acc_numb)		//deducts service fee from checking balance
{
	if(acc_numb == act_number) {
	balance -= service_charge; }
	else {
		cout << "\nwrong customer\n";
	     	return;
	     }
}		

int checking::get_id(int acc_numb)			//customer ID verifier
{
	if(acc_numb == act_no)	{
		return act_no;
				}
	else	{
		cout << "\nwrong customer\n";
		return 0;
		}
}

void checking::debit_check(int acc_numb)		//subtracts amount paid by checks
{
	float amount;
	cout << "\nenter amount paid on the check\n";
	cin >> amount;
	if(acc_numb == act_no) {
		balance -= amount;
		cout << "\ncheck processed\n";                    
		       		}
		else {
		cout << "\nwrong customer\n";
		return;
		      }
}

void checking::transfer_funds(saving& sav_pt)		//transfers funds from savings to checking account
{
	float amount;
	cout << "\nenter amount to be transferred\n";
	cin >> amount;
	sav_pt.savings_balance -= amount;
	balance += amount;                    
	return;	
}


void atm_card::output_data(int acc_numb)		//displays current account information
{
	int act_number;
	act_number=get_id(acc_numb);
	if(acc_numb == act_number){
		cout<< "\naccount number"<< act_number << "\n";
		cout << "\natm balance" << atm_balance; 
		                  }
		else {
		cout << "\nwrong customer\n";
		return;
		      }
}

void atm_card::set_service_charge()			//sets atm account service charge
{
	cout << "\nenter monthly service charge amount\n";
	cin  << service_charge;
}

void atm_card::service_charge(int acc_numb)		//deducts service fee from atm balance
{
	if(acc_numb == act_number) {
	atm_balance -= service_charge; }
	else {
		cout << "\nwrong customer\n";
	     	return;
	     }
}		
void atm_card::debit_purchase(int acc_numb)		//debits purchases from atm card
{
	int act_number;
	float amount;
	act_number=get_id(acc_numb);
	
	if(acc_numb == act_number) {
		cout << "\nenter purchase amount\n";
		cin >> amount;
		atm_balance -= amount;
				   }
	else {
		cout << "\nwrong customer\n";
	     	return;
	     }
}

//*********** member function declarations for savings and it's derived classes *******************************************************
void savings::make_deposit(int acc_numb)		//adds amount to savings balance for deposit
{
	
	float amount;
	float balance;
	if(acc_numb == act_no)  {
		cout << "\nenter amount deposited\n";
		cin >> amount;
		balance += amount;
		cout << "\ndeposit processed thank you\n";                    
		cout << "\nsaving balance in US dollar and cent is" << balance;                  
				}
		else 
		{
		cout << "\nwrong customer\n";
		return;
		}
}	

void savings::make_withdrawl(int acc_numb)		//subtracts amount from savings balance for withdrawl
{
	
	float amount;
	float balance;
	if(acc_numb == act_no)  {
		cout << "\nenter amount withdrew\n";
		cin >> amount;
		balance -= amount;
		cout << "\nwithdrawl processed thank you\n";                    
		cout << "\nchecking balance in US dollar and cent is" << balance;                  
				}
		else 
		{
		cout << "\nwrong customer\n";
		return;
		}
}	

void savings::output_data(int acc_numb)			//displays current account information
{
	if (acc_numb == act_no)  {
		cout << "\naccount number" << act_no;
		cout << "\ncurrent balance in US dollar and cent is " << balance;
		                  }
	 else   {
		cout << "\nwrong customer\n";
		return;
			}
}

void savings::output_old_data()				//displays previous account information
{
	int act_no;
	float balance;
	cout<< "\naccount number" << act_no;
	cout<< "\nbalance" << balance;
	return;
}

savings* savings::open_account(int acc_numb)		//opens new savings accounts
{
	float amount;
	if (acc_numb != 0)
		{
	cout << "\ncheck account number\n";
	return this;
		}	
	
	else	{
	cout << "\nenter account number\n";
	cin >> acc_numb;
	act_no = acc_numb;
	cout << "\nenter initial deposit\n";
	cin >> amount;
	savings_balance = amount;
	return this;
		}
}

int savings::get_id(int acc_numb)			//customer ID verifier
{
	if(acc_numb == act_no)	{
		return act_no;
				}
	else	{
		cout << "\nwrong customer\n";
		return 0;
		}
}

void savings::set_interest_rate()			//sets savings account interest rate
{
	cout << "\nenter percentage interest rate on savings account\n";
	cin  << interest_rate;
}

void savings::interest_payment(int acc_numb)		//adds interest payment to savings balance
{
	if(acc_numb == act_number) 				   {
	savings_balance = savings_balance*(1+interest_rate/100/12; }
	else {
		cout << "\nwrong customer\n";
	     	return;
	     }
}		

cd* cd::open_account(int acc_numb)			//opens new savings accounts
{
	float amount;
	if (acc_numb != 0)
		{
	cout << "\ncheck account number\n";
	return this;
		}	
	
	else	{
	cout << "\nenter account number\n";
	cin >> acc_numb;
	act_no = acc_numb;
	cout << "\nenter initial deposit\n";
	cin >> amount;
	cd_balance = amount;
	return this;
		}
}

void cd_account::output_data(int acc_numb)		//displays cd account information
{
	int act_number;
	act_number=get_id(acc_numb);
	if(acc_numb == act_number){
		cout<< "\naccount number"<< act_number << "\n";
		cout << "\ncd balance" << cd_balance; 
		                  }
		else {
		cout << "\nwrong customer\n";
		return;
		      }
}

void cd_account::set_interest_rate(int acc_numb)	//sets cd account interest rate
{
	cout << "\nenter percentage interest rate on cd account\n";
	cin  << interest_rate;
}

void cd_account::interest_payment(int acc_numb)		//adds interest payment to cd balance
{
	int days;
	if(acc_numb == act_number) 			 	{
	cout << "\nenter length in number of days for this cd\n";
	cin  << days;
	cd_balance = cd_balance*(1+interest_rate*days/100/365; 	}
	else {
		cout << "\nwrong customer\n";
	     	return;
	     }
}		

void cd_account::early_withdrawl_penalty(int acc_numb)	//deducts cd balance for early withdrawl penalty
{
	int days_earlier;
	float penalty_amt;
	if(acc_numb == act_number) 			 			{
	cout << "\nenter number of days before maturity date\n";
	cin  << days_earlier;
	cd_balance = cd_balance*(1-((interest_rate*1.50)/100*days_earlier/days)); 	}
	else {
		cout << "\nwrong customer\n";
	     	return;
	     }
}		

****************************** main program ************************************************
main()
{
	extern void exit(int);
	const int total_depositors=10;			//variables initiation
	int is_depositor=0;	
	int account_numb;
	int i;
	int actual_depositors=0;
	int ender =0 ;
	checking depositor[total_depositors];		//calling functions
	atm_card depositor[total_depositors];
	savings depositor[total_depositors];
	cd_account depositor[total_depositors];
	checking* old;
	savings* old;
	int k;
	float amount_deposited; 
	float amount_withdrew; 
	float account_balance;
	float balance;
		
	checking c;
	atm_card a;
	savings s;
	cd_account d;
	bond_account b;
	
	while(ender == 0)  {				//conditional while loop
		cout << "\nenter 0 to open a new account ";
		cout << "\nenter account number \nenter -1 to terminate program\n";
		cin >> account_numb;
		if (account_numb < 0) {break;}
		for(i=0;i< total_depositors;i++) {
			is_depositor = depositor[i].find_depositor(account_numb);
				if(is_depositor == 1){break;}
			   			 } // end for loop
						                        
	      	if((is_depositor == 0) && (account_numb != 0)) 
	      		{				//menu of options
	      	cout << "\nMenu\n";
	      	cout << "\n1 make checking deposit";
	      	cout << "\n2 make atm deposit";
	      	cout << "\n3 make checking withdrawl";
	      	cout << "\n4 make atm withdrawl";
	      	cout << "\n5 set checking service fee";
	      	cout << "\n6 set atm service fee";  	
	      	cout << "\n7 debit check";
	      	cout << "\n8 balance for checking";
	      	cout << "\n9 debit atm purchases";
	      	cout << "\n10 balance for atm";
		cout << "\n11 make savings deposit";
	      	cout << "\n12 make savings withdrawl";
	      	cout << "\n13 set savings interest rate";
	      	cout << "\n14 set cd interest rate";
	      	cout << "\n15 balance for savings";
	      	cout << "\n16 balance for cd";
		cout << "\n17 cd early withdrawl balance";
		cout << "\n18 transfer funds from savings to checking";
	      	cout << "\n19 open a new checking account";
	      	cout << "\n20 open a new savings accout";
	      	cout << "\n21 open a new cd accout";
		cout << "\n22 end program";
		cout << "\n enter your selection\n";
	      	cin >> k;
	   		          	
	      	switch(k) 		{		// switch with many cases
	      	
	      		case 1:
	      			cout << "\nenter amount deposited \n";
	      			cin >> amount_deposited;
	      			account_balance = balance + amount_deposited;
	      			cout << "\nnew balance is " << account_balance;
	      			break;
	      		
	      		case 2:
	      			cout << "\nenter amount deposited \n";
	      			cin >> amount_deposited;
	      			account_balance = atm_balance + amount_deposited;
	      			cout << "\nnew balance is " << account_balance;
	      			break;
	      			      		
	      		case 3:
	      			cout << "\nenter amount withdraw \n";
	      			cin >> amount_withdraw;
	      			account_balance = balance - amount_deposited;
	      			cout << "\nnew balance is " << account_balance;
	      			break;
	      		case 4:
	      			cout << "\nenter amount withdraw \n";
	      			cin >> amount_withdraw;
	      			account_balance = atm_balance - amount_deposited;
	      			cout << "\nnew balance is " << account_balance;
	      			break;
	      		case 5:
	      			depositor[i].checking::set_service_charge(account_numb);
	      			break;
	      		case 6:
	      			depositor[i].atm_card::set_service_charge(account_numb);
	      			break;
	      		case 7:
	      			depositor[i].debit_check(account_numb);
	      			break;
	      		case 8:
	      			depositor[i].checking::output_data(account_numb);
	      			break;
			case 9:
	      			depositor[i].debit_purchase(account_numb);
	      			break;
			case 10:
	      			depositor[i].atm_card::output_data(account_numb);
	      			break;
			case 11:
	      			cout << "\nenter amount deposited \n";
	      			cin >> amount_deposited;
	      			account_balance = savings_balance + amount_deposited;
	      			cout << "\nnew balance is " << account_balance;
	      			break;
	      			      		
	      		case 12:
	      			cout << "\nenter amount withdraw \n";
	      			cin >> amount_withdraw;
	      			account_balance = savings_balance - amount_deposited;
	      			cout << "\nnew balance is " << account_balance;
	      			break;
			case 13:
	      			depositor[i].savings::set_interest_rate(account_numb);
	      			break;

			case 14:
	      			depositor[i].cd_account::set_interest_rate(account_numb);
	      			break;

			case 15:
	      			depositor[i].savings::output_data(account_numb);
	      			break;
			case 16:
	      			depositor[i].cd_account::output_data(account_numb);
	      			break;
			case 17:
	      			depositor[i].cd_account::early_withdrawl_penalty(account_numb);
	      			break;
			case 18:
	      			depositor[i].cd_account::early_withdrawl_penalty(account_numb);
	      			break;

			case 19:
	      			if (actual_depositors == total_depositors)
	      	        		{
	      	        	cout << "\nmaximum account number reached \n";
	      	        	break;
	      	        		}
	      	        	if (actual_depositors > 0)
	      	        		{
	      	        		old->output_old_data();
	      	        		old = depositor[actual_depositors].checking::open_account(account_numb);
	      	        		actual_depositors++;
	      	        		break;
	      	        		}
	      	      	case 20:
	      			if (actual_depositors == total_depositors)
	      	        		{
	      	        	cout << "\nmaximum account number reached \n";
	      	        	break;
	      	        		}
	      	        	if (actual_depositors > 0)
	      	        		{
	      	        		old->output_old_data();
	      	        		old = depositor[actual_depositors].savings::open_account(account_numb);
	      	        		actual_depositors++;
	      	        		break;
	      	        		}
			case 21:
	      			if (actual_depositors == total_depositors)
	      	        		{
	      	        	cout << "\nmaximum account number reached \n";
	      	        	break;
	      	        		}
	      	        	if (actual_depositors > 0)
	      	        		{
	      	        		old->output_old_data();
	      	        		old = depositor[actual_depositors].cd_account::open_account(account_numb);
	      	        		actual_depositors++;
	      	        		break;
	      	        		}
			case 22:
	      	        	ender=1;
	      			break;
	      	        default:
	      			cout << "\nentry not recognized\n";
	      			break;     
	      		  } // end switch
	      		   		} //end while loop      
}