Guest User

Untitled

a guest
Apr 16th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. //Account.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <algorithm>
  8. #include <stdlib.h>
  9.  
  10. using namespace std;
  11.  
  12. class Account
  13. {
  14. private:
  15. int accountId; //account ID
  16. double balance; //current balance of the account
  17. double annualInterestRate; //current annual interest rate of the account (ex. 4.5)
  18.  
  19. public:
  20. //Account();
  21. //Account(int id, double bal, double interest);
  22. //default constructor(no parameters)
  23. Account()
  24. {
  25. //cout << "Please enter an account number 0-9: " << endl;
  26. //cin >> accountId;
  27. }
  28.  
  29.  
  30. //three-parameter constructor
  31. Account(int id, double bal, double interest)
  32. {
  33. accountId = id;
  34. balance = bal;
  35. annualInterestRate = interest;
  36. }
  37.  
  38.  
  39. //function sets id to x
  40. void setAccountId(int x)
  41. {
  42. accountId = x;
  43. }
  44.  
  45.  
  46. //function sets balance to x
  47. void setBalance(double x)
  48. {
  49. balance = x;
  50. x = (rand() % 20000 + 10000);
  51. }
  52.  
  53.  
  54. //function sets annaulInterestRate to x
  55. void setInterest(double x)
  56. {
  57. annualInterestRate = x;
  58. srand(unsigned(time(NULL)));
  59. x = (1.5 + ((double)rand() / RAND_MAX) * (5.0 - 1.5));
  60. }
  61.  
  62.  
  63. //function returns accountI
  64. int getAccountId()
  65. {
  66. return accountId;
  67. }
  68.  
  69.  
  70. // Function returns balance
  71. double getBalance()
  72. {
  73. return balance;
  74. }
  75.  
  76.  
  77. //function returns annualInterest Rate
  78. double getInterest()
  79. {
  80. return annualInterestRate;
  81. }
  82.  
  83.  
  84. //Function calculates the monthly interest rate and returns the value
  85. double getMonthlyInterestRate()
  86. {
  87. double monthlyInterest = (annualInterestRate / 12);
  88. return monthlyInterest;
  89. }
  90.  
  91.  
  92. //Function calculates the amount earned per month from interest and
  93. //returns the value rounded to 2 decimal places. (Assume interest is
  94. //not compounding)
  95. double getMonthlyInterest()
  96. {
  97. return (getMonthlyInterestRate() * getBalance());
  98. }
  99.  
  100. //Function only allows withdraw if the current balance is greater than
  101. //or equal to the withdraw amount.Return true if withdrawal is
  102. //successful, else return false.
  103. bool withdraw(double amount)
  104. {
  105. if (getBalance() >= amount)
  106. {
  107. return true;
  108. }
  109. else
  110. {
  111. return false;
  112. }
  113. }
  114.  
  115.  
  116. //Function adds the amount passed as a parameter to the current balance.
  117. void deposit(double amount)
  118. {
  119. balance = (balance + amount);
  120. }
  121. };
  122.  
  123.  
  124. //AccountMain.cpp
  125. #include "Account.cpp"
  126. #include <iostream>
  127. #include <string>
  128. #include <sstream>
  129. #include <fstream>
  130. #include <iomanip>
  131. #include <algorithm>
  132.  
  133. using namespace std;
  134.  
  135. double fRand(double fMin, double fMax)
  136. {
  137. double f = (double)rand() / RAND_MAX;
  138. return fMin + f * (fMax - fMin);
  139. }
  140.  
  141. int menu(int inputaccnum, int menuchoice, Account list[10])
  142. {
  143. if (menuchoice == 1)
  144. {
  145. int depositinput;
  146. cout << fixed << setprecision(2) << "Your current Balance is: $" << list[inputaccnum].getBalance() << endl;
  147. cout << "Enter the amount you'd like you to deposit: ";
  148. cin >> depositinput;
  149. list[inputaccnum].deposit(depositinput);
  150. cout << endl;
  151. }
  152. else if (menuchoice == 2)
  153. {
  154. int withdrawinput;
  155. cout << fixed << setprecision(2) << "Your current Balance is: $" << list[inputaccnum].getBalance() << endl;
  156. cout << "Enter the amount you'd like to withdraw: ";
  157. cin >> withdrawinput;
  158. list[inputaccnum].withdraw(withdrawinput);
  159. cout << endl;
  160. }
  161. else if (menuchoice == 3)
  162. {
  163. cout << "Current Balance: $" << list[inputaccnum].getBalance() << endl;
  164. }
  165. else if (menuchoice == 4)
  166. {
  167. cout << "Monthly interest rate: " << list[inputaccnum].getMonthlyInterestRate() << "%" << endl;
  168. cout << "Yearly interest rate: " << setprecision(2) << list[inputaccnum].getInterest() << "%" << endl;
  169. }
  170. else //if (menuchoice = 5)
  171. {
  172. cout << "Account ID: " << list[inputaccnum].getAccountId() << endl;
  173. cout << "Current Balance: " << list[inputaccnum].getBalance() << endl;
  174. cout << "Monthly interest rate: " << list[inputaccnum].getMonthlyInterestRate() << endl;
  175. cout << "Monthly interest gain amount: " << list[inputaccnum].getMonthlyInterest() << endl;
  176. }
  177. return 0;
  178. }
  179.  
  180. int main()
  181. {
  182. Account acc0, acc1, acc2, acc3, acc4, acc5, acc6, acc7, acc8, acc9;
  183. Account list[10] = { acc0, acc1, acc2, acc3, acc4, acc5, acc6, acc7, acc8, acc9 };
  184.  
  185. acc0.setAccountId(0), acc1.setAccountId(1), acc2.setAccountId(2), acc3.setAccountId(3),
  186. acc4.setAccountId(4), acc5.setAccountId(5), acc6.setAccountId(6), acc7.setAccountId(7),
  187. acc8.setAccountId(8), acc9.setAccountId(9);
  188.  
  189. int inputaccnum;
  190. cout << "Please enter an account number from 0-9: " << endl;
  191. cin >> inputaccnum;
  192. cout << list[inputaccnum].getAccountId();
  193.  
  194. srand(unsigned(time(NULL)));
  195. list[inputaccnum].setBalance(fRand(10000.00, 20000.00));
  196. srand(unsigned(time(NULL)));
  197. list[inputaccnum].setInterest(1.5 + ((double)rand() / RAND_MAX) * (5.0 - 1.5));
  198.  
  199. int menuchoice;
  200. cout << endl << "Enter 1 to make a deposit" << endl;
  201. cout << "Enter 2 to make a withdraw" << endl;
  202. cout << "Enter 3 to check balance" << endl;
  203. cout << "Enter 4 to check interest rate" << endl;
  204. cout << "Enter 5 to display account summary" << endl;
  205. cout << "Enter -1 to return to the main menu" << endl;
  206. cout << list[inputaccnum].getAccountId();
  207. cin >> menuchoice;
  208.  
  209. int count = -1;
  210. for (int i = 0; i > count; i++)
  211. {
  212. if (menuchoice != -1)
  213. {
  214. menu(inputaccnum, menuchoice, list);
  215. cout << endl << "Enter 1 to make a deposit" << endl;
  216. cout << "Enter 2 to make a withdraw" << endl;
  217. cout << "Enter 3 to check balance" << endl;
  218. cout << "Enter 4 to check interest rate" << endl;
  219. cout << "Enter 5 to display account summary" << endl;
  220. cout << "Enter -1 to return to the main menu" << endl;
  221. cin >> menuchoice;
  222. }
  223. else
  224. {
  225. cout << "Please enter an account number from 0-9: " << endl;
  226. cin >> inputaccnum;
  227. srand(unsigned(time(NULL)));
  228. list[inputaccnum].setBalance((rand()) / (RAND_MAX / (20000.00 - 10000.00)));
  229. list[inputaccnum].setInterest(1.5 + ((double)rand() / RAND_MAX) * (5.0 - 1.5));
  230. cout << endl << "Enter 1 to make a deposit" << endl;
  231. cout << "Enter 2 to make a withdraw" << endl;
  232. cout << "Enter 3 to check balance" << endl;
  233. cout << "Enter 4 to check interest rate" << endl;
  234. cout << "Enter 5 to display account summary" << endl;
  235. cout << "Enter -1 to return to the main menu" << endl;
  236. cin >> menuchoice;
  237. }
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment