-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverController.cs
More file actions
118 lines (108 loc) · 4.11 KB
/
DriverController.cs
File metadata and controls
118 lines (108 loc) · 4.11 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
using CarDrivingDataManagement.Entity;
using CarDrivingDataManagement.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarDrivingDataManagement
{
public class DriverController
{
BTree<Driver> DriverTree { get; set; }
public DriverController(String driverFilename, int driverRecordsCountPerBlock)
{
DriverTree = new BTree<Driver>(driverRecordsCountPerBlock, driverFilename);
}
public DriverController(String driverFilename)
{
DriverTree = new BTree<Driver>(driverFilename);
}
public String DescribeDriverDBStructure()
{
return DriverTree.DescribeFileStructure();
}
public void GenerateData(int addCount)
{
Random random = new Random();
for (int i = 0; i < addCount; i++)
{
int randomNumber = random.Next(1, addCount*100);
DriverTree.Add(new Driver("Name" + i, "Surname" + i,
randomNumber, DateTime.Now.AddDays(randomNumber), false, randomNumber % 3
));
}
File.WriteAllText("C:/Users/User/source/repos/CarDrivingDataManagementTest/driverTreeLevelOrder.txt",
DriverTree.TraceLevelOrder());
}
public bool AddDriver(String name, String surname, int id, DateTime endDate, bool drivingForbidden,
int ruleViolationsCount)
{
return DriverTree.Add(new Driver(name, surname, id, endDate, drivingForbidden, ruleViolationsCount));
}
public bool UpdateDriver(String name, String surname, int id, DateTime endDate, bool drivingForbidden,
int ruleViolationsCount)
{
Driver searchObj = new Driver();
searchObj.CardID = id;
Block<Driver> block = DriverTree.Find(searchObj);
if (block != null)
{
Record<Driver>[] records = block.RecordsArray.Records;
for (int i = 0; i < records.Length; i++)
{
if (records[i].Data.CompareTo(searchObj) == 0)
{
searchObj.Name = name;
searchObj.Surname = surname;
searchObj.CardEndDate = endDate;
searchObj.DrivingForbidden = drivingForbidden;
searchObj.RuleViolationsCount = ruleViolationsCount;
records[i] = new Record<Driver>(searchObj);
DriverTree.WriteBlock(block);
return true;
}
}
}
return false;
}
public String[] GetDriverByID(int id)
{
String[] result = new string[6];
Driver driver = GetDriverByIDAsObject(id);
if(driver != null)
{
result[0] = driver.Name;
result[1] = driver.Surname;
result[2] = driver.CardID.ToString();
result[3] = driver.CardEndDate.ToString();
result[4] = driver.DrivingForbidden.ToString();
result[5] = driver.RuleViolationsCount.ToString();
}
return result;
}
private Driver GetDriverByIDAsObject(int id)
{
Driver searchObj = new Driver();
searchObj.CardID = id;
Block<Driver> block = DriverTree.Find(searchObj);
if (block != null)
{
Record<Driver>[] records = block.RecordsArray.Records;
foreach (Record<Driver> record in records)
{
if (record.Data.CompareTo(searchObj) == 0)
{
return record.Data;
}
}
}
return null;
}
public void Close()
{
DriverTree.CloseFileInteraction();
}
}
}