From a2553496729195462b202b6f7c3bbf6d6969cbf2 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Thu, 26 Nov 2015 19:11:25 +0100 Subject: [PATCH] Initial Prescription model with factory. --- Tests/Medication/PrescriptionTest.cs | 48 ++++++++++++ Tests/Tests.csproj | 1 + zaaReloaded2/Medication/Prescription.cs | 99 +++++++++++++++++++++++++ zaaReloaded2/zaaReloaded2.csproj | 1 + 4 files changed, 149 insertions(+) create mode 100755 Tests/Medication/PrescriptionTest.cs create mode 100755 zaaReloaded2/Medication/Prescription.cs diff --git a/Tests/Medication/PrescriptionTest.cs b/Tests/Medication/PrescriptionTest.cs new file mode 100755 index 0000000..139870c --- /dev/null +++ b/Tests/Medication/PrescriptionTest.cs @@ -0,0 +1,48 @@ +/* PrescriptionTest.cs + * part of zaaReloaded2 + * + * Copyright 2015 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using zaaReloaded2.Medication; + +namespace Tests.Medication +{ + [TestFixture] + class PrescriptionTest + { + [Test] + [TestCase("Ramipril 5 mg 1-2-3", "Ramipril 5 mg", "1", "2", "3")] + [TestCase("Ramipril 5 mg 1 -2 - 3", "Ramipril 5 mg", "1", "2", "3")] + [TestCase("Ramipril 5 mg 1 - 2 - 3", "Ramipril 5 mg", "1", "2", "3")] + [TestCase("Ramipril 5 mg 1 - 2 - 3", "Ramipril 5 mg", "1", "2", "3")] + [TestCase("Ramipril 5 mg 1 1/2-2-3", "Ramipril 5 mg", "1 1/2", "2", "3")] + [TestCase("Ramipril 5 mg 1 1/2 - 2 - 3", "Ramipril 5 mg", "1 1/2", "2", "3")] + [TestCase("Ramipril 5 mg ½-⅓-¼", "Ramipril 5 mg", "½", "⅓", "¼")] + public void ParseLine(string line, string drug, string morning, + string noon, string evening) + { + Prescription p = Prescription.FromLine(line); + Assert.AreEqual(drug, p.Drug, "Drug should be " + drug); + Assert.AreEqual(morning, p.Morning, "Morning should be " + morning); + Assert.AreEqual(noon, p.Noon, "Noon should be " + noon); + Assert.AreEqual(evening, p.Evening, "Evening should be " + evening); + } + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index c65c868..d47ddf6 100755 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -83,6 +83,7 @@ + diff --git a/zaaReloaded2/Medication/Prescription.cs b/zaaReloaded2/Medication/Prescription.cs new file mode 100755 index 0000000..1615044 --- /dev/null +++ b/zaaReloaded2/Medication/Prescription.cs @@ -0,0 +1,99 @@ +/* Prescription.cs + * part of zaaReloaded2 + * + * Copyright 2015 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace zaaReloaded2.Medication +{ + /// + /// Represents a prescription + /// + public class Prescription + { + #region Factory + + /// + /// Creates a new Prescription object by parsing a line (e.g., + /// from a physician's letter). + /// + /// Line to parse + /// Prescription created from the + public static Prescription FromLine(string line) + { + // Replace any runs of whitespace with a single space + // (from http://stackoverflow.com/a/206946/270712) + line = Regex.Replace(line, @"\s+", " "); + Match m = lineRegex.Match(line); + int n = m.Groups[DOSING_GROUP].Captures.Count; + + return new Prescription( + m.Groups["drug"].Value, + n > 0 ? m.Groups[DOSING_GROUP].Captures[0].Value : String.Empty, + n > 1 ? m.Groups[DOSING_GROUP].Captures[1].Value : String.Empty, + n > 2 ? m.Groups[DOSING_GROUP].Captures[2].Value : String.Empty, + n > 3 ? m.Groups[DOSING_GROUP].Captures[3].Value : String.Empty + ); + } + + #endregion + + #region Properties + + public string Drug { get; set; } + + public string Morning { get; set; } + + public string Noon { get; set; } + + public string Evening { get; set; } + + public string Night { get; set; } + + #endregion + + #region Constructor + + public Prescription() { } + + public Prescription(string drug, string morning, string noon, + string evening, string night) + { + Drug = drug; + Morning = morning; + Noon = noon; + Evening = evening; + Night = night; + } + + #endregion + + #region Fields + + private const string DOSING_GROUP = "dosing"; + private const string DOSING = @"(\d{1,3}((\s1)?/[234])?|[\u00bd\u2153\u00bc])"; + private const string SPACER = @"\s*[-\u2012\u2013\u2014]+\s*"; + private static readonly Regex lineRegex = new Regex( + @"(?.+?)\s(?" + DOSING + + @")(\s?[-\u2012\u2013\u2014]+\s?(?" + DOSING + @")){1,3}"); + + #endregion + } +} diff --git a/zaaReloaded2/zaaReloaded2.csproj b/zaaReloaded2/zaaReloaded2.csproj index 91a98c3..2e1f7cc 100755 --- a/zaaReloaded2/zaaReloaded2.csproj +++ b/zaaReloaded2/zaaReloaded2.csproj @@ -221,6 +221,7 @@ +