コード例 #1
0
        static void Main(string[] args)
        {
            // A value used in the calculation of the bonus.
            // Note: This local variable will become a "captured outer variable".
            decimal multiplier = 2;

            // This delegate is defined as a named method.
            CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);

            // This delegate is anonymous - there is no named method.
            // It defines an alternative bonus calculation algorithm.
            CalculateBonus enhanced_bonus = delegate(decimal sales) { return(multiplier * sales / 10); };

            // Declare some Employee objects.
            Employee[] staff = new Employee[5];

            // Populate the array of Employees.
            for (int i = 0; i < 5; i++)
            {
                staff[i] = new Employee();
            }

            // Assign initial values to Employees.
            staff[0].name  = "Mr Apple";
            staff[0].sales = 100;
            staff[0].calculation_algorithm = standard_bonus;

            staff[1].name  = "Ms Banana";
            staff[1].sales = 200;
            staff[1].calculation_algorithm = standard_bonus;

            staff[2].name  = "Mr Cherry";
            staff[2].sales = 300;
            staff[2].calculation_algorithm = standard_bonus;

            staff[3].name  = "Mr Date";
            staff[3].sales = 100;
            staff[3].calculation_algorithm = enhanced_bonus;

            staff[4].name  = "Ms Elderberry";
            staff[4].sales = 250;
            staff[4].calculation_algorithm = enhanced_bonus;

            // Calculate bonus for all Employees
            foreach (Employee person in staff)
            {
                PerformBonusCalculation(person);
            }

            // Display the details of all Employees
            foreach (Employee person in staff)
            {
                DisplayPersonDetails(person);
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // Значение, используемое при расчете бонуса.
            // Примечание. Эта локальная переменная станет "зафиксированной внешней переменной".
            decimal multiplier = 2;

            // Этот делегат определяется как именованный метод.
            CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);

            // Этот делегат анонимный -- для него нет именованного метода.
            // Он определяет альтернативный алгоритм расчета бонуса.
            CalculateBonus enhanced_bonus = delegate(decimal sales) { return(multiplier * sales / 10); };

            // Объявление нескольких объектов Employee.
            Employee[] staff = new Employee[5];

            // Заполнение массива Employees.
            for (int i = 0; i < 5; i++)
            {
                staff[i] = new Employee();
            }

            // Присвоение начальных значений объектам Employees.
            staff[0].name  = "Mr Apple";
            staff[0].sales = 100;
            staff[0].calculation_algorithm = standard_bonus;

            staff[1].name  = "Ms Banana";
            staff[1].sales = 200;
            staff[1].calculation_algorithm = standard_bonus;

            staff[2].name  = "Mr Cherry";
            staff[2].sales = 300;
            staff[2].calculation_algorithm = standard_bonus;

            staff[3].name  = "Mr Date";
            staff[3].sales = 100;
            staff[3].calculation_algorithm = enhanced_bonus;

            staff[4].name  = "Ms Elderberry";
            staff[4].sales = 250;
            staff[4].calculation_algorithm = enhanced_bonus;

            // Расчет бонуса для всех объектов Employees
            foreach (Employee person in staff)
            {
                PerformBonusCalculation(person);
            }

            // Отображение подробных сведений обо всех объектах Employees
            foreach (Employee person in staff)
            {
                DisplayPersonDetails(person);
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            // 奖金计算中用到的值。
            // 注意: 此局部变量将变为“捕获的外部变量”。
            decimal multiplier = 2;

            // 将此委托定义为命名方法。
            CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);

            // 此委托是匿名的,没有命名方法。
            // 它定义了一个备选的奖金计算算法。
            CalculateBonus enhanced_bonus = delegate(decimal sales) { return(multiplier * sales / 10); };

            // 声明一些 Employee 对象。
            Employee[] staff = new Employee[5];

            // 填充 Employees 数组。
            for (int i = 0; i < 5; i++)
            {
                staff[i] = new Employee();
            }

            // 将初始值赋给 Employees。
            staff[0].name  = "Mr Apple";
            staff[0].sales = 100;
            staff[0].calculation_algorithm = standard_bonus;

            staff[1].name  = "Ms Banana";
            staff[1].sales = 200;
            staff[1].calculation_algorithm = standard_bonus;

            staff[2].name  = "Mr Cherry";
            staff[2].sales = 300;
            staff[2].calculation_algorithm = standard_bonus;

            staff[3].name  = "Mr Date";
            staff[3].sales = 100;
            staff[3].calculation_algorithm = enhanced_bonus;

            staff[4].name  = "Ms Elderberry";
            staff[4].sales = 250;
            staff[4].calculation_algorithm = enhanced_bonus;

            // 计算所有 Employee 的奖金
            foreach (Employee person in staff)
            {
                PerformBonusCalculation(person);
            }

            // 显示所有 Employee 的详细信息
            foreach (Employee person in staff)
            {
                DisplayPersonDetails(person);
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            // A value used in the calculation of the bonus.
            // Note: This local variable will become a "captured outer variable".
            decimal multiplier = 2;

            // This delegate is defined as a named method.
            CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);

            // This delegate is anonymous - there is no named method.
            // It defines an alternative bonus calculation algorithm.
            CalculateBonus enhanced_bonus = delegate(decimal sales) { return multiplier * sales / 10; };

            // Declare some Employee objects.
            Employee[] staff = new Employee[5];

            // Populate the array of Employees.
            for (int i = 0; i < 5; i++)
                staff[i] = new Employee();

            // Assign initial values to Employees.
            staff[0].name = "Mr Apple";
            staff[0].sales = 100;
            staff[0].calculation_algorithm = standard_bonus;

            staff[1].name = "Ms Banana";
            staff[1].sales = 200;
            staff[1].calculation_algorithm = standard_bonus;

            staff[2].name = "Mr Cherry";
            staff[2].sales = 300;
            staff[2].calculation_algorithm = standard_bonus;

            staff[3].name = "Mr Date";
            staff[3].sales = 100;
            staff[3].calculation_algorithm = enhanced_bonus;

            staff[4].name = "Ms Elderberry";
            staff[4].sales = 250;
            staff[4].calculation_algorithm = enhanced_bonus;

            // Calculate bonus for all Employees
            foreach (Employee person in staff)
                PerformBonusCalculation(person);

            // Display the details of all Employees
            foreach (Employee person in staff)
                DisplayPersonDetails(person);
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            var owner = new AccountOwner()
            {
                Name    = "Ivan",
                Surname = "Ivanov"
            };

            var type1 = AccountType.BaseAccount;
            var type2 = AccountType.PlatinumAccount;

            var account1 = new BankAccount()
            {
                Number      = "123456",
                AccountType = type1,
                Owner       = owner,
                Sum         = 10m,
                Bonus       = 0m,
                State       = AccountState.Active
            };

            var account2 = new BankAccount()
            {
                Number      = "1248956",
                AccountType = type2,
                Owner       = owner,
                Sum         = 0m,
                Bonus       = 0m,
                State       = AccountState.Active
            };

            var calculateBonus = new CalculateBonus();

            var appFolder     = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var storageFolder = Path.Combine(appFolder, "Storage");

            Directory.CreateDirectory(storageFolder);
            var path = Path.Combine(storageFolder, "BankAccountStorage");

            var storage = new AccountStorage(path);
            var manager = new AccountManager(calculateBonus, storage);

            manager.AddBankAccount(account1);
            manager.AddBankAccount(account2);
            var accounts1 = manager.GetAccounts();

            foreach (var bankAccount in accounts1)
            {
                Console.WriteLine(bankAccount.ToString());
            }

            Console.ReadKey();
            Console.WriteLine();

            manager.RefillAccount(account1, 500m);
            manager.RefillAccount(account2, 1000m);

            var accounts2 = manager.GetAccounts();

            foreach (var bankAccount in accounts2)
            {
                Console.WriteLine(bankAccount.ToString());
            }

            Console.ReadKey();
            Console.WriteLine();

            manager.WithdrawalAccount(account2, 1000m);

            var accounts3 = manager.GetAccounts();

            foreach (var bankAccount in accounts3)
            {
                Console.WriteLine(bankAccount.ToString());
            }

            Console.ReadKey();
            Console.WriteLine();

            manager.CloseBankAccount(account2);

            var accounts4 = manager.GetAccounts();

            foreach (var bankAccount in accounts4)
            {
                Console.WriteLine(bankAccount.ToString());
            }

            Console.ReadKey();
            Console.WriteLine();

            manager.Save();
            manager.Reload();

            var accounts5 = manager.GetAccounts();

            foreach (var bankAccount in accounts5)
            {
                Console.WriteLine(bankAccount.ToString());
            }

            Console.ReadKey();
            File.Delete(path);
        }