Skip to content

Commit 70e6d23

Browse files
authored
Merge pull request #311 from SamSmithNZ-dotcom/AddingJellyBeans
Added JellyBeans
2 parents 98dccfe + 69f891e commit 70e6d23

File tree

17 files changed

+514
-132
lines changed

17 files changed

+514
-132
lines changed

src/MandMCounter.Core/Calculator.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public static float CountSkittles(string unit, float quantity)
3030
return numberOfMandMs;
3131
}
3232

33+
public static float CountJellyBeans(string unit, float quantity)
34+
{
35+
float numberOfJellyBeans = GetCubicCmForVolume(unit, quantity) * Constants.JellyBeansDensityPercent / Constants.JellyBeansVolumeCubicCm;
36+
return numberOfJellyBeans;
37+
}
38+
3339
/// <summary>
3440
/// To count the number of M&Ms in a container based on rectangular container
3541
/// </summary>
@@ -56,6 +62,12 @@ public static float CountSkittles(string unit, float height, float width, float
5662
return numberOfMandMs;
5763
}
5864

65+
public static float CountJellyBeans(string unit, float height, float width, float length)
66+
{
67+
float numberOfJellyBeans = GetCubicCmForRectangle(unit, height, width, length) * Constants.JellyBeansDensityPercent / Constants.JellyBeansVolumeCubicCm;
68+
return numberOfJellyBeans;
69+
}
70+
5971
/// <summary>
6072
/// To count the number of M&Ms in a container based on rectangular container
6173
/// </summary>
@@ -81,6 +93,12 @@ public static float CountSkittles(string unit, float height, float radius)
8193
return numberOfMandMs;
8294
}
8395

96+
public static float CountJellyBeans(string unit, float height, float radius)
97+
{
98+
float numberOfJellyBeans = GetCubicCmForCylinder(unit, height, radius) * Constants.JellyBeansDensityPercent / Constants.JellyBeansVolumeCubicCm;
99+
return numberOfJellyBeans;
100+
}
101+
84102
private static float GetCubicCmForVolume(string unit, float quantity)
85103
{
86104
if (string.IsNullOrEmpty(unit))

src/MandMCounter.Core/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class Constants
1111
//Reference: this one thinks it's 0.625f http://www.answers.com/Q/What_is_a_volume_of_one_skittle
1212
//Reference 2: this one thinks it's 0.7418629f https://community.babycenter.com/post/a37375396/guess_how_many_skittles?cpg=2
1313
public const float SkittlesVolumeCubicCm = 0.7418629f; //Going with the bigger number, as skittles are bigger than M&Ms
14+
//Reference: Volume of a jellybean is 3.5325, so the volume in one cubic CM is 1/3.5325 = 0.2831
15+
public const float JellyBeansVolumeCubicCm = 0.2831f;
1416

1517
//Reference: https://yenra.com/particle-packing/
1618
//Some people think this is 68.5%. https://cims.nyu.edu/~donev/Thesis.pdf
@@ -19,6 +21,8 @@ public class Constants
1921
public const float PeanutMandMSDensityPercent = 0.64f;
2022
//Reference: https://community.babycenter.com/post/a37375396/guess_how_many_skittles?cpg=2
2123
public const float SkittlesDensityPercent = 0.665f; //This feels right too, as skittles are more round than an M&M
24+
//Reference: https://www.pearson.com/channels/physics/asset/9b2fde72/ii-estimate-the-number-of-jelly-beans-in-the-jar-of-fig-113-and-ltimage-and-gt
25+
public const float JellyBeansDensityPercent = 0.64f;
2226

2327
//Conversions done using Google conversion calculater
2428
public const float USGallonToCubicCM = 3785.4119997685f;

src/MandMCounter.MCP/Tools.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public class SkittleCounterTool
4545
public static float GetDataForCylinder(string unit, float height, float radius) => Calculator.CountSkittles(unit, height, radius);
4646
}
4747

48+
// MCP tool for Jelly Bean counting
49+
[McpServerToolType]
50+
public class JellyBeanCounterTool
51+
{
52+
[McpServerTool]
53+
public static float GetDataForUnit(string unit, float quantity) => Calculator.CountJellyBeans(unit, quantity);
54+
55+
[McpServerTool]
56+
public static float GetDataForRectangle(string unit, float height, float width, float length) => Calculator.CountJellyBeans(unit, height, width, length);
57+
58+
[McpServerTool]
59+
public static float GetDataForCylinder(string unit, float height, float radius) => Calculator.CountJellyBeans(unit, height, radius);
60+
}
61+
4862
// MCP tool for units
4963
[McpServerToolType]
5064
public class UnitsTool
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using MandMCounter.Core;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace MandMCounter.Service.Controllers
5+
{
6+
[Produces("application/json")]
7+
[Route("api/JellyBeanCounter")]
8+
public class JellyBeanCounterController : Controller
9+
{
10+
[HttpGet("GetDataForUnit")]
11+
public float GetDataForUnit(string unit, float quantity)
12+
{
13+
return Calculator.CountJellyBeans(unit, quantity);
14+
}
15+
16+
[HttpGet("GetDataForRectangle")]
17+
public float GetDataForRectangle(string unit, float height, float width, float length)
18+
{
19+
return Calculator.CountJellyBeans(unit, height, width, length);
20+
}
21+
22+
[HttpGet("GetDataForCylinder")]
23+
public float GetDataForCylinder(string unit, float height, float radius)
24+
{
25+
return Calculator.CountJellyBeans(unit, height, radius);
26+
}
27+
}
28+
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using Microsoft.AspNetCore;
1+
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Hosting;
34

45
namespace MandMCounter.Service
56
{
@@ -8,13 +9,14 @@ public class Program
89
{
910
public static void Main(string[] args)
1011
{
11-
BuildWebHost(args).Run();
12+
CreateHostBuilder(args).Build().Run();
1213
}
1314

14-
public static IWebHost BuildWebHost(string[] args) =>
15-
WebHost.CreateDefaultBuilder(args)
16-
.UseApplicationInsights()
17-
.UseStartup<Startup>()
18-
.Build();
15+
public static IHostBuilder CreateHostBuilder(string[] args) =>
16+
Host.CreateDefaultBuilder(args)
17+
.ConfigureWebHostDefaults(webBuilder =>
18+
{
19+
webBuilder.UseStartup<Startup>();
20+
});
1921
}
2022
}

src/MandMCounter.Service/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public Startup(IConfiguration configuration)
2323
// This method gets called by the runtime. Use this method to add services to the container.
2424
public void ConfigureServices(IServiceCollection services)
2525
{
26+
services.AddApplicationInsightsTelemetry();
27+
2628
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
2729
.AllowAnyMethod()
2830
.AllowAnyHeader()));
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using MandMCounter.Service.Controllers;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace MandMCounter.Tests.Controllers
5+
{
6+
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
7+
[TestClass]
8+
public class JellyBeanControllerTests
9+
{
10+
private JellyBeanCounterController _controller;
11+
12+
[TestInitialize]
13+
public void Setup()
14+
{
15+
_controller = new JellyBeanCounterController();
16+
}
17+
18+
[TestMethod]
19+
public void ControllerCountJellyBeansInAUSGallonTest()
20+
{
21+
//Arrange
22+
string unit = "Gallon";
23+
float quantity = 1f;
24+
const float expectedJellyBeans = 8558f;
25+
26+
//Act
27+
float result = _controller.GetDataForUnit(unit, quantity);
28+
29+
//Assert
30+
Assert.IsLessThan(expectedJellyBeans, result);
31+
}
32+
33+
[TestMethod]
34+
public void ControllerCountJellyBeansInAUSQuartTest()
35+
{
36+
//Arrange
37+
string unit = "Quart";
38+
float quantity = 1f;
39+
const float expectedJellyBeans = 2140f;
40+
41+
//Act
42+
float result = _controller.GetDataForUnit(unit, quantity);
43+
44+
//Assert
45+
Assert.IsLessThan(expectedJellyBeans, result);
46+
}
47+
48+
[TestMethod]
49+
public void ControllerCountJellyBeansInAUSCupTest()
50+
{
51+
//Arrange
52+
string unit = "Cup";
53+
float quantity = 1f;
54+
const float expectedJellyBeans = 535f;
55+
56+
//Act
57+
float result = _controller.GetDataForUnit(unit, quantity);
58+
59+
//Assert
60+
Assert.IsLessThan(expectedJellyBeans, result);
61+
}
62+
63+
[TestMethod]
64+
public void ControllerCountJellyBeansInAUSTableSpoonTest()
65+
{
66+
//Arrange
67+
string unit = "Tablespoon";
68+
float quantity = 1f;
69+
const float expectedJellyBeans = 34f;
70+
71+
//Act
72+
float result = _controller.GetDataForUnit(unit, quantity);
73+
74+
//Assert
75+
Assert.IsLessThan(expectedJellyBeans, result);
76+
}
77+
78+
[TestMethod]
79+
public void ControllerCountJellyBeansInALiterTest()
80+
{
81+
//Arrange
82+
string unit = "Liter";
83+
float quantity = 1f;
84+
const float expectedJellyBeans = 2261f;
85+
86+
//Act
87+
float result = _controller.GetDataForUnit(unit, quantity);
88+
89+
//Assert
90+
Assert.IsLessThan(expectedJellyBeans, result);
91+
}
92+
93+
[TestMethod]
94+
public void ControllerCountJellyBeansInA1CubicCMTest()
95+
{
96+
//Arrange
97+
string unit = "cm";
98+
float height = 10;
99+
float width = 10;
100+
float length = 10;
101+
const float expectedJellyBeans = 2261f;
102+
103+
//Act
104+
float result = _controller.GetDataForRectangle(unit, height, width, length);
105+
106+
//Assert
107+
Assert.IsLessThan(expectedJellyBeans, result);
108+
}
109+
110+
[TestMethod]
111+
public void ControllerCountJellyBeansInA1CubicInchTest()
112+
{
113+
//Arrange
114+
string unit = "inch";
115+
float height = 1;
116+
float width = 1;
117+
float length = 1;
118+
const float expectedJellyBeans = 38f;
119+
120+
//Act
121+
float result = _controller.GetDataForRectangle(unit, height, width, length);
122+
123+
//Assert
124+
Assert.IsLessThan(expectedJellyBeans, result);
125+
}
126+
127+
[TestMethod]
128+
public void ControllerCountJellyBeansInACylinderWithCMTest()
129+
{
130+
//Arrange
131+
string unit = "cm";
132+
float height = 10;
133+
float radius = 5;
134+
const float expectedJellyBeans = 1776f;
135+
136+
//Act
137+
float result = _controller.GetDataForCylinder(unit, height, radius);
138+
139+
//Assert
140+
Assert.IsLessThan(expectedJellyBeans, result);
141+
}
142+
143+
[TestMethod]
144+
public void ControllerCountJellyBeansInACylinderWithInchTest()
145+
{
146+
//Arrange
147+
string unit = "inch";
148+
float height = 4;
149+
float radius = 2;
150+
const float expectedJellyBeans = 1863f;
151+
152+
//Act
153+
float result = _controller.GetDataForCylinder(unit, height, radius);
154+
155+
//Assert
156+
Assert.IsLessThan(expectedJellyBeans, result);
157+
}
158+
159+
}
160+
}
Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
21
using MandMCounter.Service.Controllers;
3-
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
43

54
namespace MandMCounter.Tests.Controllers
65
{
76
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
87
[TestClass]
98
public class MandMControllerTests
109
{
11-
#region "Testing units"
10+
private MandMCounterController _controller;
11+
12+
[TestInitialize]
13+
public void Setup()
14+
{
15+
_controller = new();
16+
}
1217

1318
[TestMethod]
1419
public void ControllerCountMandMsInAUSGallonTest()
@@ -18,17 +23,12 @@ public void ControllerCountMandMsInAUSGallonTest()
1823
float quantity = 1f;
1924

2025
//Act
21-
MandMCounterController controller = new MandMCounterController();
22-
float result = controller.GetDataForUnit(unit, quantity);
26+
float result = _controller.GetDataForUnit(unit, quantity);
2327

2428
//Assert
25-
Assert.IsTrue(Math.Abs(System.Math.Round(result, 0) - 253f) < 0.5);
29+
Assert.IsLessThan(253f, result);
2630
}
2731

28-
#endregion
29-
30-
#region " Testing volume in a rectangle"
31-
3232
[TestMethod]
3333
public void ControllerCountMandMsInA1CubicCMTest()
3434
{
@@ -37,36 +37,30 @@ public void ControllerCountMandMsInA1CubicCMTest()
3737
float height = 10;
3838
float width = 10;
3939
float length = 10;
40+
const float expectedMandMs = 1070f;
4041

4142
//Act
42-
MandMCounterController controller = new MandMCounterController();
43-
float result = controller.GetDataForRectangle(unit, height, width, length);
43+
float result = _controller.GetDataForRectangle(unit, height, width, length);
4444

4545
//Assert
46-
Assert.IsTrue(Math.Abs(System.Math.Round(result, 0) - 1069f) < 0.5);
46+
Assert.IsLessThan(expectedMandMs, result);
4747
}
4848

49-
#endregion
50-
51-
#region " Testing volume in a cylinder"
52-
5349
[TestMethod]
5450
public void ControllerCountMandMsInACylinderWithCMTest()
5551
{
5652
//Arrange
5753
string unit = "cm";
5854
float height = 10;
5955
float radius = 5;
56+
const float expectedMandMs = 840f;
6057

6158
//Act
62-
MandMCounterController controller = new MandMCounterController();
63-
float result = controller.GetDataForCylinder(unit, height, radius);
59+
float result = _controller.GetDataForCylinder(unit, height, radius);
6460

6561
//Assert
66-
Assert.IsTrue(Math.Abs(System.Math.Round(result, 0) - 840f) < 0.5);
62+
Assert.IsLessThan(expectedMandMs, result);
6763
}
6864

69-
#endregion
70-
7165
}
7266
}

0 commit comments

Comments
 (0)