Last active
May 13, 2018 14:21
-
-
Save miteshsureja/cccb5aab4e07a049bd93028c76d8a22a to your computer and use it in GitHub Desktop.
Revisions
-
miteshsureja revised this gist
May 13, 2018 . 1 changed file with 130 additions and 130 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,140 +6,140 @@ namespace StatePattern { //state public abstract class State { public BankAccount Account { get; set; } public double Balance { get; set; } public abstract void Deposit(double amount); public abstract void Withdraw(double amount); } //concrete state public class Normal : State { public Normal(State state) { Balance = state.Balance; Account = state.Account; } public Normal(double balance, BankAccount account) { Balance = balance; Account = account; } public override void Deposit(double amount) { Balance += amount; CheckState(); } public override void Withdraw(double amount) { Balance -= amount; CheckState(); } void CheckState() { if (Balance > 1000) Account.State = new Classic(this); } } //concrete state public class Classic : State { public Classic(State state) { Balance = state.Balance; Account = state.Account; } public Classic(double balance, BankAccount account) { Balance = balance; Account = account; } public override void Deposit(double amount) { Balance += amount; CheckState(); } public override void Withdraw(double amount) { Balance -= amount; CheckState(); } void CheckState() { if (Balance < 1000) Account.State = new Normal(this); else if (Balance > 2000) Account.State = new Platinum(this); } } //concrete state public class Platinum : State { public Platinum(State state) { Balance = state.Balance; Account = state.Account; } public Platinum(double balance, BankAccount account) { Balance = balance; Account = account; } public override void Deposit(double amount) { Balance += amount; CheckState(); } public override void Withdraw(double amount) { Balance -= amount; CheckState(); } void CheckState() { if (Balance < 2000) Account.State = new Classic(this); else if (Balance < 1000) Account.State = new Normal(this); } } //context public class BankAccount { public State State { get; set; } public string Name { get; set; } public BankAccount(string name) { Name = name; State = new Normal(0, this); } public double Balance { get { return State.Balance; } } public void Deposit(double amount) { State.Deposit(amount); Console.WriteLine("Deposited - {0}", amount); Console.WriteLine("Balance - {0}", Balance); Console.WriteLine("Account Status - {0}", State.GetType().Name); Console.WriteLine(new string('-', 50)); } public void Withdraw(double amount) { State.Withdraw(amount); Console.WriteLine("Withdrawn - {0}", amount); Console.WriteLine("Balance - {0}", Balance); Console.WriteLine("Account Status - {0}", State.GetType().Name); Console.WriteLine(new string('-', 50)); } } class Program { //entry point static void Main(string[] args) { -
miteshsureja created this gist
May 6, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,155 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StatePattern { class Program { //state public abstract class State { public BankAccount Account { get; set; } public double Balance { get; set; } public abstract void Deposit(double amount); public abstract void Withdraw(double amount); } //concrete state public class Normal : State { public Normal(State state) { Balance = state.Balance; Account = state.Account; } public Normal(double balance, BankAccount account) { Balance = balance; Account = account; } public override void Deposit(double amount) { Balance += amount; CheckState(); } public override void Withdraw(double amount) { Balance -= amount; CheckState(); } void CheckState() { if (Balance > 1000) Account.State = new Classic(this); } } //concrete state public class Classic : State { public Classic(State state) { Balance = state.Balance; Account = state.Account; } public Classic(double balance, BankAccount account) { Balance = balance; Account = account; } public override void Deposit(double amount) { Balance += amount; CheckState(); } public override void Withdraw(double amount) { Balance -= amount; CheckState(); } void CheckState() { if (Balance < 1000) Account.State = new Normal(this); else if (Balance > 2000) Account.State = new Platinum(this); } } //concrete state public class Platinum : State { public Platinum(State state) { Balance = state.Balance; Account = state.Account; } public Platinum(double balance, BankAccount account) { Balance = balance; Account = account; } public override void Deposit(double amount) { Balance += amount; CheckState(); } public override void Withdraw(double amount) { Balance -= amount; CheckState(); } void CheckState() { if (Balance < 2000) Account.State = new Classic(this); else if (Balance < 1000) Account.State = new Normal(this); } } //context public class BankAccount { public State State { get; set; } public string Name { get; set; } public BankAccount(string name) { Name = name; State = new Normal(0,this); } public double Balance { get { return State.Balance; } } public void Deposit(double amount) { State.Deposit(amount); Console.WriteLine("Deposited - {0}",amount); Console.WriteLine("Balance - {0}", Balance); Console.WriteLine("Account Status - {0}", State.GetType().Name); Console.WriteLine(new string('-', 50)); } public void Withdraw(double amount) { State.Withdraw(amount); Console.WriteLine("Withdrawn - {0}", amount); Console.WriteLine("Balance - {0}", Balance); Console.WriteLine("Account Status - {0}", State.GetType().Name); Console.WriteLine(new string('-', 50)); } } //entry point static void Main(string[] args) { BankAccount account = new BankAccount("Mitesh Sureja"); account.Deposit(500); account.Deposit(600); account.Deposit(1000); account.Withdraw(500); account.Withdraw(1500); Console.Read(); } } }