-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFile.cs
More file actions
85 lines (78 loc) · 2.35 KB
/
TextFile.cs
File metadata and controls
85 lines (78 loc) · 2.35 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dogs
{
class TextFile : IFileManager
{
public List<Dog> LoadFromFile(string fileName)
{
using (StreamReader sr = new StreamReader(fileName, System.Text.Encoding.UTF8))
{
List<Dog> list = new List<Dog>();
while (sr.Peek() > -1)
{
string breed = sr.ReadLine();
int age;
int.TryParse(sr.ReadLine(), out age);
string fio = sr.ReadLine();
Marks mark = StringToKindFromFile(sr.ReadLine());
sr.ReadLine();
Dog tmp = new Dog(breed, age, fio, mark);
list.Add(tmp);
}
sr.Close();
return list;
}
}
public void PrintToFile(List<Dog> list, string fileName)
{
try
{
using (StreamWriter sw = new StreamWriter(fileName, false, System.Text.Encoding.UTF8))
{
foreach (var item in list)
{
PrintToSW(item, sw);
}
sw.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void PrintToSW(Dog test, StreamWriter sw)
{
sw.WriteLine(test.Breed);
sw.WriteLine(test.Age.ToString());
sw.WriteLine(test.Fio);
sw.WriteLine(test.Mark);
sw.WriteLine();
}
public static Marks StringToKindFromFile(string test)
{
if (test == "Удовл")
{
return Marks.Удовл;
}
if (test == "Хор")
{
return Marks.Хор;
}
if (test == "Очень_хор")
{
return Marks.Очень_хор;
}
if (test == "Отл")
{
return Marks.Отл;
}
return Marks.Null;
}
}
}