Question: Your task for this lab is to complete the implementation of the Account module for holding a bank account number (an integer) and the balance

Your task for this lab is to complete the implementation of theAccountmodule for holding a bank account number (an integer) and the balance of the account (a double value).

The Account class

The Account class has two attributes; one integer for an account number (m_number) and a double for the balance of the account (m_balance)

TheAccountclass can be in three different states:

  • Invalid Empty State
  • An account is in aninvalidstate if invalid information is fed into the account. In these types of situations the account number is set to-1and the balance is set to0. The Account object in this case is rendered inactive and can not be used anymore.
  • New
  • An account is consideredNewornot setwhen it is just created and the account number is not assigned yet. This state of theAccountclass is flagged by setting the account number (m_number) to0.
  • Valid
  • AnAccountis consideredvalidif the account number is a 5 digit integer with a zero or positive balance.

Already implemented parts:

Constructors

TheAccountcan be created in two different ways:

  • default constructor(implemented)
 Account(); 

The default constructor sets the account asnewwith a balance of0.

  • Two argument constructor (and integer and a double)(implemented)
 Account(int number, double balance); 

The two-argument constructor sets the account number and balance to incoming arguments only if both values are valid. If an invalid account number or a negative balance is passed to the constructor, the object will be set into an invalid empty state.

display function.

(implemented)

 ostream& display() const 

Displays the account on the screen.

If the account is invalid, it is going to print:Bad Account. If the account is a new account, instead of the account number it will printNew.

At the enddisplaywill return thecoutobject.

To be implemented:

type conversion operators

  • operator bool
  • returnstrueif the account isvalidand otherwisefalse. This operator can not modify theaccountobject.
  • operator int
  • returns theaccount number. This operator can not modify theaccountobject.
  • operator double
  • returns thebalance value. This operator can not modify theaccountobject.

Unary member operator

  • bool operator ~()
  • This operator returnstrueis the account isnewornot set(i.e. if theaccount numberiszero), otherwise it will returnfalse. This operator can not modify theaccountobject.

Binary member operators

Note: All the binary member operatorswill not take any actionif theaccountis in aninvalidstate.

assignment operators

  • overload theassignment operatorso aNEW accountcan be set to aninteger. Doing so should set theaccount numberof theaccountto theinteger value.
  • If theinteger valueis an invalid account number, the object should be set to aninvalid empty stateinstead.
  • If the account is not new, then this operator should not perform any action.
  • In any case, a reference of thecurrent object(account) should be returned.
Account A,B; A = 55555; // the account number of A will be set to 55555 B = 555; // the account B will be set to invalid state B = 66666; // no action will be taken since the B is in not new A = 66666; // no action will be taken since the A is in not new 
  • overload theassignment operatorso aNEW accountcan be set to anotherAccountobject. This action shouldmovethe balance and the account number from one account to another; therefore unlike the usual assignment operator that affects the left operand only, this operator will affect both operands; the balance of the left account will be set to the balance of the right account and then the balance of the right account will be set to zero. The same will happen to the account number
  • If the left Account operand is not new or the right account operand is not valid, no action should be taken.
  • In any case, a reference of thecurrent object(account) should be returned.
 Account A, B(66666, 400), Bad(555, -10); A = B; // A will have the properties of B and B will become a NEW account B = Bad; // Nothing will happen since Bad is not new Bad = B; // Nothing will hapen since Bad is invalid 
  • overload the+= operatorto add a double value to an account. This should act like depositing money into an account. (i.e. the value of the double should be added to the balance)
  • if the account is in an invalid state or the double value is negative, no action should be taken.
  • In any case, a reference of thecurrent object(account) should be returned.
Account A(55555, 400.0), Bad(555, -10); A += 200.0; // A will have a balance of 600  Bad += 300.0; // Nothing will happen since Bad is invalid A += -20.0; // Nothing will happen since double value is negative 
  • overload the-= operatorto reduce an account balance by a double value . This should act like withdrawing money from an account. (i.e. the value of the balance should be reduced by the double value)
  • if the account is in an invalid state, the double value is negative or there is not enough money in the account no action should be taken.
  • In any case, a reference of thecurrent object(account) should be returned.
Account A(55555, 400.0), Bad(555, -10); A -= 150.0; // A will have a balance of 250  A -= 300.0; // Nothing will happen since there not enough money in A A -= -20.0; // Nothing will happen since double value is negative Bad -= 20.0 // Nothing will happen since Bad is invalid 
  • overload the<< operator(left shift operator) to move funds from the right account to the left. After this operation, the balance of the left account will be the sum of both and the balance of the right account will be zero.
  • Funds of an account should not be able to be moved to itself. This operation does not affect the account.
  • In any case, a reference of thecurrent object(account) should be returned.
Account A(55555, 400.0),B(66666, 500.0), Bad(555, -10); A << B; // A will have a balance of 900.0, B will have a balance of zero A << A; // Nothing will happen  A << Bad; // Nothing will happen Bad << A; // Nothing will happen 
  • overload the>> operator(right shift operator) to move funds from the left account to the right. After this operation, the balance of the right account will be the sum of both and the balance of the left account will be zero.
  • Funds of an account should not be able to be moved to itself. This operation does not affect the account.
  • In any case, a reference of thecurrent object(account) should be returned.
Account A(55555, 400.0),B(66666, 500.0), Bad(555, -10); A >> B; // B will have a balance of 900.0, A will have a balance of zero B >> B; // Nothing will happen  B >> Bad; // Nothing will happen Bad >> B; // Nothing will happen 

Binary helper operators

-- make a binary stand alone helper+ operatorthat accepts aconstant account referenceat left and anotherconstant account referenceat right and returns adoublevalue.

Thedoublevalue should be thesumof thebalancesof the two accounts.

If any of the two accounts isinvalid, thenzerois returned.

 Account A(55555, 400.0), B(66666, 600.0), Bad(555, -10); double sum; sum = A + B; // sum should be 1000.0 sum = A + Bad; // sum should be 0 since Bad is invalid sum = Bad + B; // sum should be 0 since Bad is invalid 

-- Make a binary stand alone helper+= operatorthat accepts adouble referenceat left and aconstant account referenceat right and returns adoublevalue.

The value of the balance of the right operand (account reference) should be added to the left operand (double reference)

Then the value of the double reference is returned.

 Account A(55555, 400.0), B(66666, 600.0), Bad(555, -10); double sum = 100, ret; ret = sum += A; // sum and ret should be 500.0 

The tester program.

The tester program tests all the operator overloads and the output should be as follows:

Using bool conversion overload and operator ~ to print the accounts: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 111.11 | |002| 22222 | 222.22 | |003| BAD | ACCOUNT | |004| 44444 | 4444.44 | |005| 55555 | 555.55 | |006| BAD | ACCOUNT | |007| 77777 | 777.77 | |008| NEW | 0.00 | |009| NEW | 0.00 | |010| NEW | 0.00 | +---+-------+--------------+ Depositing $50.02 into account #1 using += operator: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 161.13 | +---+-------+--------------+ Withdrawing $100.01 from account #2 using -= operator: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 22222 | 122.21 | +---+-------+--------------+ Attempting to withdraw too much from account #4 using -= operator: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 44444 | 4444.44 | +---+-------+--------------+ Attempting to deposit and withdraw negative amounts on account #4: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 44444 | 4444.44 | +---+-------+--------------+ +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 44444 | 4444.44 | +---+-------+--------------+ Having these two accounts: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 161.13 | |002| 22222 | 122.21 | +---+-------+--------------+ Moving funds from first account to second: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 0.00 | |002| 22222 | 283.34 | +---+-------+--------------+ Moving funds from second account to first: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 283.34 | |002| 22222 | 0.00 | +---+-------+--------------+ Attempting to move funds from an account to itself +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 283.34 | +---+-------+--------------+ Sum of the balance of accounts 5 and 7 in three different ways: 1333.32, 1333.32, and 1333.32 Setting the account number of account#8 to 88888: before: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| NEW | 0.00 | +---+-------+--------------+ After: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 88888 | 0.00 | +---+-------+--------------+ Moving the Account from Account# 8 to Account #9 using operator=: Before: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 88888 | 0.00 | |002| NEW | 0.00 | +---+-------+--------------+ After: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| NEW | 0.00 | |002| 88888 | 0.00 | +---+-------+--------------+ Displaying all accounts: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 11111 | 283.34 | |002| 22222 | 0.00 | |003| BAD | ACCOUNT | |004| 44444 | 4444.44 | |005| 55555 | 555.55 | |006| BAD | ACCOUNT | |007| 77777 | 777.77 | |008| NEW | 0.00 | |009| 88888 | 0.00 | |010| NEW | 0.00 | +---+-------+--------------+ Attempting to change the account number of the valid account#7: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 77777 | 777.77 | +---+-------+--------------+ Attempting to set accounts that are not new: (7 and 6) This attempt should not affect either account Before: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| BAD | ACCOUNT | |002| 77777 | 777.77 | +---+-------+--------------+ After: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| BAD | ACCOUNT | |002| 77777 | 777.77 | +---+-------+--------------+ Setting a new account (#8) to a valid account(#7) Before: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| 77777 | 777.77 | |002| NEW | 0.00 | +---+-------+--------------+ After: +---+-------+--------------+ |ROW| ACC# | BALANCE | +---+-------+--------------+ |001| NEW | 0.00 | |002| 77777 | 777.77 | +---+-------+--------------+ Display raw account numbers of all the accounts using int conversion operator 11111 22222 -1 44444 55555 -1 0 77777 88888 0 Display raw balances of the accounts: using double conversion operator 283.34 0.00 0.00 4444.44 555.55 0.00 0.00 777.77 0.00 0.00 Total of 2 new accounts Total of 2 invalid accounts 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!