Answer to Question #196081 in C++ for Ali raza

Question #196081

Write a program that model a bank system, Program should be able to add, delete, modify and View the Record.

Program must contain following functions

Ask for Account No., Title and Balance

Credit any amount

Debit any amount

Display Current Balance


1
Expert's answer
2021-05-20T12:11:04-0400
#include <iostream>
#include <string>


using namespace std;


class Account{
private:
	//Account No., Title and Balance
	string no; 
	string title;
	double balance;
public:
	/**
	* Constructor
	*/
	Account(){
		this->balance=0;
	}
	/**
	* Constructor
	*/
	Account(string no,string title){
		this->no=no;
		this->title=title;
		this->balance=0;
	}
	/**
	* @return the no
	*/
	string getNo() {
		return no;
	}


	/**
	* @param no the no to set
	*/
	void setNo(string no) {
		this->no = no;
	}
	/**
	* @return the title
	*/
	string getTitle() {
		return title;
	}
	/**
	* @param title the title to set
	*/
	void setTitle(string title) {
		this->title = title;
	}
	/**
	* @return the balance
	*/
	float getBalance() {
		return balance;
	}
	/**
	* @param balance the balance to set
	*/
	void setBalance(float balance) {
		this->balance = balance;
	}
	//credit amount
	void creditAmount(double amount){
		this->balance += amount;
	}
	//debit amount
	void debitAmount(double amount){
		if((this->balance - amount)>=0){
			this->balance -= amount;
		}else{
			cout<<"\nWrong amount.\n\n";
		}
	}
};


int searchAccountByNo(Account accounts[],int totalNumberAccounts,string no);


int main (){
	int totalNumberAccounts=0;
	Account accounts[100];
	int ch=-1;
	string no; 
	string title;
	double balance;
	double amount;
	while(ch!=8){
		//add, delete, modify and View the Record. Credit any amount Debit any amount
		cout<<"1. Add a new account\n";
		cout<<"2. Delete account\n";
		cout<<"3. Modify account\n";
		cout<<"4. View one account\n";
		cout<<"5. View all accounts\n";
		cout<<"6. Credit any amount\n";
		cout<<"7. Debit any amount\n";
		cout<<"8. Exit\n";
		cout<<"Your choice: ";
		cin>>ch;
		cin.ignore();
		if(ch==1){
			cout<<"Enter no of account: ";
			getline(cin,no);
			cout<<"Enter title of account: ";
			getline(cin,title);
			Account newAccount(no,title);
			accounts[totalNumberAccounts]=newAccount;
			totalNumberAccounts++;
			cout<<"\nA new account has been added.\n\n";
		}else if(ch==2){
			if(totalNumberAccounts>0){
				cout<<"Enter account No to delete: ";
				getline(cin,no);
				int selectedAccount=searchAccountByNo(accounts,totalNumberAccounts,no);
				if(selectedAccount!=-1){
					for(int i=selectedAccount;i<totalNumberAccounts-1;i++){
						accounts[i]=accounts[i+1];
					}
					totalNumberAccounts--;
					cout<< "\nThe account has been deleted.\n\n";
				} else {
					cout<<"\nWrong the account No.\n\n";
				}
			}else{
				cout<<"\nThe account array is empty.\n\n";
			}
		}else if(ch==3){
			if(totalNumberAccounts>0){
				cout<<"Enter account No to modify: ";
				getline(cin,no);
				int selectedAccount=searchAccountByNo(accounts,totalNumberAccounts,no);
				if(selectedAccount!=-1){
					cout<<"Enter a new title of account: ";
					getline(cin,title);
					cout<<"Enter a new balance of account: ";
					cin>>balance;
					accounts[selectedAccount].setTitle(title);
					accounts[selectedAccount].setBalance(amount);
					cout<< "\nThe account has been updated.\n\n";
				} else {
					cout<<"\nWrong the account No.\n\n";
				}
			}else{
				cout<<"\nThe account array is empty.\n\n";
			}
		}else if(ch==4){
			if(totalNumberAccounts>0){
				cout<<"Enter account No to view: ";
				getline(cin,no);
				int selectedAccount=searchAccountByNo(accounts,totalNumberAccounts,no);
				if(selectedAccount!=-1){
					cout<<"\nAccount No: "<<accounts[selectedAccount].getNo()<<"\n";
					cout<<"Account Title: "<<accounts[selectedAccount].getTitle()<<"\n";
					cout<<"Account Balance: "<<accounts[selectedAccount].getBalance()<<"\n\n";
				} else {
					cout<<"\nWrong the account No.\n\n";
				}
			}else{
				cout<<"\nThe account array is empty.\n\n";
			}
		}else if(ch==5){
			cout<<"\nAll accounts\n";
			for(int i=0;i<totalNumberAccounts;i++){
				cout<<"Account No: "<<accounts[i].getNo()<<"\n";
				cout<<"Account Title: "<<accounts[i].getTitle()<<"\n";
				cout<<"Account Balance: "<<accounts[i].getBalance()<<"\n\n";
			}
		}else if(ch==6){
			if(totalNumberAccounts>0){
				cout<<"Enter account No to credit: ";
				getline(cin,no);
				int selectedAccount=searchAccountByNo(accounts,totalNumberAccounts,no);
				if(selectedAccount!=-1){
					cout<<"Enter amount to credit: ";
					cin>>amount;
					accounts[selectedAccount].creditAmount(amount);
				} else {
					cout<<"\nWrong the account No.\n\n";
				}
			}else{
				cout<<"\nThe account array is empty.\n\n";
			}
		}else if(ch==7){
			if(totalNumberAccounts>0){
				cout<<"Enter account No to debit: ";
				getline(cin,no);
				int selectedAccount=searchAccountByNo(accounts,totalNumberAccounts,no);
				if(selectedAccount!=-1){
					cout<<"Enter amount to debit: ";
					cin>>amount;
					accounts[selectedAccount].debitAmount(amount);
				} else {
					cout<<"\nWrong the account No.\n\n";
				}
			}else{
				cout<<"\nThe account array is empty.\n\n";
			}
		}
		else if(ch==8){
			//exit
		}else{
			cout<<"\nSelect a correct menu item.\n\n";
		}
	}
	system("pause");
	return 0;
}
int searchAccountByNo(Account accounts[],int totalNumberAccounts,string no){
	for(int i=0;i<totalNumberAccounts;i++){
		if(accounts[i].getNo().compare(no)==0){
			return i;
		}
	}
	return -1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog