Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions eltoby/StringCalculator/StringCalculator.Logic/Calculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace StringCalculator.Logic
{
public class Calculator
{
public int Sum(int[] numbers)
{
List<int> negatives = new List<int>();

int sum = 0;
foreach (int number in numbers)
if (number < 0)
negatives.Add(number);
else
if (number <= 1000)
sum += number;

if (negatives.Count >0)
throw new NegativesNotAllowedException(negatives.ToArray(),"numbers");
else
return sum;
}

public int Sum(string input)
{
InputParser ip = new InputParser();
return Sum(ip.Parse(input));
}

}
}
68 changes: 68 additions & 0 deletions eltoby/StringCalculator/StringCalculator.Logic/InputParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace StringCalculator.Logic
{
internal class InputParser
{
public int[] Parse(string input)
{
List<string> delimiters = new List<string>();
delimiters.AddRange(new string[] {",","\n"});
if (hasSpecialDelimiter(input))
{
delimiters.AddRange(getSpecialDelimiters(input));
input = removeSpecialDelimiterLine(input);
}

List<int> numbers = new List<int>();
if (!String.IsNullOrEmpty(input.Trim()))
foreach (string part in input.Split(delimiters.ToArray(), StringSplitOptions.RemoveEmptyEntries))
numbers.Add(int.Parse(part));

return numbers.ToArray();
}

bool hasSpecialDelimiter(string input)
{
return (input.StartsWith("//"));
}

string[] getSpecialDelimiters(string input)
{
List<string> delimiters = new List<string>();
string delimiterLine = input.Substring(2, input.IndexOf('\n') -2);
StringBuilder stbDelimiter = new StringBuilder();
bool inDelimiter = false;
foreach (char c in delimiterLine)
{
switch (c)
{
case '[':
inDelimiter = true;
stbDelimiter = new StringBuilder();
break;
case ']':
inDelimiter = false;
delimiters.Add(stbDelimiter.ToString());
break;
default:
if (inDelimiter)
stbDelimiter.Append(c);
else
delimiters.Add(c.ToString());
break;
}
}
return delimiters.ToArray();
}

string removeSpecialDelimiterLine(string input)
{
int startIndex = input.IndexOf('\n') + 1;
return input.Substring(startIndex);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace StringCalculator.Logic
{
public class NegativesNotAllowedException :ArgumentOutOfRangeException
{
int[] negatives;
public NegativesNotAllowedException(int[] negatives, string paramName) :base(paramName,negatives,"Negatives not allowed")
{
this.negatives = negatives;
}

public string Negatives
{
get
{
return getString(negatives);
}
}

string getString(int[] numbers)
{
StringBuilder stb = new StringBuilder();
foreach (int number in numbers)
{
if (stb.Length > 0)
stb.Append(",");
stb.Append(number);
}
return stb.ToString();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("StringCalculator.Logic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StringCalculator.Logic")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8738bc32-b798-4010-9990-4a2eb8eeceac")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{626EF91B-F885-4FC3-8D57-F323B2F1FA22}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StringCalculator.Logic</RootNamespace>
<AssemblyName>StringCalculator.Logic</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Calculator.cs" />
<Compile Include="InputParser.cs" />
<Compile Include="NegativesNotAllowedException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
33 changes: 33 additions & 0 deletions eltoby/StringCalculator/StringCalculator.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
using StringCalculator.Logic;

namespace StringCalculator.Test
{
class Program
{
static void Main(string[] args)
{
string[] tests = {"","1","1,2","1,2,3","1,2,3,4","1\n2,3","//;\n1;2","-1,2,-5", "1001,2","//[***]\n1***2***3","//[*][%]\n1*2%3", "//[^^][*]\n1^^2*3"};

foreach (string test in tests)
{
Console.WriteLine(String.Format("Test: {0}", test));
try
{
Console.WriteLine(string.Format("Result: {0}", new Calculator().Sum(test)));
}
catch (NegativesNotAllowedException ex)
{
Console.WriteLine("Controlled Exception:");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.Negatives);
}
Console.WriteLine("-------------");
}
Console.WriteLine("Test Ended. Press Enter to continue.");
Console.Read();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("StringCalculator.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StringCalculator.Test")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6de8f9c2-3a96-41c9-804b-dd62108f3007")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{40780F28-448C-4A2C-B0E5-85EF653FE5D4}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StringCalculator.Test</RootNamespace>
<AssemblyName>StringCalculator.Test</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StringCalculator.Logic\StringCalculator.Logic.csproj">
<Project>{626EF91B-F885-4FC3-8D57-F323B2F1FA22}</Project>
<Name>StringCalculator.Logic</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
26 changes: 26 additions & 0 deletions eltoby/StringCalculator/StringCalculator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringCalculator.Logic", "StringCalculator.Logic\StringCalculator.Logic.csproj", "{626EF91B-F885-4FC3-8D57-F323B2F1FA22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringCalculator.Test", "StringCalculator.Test\StringCalculator.Test.csproj", "{40780F28-448C-4A2C-B0E5-85EF653FE5D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{626EF91B-F885-4FC3-8D57-F323B2F1FA22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{626EF91B-F885-4FC3-8D57-F323B2F1FA22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{626EF91B-F885-4FC3-8D57-F323B2F1FA22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{626EF91B-F885-4FC3-8D57-F323B2F1FA22}.Release|Any CPU.Build.0 = Release|Any CPU
{40780F28-448C-4A2C-B0E5-85EF653FE5D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40780F28-448C-4A2C-B0E5-85EF653FE5D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40780F28-448C-4A2C-B0E5-85EF653FE5D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40780F28-448C-4A2C-B0E5-85EF653FE5D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal