예제 #1
0
파일: Program.cs 프로젝트: schangxiang/Code
        static void CompositeInvoke()
        {
            // 生成树根,并为其增加两个叶子节点
            Composite.Composite root = new Composite.Composite("Root");
            root.Add(new Leaf("Leaf A in Root"));
            root.Add(new Leaf("Leaf B in Root"));

            // 为根增加两个枝节点
            Composite.Composite branchX = new Composite.Composite("Branch X in Root");
            Composite.Composite branchY = new Composite.Composite("Branch Y in Root");
            root.Add(branchX);
            root.Add(branchY);

            // 为BranchX增加页节点
            branchX.Add(new Leaf("Leaf A in Branch X"));

            // 为BranchX增加枝节点
            Composite.Composite branchZ = new Composite.Composite("Branch Z in Branch X");
            branchX.Add(branchZ);

            // 为BranchY增加叶节点
            branchY.Add(new Leaf("Leaf in Branch Y"));

            // 为BranchZ增加叶节点
            branchZ.Add(new Leaf("Leaf in Branch Z"));

            // 显示树
            root.Display(1);
        }
예제 #2
0
        public void CompositeTest()
        {
            // This way the client code can support the simple leaf components...
            var leaf = new Leaf();

            Client.ClientCode(leaf).Should().Be(StructuralText.Leaf);

            // ...as well as the complex composites.
            var tree    = new Composite.Composite();
            var branch1 = new Composite.Composite();

            branch1.Add(new Leaf());
            branch1.Add(new Leaf());
            var branch2 = new Composite.Composite();

            branch2.Add(new Leaf());
            tree.Add(branch1);
            tree.Add(branch2);
            Client.ClientCode(tree).Should().Be(StructuralText.CompositeResultA);
            Client.ClientCode2(tree, leaf).Should().Be(StructuralText.CompositeResultB);
        }
예제 #3
0
        private static void Main()
        {
            // ある意味これだけでFacadeとして体を成しているので
            // わざわざダミーのモジュールなどを作るまでもない。
            var root = new Composite.Composite("");

            root.Add(new Leaf("boot"));
            root.Add(new Composite.Composite("bin")).Add(new Leaf("ls"));
            var usr = root.Add(new Composite.Composite("usr"));

            usr.Add(new Composite.Composite("local"));
            usr.Add(new Composite.Composite("bin")).Add(new Leaf("perl"));
            usr.Add(new Composite.Composite("share")).Add(new Leaf("redmine"));
            usr.Add(new Composite.Composite("src"));
            root.Add(new Leaf("sbin"));
            root.Add(new Leaf("swap"));
            foreach (var p in root.GetDump())
            {
                Console.WriteLine(p);
            }
            Console.ReadKey();
        }
예제 #4
0
        private IEnumerable <(DateTime Date, double Value)> GetItems(CashAccount account, IEntitiesSerializer serializer, DateTime until)
        {
            var user = $"U{account.User.AsUser()}";
            var curr = $"@{account.Currency}";

            if (account.Reimburse != null)
            {
                var rb  = new Composite.Composite(Accountant);
                var tmp = Composite.Composite.GetTemplate(account.Reimburse);
                var rng = Composite.Composite.DateRange(tmp.Day);
                rb.DoInquiry(rng, tmp, out var rbVal, BaseCurrency.Now, serializer);
                // ReSharper disable once PossibleInvalidOperationException
                var rbF = rng.EndDate.Value;
                yield return(rbF, rbVal);
            }

            foreach (var debt in account.Items)
            {
                switch (debt)
                {
                case OnceItem oi:
                    yield return(oi.Date, oi.Fund);

                    break;

                case OnceQueryItem oqi:
                    yield return(oqi.Date, Accountant.RunGroupedQuery($"{user} {curr}*({oqi.Query})``v").Fund);

                    break;

                case MonthlyItem mn:
                    for (var d = NextDate(mn.Day); d <= until; d = NextDate(mn.Day, d))
                    {
                        if (mn.Since != default(DateTime) && d < mn.Since)
                        {
                            continue;
                        }
                        if (mn.Till != default(DateTime) && d > mn.Till)
                        {
                            continue;
                        }

                        yield return(d, mn.Fund);
                    }

                    break;

                case SimpleCreditCard cc:
                    var rng = $"[{ClientDateTime.Today.AddMonths(-3).AsDate()}~]";
                    var mv  = $"{{({user}*{cc.Query})+{user} T3999+{user} T6603 A {rng}}}";
                    var mos = new Dictionary <DateTime, double>();
                    foreach (var grpC in Accountant.RunGroupedQuery($"{{{user}*({cc.Query})*({user} <+(-{user} {curr})) {rng}}}+{mv}:{user}*({cc.Query})`Cd")
                             .Items
                             .Cast <ISubtotalCurrency>())
                    {
                        foreach (var b in grpC.Items.Cast <ISubtotalDate>())
                        {
                            var mo  = NextDate(cc.RepaymentDay, NextDate(cc.BillDay, b.Date.Value), true);
                            var cob = Accountant.From(mo, grpC.Currency)
                                      * Accountant.To(mo, account.Currency)
                                      * b.Fund;
                            if (mos.ContainsKey(mo))
                            {
                                mos[mo] += cob;
                            }
                            else
                            {
                                mos[mo] = cob;
                            }
                        }
                    }

                    foreach (var b in Accountant.RunGroupedQuery($"{{{user}*({cc.Query})*({user} {curr}>) {rng}}}-{mv}:{user}*({cc.Query})`d")
                             .Items
                             .Cast <ISubtotalDate>())
                    {
                        var mo = NextDate(cc.RepaymentDay, b.Date.Value, true);
                        if (mos.ContainsKey(mo))
                        {
                            mos[mo] += b.Fund;
                        }
                        else
                        {
                            mos[mo] = b.Fund;
                        }
                    }

                    for (var d = ClientDateTime.Today; d <= until; d = NextDate(cc.BillDay, d))
                    {
                        var mo  = NextDate(cc.RepaymentDay, NextDate(cc.BillDay, d), true);
                        var cob = -(NextDate(cc.BillDay, d) - d).TotalDays * cc.MonthlyFee / (365.2425 / 12);
                        if (mos.ContainsKey(mo))
                        {
                            mos[mo] += cob;
                        }
                        else
                        {
                            mos[mo] = cob;
                        }
                    }

                    foreach (var kvp in mos)
                    {
                        yield return(kvp.Key, kvp.Value);
                    }

                    break;

                case ComplexCreditCard cc:
                    var stmt = -Accountant.RunGroupedQuery($"({cc.Query})*({user} {curr})-{user} \"\"``v").Fund;
                    var pmt  = Accountant.RunGroupedQuery($"({cc.Query})*({user} {curr} \"\" >)``v").Fund;
                    var nxt  = -Accountant.RunGroupedQuery($"({cc.Query})*({user} {curr} \"\" <)``v").Fund;
                    if (pmt < stmt)
                    {
                        if (NextDate(cc.BillDay, NextDate(cc.RepaymentDay)) == NextDate(cc.BillDay))
                        {
                            yield return(NextDate(cc.RepaymentDay), pmt - stmt);
                        }
                        else
                        {
                            nxt += stmt - pmt;     // Not paid in full
                        }
                    }
                    else
                    {
                        nxt -= pmt - stmt;     // Paid too much
                    }
                    for (var d = ClientDateTime.Today; d <= until; d = NextDate(cc.BillDay, d))
                    {
                        nxt += (NextDate(cc.BillDay, d) - d).TotalDays * cc.MonthlyFee / (365.2425 / 12);
                        if (cc.MaximumUtility >= 0 && cc.MaximumUtility < nxt)
                        {
                            yield return(NextDate(cc.BillDay, d), cc.MaximumUtility - nxt);

                            nxt = cc.MaximumUtility;
                        }

                        yield return(NextDate(cc.RepaymentDay, d).AddMonths(1), -nxt);

                        nxt = 0;
                    }

                    break;

                default:
                    throw new InvalidOperationException();
                }
            }
        }
예제 #5
0
        private IEnumerable <(DateTime Date, double Value)> GetItems(CashAccount account, IEntitiesSerializer serializer)
        {
            var curr = $"@{account.Currency}";
            var init = Accountant.RunGroupedQuery($"{curr}*({account.QuickAsset}) [~.]``v").Fund;

            yield return(ClientDateTime.Today, init);

            if (account.Reimburse != null)
            {
                var rb  = new Composite.Composite(Accountant);
                var tmp = Composite.Composite.GetTemplate(account.Reimburse);
                var rng = Composite.Composite.DateRange(tmp.Day);
                rb.DoInquiry(rng, tmp, out var rbVal, BaseCurrency.Now, serializer);
                // ReSharper disable once PossibleInvalidOperationException
                var rbF = rng.EndDate.Value;
                yield return(rbF, rbVal);
            }

            foreach (var debt in account.Items)
            {
                switch (debt)
                {
                case FixedItem fi:
                    yield return(fi.Day, fi.Fund);

                    break;

                case SimpleItem sd:
                    yield return(sd.Day, Accountant.RunGroupedQuery($"{curr}*({sd.Query})``v").Fund);

                    break;

                case CreditCard cd:
                    foreach (var grpC in Accountant.RunGroupedQuery(
                                 $"{{{{({cd.Query})*(<+(-{curr}))}}+{{({cd.Query})+T3999+T6603 A}}}}*{{[{ClientDateTime.Today.AddMonths(-3).AsDate()}~]}}:{cd.Query}`Cd")
                             .Items
                             .Cast <ISubtotalCurrency>())
                    {
                        foreach (var b in grpC.Items.Cast <ISubtotalDate>())
                        {
                            // ReSharper disable once PossibleInvalidOperationException
                            var d  = b.Date.Value;
                            var mo = new DateTime(d.Year, d.Month, 1, 0, 0, 0, DateTimeKind.Utc);
                            if (d.Day >= cd.BillDay)
                            {
                                mo = mo.AddMonths(1);
                            }
                            if (cd.RepaymentDay <= cd.BillDay)
                            {
                                mo = mo.AddMonths(1);
                            }
                            mo = mo.AddDays(cd.RepaymentDay - 1);
                            if (mo <= ClientDateTime.Today)
                            {
                                continue;
                            }

                            var cob = ExchangeFactory.Instance.From(mo, grpC.Currency)
                                      * ExchangeFactory.Instance.To(mo, account.Currency)
                                      * b.Fund;
                            yield return(mo, cob);
                        }
                    }

                    foreach (var b in Accountant.RunGroupedQuery(
                                 $"{{({cd.Query})*({curr}>) [{ClientDateTime.Today.AddMonths(-3).AsDate()}~]}}-{{({cd.Query})+T3999+T6603 A}}:{cd.Query}`d")
                             .Items
                             .Cast <ISubtotalDate>())
                    {
                        // ReSharper disable once PossibleInvalidOperationException
                        var d  = b.Date.Value;
                        var mo = new DateTime(d.Year, d.Month, 1, 0, 0, 0, DateTimeKind.Utc);
                        if (d.Day > cd.RepaymentDay)
                        {
                            mo = mo.AddMonths(1);
                        }
                        mo = mo.AddDays(cd.RepaymentDay - 1);
                        if (mo <= ClientDateTime.Today)
                        {
                            continue;
                        }

                        yield return(mo, b.Fund);
                    }

                    break;

                default:
                    throw new InvalidOperationException();
                }
            }
        }
예제 #6
0
 public void ClientCode2(Composite.Composite tree, Soldier leaf)
 {
     throw new NotImplementedException();
 }