static void Main(string[] args) { 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 }); //calculate networth //int netWorth = 0; //foreach (var bankAccount in person.BankAccounts) //{ // netWorth += bankAccount.Amount; //} //foreach (var realEstate in person.RealEstates) //{ // netWorth += realEstate.EstimatedValue; //} //foreach (var loan in person.Loans) //{ // netWorth -= loan.Owed; //} var netWorthVisitor = new NetWorthVisitor(); person.Accept(netWorthVisitor); var monthlyIncomeVisitor = new IncomeVisitor(); person.Accept(monthlyIncomeVisitor); Console.WriteLine(netWorthVisitor.Total); Console.WriteLine(monthlyIncomeVisitor.Amount); Console.ReadLine(); }
static void Main(string[] args) { Person person = new Person(); person.Assets.Add(new BankAccount { Amount = 1000, MonthlyInterest = 0.01M }); person.Assets.Add(new BankAccount { Amount = 2000, MonthlyInterest = 0.02M }); person.Assets.Add(new Loan { MonthlyPayment = 40, Owed = 40000 }); person.Assets.Add(new RealEstate { EstimatedValue = 79000, MonthlyRent = 500 }); //Un refactored Code //decimal netWorth = 0; //foreach (var acct in person.bankAccounts) //{ // netWorth += acct.Amount; //} //foreach (var loan in person.loans) //{ // netWorth -= loan.Owed; //} //foreach (var RE in person.realEstate) //{ // netWorth += RE.EstimatedValue; //} //Console.WriteLine(netWorth); NetWorth netWorth = new NetWorth();//visitor or fuctionality need to be added or any new functionality person.Accept(netWorth); Console.WriteLine(netWorth.Total); }
static void Main(string[] args) { 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(); person.Accept(netWorthVisitor); Console.WriteLine(netWorthVisitor.Total); Console.ReadKey(); }
static void Main(string[] args) { 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(); person.Accept(netWorthVisitor); Console.WriteLine(netWorthVisitor.Total); Console.ReadKey(); }