Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. public class EftContext
  2. {
  3. private readonly List<Customer> _customers;
  4. public EftContext()
  5. {
  6. var file = File.ReadAllText("Database.json");
  7.  
  8. if (string.IsNullOrEmpty(file))
  9. {
  10. var serialize = JsonConvert.SerializeObject(SampleCustomers);
  11. File.WriteAllText("Database.json", serialize);
  12. file = serialize;
  13. }
  14.  
  15. _customers = JsonConvert.DeserializeObject<List<Customer>>(file);
  16. }
  17.  
  18.  
  19. #region İlk Datalar
  20.  
  21. private List<Customer> SampleCustomers => new List<Customer>
  22. {
  23. new Customer
  24. {
  25. Id = 1,
  26. Name = "Test",
  27. Balance = 20.2
  28. },
  29. new Customer
  30. {
  31. Id = 2,
  32. Name = "Test2",
  33. Balance = 30.5
  34. },
  35. new Customer
  36. {
  37. Id = 3,
  38. Name = "Test3",
  39. Balance = 220
  40. }, new Customer
  41. {
  42. Id = 4,
  43. Name = "Test4",
  44. Balance =420
  45. },
  46. new Customer
  47. {
  48. Id = 5,
  49. Name = "Test5",
  50. Balance = 122.522
  51. }
  52. };
  53.  
  54. #endregion
  55.  
  56.  
  57. public Customer GetCustomer(int userId)
  58. {
  59. return _customers.FirstOrDefault(p => p.Id == userId);
  60. }
  61.  
  62. public void Send(SendingEftModel model)
  63. {
  64. var fromCustomer = _customers.FirstOrDefault(p => p.Id == model.FromId);
  65. if (fromCustomer == null)
  66. {
  67. throw new ArgumentNullException("From Customer Not Found");
  68. }
  69.  
  70. var toCustomer = _customers.FirstOrDefault(p => p.Id == model.ToId);
  71. if (toCustomer == null)
  72. {
  73. throw new ArgumentNullException("To Customer Not Found");
  74. }
  75.  
  76. if (fromCustomer.Balance < model.Money)
  77. {
  78. throw new ArgumentNullException("Insufficient balance");
  79. }
  80.  
  81. fromCustomer.Balance -= model.Money;
  82. toCustomer.Balance += model.Money;
  83.  
  84. var serialize = JsonConvert.SerializeObject(_customers);
  85. File.WriteAllText("Database.json", serialize);
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement