Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // State pattern
  7. namespace BankAccount_StatePattern
  8. {
  9. // Represents Account Type - it playes a role of "State" class in State pattern
  10. //
  11. // AccountType containns abstract methods,
  12. // These methods are used by Account object
  13. // Inhereited classes contain concrete implementation of these methods
  14. public abstract class AccountType
  15. {
  16. // witdrawal operation - will be overriden in inherited classes
  17. public abstract void Withdraw(Account account, double amount);
  18.  
  19.  
  20. // used to change account type -
  21. public abstract void ChangeAccountType(Account account);
  22. }
  23.  
  24. // Represents Standard Account type - it playes a role of "Concrete State" in State pattern
  25. public class StandardAccountType : AccountType
  26. {
  27. protected const double withdrawLimit = 5000;
  28.  
  29. protected const double serviceFee = 3;
  30.  
  31. // Operacja wypłaty dla konta Standard
  32. //
  33. // Obiekt Account wywoła tę wersję metody Withdraw(),
  34. // jeśli znajdzie się w "stanie" VIPAccountType.
  35. public override void Withdraw(Account account, double amount)
  36. {
  37. if ((amount > withdrawLimit) || (account.Balance < amount + serviceFee))
  38. {
  39. Console.WriteLine("Withdrawal operation of {0} from Standard Account of {1} failed", amount, account.Owner);
  40. return;
  41. }
  42.  
  43. account.Balance -= amount + serviceFee;
  44. Console.WriteLine("{0} was withdrowed from {1} Standard account", amount, account.Owner);
  45. }
  46.  
  47. // Operacja zmiany rodzaju konta
  48. //
  49. // Obiekt Account wywoła tę wersję metody ChangeAccountType(),
  50. // jeśli znajdzie się w "stanie" StandardAccountState.
  51. public override void ChangeAccountType(Account account)
  52. {
  53. // jeśli saldo jest większe od 30000, konto Standard może być zmienione na VIP
  54. if (account.Balance > 30000)
  55. {
  56. account.AccountType = new VIPAccountType();
  57. Console.WriteLine("{0} Standard account was changed to VIP", account.Owner);
  58. }
  59. else
  60. Console.WriteLine("The operation of changing {0} Standard account type to VIP failed", account.Owner);
  61. }
  62. }
  63.  
  64. // Reprezentuje rodzaj konta VIP - we wzorcu State pełni rolę "Concrete State"
  65. public class VIPAccountType : AccountType
  66. {
  67. // Operacja wypłaty dla konta VIP
  68. //
  69. // Obiekt Account wywoła tę wersję metody Withdraw(),
  70. // jeśli znajdzie się w "stanie" VIPAccountType
  71. public override void Withdraw(Account account, double amount)
  72. {
  73. if (account.Balance < amount)
  74. {
  75. Console.WriteLine("Withdrawal operation of {0} from VIP Account of {1} failed", amount, account.Owner);
  76. return;
  77. }
  78.  
  79. account.Balance -= amount;
  80. Console.WriteLine("{0} was withdrowed from {1} VIP account", amount, account.Owner);
  81. }
  82.  
  83. // operacja zmiany rodzaju konta
  84. //
  85. // Obiekt Account wywoła tę wersję metody ChangeAccountType(),
  86. // jeśli znajdzie się w "stanie" VIPAccountState.
  87. public override void ChangeAccountType(Account account)
  88. {
  89. // konto VIP można zmienić na Standard być bez żadnych warunków
  90. account.AccountType = new StandardAccountType();
  91. Console.WriteLine("VIP account: {0} is changed to Standard", account.Owner);
  92. }
  93. }
  94.  
  95. public class JuniorAccountType : AccountType
  96. {
  97. protected const double withdrawLimit = 3000;
  98.  
  99. protected const double serviceFee = 0;
  100.  
  101. // Operacja wypłaty dla konta Junior
  102. //
  103. // Obiekt Account wywoła tę wersję metody Withdraw(),
  104.  
  105. public override void Withdraw(Account account, double amount)
  106. {
  107. if ((amount > withdrawLimit) || (account.Balance < amount + serviceFee))
  108. {
  109. Console.WriteLine("Withdrawal operation of {0} from Junior Account of {1} failed", amount, account.Owner);
  110. return;
  111. }
  112.  
  113. account.Balance -= amount + serviceFee;
  114. Console.WriteLine("{0} was withdrowed from {1} Junior account", amount, account.Owner);
  115. }
  116.  
  117. // Operacja zmiany rodzaju konta
  118. //
  119. // Obiekt Account wywoła tę wersję metody ChangeAccountType(),
  120. // jeśli znajdzie się w "stanie" StandardAccountState.
  121. public override void ChangeAccountType(Account account)
  122. {
  123. // jeśli saldo jest większe od 30000, konto Standard może być zmienione na VIP
  124.  
  125. account.AccountType = new StandardAccountType();
  126. Console.WriteLine("{0} Junior account was changed to Standard", account.Owner);
  127.  
  128. }
  129. }
  130. // Reprezentuje konto bankowe - we wzorcu "State" pełni rolę "Context"
  131. //
  132. // Obiekt typu Account zawiera referencje do jednego obiektu typu AccountType.
  133. // Obiekt typu Account może zmienić "stan" poprzez zmianę skojarzonego z nim obiektu AccountType.
  134. public class Account
  135. {
  136. // właściciel konta
  137. private string _owner;
  138. public string Owner
  139. {
  140. get { return this._owner; }
  141. set { this._owner = value; }
  142. }
  143.  
  144. // saldo
  145. private double _balance;
  146. public double Balance
  147. {
  148. get { return this._balance; }
  149. set { this._balance = value;} // docelowo należy "opakować" lub użyć "Proxy"
  150. }
  151.  
  152. // "Stan" - reprezentuje rodzaj konta (Junior, Standard, VIP).
  153. private AccountType _accountType;
  154. public AccountType AccountType
  155. {
  156. get { return this._accountType; }
  157. set { this._accountType = value; }
  158. }
  159.  
  160. public Account(string owner)
  161. {
  162. this._owner = owner;
  163. this._accountType = new JuniorAccountType();
  164. }
  165.  
  166. // operacja wypłaty
  167. public void Withdraw(double amount)
  168. {
  169. // Deleguje wykonanie operacji do obiektu typu AccountType,
  170. // wywołując na nim polimorficzną metodę Withdraw().
  171. this._accountType.Withdraw(this, amount);
  172. }
  173.  
  174. // operacja zmiany "stanu" (rodzaju konta)
  175. public void ChangeAccountType()
  176. {
  177. // Deleguje wykonanie operacji do obiektu typu AccountType,
  178. // wywołując na nim polimorficzną metodę ChangeState().
  179. this._accountType.ChangeAccountType(this);
  180. }
  181.  
  182. // Operacja wpłaty na konto
  183. //
  184. // Ze względu na identyczne zachowanie metody Deposit() we wszystkich
  185. // "stanach" - jej implementację umieszczono w klasie Account.
  186. public void Deposit(double amount)
  187. {
  188. this._balance += amount;
  189.  
  190. Console.WriteLine("Deposit of {0} to {1} account", amount, this._owner);
  191. Console.WriteLine("The balanse of {0} account is equal {1}", this._owner, this._balance);
  192. }
  193. }
  194.  
  195. class Program
  196. {
  197. static void Main(string[] args)
  198. {
  199. Account johnAccount = new Account("John");
  200. Account katieAccount = new Account("Katie");
  201.  
  202. johnAccount.Deposit(38000);
  203. katieAccount.Deposit(4500);
  204.  
  205. Console.WriteLine("----------");
  206. Console.ReadKey();
  207. johnAccount.Withdraw(3000);
  208.  
  209. Console.WriteLine("----------");
  210. Console.ReadKey();
  211. johnAccount.Withdraw(20000);
  212. katieAccount.Withdraw(20000);
  213.  
  214. Console.WriteLine("----------");
  215. Console.ReadKey();
  216. johnAccount.ChangeAccountType();
  217. katieAccount.ChangeAccountType();
  218.  
  219. Console.WriteLine("----------");
  220. Console.ReadKey();
  221. johnAccount.Withdraw(10000);
  222.  
  223. Console.WriteLine("----------");
  224. Console.ReadKey();
  225. johnAccount.ChangeAccountType();
  226.  
  227. Console.WriteLine("----------");
  228. Console.ReadKey();
  229. johnAccount.Withdraw(10000);
  230.  
  231. Console.WriteLine("----------");
  232. Console.ReadKey();
  233.  
  234. Account julia = new Account("Julia");
  235. julia.Deposit(4000);
  236. julia.Withdraw(3001);
  237. julia.ChangeAccountType();
  238. julia.Withdraw(3001);
  239. julia.Deposit(1000000);
  240. julia.ChangeAccountType();
  241. }
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement