Skip to content

Commit 2e4728c

Browse files
committed
refactor: remove Bank model and integrate services for account management
1 parent 04938bb commit 2e4728c

14 files changed

Lines changed: 123 additions & 198 deletions

File tree

src/main/java/io/codeforall/bootcamp/javabank/App.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package io.codeforall.bootcamp.javabank;
22

33
import io.codeforall.bootcamp.javabank.controller.LoginController;
4-
import io.codeforall.bootcamp.javabank.model.Bank;
54

65
public class App {
76

8-
private Bank bank;
9-
107
public static void main(String[] args) {
118

129
App app = new App();
@@ -16,9 +13,9 @@ public static void main(String[] args) {
1613
private void bootStrap() {
1714

1815
Bootstrap bootstrap = new Bootstrap();
19-
bank = bootstrap.generateTestData();
16+
bootstrap.generateTestData();
2017

21-
LoginController loginController = bootstrap.wireObjects(bank);
18+
LoginController loginController = bootstrap.wireObjects();
2219

2320
// start application
2421
loginController.init();

src/main/java/io/codeforall/bootcamp/javabank/Bootstrap.java

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import io.codeforall.bootcamp.javabank.controller.*;
44
import io.codeforall.bootcamp.javabank.controller.transaction.DepositController;
55
import io.codeforall.bootcamp.javabank.controller.transaction.WithdrawalController;
6-
import io.codeforall.bootcamp.javabank.model.Bank;
76
import io.codeforall.bootcamp.javabank.model.Customer;
7+
import io.codeforall.bootcamp.javabank.services.AccountService;
88
import io.codeforall.bootcamp.javabank.services.AccountServiceImpl;
9+
import io.codeforall.bootcamp.javabank.services.AuthService;
910
import io.codeforall.bootcamp.javabank.services.AuthServiceImpl;
11+
import io.codeforall.bootcamp.javabank.services.CustomerService;
1012
import io.codeforall.bootcamp.javabank.services.CustomerServiceImpl;
1113
import io.codeforall.bootcamp.javabank.view.*;
1214
import org.academiadecodigo.bootcamp.Prompt;
@@ -19,39 +21,32 @@
1921
*/
2022
public class Bootstrap {
2123

24+
private CustomerService customerService;
25+
private AccountService accountService;
26+
private AuthService authService;
27+
2228
/**
23-
* Creates a {@code Bank} and populates it with data
24-
*
25-
* @return the bank
29+
* Initializes services and populates test data
2630
*/
27-
public Bank generateTestData() {
28-
29-
Bank bank = new Bank();
30-
CustomerServiceImpl customerService = new CustomerServiceImpl();
31-
AccountServiceImpl accountService = new AccountServiceImpl();
32-
AuthServiceImpl authService = new AuthServiceImpl(customerService);
33-
34-
bank.setCustomerService(customerService);
35-
bank.setAccountService(accountService);
36-
bank.setAuthService(authService);
31+
public void generateTestData() {
32+
customerService = new CustomerServiceImpl();
33+
accountService = new AccountServiceImpl();
34+
authService = new AuthServiceImpl(customerService);
3735

3836
Customer c1 = new Customer(1, "Rui");
3937
Customer c2 = new Customer(2, "Sergio");
4038
Customer c3 = new Customer(3, "Bruno");
41-
bank.addCustomer(c1);
42-
bank.addCustomer(c2);
43-
bank.addCustomer(c3);
44-
45-
return bank;
39+
customerService.add(c1);
40+
customerService.add(c2);
41+
customerService.add(c3);
4642
}
4743

4844
/**
4945
* Wires the necessary object dependencies
5046
*
51-
* @param bank the bank to wire
5247
* @return the login controller
5348
*/
54-
public LoginController wireObjects(Bank bank) {
49+
public LoginController wireObjects() {
5550

5651
// attach all input to standard i/o
5752
Prompt prompt = new Prompt(System.in, System.out);
@@ -60,15 +55,16 @@ public LoginController wireObjects(Bank bank) {
6055
LoginController loginController = new LoginController();
6156
LoginView loginView = new LoginView();
6257
loginController.setView(loginView);
63-
loginController.setBank(bank);
64-
loginView.setBank(bank);
58+
loginController.setAuthService(authService);
59+
loginView.setCustomerService(customerService);
6560
loginView.setLoginController(loginController);
6661
loginView.setPrompt(prompt);
6762

6863
// wire main controller and view
6964
MainController mainController = new MainController();
7065
MainView mainView = new MainView();
71-
mainView.setBank(bank);
66+
mainView.setCustomerService(customerService);
67+
mainView.setAuthService(authService);
7268
mainView.setPrompt(prompt);
7369
mainView.setMainController(mainController);
7470
mainController.setView(mainView);
@@ -78,12 +74,13 @@ public LoginController wireObjects(Bank bank) {
7874
BalanceController balanceController = new BalanceController();
7975
BalanceView balanceView = new BalanceView();
8076
balanceController.setView(balanceView);
81-
balanceView.setBank(bank);
77+
balanceView.setAuthService(authService);
8278

8379
// wire new account controller and view
8480
NewAccountView newAccountView = new NewAccountView();
8581
NewAccountController newAccountController = new NewAccountController();
86-
newAccountController.setBank(bank);
82+
newAccountController.setAccountService(accountService);
83+
newAccountController.setAuthService(authService);
8784
newAccountController.setView(newAccountView);
8885
newAccountView.setNewAccountController(newAccountController);
8986

@@ -92,14 +89,14 @@ public LoginController wireObjects(Bank bank) {
9289
WithdrawalController withdrawalController = new WithdrawalController();
9390
AccountTransactionView depositView = new AccountTransactionView();
9491
AccountTransactionView withdrawView = new AccountTransactionView();
95-
depositController.setBank(bank);
92+
depositController.setAccountService(accountService);
9693
depositController.setView(depositView);
97-
withdrawalController.setBank(bank);
94+
withdrawalController.setAccountService(accountService);
9895
withdrawalController.setView(withdrawView);
99-
depositView.setBank(bank);
96+
depositView.setAuthService(authService);
10097
depositView.setPrompt(prompt);
10198
depositView.setTransactionController(depositController);
102-
withdrawView.setBank(bank);
99+
withdrawView.setAuthService(authService);
103100
withdrawView.setPrompt(prompt);
104101
withdrawView.setTransactionController(withdrawalController);
105102

src/main/java/io/codeforall/bootcamp/javabank/controller/LoginController.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.codeforall.bootcamp.javabank.controller;
22

3-
import io.codeforall.bootcamp.javabank.model.Bank;
43
import io.codeforall.bootcamp.javabank.services.AuthService;
54
import io.codeforall.bootcamp.javabank.view.LoginView;
65

@@ -10,15 +9,15 @@
109
public class LoginController extends AbstractController {
1110

1211
private Controller nextController;
13-
private Bank bank;
12+
private AuthService authService;
1413

1514
/**
16-
* Sets the bank
15+
* Sets the auth service
1716
*
18-
* @param bank the bank to set
17+
* @param authService the auth service to set
1918
*/
20-
public void setBank(Bank bank) {
21-
this.bank = bank;
19+
public void setAuthService(AuthService authService) {
20+
this.authService = authService;
2221
}
2322

2423
/**
@@ -36,7 +35,7 @@ public void setNextController(Controller nextController) {
3635
* @param id the customer id
3736
*/
3837
public void onLogin(int id) {
39-
bank.getAuthService().authenticate(id);
38+
authService.authenticate(id);
4039
nextController.init();
4140
}
4241
}

src/main/java/io/codeforall/bootcamp/javabank/controller/NewAccountController.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.codeforall.bootcamp.javabank.controller;
22

33
import io.codeforall.bootcamp.javabank.factories.AccountFactory;
4-
import io.codeforall.bootcamp.javabank.model.Bank;
54
import io.codeforall.bootcamp.javabank.model.account.Account;
65
import io.codeforall.bootcamp.javabank.model.account.AccountType;
6+
import io.codeforall.bootcamp.javabank.services.AccountService;
77
import io.codeforall.bootcamp.javabank.services.AuthService;
88
import io.codeforall.bootcamp.javabank.view.NewAccountView;
99

@@ -12,17 +12,27 @@
1212
*/
1313
public class NewAccountController extends AbstractController {
1414

15-
private Bank bank;
15+
private AccountService accountService;
16+
private AuthService authService;
1617
private Integer newAccountId;
1718
private AccountFactory accountFactory = new AccountFactory();
1819

1920
/**
20-
* Sets the bank
21+
* Sets the account service
2122
*
22-
* @param bank the bank to set
23+
* @param accountService the account service to set
2324
*/
24-
public void setBank(Bank bank) {
25-
this.bank = bank;
25+
public void setAccountService(AccountService accountService) {
26+
this.accountService = accountService;
27+
}
28+
29+
/**
30+
* Sets the auth service
31+
*
32+
* @param authService the auth service to set
33+
*/
34+
public void setAuthService(AuthService authService) {
35+
this.authService = authService;
2636
}
2737

2838
/**
@@ -47,8 +57,8 @@ public void init() {
4757

4858
private int createAccount() {
4959
Account newAccount = accountFactory.createAccount(AccountType.CHECKING);
50-
bank.getAccountService().add(newAccount);
51-
bank.getAuthService().getAccessingCustomer().addAccount(newAccount);
60+
accountService.add(newAccount);
61+
authService.getAccessingCustomer().addAccount(newAccount);
5262
return newAccount.getId();
5363
}
5464
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.codeforall.bootcamp.javabank.controller.transaction;
22

33
import io.codeforall.bootcamp.javabank.controller.AbstractController;
4-
import io.codeforall.bootcamp.javabank.model.Bank;
4+
import io.codeforall.bootcamp.javabank.services.AccountService;
55

66
/**
77
* A generic account transaction controller to be used as a base for concrete transaction controller implementations
@@ -10,14 +10,14 @@
1010
*/
1111
public abstract class AbstractAccountTransactionController extends AbstractController implements AccountTransactionController {
1212

13-
protected Bank bank;
13+
protected AccountService accountService;
1414

1515
/**
16-
* Sets the bank
16+
* Sets the account service
1717
*
18-
* @param bank the bank to set
18+
* @param accountService the account service to set
1919
*/
20-
public void setBank(Bank bank) {
21-
this.bank = bank;
20+
public void setAccountService(AccountService accountService) {
21+
this.accountService = accountService;
2222
}
2323
}

src/main/java/io/codeforall/bootcamp/javabank/controller/transaction/DepositController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class DepositController extends AbstractAccountTransactionController {
1313
*/
1414
@Override
1515
public void submitTransaction(int accountId, double amount) {
16-
bank.getAccountService().deposit(accountId, amount);
16+
accountService.deposit(accountId, amount);
1717
}
1818
}

src/main/java/io/codeforall/bootcamp/javabank/controller/transaction/WithdrawalController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class WithdrawalController extends AbstractAccountTransactionController {
1313
*/
1414
@Override
1515
public void submitTransaction(int accountId, double amount) {
16-
bank.getAccountService().withdraw(accountId, amount);
16+
accountService.withdraw(accountId, amount);
1717
}
1818
}

src/main/java/io/codeforall/bootcamp/javabank/model/Bank.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)