summaryrefslogtreecommitdiff
path: root/DesignPatterns/src/app/DesignPatterns.State
diff options
context:
space:
mode:
authormo.khan <mo.khan@a0a4a051-f042-0410-9e78-9fae330bdb64>2008-01-05 07:16:52 +0000
committermo.khan <mo.khan@a0a4a051-f042-0410-9e78-9fae330bdb64>2008-01-05 07:16:52 +0000
commit8c137f229c36a777ead5cacb3350cb8692646292 (patch)
tree92876c5da0ffd17767e38f94a44415ac27a3c73e /DesignPatterns/src/app/DesignPatterns.State
parentcbdf42a6427eef7849d1ab7731f9185b410431d3 (diff)
git-svn-id: http://mokhan.googlecode.com/svn/trunk@9 a0a4a051-f042-0410-9e78-9fae330bdb64
Diffstat (limited to 'DesignPatterns/src/app/DesignPatterns.State')
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/AmountEnteredState.cs32
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/CardSwipedState.cs32
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/DesignPatterns.State.csproj58
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/IPosTerminal.cs19
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/IPosTransaction.cs13
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/IState.cs13
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/IdleState.cs37
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/PinEnteredState.cs31
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/PosTerminal.cs44
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/PosTransaction.cs30
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/ProcessingTransactionState.cs37
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/Properties/AssemblyInfo.cs35
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/TransactionApprovedState.cs36
-rw-r--r--DesignPatterns/src/app/DesignPatterns.State/TransactionRejectedState.cs35
14 files changed, 452 insertions, 0 deletions
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 @@
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.50727</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{21E02E54-3D4F-457A-B521-D2472ABC55CE}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>DesignPatterns.State</RootNamespace>
+ <AssemblyName>DesignPatterns.State</AssemblyName>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="AmountEnteredState.cs" />
+ <Compile Include="CardSwipedState.cs" />
+ <Compile Include="IdleState.cs" />
+ <Compile Include="IPosTerminal.cs" />
+ <Compile Include="IPosTransaction.cs" />
+ <Compile Include="IState.cs" />
+ <Compile Include="PinEnteredState.cs" />
+ <Compile Include="PosTerminal.cs" />
+ <Compile Include="PosTransaction.cs" />
+ <Compile Include="ProcessingTransactionState.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="TransactionApprovedState.cs" />
+ <Compile Include="TransactionRejectedState.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ 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