-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStore.cs
187 lines (133 loc) · 5.66 KB
/
DataStore.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace RomanNumerals.V2
{
internal class DataStore
{
public List<string> EnteredInputs { get; set; }
public List<string> Results { get; set; }
// private write data method.
private void WriteData(StreamWriter writer)
{
for (int i = 0; i < EnteredInputs.Count; i++)
{
writer.WriteLine($"{EnteredInputs.ElementAt(i)},{Results.ElementAt(i)}");
}
}
// declare the file that will be written to and read from. Outside of each method so they can both access it.
string numeralHistoryData = @"C:\Users\Georgina.Bidder\.vscode\textFile.csv";
// write to the file.
public void Write(NumeralConversion numeralConversion)
{
// added to catch any edge cases.
if (numeralConversion == null || numeralHistoryData.Length == 0)
{
Console.WriteLine("There are no records.");
}
else // if the numeral conversion is valid then write the entered input and the result (taken from the numeral conversion class).
{
EnteredInputs.Add(numeralConversion.EnteredValue);
Results.Add(numeralConversion.NewValue);
// initalize a new instance of the built in StreamWriter class so that we can write to the numeralHistoryData file.
using var writer = new StreamWriter(numeralHistoryData);
//the private writedata method is declared, taking the new instance of StreamWriter.
WriteData(writer);
}
}
// read the file.
public IList<string[]> Read(NumeralConversion numeralConversion)
{
var results = new List<string[]>();
if (!File.Exists(numeralHistoryData))
{
Console.WriteLine($"The data file does not exist at {numeralHistoryData}.");
return results;
}
using (var reader = new StreamReader(numeralHistoryData))
{
string output;
EnteredInputs = new List<string>();
Results = new List<string>();
while ((output = reader.ReadLine()) != null)
{
List<string> historicData = new List<string>();
var stringBuilder = new StringBuilder();
stringBuilder.Append(output);
// temp array to hold each line as it loops
string[] tempArray = stringBuilder.ToString().Split(',');
foreach (string t in tempArray)
{
historicData.Add(t);
}
if (historicData.ElementAt(0) != "" && historicData.Count < 10)
{
EnteredInputs.Add(historicData.ElementAt(0));
Results.Add(historicData.ElementAt(1));
}
}
//the t
if (EnteredInputs.Count >= 4)
{
EnteredInputs.RemoveAt(0);
Results.RemoveAt(0);
}
for (int i = 0; i < EnteredInputs.Count; i++)
{
Console.WriteLine($"{EnteredInputs.ElementAt(i)}, {Results.ElementAt(i)}");
}
}
return results;
}
}
}
// Stream numeralHistoryData = new FileStream(@"C:\Users\Georgina.Bidder\.vscode\textFile.csv", FileMode.Create, FileAccess.Write);
//StreamWriter writer = new StreamWriter(numeralHistoryData);
// write to file
//try
//{
// // FileStream stream = new FileStream(numeralHistoryData, FileMode.Open);
// using (FileStream stream = new FileStream(numeralHistoryData, FileMode.Open))
// {
// using (var writer = new BinaryWriter(stream, Encoding.UTF8))
// {
// writer.Write($"{EnteredInputs}, {Results}");
// File.AppendText(numeralHistoryData);
// }
// }
//}
//catch (Exception exp)
//{
// Console.Write(exp.Message);
//}
// }
// public void DisplayValues()
//{
// string readValues;
// if (File.Exists(numeralHistoryData))
// {
// using (var stream = File.Open(numeralHistoryData, FileMode.Open))
// {
// using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
// {
// readValues = reader.ReadString();
// }
// }
// }
//// StreamReader reader = new StreamReader(File.OpenRead(@"C:\Users\Georgina.Bidder\.vscode\textFile.csv"));
//// object written to files
//IFormatter formatter = new BinaryFormatter();
//Stream stream = new FileStream(@"C:\Users\Georgina.Bidder\.vscode\textFile.csv", FileMode.Create, FileAccess.Write);
//formatter.Serialize(stream, numeralConversion);
// stream.Close();
// // deserialization to extract the data from the application for further use.
// stream = new FileStream(@"C:\Users\Georgina.Bidder\.vscode\textFile.csv", FileMode.Open, FileAccess.Read);
//NumeralConversion usersInputs = (NumeralConversion)formatter.Deserialize(stream);
//Console.WriteLine($"{usersInputs.OldValue} - {usersInputs.NewValue}");
// //stream writer, text writer, append - append to end. text over binary. writer.WriteLine. create streamwriter, give it the path(as a string) writer.writeline.
// // stream reader, string reader, string builder.