-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManipulator.vb
269 lines (232 loc) · 10.9 KB
/
FileManipulator.vb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Imports System.IO
Public Class FileManipulator
Public Shared accountsDatabaseFilePath As String = "nomVetAccounts.txt"
Public Shared bookingsDatabaseFilePath As String = "nomVetBookings.txt"
Public Shared sessionsDatabaseFilePath As String = "nomVetSessions.txt"
Public Shared Function findPetOwner(lines() As String, username As String, password As String) As PetOwner
Dim petOwnerObject As PetOwner
For Each line As String In lines
petOwnerObject = parseAsPetOwner(line)
If petOwnerObject.getUsername = username AndAlso petOwnerObject.getPassword = password Then
Return petOwnerObject
End If
Next
Return Nothing
End Function
Public Shared Function findPetOwnerWithName(petOwnerName As String) As PetOwner
Dim linessss() As String = readData(accountsDatabaseFilePath)
Dim petOwnerObject As PetOwner
For Each line As String In linessss
petOwnerObject = parseAsPetOwner(line)
If petOwnerObject.strName = petOwnerName Then
Return petOwnerObject
End If
Next
Return Nothing
End Function
Public Shared Function readData(fileName As String)
Dim lines() As String = {}
If File.Exists(fileName) Then
lines = File.ReadAllLines(fileName)
Else
MessageBox.Show("No database file found.")
End If
Return lines
End Function
Public Shared Sub createDataBaseFile(filename As String)
Dim databaseFilePath As String = filename
If Not File.Exists(databaseFilePath) Then
File.Create(databaseFilePath).Dispose()
End If
End Sub
Public Shared Sub initializeDataBaseFiles()
Dim databaseFilePaths() As String = {accountsDatabaseFilePath, bookingsDatabaseFilePath, sessionsDatabaseFilePath}
For Each path In databaseFilePaths
If Not File.Exists(path) Then
File.Create(path).Dispose()
End If
Next
End Sub
Public Shared Sub SaveUser(petOwnerObject As PetOwner)
'creating user's own database for pets
Dim petOwnerFileName As String = petOwnerObject.getUsername & ".txt"
createDataBaseFile(petOwnerFileName)
'saving to accounts database
Using writer As StreamWriter = File.AppendText(accountsDatabaseFilePath)
writer.WriteLine(petOwnerObject.getUsername & "|" & petOwnerObject.getPassword & "|" & petOwnerObject.strName & "|" & petOwnerObject.intAge & "|" & petOwnerObject.strSex & "|" & petOwnerObject.strAddress)
End Using
End Sub
Public Shared Function ReadPetOwners()
Dim lines() As String = readData(accountsDatabaseFilePath)
Dim petownersList As New List(Of PetOwner)
For Each line As String In lines
Dim petOwnerObject As PetOwner = parseAsPetOwner(line)
petownersList.Add(petOwnerObject)
Next
Return petownersList
End Function
Public Shared Function parseAsPetOwner(line As String) As PetOwner
Dim parsedStringsList() As String = line.Split("|"c)
Dim username = parsedStringsList(0)
Dim password = parsedStringsList(1)
Dim name = parsedStringsList(2)
Dim age = parsedStringsList(3)
Dim sex = parsedStringsList(4)
Dim address = parsedStringsList(5)
Dim petOwnerObject As New PetOwner(username, password, name, age, sex, address, New List(Of Pet))
Return petOwnerObject
End Function
Public Shared Sub ClearPets(petOwnerObject As PetOwner)
File.WriteAllText(petOwnerObject.getUsername + ".txt", String.Empty)
End Sub
Public Shared Function ReadPets(activeAccount As PetOwner)
Dim lines() As String = readData(activeAccount.getUsername() & ".txt")
Dim petsList As New List(Of Pet)
For Each line As String In lines
Dim petObject As Pet = parseAsPet(line)
petsList.Add(petObject)
Next
Return petsList
End Function
Public Shared Sub SavePets(petOwner As PetOwner)
ClearPets(petOwner)
For Each pet In petOwner.petsList
SavePet(petOwner, pet)
Next
End Sub
Public Shared Sub SavePet(petOwnerObject As PetOwner, petObject As Pet)
Using writer As StreamWriter = File.AppendText(petOwnerObject.getUsername + ".txt")
writer.WriteLine(petObject.strName & "," & petObject.intAge & "," & petObject.dateBirthday & "," & petObject.dblWeight & "," & petObject.strType & "," & petObject.boolVaccinated & "," & petObject.dateOfNextVisit)
End Using
End Sub
Public Shared Function parseAsPet(line As String) As Pet
Dim parsedStringsList() As String = line.Split(","c)
Dim name = parsedStringsList(0)
Dim age = parsedStringsList(1)
Dim bday = parsedStringsList(2)
Dim weight = parsedStringsList(3)
Dim type = parsedStringsList(4)
Dim vaccineStatus = parsedStringsList(5)
Dim nextVisit = parsedStringsList(6)
Dim petObject As New Pet(name, age, bday, weight, type, vaccineStatus, nextVisit)
Return petObject
End Function
Public Shared Function getListOfPetsName(ByVal bookingLists As List(Of Appointment)) As List(Of String)
Dim petsNameLists As New List(Of String)
For Each book In bookingLists
petsNameLists.Add(book.pet)
Next
Return petsNameLists
End Function
'Public Shared Sub SaveAppointment(petOwnerObject As PetOwner, petObject As Pet)
' Using writer As StreamWriter = File.AppendText(petOwnerObject.getUsername + ".txt")
' writer.WriteLine(petObject.strName & "," & petObject.intAge & "," & petObject.dateBirthday & "," & petObject.dblWeight & "," & petObject.strType & "," & petObject.boolVaccinated & "," & petObject.appointment.strProcedure & "," & petObject.appointment.dateAppointment)
' End Using
'End Sub
Public Shared Sub ClearBookings()
File.WriteAllText(bookingsDatabaseFilePath, String.Empty)
End Sub
Public Shared Sub SaveBookings(ByVal apptList As List(Of Appointment))
ClearBookings()
For Each appointment In apptList
SaveBooking(appointment)
Next
End Sub
Public Shared Sub SaveBooking(appointment As Appointment)
Using writer As StreamWriter = File.AppendText(bookingsDatabaseFilePath)
writer.WriteLine(appointment.appointmentId & "," & appointment.petOwner & "," & appointment.pet & "," & appointment.petVacStatus & "," & appointment.strProcedure & "," & appointment.dateAppointment)
End Using
End Sub
Public Shared Function ReadBookings() As List(Of Appointment)
Dim lines() As String = readData(bookingsDatabaseFilePath)
Dim bookingsList As New List(Of Appointment)
For Each line As String In lines
Dim bookingObject As Appointment = parseAsBooking(line)
bookingsList.Add(bookingObject)
Next
Return bookingsList
End Function
Public Shared Function ReadBookings(ByVal nameOfPetOwner As String) As List(Of Appointment)
Dim lines() As String = readData(bookingsDatabaseFilePath)
Dim bookingsList As New List(Of Appointment)
For Each line As String In lines
Dim bookingObject As Appointment = parseAsBooking(line)
If bookingObject.petOwner = nameOfPetOwner Then
bookingsList.Add(bookingObject)
End If
Next
Return bookingsList
End Function
Public Shared Function parseAsBooking(line As String) As Appointment
Dim parsedStringsList() As String = line.Split(","c)
Dim appointmentId As String = parsedStringsList(0)
Dim petOwnerName As String = parsedStringsList(1)
Dim petName = parsedStringsList(2)
Dim petVacStatus = parsedStringsList(3)
Dim procedure = parsedStringsList(4)
Dim dateOfAppointment = parsedStringsList(5)
Dim bookingObject As New Appointment(appointmentId, petOwnerName, petName, petVacStatus, procedure, dateOfAppointment)
Return bookingObject
End Function
Public Shared Sub SavePetOwner(petOwner As PetOwner)
Dim petOwnerFileName As String = petOwner.getUsername & ".txt"
createDataBaseFile(petOwnerFileName)
Using writer As StreamWriter = File.AppendText(accountsDatabaseFilePath)
writer.WriteLine(petOwner.getUsername() & "|" & petOwner.getPassword() & "|" & petOwner.strName & "|" & petOwner.intAge & "|" & petOwner.strSex & "|" & petOwner.strAddress)
End Using
For Each pet In petOwner.petsList
SavePet(petOwner, pet)
Next
End Sub
'Public Shared Function ReadPetOwners()
' Dim petOwnersList As New List(Of String)
' Using reader As New System.IO.StreamReader(accountsDatabaseFilePath)
' Dim line As String
' Do While reader.Peek() >= 0
' line = reader.ReadLine()
' petOwnersList.Add(line)
' Loop
' End Using
' Return petOwnersList
'End Function
Public Shared Sub ClearSessions()
File.WriteAllText(sessionsDatabaseFilePath, String.Empty)
End Sub
Public Shared Sub SaveSessions(ByVal sessionsList As List(Of Session))
ClearSessions()
For Each session In sessionsList
SaveSession(session)
Next
End Sub
Public Shared Sub SaveSession(ByVal session As Session)
Using writer As StreamWriter = File.AppendText(sessionsDatabaseFilePath)
Dim str As String = ""
str &= session.petWithProcedureList.Item(0)
For i = 1 To session.petWithProcedureList.Count - 1
str &= ("%" & session.petWithProcedureList.Item(i))
Next
writer.WriteLine(session.sessionCodeId & "," & session.petOwner.strName & "," & session.dateMade.Date & "," & str)
End Using
End Sub
Public Shared Function ReadSessions() As List(Of Session)
Dim lines() As String = readData(sessionsDatabaseFilePath)
Dim sessionsList As New List(Of Session)
If (lines.Length > 0) Then
For Each line As String In lines
Dim sessionObject As Session = parseAsSession(line)
sessionsList.Add(sessionObject)
Next
End If
Return sessionsList
End Function
Public Shared Function parseAsSession(line As String) As Session
Dim parsedStringsList() As String = line.Split(","c)
Dim sessionId As String = parsedStringsList(0)
Dim petOwnerName As String = parsedStringsList(1)
Dim dateMade As String = parsedStringsList(2)
Dim petsList As List(Of String) = parsedStringsList(3).Split("%"c).ToList()
Dim petOwnerObject As PetOwner = findPetOwnerWithName(petOwnerName)
Dim sessionObject As New Session(sessionId, petOwnerObject, dateMade, petsList)
Return sessionObject
End Function
End Class