Skip to Content.
Sympa Menu

notify-dpml - svn commit: r2343 - in development/planet/users/niclas/book/atm/simple/accounts/src/main: . net net/dpml net/dpml/book net/dpml/book/atm net/dpml/book/atm/simple net/dpml/book/atm/simple/accounts

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: niclas AT hedhman.org
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r2343 - in development/planet/users/niclas/book/atm/simple/accounts/src/main: . net net/dpml net/dpml/book net/dpml/book/atm net/dpml/book/atm/simple net/dpml/book/atm/simple/accounts
  • Date: Thu, 21 Apr 2005 12:53:39 -0400

Author: niclas AT hedhman.org
Date: Thu Apr 21 12:53:35 2005
New Revision: 2343

Added:
development/planet/users/niclas/book/atm/simple/accounts/src/main/
development/planet/users/niclas/book/atm/simple/accounts/src/main/net/
development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/

development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/

development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/atm/

development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/atm/simple/

development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/atm/simple/accounts/

development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/atm/simple/accounts/SimpleAccountManager.java
Log:


Added:
development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/atm/simple/accounts/SimpleAccountManager.java
==============================================================================
--- (empty file)
+++
development/planet/users/niclas/book/atm/simple/accounts/src/main/net/dpml/book/atm/simple/accounts/SimpleAccountManager.java
Thu Apr 21 12:53:35 2005
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.book.atm.simple.accounts;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.Properties;
+
+import net.dpml.book.atm.BackEndProvider;
+import net.dpml.book.atm.WithdrawalException;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+/** This simple BackEndProvider implementation uses java.util.Properties
+ * files to emulate a backend banking host.
+ *
+ * @author <a href="mailto:niclas AT hedhman.org";>Niclas Hedhman</a>
+ * @metro.component name="simple-accountmanager" lifestyle="singleton"
+ * @metro.service type="net.dpml.book.atm.BackEndProvider"
+ */
+public class SimpleAccountManager
+ implements BackEndProvider
+{
+ private String m_accountsFilename;
+ private Properties m_accounts;
+ private Properties m_pins;
+
+ public SimpleAccountManager( Parameters params )
+ throws ParameterException, IOException
+ {
+ m_accountsFilename = params.getParameter( "accounts-filename" );
+ String pinsFilename = params.getParameter( "pinnumbers-filename" );
+
+ m_accounts = loadProperties( m_accountsFilename );
+ m_pins = loadProperties( pinsFilename );
+ }
+
+ public boolean requestWithdrawal( String account, String pin, String
amount )
+ {
+ if( validate( account, pin ) == false )
+ return false;
+ return haveEnough( account, amount );
+ }
+
+ public void withdraw( String account, String amount )
+ throws WithdrawalException
+ {
+ synchronized( this )
+ {
+ if( haveEnough( account, amount ) == false )
+ throw new WithdrawalException( "Not enough credit for the
account to make withdrawal of " + amount );
+ BigDecimal balance = getBalance( account );
+ BigDecimal withdrawn = convert( amount );
+ BigDecimal newBalance = balance.subtract( withdrawn );
+ m_accounts.setProperty( account, newBalance.toString() );
+ try
+ {
+ saveAccounts();
+ } catch( IOException e )
+ {
+ throw new WithdrawalException( "Withdrawal failed due to
IOException." );
+ }
+ }
+ }
+
+ private boolean validate( String account, String pin )
+ {
+ String correct = m_pins.getProperty( account );
+ return pin.equals( correct );
+ }
+
+ private boolean haveEnough( String account, String amount )
+ {
+ BigDecimal balance = getBalance( account );
+ BigDecimal askingFor = convert( amount );
+ return balance.subtract( askingFor ).floatValue() > 0 ;
+ }
+
+ private BigDecimal getBalance( String account )
+ {
+ String amount = m_accounts.getProperty( account );
+ return convert( amount );
+ }
+
+ private BigDecimal convert( String amount )
+ {
+ return new BigDecimal( amount );
+ }
+
+ private Properties loadProperties( String filename )
+ throws IOException
+ {
+ FileInputStream in = new FileInputStream( filename );
+ Properties props = new Properties();
+ props.load( in );
+ return props;
+ }
+
+ private void saveAccounts()
+ throws IOException
+ {
+ FileOutputStream out = new FileOutputStream( m_accountsFilename );
+ m_accounts.store( out, "Account = amount " );
+ }
+}



  • svn commit: r2343 - in development/planet/users/niclas/book/atm/simple/accounts/src/main: . net net/dpml net/dpml/book net/dpml/book/atm net/dpml/book/atm/simple net/dpml/book/atm/simple/accounts, niclas, 04/21/2005

Archive powered by MHonArc 2.6.24.

Top of Page