Repository navigation
Expand file tree
/
Copy pathDriverList.cs
More file actions
151 lines (124 loc) · 3.21 KB
/
Copy pathDriverList.cs
File metadata and controls
151 lines (124 loc) · 3.21 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
namespace DriverDatabase
{
class OrigList
{
public event EventHandler ListChanged;
public void FireListChanged()
{
if (ListChanged != null)
ListChanged(this, null);
}
}
class DriverList : OrigList
{
private static DriverList instance = new DriverList();
public static DriverList Instance
{
get { return instance; }
set { }
}
public List<Driver> Drivers = new List<Driver>();
public Driver this[int index]
{
get { return Drivers[index]; }
set { Drivers[index] = value; FireListChanged(); }
}
public int Count { get { return Drivers.Count; } }
public void Add(Driver driver)
{
Drivers.Add(driver);
FireListChanged();
}
public void Remove(Driver driver)
{
foreach (var car in CarList.Instance.Cars.ToList())
{
car.Drivers.Remove(driver);
if (car.Drivers.Count == 0)
CarList.Instance.Cars.Remove(car);
}
Drivers.Remove(driver);
FireListChanged();
}
public static readonly string DefaultFileName = @"drivers.xml";
public void LoadFromFile(String fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Driver>));
StreamReader reader = new StreamReader(fileName);
this.Drivers = (List<Driver>)serializer.Deserialize(reader);
reader.Close();
FireListChanged();
}
public void SaveToFile(String fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Driver>));
StreamWriter writer = new StreamWriter(fileName);
serializer.Serialize(writer, this.Drivers);
writer.Close();
}
public void CreateFile(String fileName)
{
using (FileStream fs = File.Create(fileName))
{
}
}
}
class CarList : OrigList
{
private static CarList instance = new CarList();
public static CarList Instance { get { return instance; } set { } }
public List<Car> Cars = new List<Car>();
public Car this[int index]
{
get { return Cars[index]; }
set { Cars[index] = value; FireListChanged(); }
}
public int Count { get { return Cars.Count; } }
public void Add(Car car)
{
Cars.Add(car);
FireListChanged();
}
public void Remove(Car car)
{
foreach (var driver in DriverList.Instance.Drivers)
{
driver.Cars.Remove(car);
}
Cars.Remove(car);
FireListChanged();
}
public void LoadFromDrivers()
{
foreach (var driver in DriverList.Instance.Drivers)
{
foreach (var car in driver.Cars.ToList())
{
if (!CarList.Instance.Cars.Any(el => Car.Compare(el, car)))
{
CarList.Instance.Cars.Add(car);
car.Drivers.Add(driver);
}
else
{
Car createdCar = CarList.Instance.Cars.Find(el => Car.Compare(el, car));
createdCar.Drivers.Add(driver);
driver.Cars.Remove(car);
driver.Cars.Add(createdCar);
}
}
}
FireListChanged();
}
public void CreateFile(String fileName)
{
using (FileStream fs = File.Create(fileName)) {
}
}
}
}