You are on page 1of 7

Student Name: Georges Georges Karam

ID:12170059
Assignment 3

You need to program a basic banking system.

Each account has a number, a deposit, and a type (Current or Savings).

A customer or the bank can create, print, deposit and withdraw amount in the
account. The bank can also add interest to the amount.

For this we start by modeling the following functions and variables.

1. Start by creating three global variables : An account number (integer), an


account type (char) and a deposit (integer).

2. Write a function that creates an account. This function does not have any
parameters nor has a return value. In the body of this function you should allow
the user to enter the values of the global variables. Assert that the account number
is between 1 and 10, the account type is either C or S and the deposit is between
500 and 5000.

3. Write a function that prints an account on the screen. It does not have any
parameters nor has a return value. It only prints the values of the variables.

4. Write a function checkAccount() that checks if account number is different


than 0. This would tells us if the user has already created an account or not. This
function does not have any parameters but it returns a Boolean.

5. Write a function that adds an amount to the deposit. This function has an
integer parameter which is the amount to be added to the deposit. It does not
return anything.

6. Write a function that withdraws an amount from the deposit. This function
has an integer parameter which is the amount to be withdrawn from the deposit. It
does not return anything. Make sure that the account has enough money before
you make the withdrawal.

7. Write a function that gives you back the current interest. This function does
not have any parameter but it returns an integer. The interest is computed
randomly using a random number. It has a value between 1 and 10. Moreover, this
function should compare the newly computed interest to the old one given. If the
old one is greater than the new interest, the function returns back the value of the
old interest, otherwise it gives back the value of the new one (Hint : static).

8. Write the main function in which you call the above functions. In the main
you should offer a menu as the following :
Select your Option (1-6) :
1. Create Account
2. Show Account
3. Add
4. Withdraw
5. Interest
6. Exit
Notice that the checkAccount() is not in the menu, but it is used before calling
certain other functions. For example before adding an amount to the account you
need to check if the accounts already exists.

Finally, as long as the user has not chosen 6 to exit, the menu should be shown
again so the user can choose another operation to do.

Answer :
#include<iostream>
#include <assert.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int acc_no;
char acc_type;
int deposit;

bool checkAccount(){
return acc_no != 0;
}

void createAccount(){
cout << "Enter Account Number(1-10)\n";
cin >> acc_no;
assert(acc_no>=1 && acc_no<=10);
cout << "Enter Account Type(C or S)\n";
cin >> acc_type;
assert(acc_type=='c'||acc_type=='C' || acc_type=='s' || acc_type=='S');
cout << "Enter Deposit Amount(500-5000)\n";
cin >> deposit;
assert(deposit>=500 && deposit<=5000);
}

void showAccount(){
cout << "Account Number: " << acc_no << endl;
cout << "Account Type: " << acc_type << endl;
cout << "Amount: " << deposit << endl;
}

void add(int val){


deposit += val;
cout << "Deposit Successful\n";
}

void withdraw(int val){


if(val>deposit){
cout << "Insufficient Amount in account\n";
} else {
deposit -= val;
cout << "Withdrawal Successful\n";
}
}

int interest(){
static int interest = 0;
int temp = rand()%10 + 1;
interest = max(interest,temp);
return interest;
}

int main(){
acc_no = 0;
bool exit = false;
while(!exit){
cout << "Select your Option(1-6)\n";
cout << "1. Create Account\n";
cout << "2. Show Account\n";
cout << "3. Add\n";
cout << "4. Withdraw\n";
cout << "5. Interest\n";
cout << "6. Exit\n";
int choice;
cin >> choice;
switch(choice){
case 1:
if(checkAccount()){
cout << "Account Exists. Continue Anyway? (1-Yes/0-No)\n";
int ch;
cin >> ch;
while(ch!=1 && ch!=0){
cout << "Invalid input. Enter 1/0\n";
cin >> ch;
}
if(ch==1){
createAccount();
}
} else {
createAccount();
}
break;
case 2:
if(checkAccount()){
showAccount();
}
else{
cout << "No account exists\n";
}
break;
case 3:
if(!checkAccount()){
cout << "No account exists\n";
break;
}
int aval;
cout << "Enter Deposit Amount\n";
cin >> aval;
add(aval);
break;
case 4:
if(!checkAccount()){
cout << "No account exists\n";
break;
}
int dval;
cout << "Enter Withdrawal Amount\n";
cin >> dval;
withdraw(dval);
break;
case 5:
if(!checkAccount()){
cout << "No account exists\n";
break;
}
cout << "Current Interest: " << interest() << endl;
break;
case 6:
exit = true;
break;
default:
cout << "Invalid Choice\n";
break;
}
}
return 1;
}

You might also like