-
Notifications
You must be signed in to change notification settings - Fork 0
/
Team.cs
200 lines (157 loc) · 4.79 KB
/
Team.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
public enum Func
{
Win,
Draw,
Loss
}
public class Team
{
public int MathPlayed { get; set; }
public int Wins { get; set; }
public int Loss { get; set; }
public int Draw { get; set; }
public int Points { get; set; }
public string Name { get; set; }
public Team(string name)
{
Name = name;
Wins = 0;
Loss = 0;
Draw = 0;
Points = 0;
MathPlayed = 0;
}
public void add(Func fun)
{
switch (fun)
{
case Func.Win:
this.Wins++;
this.MathPlayed++;
break;
case Func.Loss:
this.Loss++;
this.MathPlayed++;
break;
case Func.Draw:
this.Draw++;
this.MathPlayed++;
break;
default:
throw new InvalidCastException();
}
}
public void Calculate()
{
var sum = 0;
sum += (this.Wins * 3);
sum += this.Draw;
this.Points = sum;
}
}
public static class Tournament
{
public static void Tally(Stream inStream, Stream outStream)
{
if (0 == inStream.Length)
{
StreamWriter writer = new StreamWriter(outStream);
writer.Write("Team | MP | W | D | L | P");
writer.Dispose();
writer.Close();
}
else
{
try
{
StreamReader reder = new StreamReader(inStream);
string? g;
string[] r;
var lst = new List<string>();
var lst2 = new List<Team>();
while (!(reder.EndOfStream))
{
g = reder.ReadLine();
if (!string.IsNullOrWhiteSpace(g))
{
r = g.Split(";");
switch (r[2])
{
case "win":
lst2.First(a => a.Name == r[0]).add(Func.Win);
lst2.First(a => a.Name == r[1]).add(Func.Loss);
break;
case "draw":
lst2.First(a => a.Name == r[0]).add(Func.Draw);
lst2.First(a => a.Name == r[1]).add(Func.Draw);
break;
case "loss":
lst2.First(a => a.Name == r[0]).add(Func.Loss);
lst2.First(a => a.Name == r[1]).add(Func.Win);
break;
default:
throw new InvalidOperationException();
}
}
}
inStream.Dispose();
inStream.Close();
reder.Dispose();
reder.Close();
foreach (var item in lst2)
{
item.Calculate();
}
var gg = lst2.OrderBy(a => a.Name);
gg = gg.OrderByDescending(a => a.Points);
List<string> jdvl = new List<string>();
jdvl.Add("Team | MP | W | D | L | P");
string fsle = "";
foreach (var item in gg)
{
for (int i = 0; i < 31 - item.Name.Length; i++)
{
fsle += " ";
}
var fslep = 2;
if (item.Points > 9)
{
fslep = 1;
}
var st = "";
for (int i = 0; i < fslep; i++)
{
st += " ";
}
jdvl.Add($"{item.Name}{fsle}| {item.MathPlayed} | {item.Wins} | {item.Draw} | {item.Loss} |{st}{item.Points}");
fsle = "";
}
StreamWriter writer = new StreamWriter(outStream);
foreach (var item in jdvl)
{
if (jdvl.Last() == item)
{
writer.Write(item);
}
else
{
writer.WriteLine(item);
}
}
writer.Dispose();
writer.Close();
}
catch (System.Exception)
{
throw;
}
finally
{
}
}
}
}