diff --git a/Tests/Medication/PrescriptionTest.cs b/Tests/Medication/PrescriptionTest.cs index 139870c..ae68e3f 100755 --- a/Tests/Medication/PrescriptionTest.cs +++ b/Tests/Medication/PrescriptionTest.cs @@ -44,5 +44,31 @@ namespace Tests.Medication Assert.AreEqual(noon, p.Noon, "Noon should be " + noon); Assert.AreEqual(evening, p.Evening, "Evening should be " + evening); } + + [Test] + public void MultiplePrescriptions() + { + IEnumerable list = Prescription.ManyFromLine( + "Ramipril 5 mg 1-0-0 Prograf 1 mg 1-0-1"); + Assert.AreEqual(2, list.Count()); + Assert.AreEqual("Ramipril 5 mg\t1-0-0", list.First().ToString()); + Assert.AreEqual("Prograf 1 mg\t1-0-1", list.Last().ToString()); + } + + [Test] + [TestCase("Ramipril 5 mg", "1", "0", "0", "0", "Ramipril 5 mg\t1-0-0-0")] + [TestCase("Ramipril 5 mg", "1", "0", "0", "", "Ramipril 5 mg\t1-0-0")] + [TestCase("Ramipril 5 mg", "1", "0", "", "", "Ramipril 5 mg\t1-0")] + [TestCase("Ramipril 5 mg", "1", "", "", "", "Ramipril 5 mg\t1")] + [TestCase("Ramipril 5 mg", "1", "", "0", "0", "Ramipril 5 mg\t1-0-0-0")] + [TestCase("Ramipril 5 mg", "1", "0", "", "0", "Ramipril 5 mg\t1-0-0-0")] + [TestCase("Ramipril 5 mg", "1", "", "", "0", "Ramipril 5 mg\t1-0-0-0")] + [TestCase("Ramipril 5 mg", "", "", "", "", "Ramipril 5 mg\t")] + public void PrescriptionToString(string drug, string morning, string noon, + string evening, string night, string formatted) + { + Prescription p = new Prescription(drug, morning, noon, evening, night); + Assert.AreEqual(formatted, p.ToString()); + } } } diff --git a/zaaReloaded2/Medication/Prescription.cs b/zaaReloaded2/Medication/Prescription.cs index 1615044..7220230 100755 --- a/zaaReloaded2/Medication/Prescription.cs +++ b/zaaReloaded2/Medication/Prescription.cs @@ -53,6 +53,30 @@ namespace zaaReloaded2.Medication ); } + /// + /// Extracts several prescriptions from a given line. + /// + /// Line that contains several prescriptions. + /// Enumerable with s. + public static IEnumerable ManyFromLine(string line) + { + MatchCollection mc = lineRegex.Matches(line); + List list = new List(); + foreach (Match m in mc) + { + int n = m.Groups[DOSING_GROUP].Captures.Count; + list.Add(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 + ) + ); + } + return list; + } + #endregion #region Properties @@ -69,6 +93,54 @@ namespace zaaReloaded2.Medication #endregion + #region Overrides + + public override string ToString() + { + string s = Drug + "\t"; + if (!String.IsNullOrEmpty(Morning)) + { + s += Morning; + } + else + { + if (!(String.IsNullOrEmpty(Noon) && String.IsNullOrEmpty(Evening) && + String.IsNullOrEmpty(Night))) + { + s += "0"; + } + } + if (!String.IsNullOrEmpty(Noon)) + { + s += "-" + Noon; + } + else + { + if (!(String.IsNullOrEmpty(Evening) && String.IsNullOrEmpty(Night))) + { + s += "-0"; + } + } + if (!String.IsNullOrEmpty(Evening)) + { + s += "-" + Evening; + } + else + { + if (!String.IsNullOrEmpty(Night)) + { + s += "-0"; + } + } + if (!String.IsNullOrEmpty(Night)) + { + s += "-" + Night; + } + return s; + } + + #endregion + #region Constructor public Prescription() { } @@ -76,11 +148,11 @@ namespace zaaReloaded2.Medication public Prescription(string drug, string morning, string noon, string evening, string night) { - Drug = drug; - Morning = morning; - Noon = noon; - Evening = evening; - Night = night; + Drug = drug.Trim(); + Morning = morning.Trim(); + Noon = noon.Trim(); + Evening = evening.Trim(); + Night = night.Trim(); } #endregion @@ -90,9 +162,12 @@ namespace zaaReloaded2.Medication 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*"; + + // Enclose entire regular expression in parentheses so we can use it + // to split a line and capture the delimiter. private static readonly Regex lineRegex = new Regex( - @"(?.+?)\s(?" + DOSING + - @")(\s?[-\u2012\u2013\u2014]+\s?(?" + DOSING + @")){1,3}"); + @"((?.+?)\s(?" + DOSING + + @")(\s?[-\u2012\u2013\u2014]+\s?(?" + DOSING + @")){1,3})"); #endregion }