Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. public class TransactionManager : ITransactionManager
  2. {
  3. BankOfBIT_RIContext db = new BankOfBIT_RIContext();
  4.  
  5. public void DoWork()
  6. {
  7. }
  8.  
  9. public double? Deposit(int accountId, double amount, string notes)
  10. {
  11. double? total;
  12.  
  13. try
  14. {
  15. total = Utility(accountId, amount, notes, (int)TransactionTypeValues.Deposit);
  16. }
  17.  
  18. catch
  19. {
  20. total = null;
  21. }
  22.  
  23. return total;
  24. }
  25.  
  26. private double? Utility(int accountId, double amount, string notes, int type)
  27. {
  28. double? total;
  29. try
  30. {
  31. //create bank account object using linq
  32. BankAccount bankQuery = (from bank in db.BankAccounts
  33. where bank.BankAccountId == accountId
  34. select bank).SingleOrDefault();
  35. bankQuery.Balance += amount;
  36. bankQuery.ChangeState();
  37. db.SaveChanges();
  38.  
  39. CreateTransaction(accountId, amount, notes, type);
  40.  
  41. total = bankQuery.Balance;
  42. }
  43.  
  44. catch
  45. {
  46. total = null;
  47. }
  48. return total;
  49. }
  50.  
  51. private void CreateTransaction(int accountId, double amount, string notes, int type)
  52. {
  53. //make an if based on type just to modify the deposits and withdrawals
  54. Transaction trans;
  55. if (type == (int)TransactionTypeValues.Deposit || type == (int)TransactionTypeValues.TransferRecipient)
  56. {
  57. trans = new Transaction
  58. {
  59. BankAccountId = accountId,
  60. TransactionTypeId = type,
  61. Deposit = amount,
  62. Withdrawal = 0,
  63. DateCreated = DateTime.Now,
  64. Notes = notes
  65. };
  66. trans.SetNextTransactionNumber();
  67. }
  68.  
  69. else
  70. {
  71. trans = new Transaction
  72. {
  73. BankAccountId = accountId,
  74. TransactionTypeId = type,
  75. Deposit = amount,
  76. Withdrawal = 0,
  77. DateCreated = DateTime.Now,
  78. Notes = notes
  79. };
  80. }
  81.  
  82. db.Transactions.Add(trans);
  83. db.SaveChanges();
  84. }
  85.  
  86. public double? Withdrawal(int accountId, double amount, string notes)
  87. {
  88. double? total;
  89.  
  90. try
  91. {
  92. total = Utility(accountId, -amount, notes, (int)TransactionTypeValues.Withdrawal);
  93. }
  94.  
  95. catch
  96. {
  97. total = null;
  98. }
  99.  
  100. return total;
  101. }
  102.  
  103. public double? BillPayment(int accountId, double amount, string notes)
  104. {
  105. return Utility(accountId, amount, notes, (int)TransactionTypeValues.BillPayment);
  106. }
  107.  
  108. public double? Transfer(int fromAccountId, int toAccountId, double amount, string notes)
  109. {
  110. double? total;
  111. try
  112. {
  113. total = Utility(fromAccountId, -amount, notes, (int)TransactionTypeValues.Transfer);
  114. Utility(toAccountId, amount, notes, (int)TransactionTypeValues.Transfer);
  115. }
  116.  
  117. catch
  118. {
  119. total = null;
  120. }
  121.  
  122. return total;
  123. }
  124.  
  125. public double? CalculateInterest(int accountId, string notes)
  126. {
  127. return 0;
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement