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

Write a checkbook balancing program. The program will read in, from a user selec

ID: 3702976 • Letter: W

Question

Write a checkbook balancing program. The program will read in, from a user selected input file, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0 in the file, boolean in the array). Use an array with the class at the type. The class should be a class for a check. There should be three member variables to record the check number, the check amount, and whether or not the check was cashed. The class for a check will have a member variable of type Money (as defined on page 662 in the book; Display 11.9) to record the check amount. So, you will have a class used within a class. The class for a check should have accessor and mutator functions as well as constructors and functions for both input and output of a check. In addition to the checks, the program also reads all the deposits (from the console; cin), the old and the new account balance (read this in from the user at the console; cin). You may want another array to hold the deposits. The new account balance should be the old balance plus all deposits, minus all checks that have been cashed. The program outputs the total of the checks cashed, the total of the deposits, what the new balance should be, and how much this figure differs from what the bank says the new balance is. It also outputs two lists of checks: the checks cashed since the last time you balanced your checkbook and the checks still not cashed. Display both lists of checks in sorted order from lowest to highest check number.

Display 11.9

class Money

{

public:

friend Money operator +(const Money& amount1, const Money& amount2);

friend Money operator -(const Money& amount1, const Money& amount2);

friend Money operator -(const Money& amount);

friend bool operator ==(const Money& amount1, const Money& amount2);

Money (long dollars, int cents);

Money (long dollars);

Money ();

  

double getValue() const;

  

friend istream& operator >>(istream& ins, Money& amount);

friend ostream& operator <<(ostream& outs, const Money& amount);

  

private:

long allCents;

};

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

//Class for amounts of money in U.S. currency.
class Money
{
public:
friend const Money operator +(const Money& amount1, const Money& amount2);
friend ostream &operator<<( ostream &, const Money& );
friend istream &operator>>( istream &, Money& );
Money( );
Money(double amount);
Money(int theDollars, int theCents);
Money(int theDollars);
double getAmount( ) const;
int getDollars( ) const;
int getCents( ) const;
int setCents(int ) ;
int setDollars( int );
bool operator >( const Money& amount2);
bool operator >=( const Money& amount2);
//void set(int, int); //
void input( ); //Reads the dollar sign as well as the amount number.
void output( ) const;
// const Money operator + (const Money& yourAmount) const;
const Money operator ++(); //increase dollars and cents by 1, prefix ++
private:
int dollars; //A negative amount is represented as negative dollars and
int cents; //negative cents. Negative $4.50 is represented as -4 and -50

int dollarsPart(double amount) const;
int centsPart(double amount) const;
int round(double number) const;
};

const Money operator -(const Money& amount1, const Money& amount2);

bool operator ==(const Money& amount1, const Money& amount2);

const Money operator -(const Money& amount);


Money::Money( ): dollars(0), cents(0)
{/*Body intentionally empty.*/}

Money::Money(double amount)
: dollars(dollarsPart(amount)), cents(centsPart(amount))
{/*Body intentionally empty*/}

Money::Money(int theDollars)
: dollars(theDollars), cents(0)
{/*Body intentionally empty*/}

//Uses cstdlib:
Money::Money(int theDollars, int theCents)
{
if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0))
{
cout << "Inconsistent money data. ";
exit(1);
}
dollars = theDollars;
cents = theCents;
}

double Money::getAmount( ) const
{
return (dollars + cents*0.01);
}

int Money::getDollars( ) const
{
return dollars;
}

int Money::getCents( ) const
{
return cents;
}
int Money::setCents(int amount )
{
cents=amount;
}
int Money::setDollars( int amount)
{
dollars=amount;
}
//Uses iostream and cstdlib:
void Money::output( ) const
{
int absDollars = abs(dollars);
int absCents = abs(cents);
if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0
cout << "$-";
else
cout << '$';
cout << absDollars;

if (absCents >= 10)
cout << '.' << absCents;
else
cout << '.' << '0' << absCents;
}

//Uses iostream and cstdlib:
void Money::input( )
{
char dollarSign;
cin >> dollarSign; //hopefully
if (dollarSign != '$')
{
cout << "No dollar sign in Money input. ";
exit(1);
}

double amountAsDouble;
cin >> amountAsDouble;
dollars = dollarsPart(amountAsDouble);
cents = centsPart(amountAsDouble);
}

int Money::dollarsPart(double amount) const
{
return static_cast<int>(amount);
}

int Money::centsPart(double amount) const
{
double doubleCents = amount*100;
int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives
if (amount < 0)
intCents = -intCents;
return intCents;
}

int Money::round(double number) const
{
return static_cast<int>(floor(number + 0.5));
}

/*
const Money Money::operator +(const Money & yourAmount) const
{
return Money(dollars+yourAmount.dollars, cents + yourAmount.cents);
}
*/

const Money Money::operator ++()
{
++dollars;
++cents;
return Money(dollars, cents);
}

const Money operator +(const Money& amount1, const Money& amount2)
{
//Money sum;

//int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents1 = amount1.cents + amount1.dollars*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int sumAllCents = allCents1 + allCents2;
int absAllCents = abs(sumAllCents); //Money can be negative.
int finalDollars = absAllCents/100;
int finalCents = absAllCents%100;

if (sumAllCents < 0)
{
finalDollars = -finalDollars;
finalCents = -finalCents;
}

//sum.set(finalDollars, finalCents);
//return sum;
return Money(finalDollars, finalCents);
}

//Uses cstdlib:
const Money operator -(const Money& amount1, const Money& amount2)
{
int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int diffAllCents = allCents1 - allCents2;
int absAllCents = abs(diffAllCents);
int finalDollars = absAllCents/100;
int finalCents = absAllCents%100;

if (diffAllCents < 0)
{
finalDollars = -finalDollars;
finalCents = -finalCents;
}

return Money(finalDollars, finalCents);
}

bool operator ==(const Money& amount1, const Money& amount2)
{
return ((amount1.getDollars( ) == amount2.getDollars( ))
&& (amount1.getCents( ) == amount2.getCents( )));
}
bool Money::operator >( const Money& amount2)
{
if(getDollars( ) > amount2.getDollars( ))
      return true;
if((getDollars( ) == amount2.getDollars( ))&&
   (getCents( ) > amount2.getCents( )))
      return true;
return false;
}
bool Money::operator >=(const Money& amount2)
{
if(getDollars( ) > amount2.getDollars( ))
      return true;
if((getDollars( ) == amount2.getDollars( ))&&
   (getCents( ) > amount2.getCents( )))
      return true;
return ((getDollars( ) == amount2.getDollars( ))
&& (getCents( ) == amount2.getCents( )));   

}
const Money operator -(const Money& amount)
{
return Money(-amount.getDollars( ), -amount.getCents( ));
}
ostream& operator<<( ostream &output, const Money& amount2 )

{int absDollars = abs(amount2.getDollars( ));
int absCents = abs(amount2.getCents( ));
if (amount2.getDollars( ) < 0 || amount2.getCents( ) < 0)//accounts for dollars == 0 or cents == 0
cout << "$-";
else
cout << '$';
cout << absDollars;

if (absCents >= 10)
cout << '.' << absCents;
else
cout << '.' << '0' << absCents;
return output;

}
istream &operator>>( istream & input, Money& amount2 )

{char dollarSign;
cin >> dollarSign; //hopefully
if (dollarSign != '$')
{
cout << "No dollar sign in Money input. ";
exit(1);
}

double amountAsDouble;
cin >> amountAsDouble;
amount2.setDollars(amount2.dollarsPart(amountAsDouble));
amount2.setCents( amount2.centsPart(amountAsDouble));
}

int main( )
{
Money yourAmount, myAmount(10, 9);
cout << "Enter an amount of money: ";
yourAmount.input( );

cout << "Your amount is ";
yourAmount.output( );
cout << endl;
cout << "My amount is ";
myAmount.output( );
cout << endl;
cout<<"Enter an amount ";

++myAmount;
cout << "++myAmount is ";
myAmount.output( );
cout << endl;
if (yourAmount >= myAmount)
cout << "We have the same amounts. or you have more ";
else
cout << "I am richer. ";
if (myAmount > yourAmount)
cout << "I have more. ";
else
cout << "you have more or we have the same. ";
cin>>myAmount;
cout<<myAmount<<endl;
Money ourAmount = yourAmount + myAmount;
yourAmount.output( ); cout << " + "; myAmount.output( );
cout << " equals "; ourAmount.output( ); cout << endl;

Money diffAmount = yourAmount - myAmount;
yourAmount.output( ); cout << " - "; myAmount.output( );
cout << " equals "; diffAmount.output( ); cout << endl;
system("pause");
return 0;
}

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