Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Bank
  8. {
  9. class BankAccount
  10. {
  11. // Private variables for storing the account balance and interest rate
  12. // ...
  13. private double balance;
  14. private double rate;
  15. /// <summary>
  16. /// Creates a new bank account with no starting balance and the default
  17. /// interest rate.
  18. /// </summary>
  19. public BankAccount()
  20. {
  21. balance = 0;
  22. rate = 3.2;
  23. }
  24.  
  25. /// <summary>
  26. /// Creates a new bank account with a starting balance and the default
  27. /// interest rate.
  28. /// </summary>
  29. /// <param name="startingBalance">The starting balance</param>
  30. public BankAccount(double startingBalance)
  31. {
  32. balance = startingBalance;
  33. rate = 3.2;
  34. }
  35.  
  36. /// <summary>
  37. /// Creates a new bank account with a starting balance and interest
  38. /// rate.
  39. /// </summary>
  40. /// <param name="startingBalance">The starting balance</param>
  41. /// <param name="interestRate">The interest rate (in percentage)</param>
  42. public BankAccount(double startingBalance, double interestRate)
  43. {
  44. balance = startingBalance;
  45. rate = interestRate;
  46. }
  47.  
  48. /// <summary>
  49. /// Reduce the balance of the bank account by 'amount' and return true.
  50. /// If there are insufficient funds in the account, the balance does not
  51. /// change and false is returned instead.
  52. /// </summary>
  53. /// <param name="amount">The amount of money to deduct from the account
  54. /// </param>
  55. /// <returns>True if funds were deducted from the account, and false
  56. /// otherwise</returns>
  57. public bool Withdraw(double amount)
  58. {
  59. if (balance > amount)
  60. {
  61. balance = balance - amount;
  62. return true;
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68.  
  69. }
  70.  
  71. /// <summary>
  72. /// Increase the balance of the account by 'amount'
  73. /// </summary>
  74. /// <param name="amount">The amount to increase the balance by</param>
  75. public void Deposit(double amount)
  76. {
  77. balance = balance + amount;
  78. }
  79.  
  80. /// <summary>
  81. /// Returns the total balance currently in the account.
  82. /// </summary>
  83. /// <returns>The total balance currently in the account</returns>
  84. public double QueryBalance()
  85. {
  86. return balance;
  87. }
  88.  
  89. /// <summary>
  90. /// Sets the account's interest rate to the rate provided
  91. /// </summary>
  92. /// <param name="interestRate">The interest rate for this account (%)
  93. /// </param>
  94. public void SetInterestRate(double interestRate)
  95. {
  96. rate = interestRate;
  97. }
  98.  
  99. /// <summary>
  100. /// Returns the account's interest rate
  101. /// </summary>
  102. /// <returns>The percentage interest rate of this account</returns>
  103. public double GetInterestRate()
  104. {
  105. return rate;
  106. }
  107.  
  108. /// <summary>
  109. /// Calculates the interest on the current account balance and adds it
  110. /// to the account
  111. /// </summary>
  112. public void AddInterest()
  113. {
  114. balance = balance * (1 + rate/100);
  115. }
  116. }
  117. class Program
  118. {
  119. static void Main(string[] args)
  120. {
  121. BankAccount myAccount = new BankAccount(700,5);
  122. myAccount.Withdraw(900);
  123. myAccount.AddInterest();
  124. Console.WriteLine("My current bank balance is $ {0:0.00}\n",
  125. myAccount.QueryBalance());
  126.  
  127. Console.WriteLine("\nPress enter to exit.");
  128. Console.ReadLine();
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement