Posted on

This is study resource 12 for the 24-week cohort during the summer of 2019. There is only one question for this study resource.

Click here to see all the study resources.

Question #1

CH12 #7: Write a checkbook balancing program. The program will read in, from the console, 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, boolean in the array). Use an array with the class as 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 b, 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. [ edit: if you can, Display both lists of checks in sorted order from lowest to highest check number.]

This is a rough answer – I did not get full credit for it. Please work it out.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int digit_to_int(char c);

//Money Class
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);
    friend bool operator <(const Money& amount1, const Money& amount2);
    Money(long dollars, int cents);
    Money(long dollars);
    Money();
    double get_value() const;
    friend istream& operator >>(istream& ins, Money& amount);
    friend ostream& operator <<(ostream& outs, const Money& amount);

  private:
    long all_cents;
};

Money operator +(const Money& amount1, const Money& amount2){
  Money temp;
  temp.all_cents = amount1.all_cents + amount2.all_cents;
  return temp;
}

Money operator -(const Money& amount1, const Money& amount2){
  Money temp;
  temp.all_cents = amount1.all_cents - amount2.all_cents;
  return temp;
}

Money operator -(const Money& amount){
  Money temp;
  temp.all_cents = -amount.all_cents;
  return temp;
}

bool operator ==(const Money& amount1, const Money& amount2){
  return (amount1.all_cents == amount2.all_cents);
}

bool operator <(const Money& amount1, const Money& amount2){
  return (amount1.all_cents < amount2.all_cents);
}

int digit_to_int(char c){
  return static_cast<int>(c) - static_cast<int>('0');
}

istream& operator >>(istream& ins, Money& amount){
  char one_char, decimal_point, digit1, digit2;
  long dollars;
  int cents;
  bool negative;
  ins >> one_char;
  if(one_char == '-'){
    negative = true;
    ins >> one_char;
  }else{
    negative = false;
  }

  ins >> dollars >> decimal_point >> digit1 >> digit2;

  if(one_char != '$' || decimal_point != '.' || !isdigit(digit1) || !isdigit(digit2)){
    cout << "The input is not valid.";
    exit(1);
  }

  cents = digit_to_int(digit1) * 10 + digit_to_int(digit2);
  amount.all_cents = dollars * 100 + cents;

  if(negative){
    amount.all_cents = -amount.all_cents;
  }

  return ins;
}


ostream& operator <<(ostream& outs, const Money& amount){

  long positive_cents, dollars, cents;

  positive_cents = labs(amount.all_cents);
  dollars = positive_cents / 100;
  cents = positive_cents % 100;
  if(amount.all_cents < 0){
    outs << "-$" << dollars << '.';
  }else{
    outs << "$" << dollars << ".";
  }

  if(cents < 10){
    outs << '0';
  }

  outs << cents;

  return outs;
}

Money::Money(long dollars, int cents){
  if(dollars*cents < 0){
    cout << "Invalid input for dollars and cents." << endl;
    exit(1);
  }
  all_cents = dollars * 100 + cents;
}

Money::Money(long dollars) : all_cents(dollars * 100){}
Money::Money() : all_cents(0) {}

double Money::get_value() const{
  return (all_cents*0.01);
}



//Check Class
class Check{

  public: 
    Check();
    Check(string chkNo, Money chkAmt, bool chkCashd);
    void setNumber(string no);
    void setAmount(Money amt);
    void setCashed(bool cashd);
    string getNumber();
    Money getAmount();
    bool getCashed();

    friend istream& operator >>(istream& ins, Check& chk);
    friend ostream& operator <<(ostream& outs, const Check& chkInfo);

  private:
    string number;
    Money amount;
    bool cashed;


};

Check::Check() : number(""), amount(), cashed(false) {}
Check::Check(string chkno, Money chkAmt, bool chkCashd){
  this->number = chkno;
  this->amount = chkAmt;
  this->cashed = chkCashd;
}

void Check::setNumber(string no){
  this->number = no;
}

void Check::setAmount(Money amt){
  this->amount = amt;
}

void Check::setCashed(bool cashd){
  this->cashed = cashd;
}

string Check::getNumber(){
  return this->number;
}

Money Check::getAmount(){
  return this->amount;
}

bool Check::getCashed(){
  return this->cashed;
}


istream& operator >>(istream& ins, Check& chk){
  int cashd;
  string no;
  Money amt;

  while(!(ins >> no) || !(ins >> amt) || !(ins >> cashd)){
    ins.clear();
    cin.ignore(80,'\n');
    cout << "Invalid input for check." << endl;
  }

  cin.ignore(80,'\n');
  chk.number = no;
  chk.amount = amt;
  chk.cashed = cashd;

  return ins;
}


ostream& operator <<(ostream& outs, const Check& chkInfo){
  outs << "Check no: " << chkInfo.number << ", Amount: " << chkInfo.amount << ", ";
  if(chkInfo.cashed){
    outs << "Cashed";
  }else{
    outs << "Not Cashed";
  }
  return outs;
}

int main() {
  const int size = 10;
  Check checks[size];
  int count = 0, k=0, j=0;
  double initBal, finalBal, deposit, checkAmts = 0, depositAmts = 0, checkCasedAmts = 0;

  double deposits[size];
  char ans;
  vector<string> uncashedChecks, cashedChecks;

  cout << "Enter the initial account balance: ";
  cin >> initBal;

  cout << "Enter deposit amount (max 5, -1 to stop):" <<endl;
  cin >> deposit;

  while(deposit != -1 && count<size){
    deposits[count] = deposit;
    count++;
    cin >> deposit;
  }

  for(int i=0; i<count; i++){
    initBal = initBal + deposits[i];
    depositAmts = depositAmts + deposits[i];
  }

  count = 0;

  do{
    cout << "Enter check details (number amount if cashed or not (using 1:cashed, 0: not cashed)): "<<endl;
      cin >> checks[count];
      cout << "Would you like to enter another checks details? (y/n): ";
      cin >> ans;
      count++;
  }while( ans == 'y' || ans == 'Y');

  for(int i = 0; i < size; i++){
    double value = (checks[i].getAmount()).get_value();
    if(checks[i].getCashed()){
      initBal = initBal - value;
      checkCasedAmts += value;
      cashedChecks.push_back(checks[i].getNumber());
    }else{
      if(checks[i].getNumber() != ""){
        uncashedChecks.push_back(checks[i].getNumber());
      }
    }
    checkAmts += value;
  }

  
  sort(uncashedChecks.begin(), uncashedChecks.end());
  sort(cashedChecks.begin(), cashedChecks.end());

  cout << "\nThe final balance is: $" << initBal << endl;
  cout << "The total amount of all deposits made: $" << depositAmts << endl;
  cout << "The total amount of all checks: $" << checkAmts << endl;
  cout << "The total amount of all checks cashed: $" << checkCasedAmts << endl;

  if(cashedChecks.size() > 0){
    cout << "The sorted list of cashed checks is: " << endl;
    for(int i = 0; i < cashedChecks.size(); i++){
      cout << cashedChecks[i] << "\t";
    }
    cout << endl;
  }

  if(uncashedChecks.size() > 0){
    cout << "The sorted list of uncashed checks is: " << endl;
    for(int i = 0; i < uncashedChecks.size(); i++){
      cout << uncashedChecks[i] << "\t";
    }
    cout << endl;
  }

  return 0;
}

Photo by Oskar Yildiz on Unsplash

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.