文章出處

namespace StatePatternSample
{
    public class Account
    {
        public State State {get;set;}
        public string Owner { get; set; }
        public Account(string owner)
        {
            this.Owner = owner;
            this.State = new SilverState(0.0, this);
        }

        public double Balance { get {return State.Balance; }} // 余額
        // 存錢
        public void Deposit(double amount)
        {
            State.Deposit(amount);
            Console.WriteLine("存款金額為 {0:C}——", amount);
            Console.WriteLine("賬戶余額為 =:{0:C}", this.Balance);
            Console.WriteLine("賬戶狀態為: {0}", this.State.GetType().Name);
            Console.WriteLine();
        }

        // 取錢
        public void Withdraw(double amount)
        {
            State.Withdraw(amount);
             Console.WriteLine("取款金額為 {0:C}——",amount);
            Console.WriteLine("賬戶余額為 =:{0:C}", this.Balance);
            Console.WriteLine("賬戶狀態為: {0}", this.State.GetType().Name);
            Console.WriteLine();
        }

        // 獲得利息
        public void PayInterest()
        {
            State.PayInterest();
            Console.WriteLine("Interest Paid --- ");
            Console.WriteLine("賬戶余額為 =:{0:C}", this.Balance);
            Console.WriteLine("賬戶狀態為: {0}", this.State.GetType().Name);
            Console.WriteLine();
        }
    }

    // 抽象狀態類
    public abstract class State
    {
        // Properties
        public Account Account { get; set; }
        public double Balance { get; set; } // 余額
        public double Interest { get; set; } // 利率
        public double LowerLimit { get; set; } // 下限
        public double UpperLimit { get; set; } // 上限

        public abstract void Deposit(double amount); // 存款
        public abstract void Withdraw(double amount); // 取錢
        public abstract void PayInterest(); // 獲得的利息
    }

    // Red State意味著Account透支了
    public class RedState : State
    {
        public RedState(State state)
        {
            // Initialize
            this.Balance = state.Balance;
            this.Account = state.Account;
            Interest = 0.00;
            LowerLimit = -100.00;
            UpperLimit = 0.00;
        }

        // 存款
        public override void Deposit(double amount)
        {
            Balance += amount;
            StateChangeCheck();
        }
        // 取錢
        public override void Withdraw(double amount)
        {
            Console.WriteLine("沒有錢可以取了!");
        }

        public override void PayInterest()
        {
            // 沒有利息
        }

        private void StateChangeCheck()
        {
            if (Balance > UpperLimit)
            {
                Account.State = new SilverState(this);
            }
        }
    }

    // Silver State意味著沒有利息得
    public class SilverState :State
    {
        public SilverState(State state)
            : this(state.Balance, state.Account)
        { 
        }

        public SilverState(double balance, Account account)
        {
            this.Balance = balance;
            this.Account = account;
            Interest = 0.00;
            LowerLimit = 0.00;
            UpperLimit = 1000.00;
        }

        public override void Deposit(double amount)
        {
            Balance += amount;
            StateChangeCheck();
        }
        public override void Withdraw(double amount)
        {
            Balance -= amount;
            StateChangeCheck();
        }

        public override void PayInterest()
        {
            Balance += Interest * Balance;
            StateChangeCheck();
        }

        private void StateChangeCheck()
        {
            if (Balance < LowerLimit)
            {
                Account.State = new RedState(this);
            }
            else if (Balance > UpperLimit)
            {
                Account.State = new GoldState(this);
            }
        }     
    }

    // Gold State意味著有利息狀態
    public class GoldState : State
    {
        public GoldState(State state)
        {
            this.Balance = state.Balance;
            this.Account = state.Account;
            Interest = 0.05;
            LowerLimit = 1000.00;
            UpperLimit = 1000000.00;
        }
        // 存錢
        public override void Deposit(double amount)
        {
            Balance += amount;
            StateChangeCheck();
        }
        // 取錢
        public override void Withdraw(double amount)
        {
            Balance -= amount;
            StateChangeCheck();
        }
        public override void PayInterest()
        {
            Balance += Interest * Balance;
            StateChangeCheck();
        }

        private void StateChangeCheck()
        {
            if (Balance < 0.0)
            {
                Account.State = new RedState(this);
            }
            else if (Balance < LowerLimit)
            {
                Account.State = new SilverState(this);
            }
        }
    }

    class App
    {
        static void Main(string[] args)
        {
            // 開一個新的賬戶
            Account account = new Account("Learning Hard");

            // 進行交易
            // 存錢
            account.Deposit(1000.0);
            account.Deposit(200.0);
            account.Deposit(600.0);

            // 付利息
            account.PayInterest();

            // 取錢
            account.Withdraw(2000.00);
            account.Withdraw(500.00);
            
            // 等待用戶輸入
            Console.ReadKey();
        }
    }
}

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()