Newer
Older
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.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace TUGraz.VectoCore.BusAuxiliaries.DownstreamModules.Impl.HVAC
{
public class BusDatabase : IBusDatabase
{
private List<IBus> buses = new List<IBus>();
private List<IBus> selectListBuses = new List<IBus>();
public bool AddBus(IBus bus)
{
var result = true;
try {
buses.Add(bus);
} catch (Exception ex) {
result = false;
}
return result;
}
public List<IBus> GetBuses(string busModel, bool AsSelectList = false)
{
if (AsSelectList) {
selectListBuses = new List<IBus>();
selectListBuses = buses.Where(v => v.Model == "" || v.Model.ToLower().Contains(busModel.ToLower())).ToList();
selectListBuses.Insert(0, new Bus(0, "<Select>", "low floor", "gas", 1, 1, 1, 2, false));
return selectListBuses;
}
return buses.Where(v => v.Model == "" || v.Model.ToLower().Contains(busModel.ToLower())).ToList();
}
public bool Initialise(string filepath)
{
var returnStatus = true;
if (File.Exists(filepath)) {
using (var sr = new StreamReader(filepath)) {
// get array og lines fron csv
var lines = sr.ReadToEnd().Split(new [] { Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
// Must have at least 2 entries in map to make it usable [dont forget the header row]
if (lines.Length < 2) {
return false;
}
var firstline = true;
var id = 1;
foreach (var line in lines) {
if (!firstline) {
// split the line
var elements = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// 7 or 8 entries per line required
if ((elements.Length != 7 && elements.Length != 8))
throw new ArgumentException("Incorrect number of values in csv file");
// Bus
try {
var bus = new Bus(id, elements[0], elements[1], elements[2], double.Parse(elements[3], CultureInfo.InvariantCulture), double.Parse(elements[4], CultureInfo.InvariantCulture), double.Parse(elements[5], CultureInfo.InvariantCulture), int.Parse(elements[6], CultureInfo.InvariantCulture), elements.Length == 8 ? bool.Parse(elements[7]) : false);
buses.Add(bus);
} catch (Exception ex) {
// Indicate problems
returnStatus = false;
}
id = id + 1;
} else {
firstline = false;
}
}
}
} else {
returnStatus = false;
}
var uniqueBuses = buses.Select(b => b.Model).Distinct().Count();
if (buses.Count != uniqueBuses) {
returnStatus = false;
}
return returnStatus;
}
public bool UpdateBus(int id, IBus bus)
{
var result = true;
try {
var existingBus = buses.Single(b => b.Id == id);
existingBus.Model = bus.Model;
existingBus.RegisteredPassengers = bus.RegisteredPassengers;
existingBus.FloorType = bus.FloorType;
existingBus.LengthInMetres = bus.LengthInMetres;
existingBus.WidthInMetres = bus.WidthInMetres;
existingBus.HeightInMetres = bus.HeightInMetres;
existingBus.IsDoubleDecker = bus.IsDoubleDecker;
} catch (Exception ex) {
result = false;
}
return result;
}
public bool Save(string filepath)
{
var result = true;
var output = new StringBuilder();
try {
output.AppendLine("Bus Model,Type,engine Type,length in m,wide in m,height in m,registered passengers,double decker");
foreach (var bus in buses)
output.AppendLine(bus.ToString());
File.WriteAllText(filepath, output.ToString());
} catch (Exception ex) {
result = false;
}
return result;
}
}
}