-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenuProgramu.cs
More file actions
311 lines (274 loc) · 10.5 KB
/
Copy pathMenuProgramu.cs
File metadata and controls
311 lines (274 loc) · 10.5 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace filescan
{
public class MenuProgramu
{
#region filds
/// <summary>
/// The user enviroment file path.
/// </summary>
string userEnviromentFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
#endregion
#region Properties
/// <summary>
/// Gets / sets the number of punctuation marks in file
/// </summary>
public int CountPunctMark { get; set; }
/// <summary>
/// Gets / sets the number sentences in file
/// </summary>
public int CountSentences { get; set; }
/// <summary>
/// Gets / sets the number of words in file
/// </summary>
public int WordsCount { get; set; }
/// <summary>
/// Gets / sets the number leters in file
/// </summary>
public int LetterAmount { get; set; }
/// <summary>
/// Gets or sets the file name
/// </summary>
public string FileName { get; set; } = "textfile.txt";
/// <summary>
/// Gets or sets the full file path
/// </summary>
public string FullDownloadFilePath
{
get
{
return Path.Combine(userEnviromentFilePath, "Downloads", FileName);
}
}
public string StatisticsFilePath
{
get
{
return Path.Combine(userEnviromentFilePath, "staistics.txt");
}
}
#endregion
public void StartMenuProgramu()
{
Console.Title = " Menu Programu ";
string url = "https://www.w3.org/TR/PNG/iso_8859-1.txt";
while (true)
{
Console.Clear();
Console.WriteLine("===============================================");
Console.WriteLine("1. Pobranie pliku z internetu");
Console.WriteLine("2. Liczba liter");
Console.WriteLine("3. liczba wyrazow w pliku");
Console.WriteLine("4. Liczba znaków interpunkcyjnych ");
Console.WriteLine("5. Liczba zdań w pliku ");
Console.WriteLine("6. Raport użycia A-Z ");
Console.WriteLine("7. Zapis statystyk z pkt 2-5 do pliku .txt ");
Console.WriteLine("8. Koniec programu");
Console.WriteLine("===============================================");
ConsoleKeyInfo klawisz = Console.ReadKey();
switch (klawisz.Key)
{
case ConsoleKey.D1:
Console.Clear(); DownloadFile(url); break;
case ConsoleKey.D2:
Console.Clear(); LettersCounter(); break;
case ConsoleKey.D3:
Console.Clear(); WordsCounter(); break;
case ConsoleKey.D4:
Console.Clear(); GetPunctuationMarks(); break;
case ConsoleKey.D5:
Console.Clear(); GetNumberOfSentences(); break;
case ConsoleKey.D6:
Console.Clear(); OccurencesOfChar(); break;
case ConsoleKey.D7:
Console.Clear(); SaveStatistics(); break;
case ConsoleKey.D8:
Console.Clear(); CloseApp(); break;
case ConsoleKey.Escape:
default: break;
}
}
}
static void opcjawbudowie(){
Console.Write("Opcja w budowie ! ");
Console.ReadKey();
}
public void DownloadFile(string url)
{
WebClient webClient = new WebClient();
webClient.DownloadFile(url, FullDownloadFilePath);
Console.WriteLine("File has been downloaded...");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
/// <summary>
/// Save the statistics to file
/// </summary>
public void WriteToFile()
{
using (StreamWriter sw = File.CreateText(StatisticsFilePath))
{
sw.WriteLine($"Number of punctuation marks : {CountPunctMark}");
sw.WriteLine($"Number of sentences: {CountSentences}");
sw.WriteLine($"Number of words: {WordsCount}");
sw.WriteLine($"Number of leters : {LetterAmount}");
}
}
/// <summary>
/// Edit or creat statistics file
/// </summary>
public void SaveStatistics()
{
if (File.Exists(StatisticsFilePath))
{
Console.WriteLine($"File already exists at: {StatisticsFilePath}");
Console.WriteLine("to delete file and creat new one press 'y' " +
"to override file press 'o', any key to skip:");
string input = Console.ReadLine();
if (input.Equals("y"))
{
File.Delete(StatisticsFilePath);
WriteToFile();
}
else if(input.Equals("o"))
{
Console.WriteLine("Clearing file.\nSaving...");;
File.WriteAllText(StatisticsFilePath, string.Empty);
WriteToFile();
}
Console.WriteLine("Unrecognized input, please try again!");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
else
{
Console.WriteLine("File does not exist.\nCreating File...");
WriteToFile();
Console.WriteLine($"Log file Created at: {StatisticsFilePath}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
}
/// <summary>
/// Count number of punctuation marks in the file
/// </summary>
public void GetPunctuationMarks()
{
if (File.Exists(FullDownloadFilePath))
{
var fileContent = File.ReadAllText(FullDownloadFilePath);
CountPunctMark = fileContent.ToString()
.Where(x => char.IsPunctuation(x))
.Count();
Console.WriteLine($"Total number of Punctuation Marks in file: {CountPunctMark}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
else
{
Console.WriteLine("The file does not exist!}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
}
/// <summary>
/// Count number of sentences in the file
/// </summary>
public void GetNumberOfSentences()
{
if(File.Exists(FullDownloadFilePath))
{
var fileContent = File.ReadAllText(FullDownloadFilePath);
var regex = new Regex(@"(?<=[\.!\?])\s+");
CountSentences = regex.Split(fileContent.ToString()).ToList().Count;
Console.WriteLine($"Total number of sentences if file {CountSentences}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
else
{
Console.WriteLine("The file does not exist!}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
}
public void WordsCounter()
{
if (File.Exists(FullDownloadFilePath))
{
var fileContent = File.ReadAllText(FullDownloadFilePath);
char[] separator = { ' ' };
WordsCount = fileContent.Split(separator, StringSplitOptions.RemoveEmptyEntries).Length;
Console.WriteLine($"Total number of words in file {WordsCount}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
else
{
Console.WriteLine("The file does not exist!}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
}
public void LettersCounter()
{
if (File.Exists(FullDownloadFilePath))
{
string fileContent = File.ReadAllText(FullDownloadFilePath);
LetterAmount = fileContent.Length;
Console.WriteLine($"Total number of letter in file {LetterAmount}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
else
{
Console.WriteLine("The file does not exist!}");
Console.WriteLine("Press key to continue!");
Console.ReadKey();
}
}
public void CloseApp()
{
if (File.Exists(FullDownloadFilePath))
{
File.Delete(FullDownloadFilePath);
File.Delete(StatisticsFilePath);
Console.WriteLine("The files has been DELETED!");
Console.WriteLine("Press key to close program");
Console.ReadKey();
System.Environment.Exit(0);
}
else
{
Console.WriteLine("The files does not EXISTS!");
Console.WriteLine("Press key to close program");
Console.ReadKey();
System.Environment.Exit(0);
}
}
public void OccurencesOfChar()
{
string input = File.ReadAllText(FullDownloadFilePath);
Console.WriteLine(FullDownloadFilePath.Length);
while (input.Length > 0)
{
Console.Write(input[0] + " : ");
int count = 0;
for (int j = 0; j < input.Length; j++)
{
if (input[0] == input[j])
{
count++;
}
}
Console.WriteLine(count);
input = input.Replace(input[0].ToString(), string.Empty);
}
Console.ReadLine();
}
}
}