-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
219 lines (191 loc) · 7.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Authentication.ExtendedProtection;
using ConsoleApp_CSCase.Category;
using Microsoft.Extensions.DependencyInjection;
using StructureMap;
using Container = StructureMap.Container;
namespace ConsoleApp_CSCase
{
interface ICategory
{
string Name { get; }
bool Identify(ITrade trade);
}
interface ITrade
{
double Value { get; }
string ClientSector { get; }
DateTime NextPaymentDate { get; }
bool IsPoliticallyExposed { get; }
}
[Flags]
enum EnumAllCategories
{
Expired,
MediumRisk,
HighRisk,
PEP,
NotCategorised,
}
class Program
{
public class GetCategoryFactory
{
public ICategory Create(string categoryAssemblyName, string referenceDate)
{
var type = Type.GetType(categoryAssemblyName ?? throw new InvalidOperationException());
if (type == typeof(Expired))
{
return Activator.CreateInstance(type, referenceDate) as ICategory;
}
else
{
return Activator.CreateInstance(type) as ICategory;
}
}
public ICategory GetCategory(EnumAllCategories category, string referenceDate)
{
var categoryAssemblyName = $"ConsoleApp_CSCase.Category.{category}, ConsoleApp-CSCase";
// Type t = typeof(MediumRisk);
// var name = t.AssemblyQualifiedName;
var type = Type.GetType(categoryAssemblyName ?? throw new InvalidOperationException());
if (type == typeof(Expired))
{
return Activator.CreateInstance(type, referenceDate) as ICategory;
}
else
{
return Activator.CreateInstance(type) as ICategory;
}
}
public ICategory GetCategory(string categoryType, string referenceDate)
{
if (categoryType == null)
{
return null;
}
else if (categoryType == EnumAllCategories.Expired.ToString())
{
return new Expired(referenceDate);
}
else if (categoryType == EnumAllCategories.MediumRisk.ToString())
{
return new MediumRisk();
}
else if (categoryType == EnumAllCategories.HighRisk.ToString())
{
return new HighRisk();
}
else if (categoryType == EnumAllCategories.PEP.ToString())
{
return new PEP();
}
else if (categoryType == EnumAllCategories.NotCategorised.ToString())
{
return new NotCategorised();
}
return null;
}
public List<ICategory> GetAllCategories(string referenceDate)
{
List<ICategory> categories = new List<ICategory>();
// Option using Switch
/*foreach (string categoryName in Enum.GetNames(typeof(EnumAllCategories)))
{
categories.Add(this.GetCategory(categoryName, referenceDate));
}*/
// Option using Activator
/* foreach (EnumAllCategories category in Enum.GetValues(typeof(EnumAllCategories)))
{
categories.Add(this.GetCategory(category, referenceDate));
}*/
// using classes in a namespace instead of enum
var types = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(t => t.Namespace == "ConsoleApp_CSCase.Category");
foreach (var category in types)
{
categories.Add(this.Create(category.FullName, referenceDate));
}
return categories;
}
}
public class Trade : ITrade
{
public double Value { get; }
public string ClientSector { get; }
public DateTime NextPaymentDate { get; }
public bool IsPoliticallyExposed { get; }
public Trade(string[] parameters)
{
Value = double.Parse(parameters[0]);
ClientSector = parameters[1];
NextPaymentDate = DateTime.ParseExact(parameters[2], "MM/dd/yyyy", CultureInfo.InvariantCulture);
IsPoliticallyExposed = parameters[3] == "true";
}
}
static void Main(string[] args)
{
// upload portfolio with trades
string[] lines = System.IO.File.ReadAllLines(@"Portfolio1.txt");
string referenceDate = lines[0];
int numberOfTrades;
if (!int.TryParse(lines[1], out numberOfTrades) || numberOfTrades != lines.Length - 2)
{
Console.WriteLine("ERROR: inconsistent number of trades");
return;
}
// Category Factory
GetCategoryFactory categoryFactory = new GetCategoryFactory();
List<ICategory> categories = categoryFactory.GetAllCategories(referenceDate);
// Getting categories using dependency injection
/*
var services = new ServiceCollection()
.AddSingleton<ICategory>(new Expired(referenceDate))
.AddSingleton<ICategory, HighRisk>()
.AddSingleton<ICategory, MediumRisk>()
.AddSingleton<ICategory, PEP>()
.AddSingleton<ICategory, NotCategorised>()
.BuildServiceProvider();
var categories = services.GetServices<ICategory>();
*/
// Getting categories using StructuredMap and dependency injection
/*
var services = new ServiceCollection();
var container = new Container();
container.Configure(config =>
{
config.Scan(_ =>
{
_.AssemblyContainingType(typeof(Program));
_.WithDefaultConventions();
});
config.Populate(services);
});
var serviceProvider = container.GetInstance<IServiceProvider>();
IEnumerable<ICategory> categories = serviceProvider.GetServices<ICategory>();
*/
// classifiy trades
for (int i = 2; i < lines.Length; i++)
{
string[] tradeData = lines[i].Split(" ");
Trade trade = new Trade(tradeData);
foreach (ICategory category in categories)
{
if (category.Identify(trade))
{
Console.WriteLine(category.Name);
break;
}
}
}
}
}
}