miroslavbstoyanov

Untitled

Jul 11th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using P01_BillsPaymentSystem.Data;
  3. using P01_BillsPaymentSystem.Data.Models;
  4. using System;
  5. using System.Linq;
  6.  
  7. namespace P01_BillsPaymentSystem
  8. {
  9. public class StartUp
  10. {
  11. static void Main(string[] args)
  12. {
  13. using (BillsPaymentSystemContext context = new BillsPaymentSystemContext())
  14. {
  15. context.Database.EnsureDeleted();
  16. context.Database.EnsureCreated();
  17.  
  18. Seed(context);
  19.  
  20. Console.Write("Enter UserId: ");
  21. int userId = int.Parse(Console.ReadLine());
  22. Console.Write("Ënter BillsAmount: ");
  23. decimal amount = decimal.Parse(Console.ReadLine());
  24.  
  25. PayBills(userId, amount, context);
  26.  
  27. PrintUserStatement(context, userId);
  28. }
  29. }
  30.  
  31. private static void PayBills(int userId, decimal amount, BillsPaymentSystemContext context)
  32. {
  33. var userCreditCards = context.PaymentMethods
  34. .Where(u => u.UserId == userId && u.Type == Data.Models.Type.CreditCard)
  35. .Include(e => e.CreditCard)
  36. .OrderBy(x => x.CreditCardId)
  37. .ToList();
  38.  
  39. var userBankAccounts = context.PaymentMethods
  40. .Where(u => u.UserId == userId && u.Type == Data.Models.Type.BankAccount)
  41. .Include(e => e.BankAccount)
  42. .OrderBy(x => x.BankAccountId)
  43. .ToList();
  44.  
  45. using (var transaction = context.Database.BeginTransaction())
  46. {
  47. foreach (var account in userBankAccounts)
  48. {
  49. if (amount == 0)
  50. {
  51. break;
  52. }
  53.  
  54. decimal currentBalance = account.BankAccount.Balance;
  55.  
  56. if (currentBalance >= amount)
  57. {
  58. account.BankAccount.Withdraw(amount);
  59. amount -= amount;
  60. }
  61. else
  62. {
  63. account.BankAccount.Withdraw(currentBalance);
  64. amount -= currentBalance;
  65. }
  66. }
  67.  
  68. foreach (var card in userCreditCards)
  69. {
  70. if (amount == 0)
  71. {
  72. break;
  73. }
  74.  
  75. decimal limitLeft = card.CreditCard.LimitLeft;
  76.  
  77. if (limitLeft >= amount)
  78. {
  79. card.CreditCard.Withdraw(amount);
  80. amount -= amount;
  81. }
  82. else
  83. {
  84. card.CreditCard.Withdraw(limitLeft);
  85. amount -= limitLeft;
  86. }
  87. }
  88.  
  89. if (amount == 0)
  90. {
  91. transaction.Commit();
  92. context.SaveChanges();
  93. }
  94. else
  95. {
  96. transaction.Rollback();
  97. }
  98. }
  99. }
  100.  
  101. private static void PrintUserStatement(BillsPaymentSystemContext context, int userId)
  102. {
  103. var user = context.Users
  104. .Where(u => u.UserId == userId)
  105. .Select(x => new
  106. {
  107. x.FirstName,
  108. x.LastName,
  109. BankAccounts = x.PaymentMethods.Select(p => new { p.BankAccount }).ToList(),
  110. CreditCards = x.PaymentMethods.Select(p => new { p.CreditCard }).ToList()
  111. }).FirstOrDefault();
  112.  
  113. if (user == null)
  114. {
  115. Console.WriteLine($"User with id {userId} not found!");
  116. Environment.Exit(0);
  117. }
  118.  
  119. Console.WriteLine($"User: {user.FirstName} {user.LastName}");
  120.  
  121. Console.WriteLine("Bank Accounts:");
  122. foreach (var b in user.BankAccounts.Where(x => x.BankAccount != null))
  123. {
  124. Console.WriteLine($"-- ID: {b.BankAccount.BankAccountId}");
  125. Console.WriteLine($"--- Balance: {b.BankAccount.Balance}");
  126. Console.WriteLine($"--- Bank: {b.BankAccount.BankName}");
  127. Console.WriteLine($"--- SWIFT: {b.BankAccount.SwiftCode}");
  128. }
  129.  
  130. bool areThereAnyBankAccounts = user.BankAccounts.Any(x => x.BankAccount != null);
  131.  
  132. if (!areThereAnyBankAccounts)
  133. {
  134. Console.WriteLine("No any bank accounts");
  135. }
  136.  
  137. Console.WriteLine("Credit Cards:");
  138. foreach (var c in user.CreditCards.Where(x => x.CreditCard != null))
  139. {
  140. Console.WriteLine($"-- ID: {c.CreditCard.CreditCardId}");
  141. Console.WriteLine($"--- Limit: {c.CreditCard.Limit}");
  142. Console.WriteLine($"--- Money Owed: {c.CreditCard.MoneyOwed}");
  143. Console.WriteLine($"--- Limit Left: {c.CreditCard.LimitLeft}");
  144. Console.WriteLine($"--- Expiration Date: {c.CreditCard.ExpirationDate}");
  145. }
  146.  
  147. bool areThereAnyCreditCards = user.CreditCards.Any(x => x.CreditCard != null);
  148.  
  149. if (!areThereAnyCreditCards)
  150. {
  151. Console.WriteLine("No any credit cards");
  152. }
  153. }
  154.  
  155. private static void Seed(BillsPaymentSystemContext context)
  156. {
  157. User[] users = new[]
  158. {
  159. new User { FirstName = "Georgi", LastName = "Hristov", Email = "[email protected]", Password = "pas1234" } ,
  160. new User { FirstName = "Mihail", LastName = "Radev", Email = "[email protected]", Password = "mpas234" } ,
  161. new User { FirstName = "Ivan", LastName = "Mihailov", Email = "[email protected]", Password = "mypas32" },
  162. new User { FirstName = "Guy", LastName = "Gilbert", Email = "[email protected]", Password = "gilber1234" }
  163. };
  164.  
  165. CreditCard[] creditCards = new[]
  166. {
  167. new CreditCard { ExpirationDate = new DateTime(2018, 12, 10), MoneyOwed = 100, Limit = 2000 },
  168. new CreditCard { ExpirationDate = new DateTime(2019, 03, 05), MoneyOwed = 500, Limit = 1500 },
  169. new CreditCard { ExpirationDate = new DateTime(2020, 03, 05), MoneyOwed = 100, Limit = 800 }
  170. };
  171.  
  172. BankAccount[] bankAccounts = new[]
  173. {
  174. new BankAccount("UBB", "UBBSBSF", 1200),
  175. new BankAccount("Unicredit Bulbank", "UNCRBGSF", 2000),
  176. new BankAccount("First Investment Bank", "FINVBGSF", 1000)
  177. };
  178.  
  179. PaymentMethod[] paymentMetods = new[]
  180. {
  181. new PaymentMethod { User = users[3], CreditCard = creditCards[2], Type = Data.Models.Type.CreditCard },
  182. new PaymentMethod { User = users[3], BankAccount = bankAccounts[1], Type = Data.Models.Type.BankAccount },
  183. new PaymentMethod { User = users[3], BankAccount = bankAccounts[2], Type = Data.Models.Type.BankAccount },
  184. new PaymentMethod { User = users[2], BankAccount = bankAccounts[0], Type = Data.Models.Type.BankAccount },
  185. new PaymentMethod { User = users[1], CreditCard = creditCards[1], Type = Data.Models.Type.BankAccount }
  186. };
  187.  
  188. context.Users.AddRange(users);
  189. context.PaymentMethods.AddRange(paymentMetods);
  190.  
  191. context.SaveChanges();
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment