CSC 213 Problems Sec 7.2, Fall 2007
Notre Dame University–Louaize · Program Design and Data Abstraction II · 3 pages
More CSC 213 files
- Problems Assignment 1 · Fall 2012ProblemsView
- Problems Assignment 2 · Fall 2012ProblemsView
- Problems Sec 10 · Fall 2007ProblemsView
- Problems Sec 12.2 · Fall 2007ProblemsView
- Problems Sec 12.1 · Fall 2007ProblemsView
- Problems Sec 7 · Fall 2007ProblemsView
- Problems Sec 6 · Fall 2007ProblemsView
- Problems Sec 14 · Fall 2007ProblemsView
Show the text of this problems
Exercice #7-2
1
#ifndef SAVACCT_H
#define SAVACCT_H
class SavingAccount {
private:
unsigned acctNbr;
double annualInterestRate;
double balance;
public:
friend double calAnlInt(SavingAccount&);
SavingAccount();
SavingAccount(unsigned, double, double);
void setAccount(unsigned, double, double);
void debTrans(double amnt);
void crdTrans(double amnt);
unsigned getAcctNbr() const;
double getRate() const;
double getBalance() const;
void setAcctNbr(unsigned);
void setRate(double);
void setBalance(double);
};
#endif
Modify your class SavingAccount by allowing the external function that
calculates the annual interest to access all the classe’s members (private and
public)
SavAcct.h
Exercice #7-2
2
#include <iostream.h>
#include "SavAcct.h"
SavingAccount::SavingAccount() {
setAccount(0, 0.0, 0.0);
}
SavingAccount::SavingAccount(unsigned nbr,
double amnt, double rate){
setAccount(nbr, amnt, rate);
}
void SavingAccount::setAccount(unsigned nbr,
double amnt,
double rate){
setAcctNbr(nbr);
setBalance(amnt);
setRate(rate);
}
void SavingAccount::setAcctNbr(unsigned nbr){
acctNbr = nbr > 1000 && nbr < 10000 ? nbr: 0;
}
void SavingAccount::setBalance(double amnt){
balance = amnt > 0.0 ? amnt : 0.0;
}
double SavingAccount::getRate() const {
return annualInterestRate;
}
SavAcct.cpp
Exercice #7-2
3
void SavingAccount::setRate(double rate) {
annualInterestRate =
(rate > 0.0 && rate <=100) ?
rate : 0.0;
}
unsigned SavingAccount::getAcctNbr() const {
return acctNbr;
}
double SavingAccount::getBalance() const {
return balance;
}
void SavingAccount::debTrans(double amnt) {
amnt = amnt > balance ? amnt : 0.0;
balance -= amnt;
}
void SavingAccount::crdTrans(double amnt) {
amnt = amnt > 0.0 ? amnt : 0.0;
balance += amnt;
}
Exercice #7-2
4
#include <iostream.h>
#include <iomanip.h>
#include "SavAcct.h"
void debit(SavingAccount&);
void credit(SavingAccount&);
void monthly(SavingAccount&);
void annual(SavingAccount&);
double calAnlInt(SavingAccount&);
double calMonInt(SavingAccount&);
int getChoice();
void main() {
SavingAccount acctOne(32123,20000.55, 3.0);
int choice;
cout << setiosflags(ios::showpoint |
ios::fixed) << setprecision(2);
choice = getChoice();
while(choice >=0 && choice <=3) {
switch (choice) {
case 0:debit(acctOne);
break;
case 1:credit(acctOne);
break;
case 2:monthly(acctOne);
break;
case 3:annual(acctOne);
break;
};
choice = getChoice();
}
}
Prog.cpp
Exercice #7-2
5
int getChoice() {
int choice;
cout <<"\nEnter: \n"
<< "\t 0) Debit Account.\n"
<< "\t 1) Credit Account.\n"
<< "\t 2) Calculate Monthly Interest.\n"
<< "\t 3) Calculate Yearly Interest.\n"
<< "\t 9) Exit" << endl;
cin >> choice;
return choice;
}
void debit(SavingAccount &acct) {
double amnt;
cout << "Enter amount to debit: ";
cin >> amnt;
acct.debTrans(amnt);
cout << "Account #: " << acct.getAcctNbr();
cout << "\nBalance :$ " << acct.getBalance();
cout << endl;
}
void credit(SavingAccount &acct) {
double amnt;
cout << "Enter amount to credit: ";
cin >> amnt;
acct.crdTrans(amnt);
cout << "Account #: " << acct.getAcctNbr();
cout << "\nBalance :$ " << acct.getBalance();
cout << endl;
}
Exercice #7-2
6
void monthly(SavingAccount &acct) {
cout << "Account #: " << acct.getAcctNbr();
cout << "\nBalance :$ " << acct.getBalance();
cout << "\nRate :$ " << acct.getRate();
cout << "\nInterest :$ " << calMonInt(acct);
cout << endl;
}
void annual(SavingAccount &acct) {
cout << "Account #: " << acct.getAcctNbr();
cout << "\nBalance :$ " <acct.getBalance();
cout << "\nRate :$ " << acct.getRate();
cout << "\nInterest :$ " << calAnlInt(acct);
cout << endl;
}
double calAnlInt(SavingAccount & acctRef) {
return acctRef.balance *
acctRef.annualInterestRate / 100.0;
}
double calMonInt(SavingAccount & acctRef) {
return calAnlInt(acctRef) / 12;
}