Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Object-Oriented Programming - Create a class called Account with all of thes

ID: 3880802 • Letter: C

Question

C++ Object-Oriented Programming - Create a class called Account with all of these requirements met. The Money class is given before the pictures for the requirements of the Account class.

Money.h
=========


#ifndef Money_h
#define Money_h

class Money
{
private:
int dollars, cents;
public:
Money();
Money(int dol, int cen);
Money(int dol);
Money(double amt);
int getPennies() const;
bool isNegative() const;
void add(const Money &m);
void subtract(const Money &m);
bool isEqual(const Money &m);
void print() const;
};

#endif /* Money_h */


Money.cpp
=========

#include "Money.h"
#include <iostream>
using namespace std;
Money::Money()
{
dollars = 0;
cents = 0;
}
Money::Money(int dol, int cen)
{
dollars = dol;
cents = cen;
if(dollars < 0 && cents > 0)
cents = -cents;
if(cents < 0 && dollars > 0)
dollars = -dollars;
  
//keep the dollar and cents adjusted. Don't let cents to be be 100 or more
if(cents > 100)
{
dollars += cents / 100;
cents %= 100;
}
  
}
Money::Money(int dol)
{
dollars = dol;
cents = 0;
}
Money::Money(double amt)
{
dollars = amt;
cents = (amt * 100 - dollars * 100);
}
int Money::getPennies() const
{
return dollars * 100 + cents;
}
bool Money::isNegative() const
{
if(dollars < 0 || cents < 0)
return true;
else
return false;
}
void Money::add(const Money &m)
{
int pennies = getPennies() + m.getPennies();
dollars = pennies / 100;
cents = pennies % 100;
}

void Money::subtract(const Money &m)
{
int pennies = getPennies() - m.getPennies();
dollars = pennies / 100;
cents = pennies % 100;
}
bool Money::isEqual(const Money &m)
{
if(getPennies() == m.getPennies())
return true;
else
return false;
}
void Money::print() const
{
if(isNegative())
cout << "$(" << -dollars << "." << -cents << ")" << endl;
else
cout << "$" << dollars << "." << cents << endl;
  
}

Account Class Requirements The Account class shal have 4 constructors * A default constructor that initializes your Account object with a name “Savings," an interest rate * A constructor that takes a string, double, and Money object to initialize your Account's name * A constructor that takes a string, double, and an integer that represents your initial balance (a clean * A constructor that takes a string, double, and a dou of 0.00%, and an initial balance of $0.00 interest rate, and balance respectively dollar amount) ble that represents your initial balance - std::string getName) const - double getRate ) const - const Money getBalance ) const - void setName (std::string newName) - void deposit (const Money &m;) - void deposit (int d, int c) - void deposit (int d) - void deposit (double d) * For all versions of the deposit function, the function shall not accept negative amounts of money * For all versions of the deposit function, the balance of the account shall be updated accordingly - const Money withdraw (const Money &m;) - const Money withdraw (int d, int c) - const Money withdraw (int d) - const Money withdraw (double d) * For all versions of the withdraw function, the function shall not accept negative amounts of money * For all versions of the withdraw function, the user shall not overdraw by more than $50.00 * For all versions of the withdraw function, the balance of the account shall be updated accordingly - void accrue () The accrued interest shall be calculated and added to the balance - void print ) const * This function shall only pri nt the balance; formatting requirements are identical to that of the Money class * This function prints the amount of Money you have: it must print a "$,' and always show two decimal * Negative amounts of Money shall be represented in the following manner: ($X.Xx) aces - void transferTo (Account &dest;, const Money &amount;) * The function shall not transfer negative amounts of money

Explanation / Answer


// Add Or Subtract Libraries As Needed
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

// Place Money Class Definition Here
class Money
{
    //TODO
    public:
    //Constructors
        Money();
        Money(int digerydoos, int disks);
        Money(int digerydoos);      
        Money(double dubloons);
    //Member functions
        void add(const Money &m);
        bool isEqual(const Money &m);
        void print() const;

    //Accessor functions
        int getPennies() const;

    private:
    //digerydoos are dollars, and disks are pennies
        int digerydoos;
        int disks;
};

// Place Account Class Definition Here
class Account
{
    //TODO
    public:
    //TODO initialize this constructor with string "savings", interest rate 0.00, and bal of $0.00
    //Counstructors
        Account();
        Account(std::string name, double percent, Money monies);
        Account(std::string name, double percent, int digerydoos);
        Account(std::string name, double percent, double dubloons);
  
    //Member functions
        std::string getName() const;
        double getRate() const;
        void setName(std::string newName);
    //Deposit functions
        //cannot accept negative amounts of money.
        void deposit(const Money &m);
        void deposit(int d, int c);
        void deposit(int d);
        void deposit(double d);
    //Withdraw functions
        //cannot accept negative amounts of mone
        //cannot overdraft past $50
        const Money withdraw(const Money &m);
        const Money withdraw(int d, int c);
        const Money withdraw(int d);
        const Money withdraw(double d);
      
        void accrue();
      
        void print() const;
      
        void transferTo(Account &dest, const Money &amount);
      
    private:
        Money monies;
        double interest;
        std::string name;
};

// Define Non-Member Function Here

const Account createAccount();


// Test Functions; *** DO NOT ALTER ***
bool moneyGetPennies(int arg);
bool moneyIsNegative(bool expected);
bool moneyAdd();
bool moneySubtractPos();
bool moneySubtractNeg();
void moneyPrint(double arg);
bool acctGetName();
bool acctGetRate();
bool acctGetBalance();
bool acctSetName();
bool acctDep01();
bool acctDep02();
bool acctDep03();
bool acctDep04();
void acctBadDep();
bool acctWith01();
bool acctWith02();
bool acctWith03();
bool acctWith04();
bool acctWithPart01();
bool acctWithPart02();
void acctBadWith();
bool acctAccrue();
void acctPrint(double arg);
bool acctTransfer();

// Main Function; *** DO NOT ALTER ***
int main()
{
   char prev = std::cout.fill('.');

   std::cout << "*** TESTING MONEY CLASS FUNCTIONS *** ";
   std::cout << std::setw(57) << std::left << "1. Testing Money class GetPennies:" << (moneyGetPennies(555)) << std::endl;
   std::cout << std::setw(57) << std::left << "2. Testing Money class GetPennies:" << (moneyGetPennies(-555)) << std::endl;
   std::cout << std::setw(57) << std::left << "3. Testing Money class IsNegative:" << (moneyIsNegative(true)) << std::endl;
   std::cout << std::setw(57) << std::left << "4. Testing Money class IsNegative:" << (moneyIsNegative(false)) << std::endl;
   std::cout << std::setw(57) << std::left << "5. Testing Money class Add:" << (moneyAdd()) << std::endl;
   std::cout << std::setw(57) << std::left << "6. Testing Money class Subtract:" << (moneySubtractPos()) << std::endl;
   std::cout << std::setw(57) << std::left << "7. Testing Money class Subtract:" << (moneySubtractNeg()) << std::endl;
   std::cout << std::setw(57) << std::left << "8. Testing Money class Print ( Must match $5.44 ):"; (moneyPrint(5.44)); std::cout << std::endl;
   std::cout << std::setw(57) << std::left << "9. Testing Money class Print ( Must match ($5.44) ):"; (moneyPrint(-5.44)); std::cout << std::endl;

   std::cout << " *** TESTING ACCOUNT CLASS FUNCTIONS *** ";
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "1. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class GetName:" << (acctGetName()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "2. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class GetRate:" << (acctGetRate()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "3. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class GetBalance:" << (acctGetBalance()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "4. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class SetName:" << (acctSetName()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "5. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class Deposit #1:" << (acctDep01()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "6. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class Deposit #2:" << (acctDep02()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "7. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class Deposit #3:" << (acctDep03()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "8. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class Deposit #4:" << (acctDep04()) << std::endl;
   std::cout << std::setw(4) << std::left << std::setfill(' ') << "9. " << std::setw(53) << std::left << std::setfill('.') << "Testing Account class Deposit (ERROR):"; acctBadDep();
   std::cout << std::setw(4) << std::left << "10. " << std::setw(53) << std::left << "Testing Account class Withdraw #1:" << (acctWith01()) << std::endl;
   std::cout << std::setw(4) << std::left << "11. " << std::setw(53) << std::left << "Testing Account class Withdraw #2:" << (acctWith02()) << std::endl;
   std::cout << std::setw(4) << std::left << "12. " << std::setw(53) << std::left << "Testing Account class Withdraw #3:" << (acctWith03()) << std::endl;
   std::cout << std::setw(4) << std::left << "13. " << std::setw(53) << std::left << "Testing Account class Withdraw #4:" << (acctWith04()) << std::endl;
   std::cout << std::setw(4) << std::left << "14. " << std::setw(53) << std::left << "Testing Account class Withdraw (OVERDRAW):" << (acctWithPart01()) << std::endl;
   std::cout << std::setw(4) << std::left << "15. " << std::setw(53) << std::left << "Testing Account class Withdraw (PARTIAL):" << (acctWithPart02()) << std::endl;
   std::cout << std::setw(4) << std::left << "16. " << std::setw(53) << std::left << "Testing Account class Withdraw (ERROR):"; acctBadWith();
   std::cout << std::setw(4) << std::left << "17. " << std::setw(53) << std::left << "Testing Account class Accrue: " << (acctAccrue()) << std::endl;
   std::cout << std::setw(4) << std::left << "18. " << std::setw(53) << std::left << "Testing Account class Transfer: " << (acctTransfer()) << std::endl;
   std::cout << std::setw(4) << std::left << "19. " << std::setw(53) << std::left << "Testing Account class Print ( Must match $50.00 ):"; (acctPrint(50.00)); std::cout << std::endl;
   std::cout << std::setw(4) << std::left << "20. " << std::setw(53) << std::left << "Testing Account class Print ( Must match ($50.00) ):"; (acctPrint(-50.00)); std::cout << std::endl;

   Account account = createAccount();
   std::cout << " *** TESTING NON-CLASS FUNCTION CREATEACCOUNT *** ";
   std::cout << std::setw(19) << std::left << "1. Account Name:" << account.getName() << std::endl;
   std::cout << std::setw(19) << std::left << "2. Interest Rate:" << account.getRate() << std::endl;
   std::cout << std::setw(19) << std::left << "3. Balance:"; (account.getBalance()).print(); std::cout << std::endl;

   std::cout.fill(prev);

   return 0;
}


// Implement Money Class Functions Here
//Constructors
Money();
Money(int digerydoos, int disks);
Money(int digerydoos);      
Money(double dubloons);
//Member functions
void add(const Money &m);
bool isEqual(const Money &m);
void print() const;

//Accessor functions
int getPennies() const;


// Implement Account Class Functions Here
//Counstructors
Account();
Account(std::string name, double percent, Money monies);
Account(std::string name, double percent, int digerydoos);
Account(std::string name, double percent, double dubloons);

//Member functions
std::string getName() const;
double getRate() const;
void setName(std::string newName);

//Deposit functions
//cannot accept negative amounts of money.
void deposit(const Money &m);
void deposit(int d, int c);
void deposit(int d);
void deposit(double d);

//Withdraw functions
//cannot accept negative amounts of mone
//cannot overdraft past $50
const Money withdraw(const Money &m);
const Money withdraw(int d, int c);
const Money withdraw(int d);
const Money withdraw(double d);

void accrue();

void print() const;

void transferTo(Account &dest, const Money &amount);

// Implement Non-Member Function Here

const Account createAccount();


// Test Functions; *** DO NOT ALTER ***
bool moneyGetPennies(int arg)
{
   Money money(arg / 100, arg % 100);
   return money.getPennies() == arg;
}

bool moneyIsNegative(bool expected)
{
   if (expected == true) {
       Money money1(-5, 55);
       Money money2(5, -55);
       Money money3(-5);
       Money money4(-5.55);
       return ( ( money1.isNegative() &&
                   money2.isNegative() &&
                   money3.isNegative() &&
                   money4.isNegative() ) == expected );
   } else {
       Money money1(5, 55);
       Money money2(5);
       Money money3(5.55);
       Money money4;
       return ( !( !(money1.isNegative()) &&
                   !(money2.isNegative()) &&
                   !(money3.isNegative()) &&
                   !(money4.isNegative()) ) == expected );
   }
}

void moneyPrint(double arg)
{
   Money money(arg);
   money.print();
}

bool moneyAdd()
{
   Money left(4, 64), right(5, 36);
   left.add(right);
  
   return left.isEqual(10);
}

bool moneySubtractPos()
{
   Money money(20);
   money.subtract(10.11);
  
   return money.isEqual(9.89);
}

bool moneySubtractNeg()
{
   Money money(20);
   money.subtract(30.11);
  
   return money.isEqual(-10.11);
}

bool acctGetName()
{
   Account account;

   return (account.getName() == "Savings");
}

bool acctGetRate()
{
   Account account("Savings", 0.01, Money(5, 0));

   return (account.getRate() == 0.01);
}

void acctPrint(double arg)
{
   Account account("Savings", 0.015, arg);
   account.print();
}

bool acctGetBalance()
{
   Money money(50);
   Account account("Savings", 0.01, money);
   Money bal = account.getBalance();

   return (bal.isEqual(money));
}

bool acctSetName()
{
   std::string name = "ROTH IRA";
   Account account("Savings", 0.01, 5);

   account.setName(name);
   return (name == account.getName());
}

bool acctDep01()
{
   Money money(5.63);
   Account account;

   account.deposit(money);

   return ((account.getBalance()).isEqual(money));
}

bool acctDep02()
{
   Account account;

   account.deposit(5, 63);

   return ((account.getBalance()).isEqual(5.63));
}

bool acctDep03()
{
   Account account;

   account.deposit(20);

   return ((account.getBalance()).isEqual(20));
}

bool acctDep04()
{
   Account account;

   account.deposit(5.63);

   return ((account.getBalance()).isEqual(5.63));
}


void acctBadDep()
{
   Account account;
   account.deposit(-50);
}

bool acctWith01()
{
   Money money(25.44);
   Account account("Savings", 0.015, 50);

   Money w = account.withdraw(money);

   return (w.isEqual(money) && (account.getBalance()).isEqual(24.56));
}

bool acctWith02()
{
   Account account("Savings", 0.015, 50);

   Money w = account.withdraw(25, 44);

   return (w.isEqual(25.44) && (account.getBalance()).isEqual(24.56));
}

bool acctWith03()
{
   Account account("Savings", 0.015, 50);

   Money w = account.withdraw(24);

   return (w.isEqual(24) && (account.getBalance()).isEqual(26));
}

bool acctWith04()
{
   Account account("Savings", 0.015, 50);

   Money w = account.withdraw(25.44);

   return (w.isEqual(25.44) && (account.getBalance()).isEqual(24.56));
}

bool acctWithPart01()
{
   Account account("Checking", 0.015, 54.33);

   Money w = account.withdraw(60);

   return w.isEqual(60) && (account.getBalance()).isEqual(-5.67);
}

bool acctWithPart02()
{
   Account account("Checking", 0.015, 54.33);

   Money w = account.withdraw(300);

   return w.isEqual(104.33) && (account.getBalance()).isEqual(-50);
}

void acctBadWith()
{
   Account account;

   account.withdraw(-10);
}

bool acctAccrue()
{
   Account account("Roth", 0.07, 1000);
  
   account.accrue();

   return (account.getBalance()).isEqual(1070);
}

bool acctTransfer()
{
   Account savings;
   Account checking("Checking", 0.015, 2500);

   checking.transferTo(savings, 500);

   return (savings.getBalance()).isEqual(500) && (checking.getBalance()).isEqual(2000);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote