Skip to content

Commit b7cab47

Browse files
committed
Moved CellOverseer from Models to Logic, Added ISaveLoadManager and Implementation, Added UnitTests for SaveLoadManager and implemented SaveLoadManager, Removed .suo-File from Repository
1 parent 4f68225 commit b7cab47

5 files changed

Lines changed: 87 additions & 3 deletions

File tree

.vs/CellSimulator/v15/.suo

-102 KB
Binary file not shown.

CellSimulator.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
<Compile Include="Enums\ActionEnum.cs" />
4848
<Compile Include="Interfaces\ICell.cs" />
4949
<Compile Include="Interfaces\ICellOverseer.cs" />
50+
<Compile Include="Interfaces\ISaveLoadManager.cs" />
51+
<Compile Include="Logic\SaveLoadManager.cs" />
5052
<Compile Include="Models\Cell.cs" />
51-
<Compile Include="Models\CellOverseer.cs" />
53+
<Compile Include="Logic\CellOverseer.cs" />
5254
<Compile Include="Program.cs" />
5355
<Compile Include="Properties\AssemblyInfo.cs" />
5456
</ItemGroup>

Interfaces/ISaveLoadManager.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CellSimulator.Interfaces
8+
{
9+
public interface ISaveLoadManager
10+
{
11+
void SaveToFile(string fileName, IEnumerable<ICell> cells);
12+
IEnumerable<ICell> LoadFromFile(string fileName);
13+
}
14+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using CellSimulator.Interfaces;
7+
using CellSimulator.Models;
78

8-
namespace CellSimulator.Models
9+
namespace CellSimulator.Logic
910
{
1011
public class CellOverseer : ICellOverseer
1112
{
@@ -16,7 +17,7 @@ public class CellOverseer : ICellOverseer
1617

1718
public void AddCell()
1819
{
19-
AddCell(new Cell(internalIdCounter, rand));
20+
AddCell(new Models.Cell(internalIdCounter, rand));
2021
++internalIdCounter;
2122
}
2223

Logic/SaveLoadManager.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using CellSimulator.Interfaces;
7+
using CellSimulator.Models;
8+
using System.IO;
9+
using CellSimulator.Enums;
10+
11+
namespace CellSimulator.Logic
12+
{
13+
public class SaveLoadManager : ISaveLoadManager
14+
{
15+
private const char SEPARATION_CHAR = '|';
16+
17+
public IEnumerable<ICell> LoadFromFile(string fileName)
18+
{
19+
if (FileExists(fileName))
20+
{
21+
return LoadCellList(fileName);
22+
}
23+
return null;
24+
}
25+
26+
private bool FileExists(string fileName)
27+
{
28+
return File.Exists(fileName);
29+
}
30+
31+
private IEnumerable<ICell> LoadCellList(string fileName)
32+
{
33+
List<ICell> cells = new List<ICell>();
34+
using (StreamReader reader = new StreamReader(fileName))
35+
{
36+
string[] currentLineValues;
37+
while (!reader.EndOfStream)
38+
{
39+
currentLineValues = reader.ReadLine().Split(SEPARATION_CHAR);
40+
cells.Add(new Models.Cell()
41+
{
42+
ID = int.Parse(currentLineValues[0]),
43+
Age = int.Parse(currentLineValues[1]),
44+
Food = int.Parse(currentLineValues[2]),
45+
MaxFood = int.Parse(currentLineValues[3]),
46+
Energy = int.Parse(currentLineValues[4]),
47+
MaxEnergy = int.Parse(currentLineValues[5]),
48+
LastAction = (ActionEnum)Enum.Parse(typeof(ActionEnum), currentLineValues[6])
49+
});
50+
}
51+
}
52+
return cells;
53+
}
54+
55+
public void SaveToFile(string fileName, IEnumerable<ICell> cells)
56+
{
57+
using (StreamWriter writer = new StreamWriter(fileName))
58+
{
59+
foreach (ICell cell in cells)
60+
{
61+
writer.WriteLine(string.Join(SEPARATION_CHAR.ToString(),
62+
new object[] { cell.ID, cell.Age, cell.Food, cell.MaxFood, cell.Energy, cell.MaxEnergy, cell.LastAction }));
63+
}
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)