Winter 2020

PIC 10A: Introduction to Programming

Discussion Section 3A

Wonjun Lee



Week8

Tuesday

Exercise P5.1.

Implement all member functions of the following class:

Solution
#include < iostream >
#include < string >

using namespace std;

class Person 
{ 
  public:
    Person(); 
    Person(string pname, int page); 
    void get_name() const; 
    void get_age() const; 
  private:
    string name; 
    int age; // 0 if unknown 
};

// TODO
/*

  Implement all member functions of the class Person.

*/

Person::Person()
{
  name = "";
  age  = 0;  
}

Person::Person(string pname, int page)
{
  name = pname;
  age  = page;
}

void Person::get_name() const
{
  cout << "name : " << name << endl;
}

void Person::get_age() const
{
  cout << "age  : " << age << endl;
}

// ------------------------------
// DON'T MODIFY BELOW

int main()
{
  Person person1();
  Person person2("James", 10);

  cout << "person 1" << endl;
  person1.get_name();
  person1.get_age();
  cout << "person 2" << endl;
  person2.get_name();
  person2.get_age();
}
            
Exercise P5.4.

Implement a class Account. An account has a balance, functions to add and withdraw money, and a function to query the current balance. Charge a $5 penalty if an attempt is made to withdraw more money than available in the account.

Solution
#include < iostream >
#include < string >

using namespace std;

class Account 
{ 
  public:
    Account();
    Account(double pbalance);
    void add(double money);
    void withdraw(double money);
    void query() const;
  private:
    double balance;
};

// TODO
/*

  Implement all member functions of the class Account.

*/

Account::Account()
{
  balance = 0;
}

Account::Accoutn(double pbalance)
{
  balance = pbalance;
}

void Account::add(double money)
{
  balance += money;
}

void Account::withdraw(double money)
{
  if(money > balance)
  {
    balance -= 5;
  }
  else
  {
    balance -= money;
  }
}

void Account::query()
{
  cout << "Your current balance is $ " << balance << "." << endl;
}

// ------------------------------
// DON'T MODIFY BELOW

int main()
{
  Account acc1(100);

  acc1.add(200);
  acc1.withdraw(50);

  acc1.query(); // Expect to print out $ 250

  acc1.withdraw(300);

  acc1.query(); // Expect to print out $ 245
}
            

Thursday

Exercise P5.6.

Implement a class Bank. This bank has two objects, checking and savings, of the type Account that was developed in Exercise P5.4. Implement four member functions:

void deposit(double amount, string account)

void withdraw(double amount, string account)

void transfer(double amount, string account)

void print_balances()

Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer it indicates the account from which the money is taken; the money is automatically transferred to the other account.

Solution
#include < iostream >
#include < string >

using namespace std;

class Account
{
public:
  Account();
  Account(double pbalance);

  void add(double money);    // mutator
  void withdraw(double money); // mutator

  double get_balance() const;
  void query() const; // accessor
private:
  double balance;
}; // Important to put a semicolon at the end of the class

Account::Account()
{
  balance = 0;
}

Account::Account(double pbalance)
{
  balance = pbalance;
}

void Account::add(double money)
{
  // balance = balance + money;
  balance += money; 
}

void Account::withdraw(double money)
{
  if(money > balance){
    cout << "that was not cool" << endl;
    balance -= 5;
  }else{
    balance -= money; 
  }
}

double Account::get_balance() const
{
  return balance;
}

void Account::query() const
{
  cout << "--------------------------------" << endl;
  cout << "    Your Current Balance" << endl;
  cout << "          $ " << balance << endl;
  cout << "--------------------------------" << endl;
}


// ------ BANK ------

class Bank
{
public:
  Bank();

  void deposit(double amount, string account);  //mutator
  void withdraw(double amount, string account); //mutator
  void transfer(double amount, string account); //mutator
  void print_balances() const;  // accessor
private:
  Account checking;
  Account saving;
};

Bank::Bank()
{
  checking = Account();
  saving   = Account();
}

void Bank::deposit(double amount, string account)
{
  if(account == "C"){
    checking.add(amount);
  }else if(account == "S"){
    saving.add(amount);
  }else{
    cout << "Wrong Input" << endl;
  }
}

void Bank::withdraw(double amount, string account)
{
  if(account == "C"){
    checking.withdraw(amount);
  }else if(account == "S"){
    saving.withdraw(amount);
  }else{
    cout << "Wrong Input" << endl;
  }
}

void Bank::transfer(double amount, string account)
{
  if(account == "C"){
    checking.withdraw(amount);
    saving.add(amount);
  }else if(account == "S"){
    saving.withdraw(amount);
    checking.add(amount);
  }else{
    cout << "Wrong Input" << endl;
  }
}

void Bank::print_balances() const
{
  cout << "     CHECKING   " << endl;
  checking.query();
  cout << "     SAVING" << endl;
  saving.query();
}

int main()
{
  Bank B1;

  while(true)
  {
    cout << "1. add \t 2. withdraw \t 3. transfer \t 4. query" << endl;

    int choice;
    cin >> choice;

    if(choice == 1){
      cout << "ADD" << endl;
      cout << "Which account? : ";
      string acc;
      cin >> acc;
      cout << "How much? : ";
      double amount;
      cin >> amount;

      B1.deposit(amount, acc);

    }else if(choice == 2){
      cout << "WITHDRAW" << endl;
      cout << "Which account? : ";
      string acc;
      cin >> acc;
      cout << "How much? : ";
      double amount;
      cin >> amount;

      B1.withdraw(amount, acc);

    }else if(choice == 3){
      cout << "TRANSFER" << endl;
      cout << "Which account? : ";
      string acc;
      cin >> acc;
      cout << "How much? : ";
      double amount;
      cin >> amount;

      B1.transfer(amount, acc);

    }else if(choice == 4){
      cout << "QUERY" << endl;
      B1.print_balances();
    }else{
      break;
    }

  }
}