Skip to content

Commit 0181a63

Browse files
committed
Another exam
1 parent 7d9a4c8 commit 0181a63

File tree

34 files changed

+1058
-0
lines changed

34 files changed

+1058
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27703.2026
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AquaShop", "AquaShop\AquaShop.csproj", "{C462E007-CC34-40FC-B846-BC6FF3F35C81}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C462E007-CC34-40FC-B846-BC6FF3F35C81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C462E007-CC34-40FC-B846-BC6FF3F35C81}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C462E007-CC34-40FC-B846-BC6FF3F35C81}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C462E007-CC34-40FC-B846-BC6FF3F35C81}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {CDACA7DE-DF66-492D-B822-ACADEC984399}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace AquaShop.Core.Contracts
2+
{
3+
public interface IController
4+
{
5+
string AddAquarium(string aquariumType, string aquariumName);
6+
7+
string AddDecoration(string decorationType);
8+
9+
string InsertDecoration(string aquariumName, string decorationType);
10+
11+
string AddFish(string aquariumName, string fishType, string fishName, string fishSpecies, decimal price);
12+
13+
string FeedFish(string aquariumName);
14+
15+
string CalculateValue(string aquariumName);
16+
17+
string Report();
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AquaShop.Core.Contracts
2+
{
3+
using System;
4+
5+
public interface IEngine
6+
{
7+
void Run();
8+
}
9+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using AquaShop.Core.Contracts;
3+
using AquaShop.Models.Aquariums.Contracts;
4+
using AquaShop.Repositories;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using AquaShop.Models.Aquariums;
9+
using AquaShop.Models.Decorations;
10+
using AquaShop.Models.Decorations.Contracts;
11+
using AquaShop.Models.Fish;
12+
using AquaShop.Models.Fish.Contracts;
13+
using AquaShop.Utilities.Messages;
14+
15+
namespace AquaShop.Core
16+
{
17+
public class Controller : IController
18+
{
19+
private List<IAquarium> aquariums = new List<IAquarium>();
20+
private DecorationRepository decorations = new DecorationRepository();
21+
22+
public string AddAquarium(string aquariumType, string aquariumName)
23+
{
24+
IAquarium aquarium;
25+
26+
if (aquariumType == nameof(FreshwaterAquarium))
27+
{
28+
aquarium = new FreshwaterAquarium(aquariumName);
29+
}
30+
else if (aquariumType == nameof(SaltwaterAquarium))
31+
{
32+
aquarium = new SaltwaterAquarium(aquariumName);
33+
}
34+
else
35+
{
36+
throw new InvalidOperationException(ExceptionMessages.InvalidAquariumType);
37+
}
38+
39+
aquariums.Add(aquarium);
40+
return string.Format(OutputMessages.SuccessfullyAdded, aquariumType);
41+
}
42+
43+
public string AddDecoration(string decorationType)
44+
{
45+
IDecoration decor;
46+
47+
if (decorationType == nameof(Ornament))
48+
{
49+
decor = new Ornament();
50+
}
51+
else if (decorationType == nameof(Plant))
52+
{
53+
decor = new Plant();
54+
}
55+
else
56+
{
57+
throw new InvalidOperationException(ExceptionMessages.InvalidDecorationType);
58+
}
59+
60+
decorations.Add(decor);
61+
return string.Format(OutputMessages.SuccessfullyAdded, decorationType);
62+
}
63+
64+
public string InsertDecoration(string aquariumName, string decorationType)
65+
{
66+
var targetDecor = decorations.FindByType(decorationType);
67+
var targetAquarium = aquariums.Find(x => x.Name == aquariumName);
68+
69+
if (targetDecor is null)
70+
{
71+
throw new InvalidOperationException(string.Format(ExceptionMessages.InexistentDecoration, decorationType));
72+
}
73+
74+
targetAquarium?.AddDecoration(targetDecor);
75+
decorations.Remove(targetDecor);
76+
77+
return string.Format(OutputMessages.EntityAddedToAquarium, decorationType, aquariumName);
78+
}
79+
80+
public string AddFish(string aquariumName, string fishType, string fishName, string fishSpecies, decimal price)
81+
{
82+
var targetAquarium = aquariums.Find(x => x.Name == aquariumName);
83+
IFish fish;
84+
85+
if (fishType == nameof(FreshwaterFish))
86+
{
87+
fish = new FreshwaterFish(fishName, fishSpecies, price);
88+
}
89+
else if (fishType == nameof(SaltwaterFish))
90+
{
91+
fish = new SaltwaterFish(fishName, fishSpecies, price);
92+
}
93+
else
94+
{
95+
throw new InvalidOperationException(ExceptionMessages.InvalidFishType);
96+
}
97+
98+
if (targetAquarium is FreshwaterAquarium && fishType == nameof(FreshwaterFish))
99+
{
100+
//POSSIBLE ERROR
101+
targetAquarium.AddFish(fish);
102+
return string.Format(OutputMessages.EntityAddedToAquarium, fishType, aquariumName);
103+
}
104+
if (targetAquarium is SaltwaterAquarium && fishType == nameof(SaltwaterFish))
105+
{
106+
targetAquarium.AddFish(fish);
107+
return string.Format(OutputMessages.EntityAddedToAquarium, fishType, aquariumName);
108+
}
109+
else
110+
{
111+
return (OutputMessages.UnsuitableWater);
112+
}
113+
}
114+
115+
public string FeedFish(string aquariumName)
116+
{
117+
var targetAquarium = aquariums.Find(x => x.Name == aquariumName);
118+
targetAquarium?.Feed();
119+
120+
return string.Format(OutputMessages.FishFed, targetAquarium?.Fish.Count);
121+
}
122+
123+
public string CalculateValue(string aquariumName)
124+
{
125+
var targetAquarium = aquariums.Find(x => x.Name == aquariumName);
126+
decimal decorPrice = targetAquarium.Decorations.Sum(x => x.Price);
127+
decimal fishesPrice = targetAquarium.Fish.Sum(x => x.Price);
128+
decimal total = decorPrice + fishesPrice;
129+
130+
return string.Format(OutputMessages.AquariumValue, aquariumName, total);
131+
}
132+
133+
public string Report()
134+
{
135+
StringBuilder sb = new StringBuilder();
136+
137+
foreach (var aquarium in aquariums)
138+
{
139+
sb.AppendLine(aquarium.GetInfo());
140+
}
141+
142+
return sb.ToString().TrimEnd();
143+
}
144+
}
145+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
namespace AquaShop.Core
2+
{
3+
using System;
4+
5+
using AquaShop.IO;
6+
using AquaShop.IO.Contracts;
7+
using AquaShop.Core.Contracts;
8+
9+
public class Engine : IEngine
10+
{
11+
private IWriter writer;
12+
private IReader reader;
13+
private IController controller;
14+
15+
public Engine()
16+
{
17+
this.writer = new Writer();
18+
this.reader = new Reader();
19+
this.controller = new Controller();
20+
}
21+
22+
public void Run()
23+
{
24+
while (true)
25+
{
26+
string[] input = reader.ReadLine().Split();
27+
if (input[0] == "Exit")
28+
{
29+
Environment.Exit(0);
30+
}
31+
try
32+
{
33+
string result = string.Empty;
34+
35+
if (input[0] == "AddAquarium")
36+
{
37+
string aquariumType = input[1];
38+
string aquariumName = input[2];
39+
40+
result = controller.AddAquarium(aquariumType, aquariumName);
41+
}
42+
else if (input[0] == "AddDecoration")
43+
{
44+
string decorationType = input[1];
45+
46+
result = controller.AddDecoration(decorationType);
47+
}
48+
else if (input[0] == "InsertDecoration")
49+
{
50+
string aquariumName = input[1];
51+
string decorationType = input[2];
52+
53+
result = controller.InsertDecoration(aquariumName, decorationType);
54+
}
55+
else if (input[0] == "AddFish")
56+
{
57+
string aquariumName = input[1];
58+
string fishType = input[2];
59+
string fishName = input[3];
60+
string fishSpecies = input[4];
61+
decimal price = decimal.Parse(input[5]);
62+
63+
result = controller.AddFish(aquariumName, fishType, fishName, fishSpecies, price);
64+
}
65+
else if (input[0] == "FeedFish")
66+
{
67+
string aquariumName = input[1];
68+
69+
result = controller.FeedFish(aquariumName);
70+
}
71+
else if (input[0] == "CalculateValue")
72+
{
73+
string aquariumName = input[1];
74+
75+
result = controller.CalculateValue(aquariumName);
76+
}
77+
else if (input[0] == "Report")
78+
{
79+
result = controller.Report();
80+
}
81+
82+
writer.WriteLine(result);
83+
}
84+
catch (Exception ex)
85+
{
86+
writer.WriteLine(ex.Message);
87+
}
88+
}
89+
}
90+
}
91+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AquaShop.IO.Contracts
2+
{
3+
public interface IReader
4+
{
5+
string ReadLine();
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AquaShop.IO.Contracts
2+
{
3+
public interface IWriter
4+
{
5+
void WriteLine(string message);
6+
7+
void Write(string message);
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace AquaShop.IO
2+
{
3+
using System;
4+
5+
using AquaShop.IO.Contracts;
6+
7+
public class Reader : IReader
8+
{
9+
public string ReadLine()
10+
{
11+
return Console.ReadLine();
12+
}
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace AquaShop.IO
2+
{
3+
using System;
4+
5+
using AquaShop.IO.Contracts;
6+
7+
public class Writer : IWriter
8+
{
9+
public void Write(string message)
10+
{
11+
Console.Write(message);
12+
}
13+
14+
public void WriteLine(string message)
15+
{
16+
Console.WriteLine(message);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)