-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategy.cs
139 lines (116 loc) · 3.57 KB
/
Strategy.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
using System;
using System.IO;
using System.Text;
namespace Strategy
{
// STRATEGY COMPRESSION
public interface ICompressor
{
void compress(string filename, string formatedMessage);
}
public class RAR_Compression: ICompressor
{
public void compress(string filename, string formatedMessage)
{
using (var sw = new StreamWriter(filename, false, Encoding.Unicode))
{
string text = ">>>RAR COMPRESSION<<<\n";
text += formatedMessage;
text += "<<<RAR COMPRESSION>>>\n";
sw.Write(text);
}
}
}
public class ZIP_Compression: ICompressor
{
public void compress(string filename, string formatedMessage)
{
using (var sw = new StreamWriter(filename, false, Encoding.Unicode))
{
string text = "~~>ZIP COMPRESSION<~~\n";
text += formatedMessage;
text += "<~~ZIP COMPRESSION~~>\n";
sw.Write(text);
}
}
}
// STRATEGY FORMATE FILE
public interface IFormate
{
string formate(string message);
}
public class JSON_Formate: IFormate
{
public string formate(string message)
{
string json = "{\n";
json += $" \"message\": {message}\n";
json += "}\n";
return json;
}
}
public class XML_Formate : IFormate
{
public string formate(string message)
{
string xml = "<article lang = \"\">\n";
xml += $"<para>Message: {message}</para>\n";
xml += "</article>\n";
return xml;
}
}
public class User
{
private ICompressor _strategyCompress;
private IFormate _strategyFormate;
private string username;
private string password;
public User(string username, string password, ICompressor compressor, IFormate formator)
{
this.username = username;
this.password = password;
this._strategyCompress = compressor;
this._strategyFormate = formator;
}
public void CompressFormatedFile(string filename, string fileFormat)
{
var text = _strategyFormate.formate(this.ToString());
var file = $"{filename}.{fileFormat}";
_strategyCompress.compress(file, text);
Console.WriteLine($"File: {file} has compressed\n");
}
public override string ToString()
{
return $"Username: {username}, Password: {password}";
}
public void SetStrategy(ICompressor compressor)
{
if (compressor == null)
{
throw new ArgumentNullException(nameof(compressor));
}
this._strategyCompress = compressor;
}
public void SetStrategy(IFormate formator)
{
if (formator == null)
{
throw new ArgumentNullException(nameof(formator));
}
this._strategyFormate = formator;
}
public void SetStrategy(ICompressor compressor, IFormate formator)
{
if (compressor == null)
{
throw new ArgumentNullException(nameof(compressor));
}
if (formator == null)
{
throw new ArgumentNullException(nameof(formator));
}
this._strategyCompress = compressor;
this._strategyFormate = formator;
}
}
}