To download the code, click here
001: #include <iostream>
002: using std::cin;
003: using std::cout;
004: using std::endl;
005: using std::cerr;
006: using std::ostream;
007: using std::ios;
008:
009: #include <cmath>
010: using std::floor;
011:
012: //to convert cents to pennies
013: const float PENNY = 0.01;
014:
015: //to convert percentages
016: const float ALL = 100.0;
017:
018: class bank_account{
019: public:
020: /*!**************************************************************
021: * default constructor, initializes everything to 0.
022: * @function bank_account
023: * @type constructor
024: ****************************************************************/
025: bank_account();
026:
027: /*!**************************************************************
028: * Constructor to initialize the bank account to $dollars.cents
029: * interest rate is set to rate, as a fraction
030: *
031: * @function bank_account
032: * @type constructor
033: * @param dollars: dollar portion of bank account balance
034: * @param cents: cents portion of bank account balance
035: * @param rate: interest rate as a percentage -- i.e. 4.8.
036: ****************************************************************/
037: bank_account(int dollars, int cents, double rate);
038:
039: /*!**************************************************************
040: * Constructor to initialize the bank account to $dollars
041: * interest rate is set to rate, as a fraction
042: *
043: * @function bank_account
044: * @type constructor
045: * @param dollars: dollar portion of bank account balance
046: * @param rate: interest rate as a percentage -- i.e. 4.8.
047: ****************************************************************/
048: bank_account(int dollars, float rate);
049:
050: /*!**************************************************************
051: * Postcondition: one year of simple interest has been added to
052: * the account balance
053: * @function update
054: * @type void
055: ****************************************************************/
056: void update();
057:
058: /*!**************************************************************
059: * Returns current balance, no change made
060: * @function get_balance
061: * @type double
062: ****************************************************************/
063: const double get_balance();
064:
065: /*!**************************************************************
066: * Returns current interest rate, no change made
067: * @function get_rate
068: * @type double
069: ****************************************************************/
070: const double get_rate();
071:
072: /*!**************************************************************
073: * Precondition: If outs is a file stream, it has already been
074: * connected.
075: * Postcondition: Account balance and interest rate have been
076: * written to the output stream
077: *
078: * @function output
079: * @type void
080: * @param outs: output stream, defaults to cout
081: ****************************************************************/
082: void output (ostream &outs = cout);
083:
084: private:
085: int dollars_part;
086: int cents_part;
087:
088: //expressed as a fraction
089: double interest_rate;
090:
091: /*!**************************************************************
092: * Function returns a fractional representation of the percentage
093: * @function fraction
094: * @type double
095: * @param percentage: value to be converted
096: ****************************************************************/
097: double fraction(double percentage);
098:
099: /*!**************************************************************
100: * Function returns a percentage representation of the fraction
101: * @function percent
102: * @type double
103: * @param fraction: value to be converted
104: ****************************************************************/
105: double percent(double fraction_value);
106:
107: /*!**************************************************************
108: * Function used by all constructors to set initial values
109: * @function set_values
110: * @type void
111: * @param dollars: dollars in account
112: * @param cents: cents in account
113: * @param rate: interest rate of account
114: ****************************************************************/
115: void set_values(int dollars, int cents, double rate);
116: };
117:
118: int main (int argc, char *argv[]){
119: bank_account account1(100, 2.3), account2;
120:
121: cout << "account1 has value:" << endl;
122: account1.output();
123:
124: cout << "account2 has value:" << endl;
125: account2.output();
126:
127: account1 = bank_account(999, 99, 5.5);
128: cout << "account1 now has value:" << endl;
129: account1.output();
130:
131: return EXIT_SUCCESS;
132: }
133:
134: bank_account::bank_account(){
135: this->set_values(0, 0, 0);
136: }
137:
138: bank_account::bank_account(int dollars, int cents, double rate){
139: this->set_values(dollars, cents, rate);
140: }
141:
142: bank_account::bank_account(int dollars, float rate){
143: this->set_values(dollars, 0, rate);
144: }
145:
146: void bank_account::update(){
147: double balance = this->get_balance();
148: balance += this->interest_rate * balance;
149:
150: this->dollars_part = floor(balance);
151: this->cents_part = floor((balance - dollars_part) * ALL);
152:
153: return;
154: }
155:
156: void bank_account::set_values(int dollars, int cents, double rate){
157: if ((dollars < 0) || (cents < 0) || (rate < 0)){
158: cerr << "Illegal values entered, exiting" << endl;
159: exit(1);
160: }
161: this->dollars_part = dollars;
162: this->cents_part = cents;
163: this->interest_rate = fraction(rate);
164:
165: return;
166: }
167:
168: const double bank_account::get_balance(){
169: double balance = this->dollars_part + this->cents_part * PENNY;
170: return balance;
171: }
172:
173: const double bank_account::get_rate(){
174: return this->percent(this->interest_rate);
175: }
176:
177: void bank_account::output (ostream &outs){
178: outs.setf(ios::fixed);
179: outs.setf(ios::showpoint);
180: outs.precision(2);
181:
182: outs << "Account balance: $" << this->get_balance() << endl;
183: outs << "Interest rate: " << this->get_rate() << "%" << endl;
184: }
185:
186: double bank_account::fraction(double percentage){
187: return percentage / ALL ;
188: }
189:
190: double bank_account::percent(double fraction_value){
191: return fraction_value * ALL ;
192: }
193: