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 --- .../src/app/DesignPatterns.Factory/Bank.cs | 19 +++++++ .../src/app/DesignPatterns.Factory/BankAccount.cs | 27 +++++++++ .../app/DesignPatterns.Factory/Cibc/CibcBank.cs | 9 +++ .../Cibc/CibcBankAccountFactory.cs | 13 +++++ .../Cibc/CibcChequingAccount.cs | 5 ++ .../Cibc/CibcSavingsAccount.cs | 5 ++ .../src/app/DesignPatterns.Factory/Currency.cs | 38 +++++++++++++ .../DesignPatterns.Factory.csproj | 65 ++++++++++++++++++++++ .../Exceptions/CannotAddMoniesException.cs | 13 +++++ .../Exceptions/NegativeMoneyException.cs | 15 +++++ .../app/DesignPatterns.Factory/Interfaces/IBank.cs | 6 ++ .../Interfaces/IBankAccount.cs | 8 +++ .../Interfaces/IBankAccountFactory.cs | 8 +++ .../DesignPatterns.Factory/Interfaces/ICurrency.cs | 5 ++ .../DesignPatterns.Factory/Interfaces/IMoney.cs | 13 +++++ .../src/app/DesignPatterns.Factory/Money.cs | 56 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 35 ++++++++++++ .../DesignPatterns.Factory/RoyalBank/RoyalBank.cs | 9 +++ .../RoyalBank/RoyalBankAccountFactory.cs | 13 +++++ .../RoyalBank/RoyalBankChequingAccount.cs | 5 ++ .../RoyalBank/RoyalBankSavingsAccount.cs | 5 ++ 21 files changed, 372 insertions(+) create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/CannotAddMoniesException.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Money.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/Properties/AssemblyInfo.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBank.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs create mode 100644 DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs (limited to 'DesignPatterns/src/app/DesignPatterns.Factory') diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs new file mode 100644 index 0000000..ac51e00 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs @@ -0,0 +1,19 @@ +namespace DesignPatterns.Factory { + public abstract class Bank : IBank { + private readonly string _name; + + public Bank( string name ) { + _name = name; + } + + public string Name { + get { return _name; } + } + + public override string ToString( ) { + return _name; + } + + public abstract IBankAccountFactory GetAccountFactory( ); + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs new file mode 100644 index 0000000..6bde449 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs @@ -0,0 +1,27 @@ +using System; +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + public class BankAccount : IBankAccount { + public BankAccount( ) : this( new Money( 0 ) ) {} + + public BankAccount( IMoney balance ) : this( balance, Guid.NewGuid( ).ToString( ) ) {} + + public BankAccount( IMoney balance, string accountNumber ) { + _balance = balance; + _accountNumber = accountNumber; + } + + public IMoney Balance { + get { return _balance; } + set { _balance = value; } + } + + public string AccountNumber { + get { return _accountNumber; } + } + + private IMoney _balance; + private readonly string _accountNumber; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs new file mode 100644 index 0000000..b2a80ce --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs @@ -0,0 +1,9 @@ +namespace DesignPatterns.Factory { + public class CibcBank : Bank, IBank { + public CibcBank( ) : base( "CIBC" ) {} + + public override IBankAccountFactory GetAccountFactory( ) { + return new CibcBankAccountFactory( ); + } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs new file mode 100644 index 0000000..23ac864 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs @@ -0,0 +1,13 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + internal class CibcBankAccountFactory : IBankAccountFactory { + public IBankAccount CreateChequingAccount( ) { + return new CibcChequingAccount( ); + } + + public IBankAccount CreateSavingsAccount( ) { + return new CibcSavingsAccount( ); + } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs new file mode 100644 index 0000000..29e4b92 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + internal class CibcChequingAccount : BankAccount, IBankAccount {} +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs new file mode 100644 index 0000000..1582a1d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + internal class CibcSavingsAccount : BankAccount, IBankAccount {} +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs new file mode 100644 index 0000000..2c0834e --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs @@ -0,0 +1,38 @@ +namespace DesignPatterns.Factory { + public class Currency : ICurrency { + #region Constructors + + private Currency( string name ) { + _name = name; + } + + #endregion + + #region Public Properties + + public string Name { + get { return _name; } + } + + public static readonly ICurrency Canadian = new Currency( "CAD" ); + public static readonly ICurrency American = new Currency( "USD" ); + public static readonly ICurrency GreatBritain = new Currency( "GBP" ); + public static readonly ICurrency EuropeanUnion = new Currency( "EUR" ); + + #endregion + + #region Public Methods + + public override string ToString( ) { + return _name; + } + + #endregion + + #region Private Fields + + private readonly string _name; + + #endregion + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj b/DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj new file mode 100644 index 0000000..4c50734 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj @@ -0,0 +1,65 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {0AAEDFE4-421D-4B3B-8629-5A20EC13F379} + Library + Properties + DesignPatterns.Factory + DesignPatterns.Factory + + + 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.Factory/Exceptions/CannotAddMoniesException.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/CannotAddMoniesException.cs new file mode 100644 index 0000000..421883c --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/CannotAddMoniesException.cs @@ -0,0 +1,13 @@ +using System; +using System.Runtime.Serialization; + +namespace DesignPatterns.Factory { + [Serializable] + public class CannotAddMoniesException : ArgumentException + { + public CannotAddMoniesException() { } + public CannotAddMoniesException(string message) : base(message) { } + public CannotAddMoniesException(string message, Exception innerException) : base(message, innerException) { } + protected CannotAddMoniesException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs new file mode 100644 index 0000000..d7d313d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs @@ -0,0 +1,15 @@ +using System; +using System.Runtime.Serialization; + +namespace DesignPatterns.Factory { + [Serializable] + public class NegativeMoneyException : ArgumentException { + public NegativeMoneyException( ) : this( "Cannot create a negative money." ) {} + + public NegativeMoneyException( string message ) : base( message ) {} + + public NegativeMoneyException( string message, Exception innerException ) : base( message, innerException ) {} + + protected NegativeMoneyException( SerializationInfo info, StreamingContext context ) : base( info, context ) {} + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs new file mode 100644 index 0000000..52f1e67 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Factory { + public interface IBank { + string Name { get; } + IBankAccountFactory GetAccountFactory( ); + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs new file mode 100644 index 0000000..f759e8d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs @@ -0,0 +1,8 @@ +using DesignPatterns.Factory; + +namespace DesignPatterns.Test { + public interface IBankAccount { + IMoney Balance { get; set; } + string AccountNumber{ get;} + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs new file mode 100644 index 0000000..5099fd8 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs @@ -0,0 +1,8 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + public interface IBankAccountFactory { + IBankAccount CreateChequingAccount( ); + IBankAccount CreateSavingsAccount( ); + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs new file mode 100644 index 0000000..8f1afd4 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs @@ -0,0 +1,5 @@ +namespace DesignPatterns.Factory { + public interface ICurrency { + string Name { get; } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs new file mode 100644 index 0000000..c5392ae --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs @@ -0,0 +1,13 @@ +using DesignPatterns.Factory; + +namespace DesignPatterns.Factory { + public interface IMoney { + double Amount { get; } + + ICurrency TypeOfCurrency { get; } + + IMoney Add( IMoney other ); + + IMoney Subtract( IMoney money ); + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Money.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Money.cs new file mode 100644 index 0000000..956c9a6 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Money.cs @@ -0,0 +1,56 @@ +using System; + +namespace DesignPatterns.Factory { + public class Money : IMoney { + public Money( double amount ) : this( amount, Currency.Canadian ) {} + + public Money( double amount, ICurrency currency ) { + if ( amount < 0 ) { + throw new NegativeMoneyException( ); + } + _amount = amount; + _currency = currency; + } + + public double Amount { + get { return _amount; } + } + + public ICurrency TypeOfCurrency { + get { return _currency; } + } + + public IMoney Add( IMoney other ) { + if ( other != null ) { + if ( other.TypeOfCurrency.Equals( TypeOfCurrency ) ) { + return new Money( other.Amount + Amount ); + } + throw new CannotAddMoniesException( "Cannot add monies of different currency" ); + } + return this; + } + + public IMoney Subtract( IMoney money ) { + return ( money != null ) ? new Money( Amount - money.Amount ) : null; + } + + public override bool Equals( object obj ) { + IMoney other = obj as Money; + if ( other != null ) { + return other.Amount == Amount; + } + return false; + } + + public override int GetHashCode( ) { + return base.GetHashCode( ) + new Random( ( int )DateTime.Now.Ticks ).Next( ); + } + + public override string ToString( ) { + return _amount.ToString( "F" ); + } + + private readonly double _amount; + private readonly ICurrency _currency; + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Properties/AssemblyInfo.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7e3253a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/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.Factory")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DesignPatterns.Factory")] +[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("b2eb7e33-2bbe-4cc0-a003-5ee1c528e811")] + +// 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.Factory/RoyalBank/RoyalBank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBank.cs new file mode 100644 index 0000000..1e234ab --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBank.cs @@ -0,0 +1,9 @@ +namespace DesignPatterns.Factory { + public class RoyalBank : Bank, IBank { + public RoyalBank( ) : base( "Royal Bank" ) {} + + public override IBankAccountFactory GetAccountFactory( ) { + return new RoyalBankAccountFactory( ); + } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs new file mode 100644 index 0000000..5a3c2a4 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs @@ -0,0 +1,13 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + internal class RoyalBankAccountFactory : IBankAccountFactory { + public IBankAccount CreateChequingAccount( ) { + return new RoyalBankChequingAccount( ); + } + + public IBankAccount CreateSavingsAccount( ) { + return new RoyalBankSavingsAccount( ); + } + } +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs new file mode 100644 index 0000000..30c5d2a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + internal class RoyalBankChequingAccount : BankAccount, IBankAccount {} +} \ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs new file mode 100644 index 0000000..06d3534 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test; + +namespace DesignPatterns.Factory { + internal class RoyalBankSavingsAccount : BankAccount, IBankAccount {} +} \ No newline at end of file -- cgit v1.2.3