예제 #1
0
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("Person example");

            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, MonthlyInterest = 0.01
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, MonthlyInterest = 0.02
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 77000, MonthlyRent = 500
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 400
            });

            var netWorthVisitor = new NetWorthVisitor();
            var incomeVisitor   = new IncomeVisitor();

            person.InspectAssets(netWorthVisitor);
            Console.WriteLine($"Person's net worth is {netWorthVisitor.Total:C}");

            person.InspectAssets(incomeVisitor);
            Console.WriteLine($"Person's monthly income is {incomeVisitor.Amount:C}");
        }
예제 #2
0
        static void Main(string[] args)
        {
            // Object Structure
            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, MonthlyInterestRate = 0.01
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, MonthlyInterestRate = 0.02
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 500
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 40
            });

            // Visitor
            var netWorthVisitor = new NetWorthVisitor();

            person.Accept(netWorthVisitor);

            var monthlyincome = new MonthlyIncome();

            person.Accept(monthlyincome);

            Console.WriteLine("Net Worth: {0}", netWorthVisitor.Total);
            Console.WriteLine("Monthly Income: {0}", monthlyincome.Amount);
            Console.ReadKey();
        }
        private static void CalculatePersonsNetWorthWithTheVisitorPattern()
        {
            var person = new visitorPatternEntities.Person();

            person.Assets.Add(new visitorPatternEntities.BankAccount {
                Amount = 1000, MonthlyInterest = 0.01m
            });
            person.Assets.Add(new visitorPatternEntities.BankAccount {
                Amount = 2000, MonthlyInterest = 0.02m
            });
            person.Assets.Add(new visitorPatternEntities.RealEstate {
                EstimatedValue = 79000, MonthlyRent = 500m
            });
            person.Assets.Add(new visitorPatternEntities.Loan {
                Owed = 40000, MonthlyPayment = 40
            });


            //the logic is encapsulated into one place and the operations of now and in the future can be stored in one place.
            //they are not scattered amongst the classes of now [BankAccount, Loan, RealEstate].
            //In the future if new classes came on board and they need to contribute to the networth calculation. They would simple need to implement the IAsset interface.
            var netWorthVisitor = new NetWorthVisitor();
            var incomeVisitor   = new IncomeVisitor();

            person.Accept(netWorthVisitor);
            person.Accept(incomeVisitor);

            Console.WriteLine($"netWorthVisitor.Total {netWorthVisitor.Total}");
            Console.WriteLine($"incomeVisitor.Amount {incomeVisitor.Amount}");
        }
예제 #4
0
        public void VisitorPatter_TestUseOfVisitor()
        {
            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, MonthlyInterest = 0.01
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, MonthlyInterest = 0.02
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 500
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 40
            });

            var netWorthVisitor = new NetWorthVisitor();
            var incomeVisitor   = new IncomeVisitor();

            person.Accept(netWorthVisitor);
            person.Accept(incomeVisitor);

            Console.WriteLine(netWorthVisitor.Total);
            Console.WriteLine(incomeVisitor.Amount);
        }
        public void ShowSample()
        {
            var person = new Person();

            person.Assets.Add(new BankAccount(100, 1.2));
            person.Assets.Add(new BankAccount(30, 1.1));
            person.Assets.Add(new BankAccount(80, 1.4));
            person.Assets.Add(new Loan(80, 100));
            person.Assets.Add(new RealEstate(8, 10));

            var netWorthVisitor = new NetWorthVisitor();

            person.Accept(netWorthVisitor);

            var incomeVisitor = new IncomeVisitor();

            person.Accept(incomeVisitor);

            Console.WriteLine($"NetWorthVisitor got {netWorthVisitor.NetWorth}");
            Console.WriteLine($"IncomeVisitor got {incomeVisitor.Income}");
        }
        public void Calculate_Net_Worth()
        {
            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, InterestRate = 0.05m
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, InterestRate = 0.1m
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 2000
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 100
            });

            var netWorthVisitor = new NetWorthVisitor();

            person.Accept(netWorthVisitor);

            Assert.Equal(42000, netWorthVisitor.Total);
        }