summaryrefslogtreecommitdiff
path: root/spring-framework/07-bank-jdbc-ds-transaction/src/main/java/com/example/spring/bank/BankApp.java
blob: 26b6eaf5582ea9b811cca50d177c1cb289a87909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.example.spring.bank;

import java.math.BigDecimal;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BankApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"classpath:META-INF/spring/applicationContext.xml"});

		// Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.
		// Delegates to doClose() for the actual closing procedure.
		// This will trigger the destroy-method of the DataSource in our application (for example, in case of unexpected JVM shutdown).
		ctx.registerShutdownHook();
		
		AccountManager am = ctx.getBean("accountManager", AccountManager.class);
				
		// Create an account
		System.out.println("Creating a new account...");
		Account account = am.create();

		System.out.println("Account number: " + account.getAccountNumber());
		System.out.println("Balance: " + account.getBalance());
		
		// Deposit some amount
		System.out.println("Depositing 200.0...");
		account = am.deposit(account.getAccountNumber(), new BigDecimal("200.0"));
		System.out.println("New balance: " + account.getBalance());
		
		
//		am.chargeForLowBalance(150, 10);
		
		System.out.println("Withdrawing 50.0...");
		account = am.withdraw(account.getAccountNumber(), new BigDecimal("50.0"));
		System.out.println("New balance: " + account.getBalance());
		
		// Create another account
		Account account2 = am.create();
		// Try transfer
		System.out.println("Trying transfer, 1st account balance: " + account.getBalance());
		account = am.transfer(account.getAccountNumber(), account2.getAccountNumber(), new BigDecimal("10.00"));
		System.out.println("After transfer, 1st account balance: " + account.getBalance());
		
		
		System.out.println("Trying transfer to possibly non-existing account, 1st account balance: " + account.getBalance());
		try {
		account = am.transfer(account.getAccountNumber(), 5000, new BigDecimal("10.00"));
		} catch (Exception e) {
			System.err.println("An error occurred!");
			e.printStackTrace();
		}
		
		account = am.find(account.getAccountNumber());
		System.out.println("After transfer, 1st account balance: " + account.getBalance());
		
		ctx.close();		
	}

}