Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- // State pattern
- namespace BankAccount_StatePattern
- {
- // Represents Account Type - it playes a role of "State" class in State pattern
- //
- // AccountType containns abstract methods,
- // These methods are used by Account object
- // Inhereited classes contain concrete implementation of these methods
- public abstract class AccountType
- {
- // witdrawal operation - will be overriden in inherited classes
- public abstract void Withdraw(Account account, double amount);
- // used to change account type -
- public abstract void ChangeAccountType(Account account);
- }
- // Represents Standard Account type - it playes a role of "Concrete State" in State pattern
- public class StandardAccountType : AccountType
- {
- protected const double withdrawLimit = 5000;
- protected const double serviceFee = 3;
- // Operacja wypłaty dla konta Standard
- //
- // Obiekt Account wywoła tę wersję metody Withdraw(),
- // jeśli znajdzie się w "stanie" VIPAccountType.
- public override void Withdraw(Account account, double amount)
- {
- if ((amount > withdrawLimit) || (account.Balance < amount + serviceFee))
- {
- Console.WriteLine("Withdrawal operation of {0} from Standard Account of {1} failed", amount, account.Owner);
- return;
- }
- account.Balance -= amount + serviceFee;
- Console.WriteLine("{0} was withdrowed from {1} Standard account", amount, account.Owner);
- }
- // Operacja zmiany rodzaju konta
- //
- // Obiekt Account wywoła tę wersję metody ChangeAccountType(),
- // jeśli znajdzie się w "stanie" StandardAccountState.
- public override void ChangeAccountType(Account account)
- {
- // jeśli saldo jest większe od 30000, konto Standard może być zmienione na VIP
- if (account.Balance > 30000)
- {
- account.AccountType = new VIPAccountType();
- Console.WriteLine("{0} Standard account was changed to VIP", account.Owner);
- }
- else
- Console.WriteLine("The operation of changing {0} Standard account type to VIP failed", account.Owner);
- }
- }
- // Reprezentuje rodzaj konta VIP - we wzorcu State pełni rolę "Concrete State"
- public class VIPAccountType : AccountType
- {
- // Operacja wypłaty dla konta VIP
- //
- // Obiekt Account wywoła tę wersję metody Withdraw(),
- // jeśli znajdzie się w "stanie" VIPAccountType
- public override void Withdraw(Account account, double amount)
- {
- if (account.Balance < amount)
- {
- Console.WriteLine("Withdrawal operation of {0} from VIP Account of {1} failed", amount, account.Owner);
- return;
- }
- account.Balance -= amount;
- Console.WriteLine("{0} was withdrowed from {1} VIP account", amount, account.Owner);
- }
- // operacja zmiany rodzaju konta
- //
- // Obiekt Account wywoła tę wersję metody ChangeAccountType(),
- // jeśli znajdzie się w "stanie" VIPAccountState.
- public override void ChangeAccountType(Account account)
- {
- // konto VIP można zmienić na Standard być bez żadnych warunków
- account.AccountType = new StandardAccountType();
- Console.WriteLine("VIP account: {0} is changed to Standard", account.Owner);
- }
- }
- public class JuniorAccountType : AccountType
- {
- protected const double withdrawLimit = 3000;
- protected const double serviceFee = 0;
- // Operacja wypłaty dla konta Junior
- //
- // Obiekt Account wywoła tę wersję metody Withdraw(),
- public override void Withdraw(Account account, double amount)
- {
- if ((amount > withdrawLimit) || (account.Balance < amount + serviceFee))
- {
- Console.WriteLine("Withdrawal operation of {0} from Junior Account of {1} failed", amount, account.Owner);
- return;
- }
- account.Balance -= amount + serviceFee;
- Console.WriteLine("{0} was withdrowed from {1} Junior account", amount, account.Owner);
- }
- // Operacja zmiany rodzaju konta
- //
- // Obiekt Account wywoła tę wersję metody ChangeAccountType(),
- // jeśli znajdzie się w "stanie" StandardAccountState.
- public override void ChangeAccountType(Account account)
- {
- // jeśli saldo jest większe od 30000, konto Standard może być zmienione na VIP
- account.AccountType = new StandardAccountType();
- Console.WriteLine("{0} Junior account was changed to Standard", account.Owner);
- }
- }
- // Reprezentuje konto bankowe - we wzorcu "State" pełni rolę "Context"
- //
- // Obiekt typu Account zawiera referencje do jednego obiektu typu AccountType.
- // Obiekt typu Account może zmienić "stan" poprzez zmianę skojarzonego z nim obiektu AccountType.
- public class Account
- {
- // właściciel konta
- private string _owner;
- public string Owner
- {
- get { return this._owner; }
- set { this._owner = value; }
- }
- // saldo
- private double _balance;
- public double Balance
- {
- get { return this._balance; }
- set { this._balance = value;} // docelowo należy "opakować" lub użyć "Proxy"
- }
- // "Stan" - reprezentuje rodzaj konta (Junior, Standard, VIP).
- private AccountType _accountType;
- public AccountType AccountType
- {
- get { return this._accountType; }
- set { this._accountType = value; }
- }
- public Account(string owner)
- {
- this._owner = owner;
- this._accountType = new JuniorAccountType();
- }
- // operacja wypłaty
- public void Withdraw(double amount)
- {
- // Deleguje wykonanie operacji do obiektu typu AccountType,
- // wywołując na nim polimorficzną metodę Withdraw().
- this._accountType.Withdraw(this, amount);
- }
- // operacja zmiany "stanu" (rodzaju konta)
- public void ChangeAccountType()
- {
- // Deleguje wykonanie operacji do obiektu typu AccountType,
- // wywołując na nim polimorficzną metodę ChangeState().
- this._accountType.ChangeAccountType(this);
- }
- // Operacja wpłaty na konto
- //
- // Ze względu na identyczne zachowanie metody Deposit() we wszystkich
- // "stanach" - jej implementację umieszczono w klasie Account.
- public void Deposit(double amount)
- {
- this._balance += amount;
- Console.WriteLine("Deposit of {0} to {1} account", amount, this._owner);
- Console.WriteLine("The balanse of {0} account is equal {1}", this._owner, this._balance);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Account johnAccount = new Account("John");
- Account katieAccount = new Account("Katie");
- johnAccount.Deposit(38000);
- katieAccount.Deposit(4500);
- Console.WriteLine("----------");
- Console.ReadKey();
- johnAccount.Withdraw(3000);
- Console.WriteLine("----------");
- Console.ReadKey();
- johnAccount.Withdraw(20000);
- katieAccount.Withdraw(20000);
- Console.WriteLine("----------");
- Console.ReadKey();
- johnAccount.ChangeAccountType();
- katieAccount.ChangeAccountType();
- Console.WriteLine("----------");
- Console.ReadKey();
- johnAccount.Withdraw(10000);
- Console.WriteLine("----------");
- Console.ReadKey();
- johnAccount.ChangeAccountType();
- Console.WriteLine("----------");
- Console.ReadKey();
- johnAccount.Withdraw(10000);
- Console.WriteLine("----------");
- Console.ReadKey();
- Account julia = new Account("Julia");
- julia.Deposit(4000);
- julia.Withdraw(3001);
- julia.ChangeAccountType();
- julia.Withdraw(3001);
- julia.Deposit(1000000);
- julia.ChangeAccountType();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement