-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program (13).cs
136 lines (117 loc) · 4.91 KB
/
Program (13).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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
namespace Homework8
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\V\Desktop\SoftServe\test.txt";
List<Shape> shapes = new List<Shape>();
shapes.Add(new Square("Square_A", 2));
shapes.Add(new Square("Square_B", 8));
shapes.Add(new Square("Square_C", 6));
shapes.Add(new Square("Square_D", 4));
shapes.Add(new Circle("Circle_A", 9));
shapes.Add(new Circle("Circle_B", 7));
shapes.Add(new Circle("Circle_C", 5));
shapes.Add(new Circle("Circle_D", 3));
//=========================================
IEnumerable<Shape> query = shapes.Where(x => x.Area() > 10 && x.Area() < 100);
try
{
using (StreamWriter sr = new StreamWriter(path))
{
foreach (Shape item in query)
{
item.PrintToFile(sr);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
//==========================================
IEnumerable<Shape> query2 = from s in shapes
where s.Name.ToLower().Contains('a')
select s;
try
{
using (StreamWriter sr = new StreamWriter(path))
{
foreach (Shape item in query2)
{
item.PrintToFile(sr);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
//=========================================================
IEnumerable<Shape> query3 = from s in shapes
where s.Perimeter() > 5
select s;
shapes.RemoveAll(x => x.Perimeter() < 5);
foreach (Shape item in shapes)
{
Console.WriteLine(item.Perimeter());
}
foreach (Shape item in query3)
{
Console.WriteLine(item);
}
//=======================Binary Serialize==================================
Console.WriteLine("============Binary Serialize==============");
Circle circle = new Circle("FirstCirce", 5);
BinaryFormatter formater = new BinaryFormatter();
FileStream stream = new FileStream("BinarySerialize.bin", FileMode.Create, FileAccess.Write);
formater.Serialize(stream, circle);
stream.Close();
FileStream stream1 = new FileStream("BinarySerialize.bin", FileMode.Open, FileAccess.Read);
Circle circle1 = formater.Deserialize(stream1) as Circle;
if (circle1 != null)
{
Console.WriteLine(circle1.Name);
}
stream1.Close();
//=======================XML Serialize==================================
Console.WriteLine("============XML Serialize==============");
Square square = new Square("FirstSquare", 4);
XmlSerializer xmlSerializer = new XmlSerializer(square.GetType());
FileStream stream2 = new FileStream("XMLSerialize.xml", FileMode.Create, FileAccess.Write);
xmlSerializer.Serialize(stream2, square);
stream2.Close();
FileStream stream3 = new FileStream("XMLSerialize.xml", FileMode.Open, FileAccess.Read);
Square square1 = xmlSerializer.Deserialize(stream3) as Square;
if (square1 != null)
{
Console.WriteLine(square1.Name);
}
stream3.Close();
//=======================JSON Serialize==================================
Console.WriteLine("============JSON Serialize==============");
Square square2 = new Square("SecondSquare", 8);
DataContractSerializer dataContract = new DataContractSerializer(square2.GetType());
Stream stream4 = new FileStream("JSONSerialize.json", FileMode.Create, FileAccess.ReadWrite);
dataContract.WriteObject(stream4, square2);
//stream4.Close();
stream4.Position = 0;
Square square3 = dataContract.ReadObject(stream4) as Square;
if (square3 != null)
{
Console.WriteLine(square3.Name);
Console.WriteLine(square3.Side);
}
stream4.Close();
}
}
}