In: Computer Science
Needed in C++
In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows
(1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively.
(1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the amount is more than balance. A function print(), which shall print ”A/C no: xxx Balance=xxx” (e.g., A/C no: 991234 Balance=$88.88), with balance rounded to two decimal places.
When you submit your code, please upload your class code(s) and your test code to prove that your code works correctly.
#include<iostream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
//class account
class Account
{
private:
long int accountNumber;
double balance;
public:
Account(); //constructor to assign
the account number
void credit(); //method
credit
void debit(); //method debit
void print();//method print
};
//constructor
Account :: Account()
{
//generate a random number and assign to
accountnumber
accountNumber = rand() % 9900000 + 99999;
}
//credit method
void Account :: credit()
{
double amt;
cout<<endl<<"Enter the amount to
deposit";
cin>>amt; //input the amount to
ccredit
if(amt>0)
{
balance =
balance + amt; //update the amount
cout<<endl<<"Your account credited
$"<<amt<<" successfully";
}
else
cout<<endl<<"Invalid amount";
}
//debit method
void Account :: debit()
{
double amt;
cout<<endl<<"Enter the amount to
withdraw";
cin>>amt; //input the amount to withdraw
if(amt>balance) //check the validity of
amount
{
cout<<endl<<"Amount withdrawn
exceeds the current balance";
}
else
balance= balance-amt;//update the
balance
}
//print method
void Account :: print()
{
//print the details
cout<<endl<<"Account Number :
"<<accountNumber;
cout<<endl<<"Balance in
account :
$"<<fixed<<setprecision(2)<<balance;
}
//driver program
int main()
{
Account aobj; //create object
int opt;
//infinite loop
while(1)
{ //display menu
cout<<endl<<" 1. CREDIT\n 2. DEBIT\n 3. PRINT \n
4.EXIT";
cout<<endl<<"Enter your choice : ";
cin>>opt;
if(opt==1)
//option for credit
aobj.credit();
else
if(opt==2) //option for debit
aobj.debit();
else
if(opt==3) //option for print
aobj.print();
else
if(opt==4) //option for exit
exit(0);
else
cout<<endl<<"Invalid Choice";
}
}
OUTPUT