Please help, need help fast! C++ Object-Oriented Programing - I\'m having a prob
ID: 3881645 • Letter: P
Question
Please help, need help fast! C++ Object-Oriented Programing - I'm having a problem with my withdraw functions that are within a class called Account, for some reason they aren't getting the correct numbers. I've included the functions that are all invovled within the withdraw function. I just need help figuring out what is the problem. The functions involved in the withdraw function are: isNegative(), getPennies(), and subtract().
class Money
{
public:
Money();
Money(int dol, int cen);
Money(int dol);
Money(double amt);
int getPennies() const;
bool isNegative() const;
void setMoney(int d, int c);
void add(const Money &m);
void subtract(const Money &m);
bool isEqual(const Money &m) const;
void print() const;
private:
int dollars, cents;
};
// Place Account Class Definition Here
class Account
{
public:
Account();
Account(std::string accountName, double intRate, Money money);
Account(std::string accountName, double intRate, int balance);
Account(std::string accountName, double intRate, double balance);
std::string getName() const;
double getRate() const;
const Money getBalance() const;
void setName(std::string newName);
void setRate(double x);
void setMoney(Money x);
void deposit(const Money &m);
void deposit(int d, int c);
void deposit(int d);
void deposit(double d);
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 money;
double intRate;
std::string accountName;
};
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;
}
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;
}
void Money::setMoney(int d, int c)
{
dollars = d;
cents = c;
}
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) const
{
if(getPennies() == m.getPennies()){
return true;
}
else {
return false;
}
}
void Money::print() const
{
if(isNegative()) {
std::cout << "($" << -dollars << "." << -cents << ")" << std::endl;
}
else {
std::cout << "$" << dollars << "." << cents << std::endl;
}
}
// Implement Account Class Functions Here
Account::Account() : money(0,00), intRate(0), accountName("Savings")
{
}
Account::Account(std::string accountName, double intRate, Money balance)
{
this->accountName = accountName;
this->intRate = intRate;
this->money = balance;
}
Account::Account(std::string accountName, double intRate, int balance)
{
this->accountName = accountName;
this->intRate = intRate;
this->money = balance;
}
Account::Account(std::string accountName, double intRate, double balance)
{
this->accountName = accountName;
this->intRate = intRate;
this->money = balance;
}
std::string Account::getName() const
{
return accountName;
}
double Account::getRate() const
{
return intRate;
}
const Money Account::getBalance() const
{
return (((double)money.getPennies())/100);
}
void Account::setName(std::string newName)
{
accountName = newName;
}
void Account::setRate(double x)
{
intRate = x;
}
void Account::setMoney(Money x)
{
money = x;
}
void Account::deposit(const Money &m)
{
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std::endl;
return;
}
money.add(m);
return;
}
void Account::deposit(int d, int c)
{
Money m(d,c);
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std::endl;
return;
}
money.add(m);
return;
}
void Account::deposit(int d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std:: endl;
return;
}
money.add(m);
return;
}
void Account::deposit(double d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std::endl;
return;
}
money.add(m);
return;
}
const Money Account::withdraw(const Money &m)
{
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
if((((double)m.getPennies() - money.getPennies())/100) > 50){
return -1;
}
money.subtract(m);
return money;
}
const Money Account::withdraw(int d, int c)
{
Money m(d,c);
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
if((((double)m.getPennies() - money.getPennies())/100) > 50){
return -1;
}
money.subtract(m);
return money;
}
const Money Account::withdraw(int d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
if((((double)m.getPennies() - money.getPennies())/100) > 50){
return -1;
}
money.subtract(m);
return money;
}
const Money Account::withdraw(double d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
if((((double)m.getPennies() - money.getPennies())/100) > 50){
return -1;
}
money.subtract(m);
return money;
}
void Account::accrue()
{
double interest = getRate() * money.getPennies();
money.add(interest);
}
void Account::print() const
{
money.print();
}
void Account::transferTo(Account &dest, const Money &amount)
{
if(amount.isNegative() == true){
std::cout << "Cannot transfer negative amount of money" << std::endl;
return;
}
dest.money.add(amount);
return;
}
Explanation / Answer
// Changes made are present in Bold
#include <iostream>
using namespace std;
class Money
{
public:
Money();
Money(int dol, int cen);
Money(int dol);
Money(double amt);
int getPennies() const;
bool isNegative() const;
void setMoney(int d, int c);
void add(const Money &m);
void subtract(const Money &m);
bool isEqual(const Money &m) const;
void print() const;
private:
int dollars, cents;
};
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;
}
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;
}
void Money::setMoney(int d, int c)
{
dollars = d;
cents = c;
}
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) const
{
if(getPennies() == m.getPennies()){
return true;
}
else {
return false;
}
}
void Money::print() const
{
if(isNegative()) {
std::cout << "($" << -dollars << "." << -cents << ")" << std::endl;
}
else {
std::cout << "$" << dollars << "." << cents << std::endl;
}
}
// Place Account Class Definition Here
class Account
{
public:
Account();
Account(std::string accountName, double intRate, Money money);
Account(std::string accountName, double intRate, int balance);
Account(std::string accountName, double intRate, double balance);
std::string getName() const;
double getRate() const;
const Money getBalance() const;
void setName(std::string newName);
void setRate(double x);
void setMoney(Money x);
void deposit(const Money &m);
void deposit(int d, int c);
void deposit(int d);
void deposit(double d);
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 money;
double intRate;
std::string accountName;
};
// Implement Account Class Functions Here
Account::Account() : money(0,00), intRate(0), accountName("Savings")
{}
Account::Account(std::string accountName, double intRate, Money balance)
{
this->accountName = accountName;
this->intRate = intRate;
this->money = balance;
}
Account::Account(std::string accountName, double intRate, int balance)
{
this->accountName = accountName;
this->intRate = intRate;
this->money = balance;
}
Account::Account(std::string accountName, double intRate, double balance)
{
this->accountName = accountName;
this->intRate = intRate;
this->money = balance;
}
std::string Account::getName() const
{
return accountName;
}
double Account::getRate() const
{
return intRate;
}
const Money Account::getBalance() const
{
return (((double)money.getPennies())/100);
}
void Account::setName(std::string newName)
{
accountName = newName;
}
void Account::setRate(double x)
{
intRate = x;
}
void Account::setMoney(Money x)
{
money = x;
}
void Account::deposit(const Money &m)
{
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std::endl;
return;
}
money.add(m);
return;
}
void Account::deposit(int d, int c)
{
Money m(d,c);
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std::endl;
return;
}
money.add(m);
return;
}
void Account::deposit(int d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std:: endl;
return;
}
money.add(m);
return;
}
void Account::deposit(double d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot deposit negative amount." << std::endl;
return;
}
money.add(m);
return;
}
const Money Account::withdraw(const Money &m)
{
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
// if money to withdraw is greater than the money present in account, then withdrawal cannot be done
if((((double)m.getPennies() - money.getPennies())) > 0){
return -1;
}
money.subtract(m);
return money;
}
const Money Account::withdraw(int d, int c)
{
Money m(d,c);
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
// if money to withdraw is greater than the money present in account, then withdrawal cannot be done
if((((double)m.getPennies() - money.getPennies())) > 0){
return -1;
}
money.subtract(m);
return money;
}
const Money Account::withdraw(int d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
// if money to withdraw is greater than the money present in account, then withdrawal cannot be done
if((((double)m.getPennies() - money.getPennies())) > 0){
return -1;
}
money.subtract(m);
return money;
}
const Money Account::withdraw(double d)
{
Money m(d);
if(m.isNegative() == true){
std::cout << "Cannot withdraw negative amount." << std::endl;
return -1;
}
// if money to withdraw is greater than the money present in account, then withdrawal cannot be done
if((((double)m.getPennies() - money.getPennies())) > 0){
return -1;
}
money.subtract(m);
return money;
}
void Account::accrue()
{
double interest = getRate() * money.getPennies();
money.add(interest);
}
void Account::print() const
{
money.print();
}
void Account::transferTo(Account &dest, const Money &amount)
{
if(amount.isNegative() == true){
std::cout << "Cannot transfer negative amount of money" << std::endl;
return;
}
dest.money.add(amount);
return;
}