Skip to content

Commit 5e99265

Browse files
committed
ADVANCED RELATIONS
1 parent 44e4d2a commit 5e99265

31 files changed

+867
-0
lines changed
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands
2+
{
3+
using P01_BillsPaymentSystem.Core.Commands.Contracts;
4+
5+
public abstract class Command : IExecutable
6+
{
7+
public Command(string[] data)
8+
{
9+
this.Data = data;
10+
}
11+
12+
public string[] Data { get; protected set; }
13+
14+
public abstract void Execute();
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands.Contracts
2+
{
3+
public interface IExecutable
4+
{
5+
void Execute();
6+
}
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands
2+
{
3+
public class InfoUser : Command
4+
{
5+
public InfoUser(string[] data)
6+
:base(data)
7+
{
8+
9+
}
10+
11+
public override void Execute()
12+
{
13+
//var id = int.Parse(this.Data[0]);
14+
15+
16+
}
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace P01_BillsPaymentSystem.Core.Contracts
2+
{
3+
public interface IEngine
4+
{
5+
void Run();
6+
}
7+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
namespace P01_BillsPaymentSystem.Core
2+
{
3+
using System.Linq;
4+
using System.Text;
5+
using System;
6+
7+
using Microsoft.EntityFrameworkCore;
8+
9+
using Contracts;
10+
using P01_BillsPaymentSystem.Core.IO.Contracts;
11+
using P01_BillsPaymentSystem.Data;
12+
using P01_BillsPaymentSystem.Data.Models;
13+
14+
public class Engine : IEngine
15+
{
16+
private readonly IReader reader;
17+
private readonly IWriter writer;
18+
private readonly BillsPaymentSystemContext context;
19+
20+
public Engine(IReader reader, IWriter writer, BillsPaymentSystemContext context)
21+
{
22+
this.reader = reader;
23+
this.writer = writer;
24+
this.context = context;
25+
}
26+
27+
public void Run()
28+
{
29+
this.writer.WriteLine(OutputMessages.WelcomeString());
30+
31+
while (true)
32+
{
33+
try
34+
{
35+
this.writer.WriteLine(OutputMessages.MenuOptions());
36+
37+
var result = string.Empty;
38+
var command = this.reader.ReadLine().Split();
39+
40+
if (command[0] == "1")
41+
{
42+
this.writer.WriteLine(OutputMessages.EnterId);
43+
44+
var userId = int.Parse(this.reader.ReadLine());
45+
46+
this.writer.WriteLine(OutputMessages.Loading);
47+
result = this.FindUser(userId);
48+
}
49+
else if (command[0] == "2")
50+
{
51+
this.writer.WriteLine(OutputMessages.EnterIdAndAmount);
52+
53+
var tokens = this.reader.ReadLine().Split();
54+
var userId = int.Parse(tokens[0]);
55+
var amount = decimal.Parse(tokens[1]);
56+
57+
this.writer.WriteLine(OutputMessages.Loading);
58+
result = this.PayBills(userId, amount);
59+
60+
this.context.SaveChanges();
61+
}
62+
else if (command[0] == "9")
63+
{
64+
this.writer.WriteLine("Bye bye! 4ao 4ao! Arividerchi! Адиос!");
65+
Environment.Exit(0);
66+
}
67+
68+
this.writer.WriteLine(result);
69+
}
70+
catch (Exception)
71+
{
72+
this.writer.WriteLine(OutputMessages.InvalidCommand);
73+
}
74+
}
75+
}
76+
77+
private string FindUser(int userId)
78+
{
79+
var userInfo = context.Users
80+
.Select(u => new
81+
{
82+
Id = u.UserId,
83+
Name = u.FirstName + " " + u.LastName,
84+
BankAccounts = u.PaymentMethods.Where(x => x.BankAccount != null).Select(pm => pm.BankAccount).ToList(),
85+
CreditCards = u.PaymentMethods.Where(x => x.CreditCard != null).Select(pm => pm.CreditCard).ToList(),
86+
})
87+
.SingleOrDefault(u => u.Id == userId);
88+
89+
90+
StringBuilder sb = new StringBuilder();
91+
92+
if (DoesExist(userInfo))
93+
{
94+
sb.AppendLine("User: " + userInfo.Name);
95+
96+
sb.AppendLine("Bank Accounts:");
97+
foreach (var bankAcc in userInfo.BankAccounts)
98+
{
99+
sb.AppendLine(bankAcc.ToString());
100+
}
101+
102+
sb.AppendLine("Credit Cards:");
103+
foreach (var creditCard in userInfo.CreditCards)
104+
{
105+
sb.AppendLine(creditCard.ToString());
106+
}
107+
}
108+
else
109+
{
110+
sb.AppendLine($"User with id {userId} not found!");
111+
}
112+
113+
return sb.ToString().Trim();
114+
}
115+
116+
private bool DoesExist(object userInfo) => userInfo != null;
117+
118+
private string PayBills(int userId, decimal billsAmount)
119+
{
120+
var user = this.context.Users
121+
.Include(x => x.PaymentMethods)
122+
.ThenInclude(x => x.BankAccount)
123+
.Include(x => x.PaymentMethods)
124+
.ThenInclude(x => x.CreditCard)
125+
.FirstOrDefault(x => x.UserId == userId);
126+
127+
decimal total = FindTotalMoney(user);
128+
129+
StringBuilder sb = new StringBuilder();
130+
if (total < billsAmount)
131+
{
132+
sb.AppendLine("Nqma dostatachno $$$Kinti-minti!$$$ za smetkite!");
133+
}
134+
else
135+
{
136+
var accounts = user.PaymentMethods
137+
.Where(x => x.BankAccount != null)
138+
.Select(x => x.BankAccount)
139+
.OrderBy(x => x.BankAccountId)
140+
.ToList();
141+
142+
foreach (var acc in accounts)
143+
{
144+
if (acc.Balance < billsAmount)
145+
{
146+
billsAmount -= acc.Balance;
147+
acc.Withdraw(acc.Balance);
148+
}
149+
else
150+
{
151+
acc.Withdraw(billsAmount);
152+
billsAmount = 0;
153+
break;
154+
}
155+
}
156+
157+
var creditCards = user.PaymentMethods
158+
.Where(x => x.CreditCard != null)
159+
.Select(x => x.CreditCard)
160+
.OrderBy(x => x.CreditCardId)
161+
.ToList();
162+
163+
foreach (var card in creditCards)
164+
{
165+
if (card.LimitLeft < billsAmount)
166+
{
167+
billsAmount -= card.LimitLeft;
168+
card.Withdraw(card.LimitLeft);
169+
}
170+
else
171+
{
172+
card.Withdraw(billsAmount);
173+
billsAmount = 0;
174+
break;
175+
}
176+
}
177+
178+
sb.AppendLine("Bravo! Bravo! Bravo, Gospodine! Uspq da platish parnoto! :)");
179+
}
180+
181+
return sb.ToString().Trim();
182+
}
183+
184+
private static decimal FindTotalMoney(User user)
185+
{
186+
var bankAccountTotalAmount = user.PaymentMethods.Where(x => x.BankAccount != null).Sum(x => x.BankAccount.Balance);
187+
var creditCardTotalAmount = user.PaymentMethods.Where(x => x.CreditCard != null).Sum(x => x.CreditCard.LimitLeft);
188+
var total = bankAccountTotalAmount + creditCardTotalAmount;
189+
return total;
190+
}
191+
}
192+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace P01_BillsPaymentSystem.Core.IO
2+
{
3+
using System;
4+
5+
using Contracts;
6+
7+
public class ConsoleReader : IReader
8+
{
9+
public string ReadLine()
10+
{
11+
return Console.ReadLine();
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace P01_BillsPaymentSystem.Core.IO
2+
{
3+
using System;
4+
5+
using Contracts;
6+
7+
public class ConsoleWriter : IWriter
8+
{
9+
public void WriteLine(string text)
10+
{
11+
Console.WriteLine(text);
12+
}
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace P01_BillsPaymentSystem.Core.IO.Contracts
2+
{
3+
public interface IReader
4+
{
5+
string ReadLine();
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace P01_BillsPaymentSystem.Core.IO.Contracts
2+
{
3+
public interface IWriter
4+
{
5+
void WriteLine(string text);
6+
}
7+
}

0 commit comments

Comments
 (0)