TQC+ C# 認證教學手冊

304、本利和

參考程式碼

CustomerType.cs

using System;

namespace CSD03
{
    public enum CustomerType
    {
        Standard,
        SmallAndMediumBusiness,
        Enterprise,
        Government,
        Other
    }
}

Program.cs

using System;

namespace CSD03
{
    class Program
    {
        static void Main(string[] args)
        {
            int amount = 1000000;

            Console.WriteLine("==== LOAN ====");
            Console.WriteLine("${0:N0}", LoanRateCalculator.Calculate(CustomerType.Standard, amount));
            Console.WriteLine("${0:N0}", LoanRateCalculator.Calculate(CustomerType.SmallAndMediumBusiness, amount));
            Console.WriteLine("${0:N0}", LoanRateCalculator.Calculate(CustomerType.Enterprise, amount));
            Console.WriteLine("${0:N0}", LoanRateCalculator.Calculate(CustomerType.Government, amount));

            Console.WriteLine("==== SAVING ====");
            Console.WriteLine("${0:N0}", SavingRateCalculator.Calculate(CustomerType.Standard, amount));
            Console.WriteLine("${0:N0}", SavingRateCalculator.Calculate(CustomerType.SmallAndMediumBusiness, amount));
            Console.WriteLine("${0:N0}", SavingRateCalculator.Calculate(CustomerType.Enterprise, amount));
            Console.WriteLine("${0:N0}", SavingRateCalculator.Calculate(CustomerType.Government, amount));

            //try
            //{
            //    Console.WriteLine("${0:N0}", SavingRateCalculator.Calculate(CustomerType.Other, amount));
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Error: Not Support Customer Type");
            //}

            Console.ReadLine();
        }
    }
}

LoanRateCalculator.cs

using System;

namespace CSD03
{
    class LoanRateCalculator
    {
        public static int Calculate(CustomerType type, int amount)
        {
            if (type == CustomerType.Standard)
            {
                return (int)Math.Round(amount * 1.08);
            }
            else if (type == CustomerType.SmallAndMediumBusiness)
            {
                return (int)Math.Round(amount * 1.06);
            }
            else if (type == CustomerType.Enterprise)
            {
                return (int)Math.Round(amount * 1.05);
            }
            else if (type == CustomerType.Government)
            {
                return (int)Math.Round(amount * 1.02);
            }
            throw new NotSupportedException();
        }
    }
}

SavingRateCalculator.cs

using System;

namespace CSD03
{
    class SavingRateCalculator
    {
        public static int Calculate(CustomerType type, int amount)
        {
            if (type == CustomerType.Standard)
            {
                return (int)Math.Round(amount * 1.05);
            }
            else if (type == CustomerType.SmallAndMediumBusiness)
            {
                return (int)Math.Round(amount * 1.025);
            }
            else if (type == CustomerType.Enterprise)
            {
                return (int)Math.Round(amount * 1.02);
            }
            else if (type == CustomerType.Government)
            {
                return (int)Math.Round(amount * 1.01);
            }
            throw new NotSupportedException();
        }
    }
}