From 8c137f229c36a777ead5cacb3350cb8692646292 Mon Sep 17 00:00:00 2001 From: "mo.khan" Date: Sat, 5 Jan 2008 07:16:52 +0000 Subject: git-svn-id: http://mokhan.googlecode.com/svn/trunk@9 a0a4a051-f042-0410-9e78-9fae330bdb64 --- .../app/DesignPatterns.State/AmountEnteredState.cs | 32 ++++++++++++ .../app/DesignPatterns.State/CardSwipedState.cs | 32 ++++++++++++ .../DesignPatterns.State.csproj | 58 ++++++++++++++++++++++ .../src/app/DesignPatterns.State/IPosTerminal.cs | 19 +++++++ .../app/DesignPatterns.State/IPosTransaction.cs | 13 +++++ .../src/app/DesignPatterns.State/IState.cs | 13 +++++ .../src/app/DesignPatterns.State/IdleState.cs | 37 ++++++++++++++ .../app/DesignPatterns.State/PinEnteredState.cs | 31 ++++++++++++ .../src/app/DesignPatterns.State/PosTerminal.cs | 44 ++++++++++++++++ .../src/app/DesignPatterns.State/PosTransaction.cs | 30 +++++++++++ .../ProcessingTransactionState.cs | 37 ++++++++++++++ .../Properties/AssemblyInfo.cs | 35 +++++++++++++ .../TransactionApprovedState.cs | 36 ++++++++++++++ .../TransactionRejectedState.cs | 35 +++++++++++++ 14 files changed, 452 insertions(+) create mode 100644 DesignPatterns/src/app/DesignPatterns.State/AmountEnteredState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/CardSwipedState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/DesignPatterns.State.csproj create mode 100644 DesignPatterns/src/app/DesignPatterns.State/IPosTerminal.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/IPosTransaction.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/IState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/IdleState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/PinEnteredState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/PosTerminal.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/PosTransaction.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/ProcessingTransactionState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/Properties/AssemblyInfo.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/TransactionApprovedState.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.State/TransactionRejectedState.cs (limited to 'DesignPatterns/src/app/DesignPatterns.State') diff --git a/DesignPatterns/src/app/DesignPatterns.State/AmountEnteredState.cs b/DesignPatterns/src/app/DesignPatterns.State/AmountEnteredState.cs new file mode 100644 index 0000000..ec27fdc --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/AmountEnteredState.cs @@ -0,0 +1,32 @@ +using System; + +namespace DesignPatterns.State { + public class AmountEnteredState : IState { + public AmountEnteredState( IPosTerminal terminal ) { + _terminal = terminal; + } + + public void SwipeCard( string cardNumber ) { + Console.Out.WriteLine( "You've already swiped a card!" ); + } + + public void EnterAmount( double amount ) { + Console.Out.WriteLine( "You've already entered an amount" ); + } + + public void EnterPin( string pin ) { + _terminal.Transaction.Pin = pin; + _terminal.CurrentState = new PinEnteredState( _terminal ); + } + + public void ProcessTransaction( ) { + Console.Out.WriteLine( "You haven't entered an amount yet." ); + } + + public void PrintReceipt( ) { + Console.Out.WriteLine( "You haven't entered an amount yet." ); + } + + private IPosTerminal _terminal; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/CardSwipedState.cs b/DesignPatterns/src/app/DesignPatterns.State/CardSwipedState.cs new file mode 100644 index 0000000..ee3a24a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/CardSwipedState.cs @@ -0,0 +1,32 @@ +using System; + +namespace DesignPatterns.State { + public class CardSwipedState : IState { + public CardSwipedState( IPosTerminal terminal ) { + _terminal = terminal; + } + + public void SwipeCard( string cardNumber ) { + Console.Out.WriteLine( "You already swiped a card" ); + } + + public void EnterAmount( double amount ) { + _terminal.Transaction.Amount = amount; + _terminal.CurrentState = new AmountEnteredState( _terminal ); + } + + public void EnterPin( string pin ) { + Console.Out.WriteLine( "You haven't swiped a card yet!" ); + } + + public void ProcessTransaction( ) { + Console.Out.WriteLine( "You haven't swiped a card yet!" ); + } + + public void PrintReceipt( ) { + Console.Out.WriteLine( "You haven't swiped a card yet!" ); + } + + private IPosTerminal _terminal; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/DesignPatterns.State.csproj b/DesignPatterns/src/app/DesignPatterns.State/DesignPatterns.State.csproj new file mode 100644 index 0000000..faa7708 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/DesignPatterns.State.csproj @@ -0,0 +1,58 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {21E02E54-3D4F-457A-B521-D2472ABC55CE} + Library + Properties + DesignPatterns.State + DesignPatterns.State + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/IPosTerminal.cs b/DesignPatterns/src/app/DesignPatterns.State/IPosTerminal.cs new file mode 100644 index 0000000..0e65d89 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/IPosTerminal.cs @@ -0,0 +1,19 @@ +namespace DesignPatterns.State { + public interface IPosTerminal { + void SwipeCard( string cardNumber ); + + void EnterAmount( double amount ); + + void EnterPin( string pin ); + + void AuthorizeTransaction( ); + + void PrintReceipt( ); + + void ProcessTransaction( ); + + IState CurrentState { get; set; } + + IPosTransaction Transaction { get; set; } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/IPosTransaction.cs b/DesignPatterns/src/app/DesignPatterns.State/IPosTransaction.cs new file mode 100644 index 0000000..f2ff5cc --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/IPosTransaction.cs @@ -0,0 +1,13 @@ +using System; + +namespace DesignPatterns.State { + public interface IPosTransaction { + string Pin { get; set; } + + double Amount { get; set; } + + string CardNumber { get; set; } + + DateTime Date { get; set; } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/IState.cs b/DesignPatterns/src/app/DesignPatterns.State/IState.cs new file mode 100644 index 0000000..9a2baac --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/IState.cs @@ -0,0 +1,13 @@ +namespace DesignPatterns.State { + public interface IState { + void ProcessTransaction( ); + + void PrintReceipt( ); + + void SwipeCard( string cardNumber ); + + void EnterAmount( double amount ); + + void EnterPin( string pin ); + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/IdleState.cs b/DesignPatterns/src/app/DesignPatterns.State/IdleState.cs new file mode 100644 index 0000000..52b2bc6 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/IdleState.cs @@ -0,0 +1,37 @@ +using System; + +namespace DesignPatterns.State { + public class IdleState : IState { + public IdleState( IPosTerminal terminal ) { + _terminal = terminal; + + Console.Out.WriteLine( "Mo's POS TERMINAL" ); + Console.Out.WriteLine( "Please swipe a card:" ); + } + + public void SwipeCard( string cardNumber ) { + _terminal.Transaction = new PosTransaction( ); + _terminal.Transaction.Date = DateTime.Now; + _terminal.Transaction.CardNumber = cardNumber; + _terminal.CurrentState = new CardSwipedState( _terminal ); + } + + public void EnterAmount( double amount ) { + Console.Out.WriteLine( "Please swipe a card first." ); + } + + public void EnterPin( string pin ) { + Console.Out.WriteLine( "Please swipe a card first." ); + } + + public void ProcessTransaction( ) { + Console.Out.WriteLine( "Please swipe a card first." ); + } + + public void PrintReceipt( ) { + Console.Out.WriteLine( "Please swipe a card first." ); + } + + private IPosTerminal _terminal; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/PinEnteredState.cs b/DesignPatterns/src/app/DesignPatterns.State/PinEnteredState.cs new file mode 100644 index 0000000..809186a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/PinEnteredState.cs @@ -0,0 +1,31 @@ +using System; + +namespace DesignPatterns.State { + public class PinEnteredState : IState { + public PinEnteredState( IPosTerminal terminal ) { + _terminal = terminal; + } + + public void SwipeCard( string cardNumber ) { + Console.Out.WriteLine( "You've already swiped a card." ); + } + + public void EnterAmount( double amount ) { + Console.Out.WriteLine( "You've already entered an amount." ); + } + + public void EnterPin( string pin ) { + Console.Out.WriteLine( "You've already entered a pin." ); + } + + public void ProcessTransaction( ) { + _terminal.CurrentState = new ProcessingTransactionState( _terminal ); + } + + public void PrintReceipt( ) { + Console.Out.WriteLine( "The transaction needs to be authorized first" ); + } + + private IPosTerminal _terminal; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/PosTerminal.cs b/DesignPatterns/src/app/DesignPatterns.State/PosTerminal.cs new file mode 100644 index 0000000..4afe62a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/PosTerminal.cs @@ -0,0 +1,44 @@ +namespace DesignPatterns.State { + public class PosTerminal : IPosTerminal { + public PosTerminal( ) { + _currentState = new IdleState( this ); + } + + public IState CurrentState { + get { return _currentState; } + set { _currentState = value; } + } + + public IPosTransaction Transaction { + get { return _transaction; } + set { _transaction = value; } + } + + public void SwipeCard( string cardNumber ) { + _currentState.SwipeCard( cardNumber ); + } + + public void EnterAmount( double amount ) { + _currentState.EnterAmount( amount ); + } + + public void EnterPin( string pin ) { + _currentState.EnterPin( pin ); + } + + public void AuthorizeTransaction( ) { + _currentState.ProcessTransaction( ); + } + + public void PrintReceipt( ) { + _currentState.PrintReceipt( ); + } + + public void ProcessTransaction( ) { + _currentState.ProcessTransaction( ); + } + + private IState _currentState; + private IPosTransaction _transaction; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/PosTransaction.cs b/DesignPatterns/src/app/DesignPatterns.State/PosTransaction.cs new file mode 100644 index 0000000..b9d08d9 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/PosTransaction.cs @@ -0,0 +1,30 @@ +using System; + +namespace DesignPatterns.State { + internal class PosTransaction : IPosTransaction { + public string Pin { + get { return _pin; } + set { _pin = value; } + } + + public double Amount { + get { return _amount; } + set { _amount = value; } + } + + public string CardNumber { + get { return _cardNumber; } + set { _cardNumber = value; } + } + + public DateTime Date { + get { return _date; } + set { _date = value; } + } + + private string _pin; + private double _amount; + private string _cardNumber; + private DateTime _date; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/ProcessingTransactionState.cs b/DesignPatterns/src/app/DesignPatterns.State/ProcessingTransactionState.cs new file mode 100644 index 0000000..f91cae4 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/ProcessingTransactionState.cs @@ -0,0 +1,37 @@ +using System; + +namespace DesignPatterns.State { + internal class ProcessingTransactionState : IState { + public ProcessingTransactionState( IPosTerminal terminal ) { + _terminal = terminal; + } + + public void ProcessTransaction( ) { + if ( (++_counter % 5) == 0 ) { + _terminal.CurrentState = new TransactionRejectedState( _terminal ); + } + else { + _terminal.CurrentState = new TransactionApprovedState( _terminal ); + } + } + + public void PrintReceipt( ) { + Console.Out.WriteLine( "The transaction needs to be processed first" ); + } + + public void SwipeCard( string cardNumber ) { + Console.Out.WriteLine( "You have already swiped a card" ); + } + + public void EnterAmount( double amount ) { + Console.Out.WriteLine( "You have already entered an amount" ); + } + + public void EnterPin( string pin ) { + Console.Out.WriteLine( "You have already entered a PIN" ); + } + + private readonly IPosTerminal _terminal; + private static int _counter; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/Properties/AssemblyInfo.cs b/DesignPatterns/src/app/DesignPatterns.State/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..584bd52 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("DesignPatterns.State")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DesignPatterns.State")] +[assembly: AssemblyCopyright("Copyright © 2007")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("7ddf1e6a-6ca6-4775-8fd6-c63e38833f23")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPatterns/src/app/DesignPatterns.State/TransactionApprovedState.cs b/DesignPatterns/src/app/DesignPatterns.State/TransactionApprovedState.cs new file mode 100644 index 0000000..cfedf2d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/TransactionApprovedState.cs @@ -0,0 +1,36 @@ +using System; + +namespace DesignPatterns.State { + public class TransactionApprovedState : IState { + public TransactionApprovedState( IPosTerminal terminal ) { + _terminal = terminal; + } + + public void SwipeCard( string cardNumber ) { + Console.Out.WriteLine( "The transaction was approved" ); + } + + public void EnterAmount( double amount ) { + Console.Out.WriteLine( "The transaction was approved" ); + } + + public void EnterPin( string pin ) { + Console.Out.WriteLine( "The transaction was approved" ); + } + + public void ProcessTransaction( ) { + Console.Out.WriteLine( "The transaction was approved" ); + } + + public void PrintReceipt( ) { + Console.Out.WriteLine( "** Approved **" ); + Console.Out.WriteLine( "Date: {0}", _terminal.Transaction.Date ); + Console.Out.WriteLine( "Card Number: {0}", _terminal.Transaction.CardNumber ); + Console.Out.WriteLine( "Amount: {0}", _terminal.Transaction.Amount ); + + _terminal.CurrentState = new IdleState( _terminal ); + } + + private IPosTerminal _terminal; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.State/TransactionRejectedState.cs b/DesignPatterns/src/app/DesignPatterns.State/TransactionRejectedState.cs new file mode 100644 index 0000000..5a585a3 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.State/TransactionRejectedState.cs @@ -0,0 +1,35 @@ +using System; + +namespace DesignPatterns.State { + public class TransactionRejectedState : IState { + public TransactionRejectedState( IPosTerminal terminal ) { + _terminal = terminal; + } + + public void SwipeCard( string cardNumber ) { + Console.Out.WriteLine( "The transaction was rejected" ); + } + + public void EnterAmount( double amount ) { + Console.Out.WriteLine( "The transaction was rejected" ); + } + + public void EnterPin( string pin ) { + Console.Out.WriteLine( "The transaction was rejected" ); + } + + public void ProcessTransaction( ) { + Console.Out.WriteLine( "The transaction was rejected" ); + } + + public void PrintReceipt( ) { + Console.Out.WriteLine("** Rejected **"); + Console.Out.WriteLine("Date: {0}", _terminal.Transaction.Date); + Console.Out.WriteLine("Card Number: {0}", _terminal.Transaction.CardNumber); + Console.Out.WriteLine("Amount: {0}", _terminal.Transaction.Amount); + _terminal.CurrentState = new IdleState( _terminal ); + } + + private IPosTerminal _terminal; + } +} \ No newline at end of file -- cgit v1.2.3