kartik sanghavi

Greenhorn
+ Follow
since Aug 13, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by kartik sanghavi

I am new to spring!!

I have got the source code of spring recipes book by gary mark.

In an application of bank I am trying to alter the Account class, AccountService interface and the AccountServiceImpl class to allow the setting of a credit limit on accounts and for withdrawals to be allowed up to that credit limit.

Plus I have to Alter the JUnit test in AccountServiceTests to test this new functionality.

How can I do this?

The original files are as under:

Account.java

package com.apress.springrecipes.bank;

public class Account {

private String accountNo;
private double balance;

public Account() {}

public Account(String accountNo, double balance) {
this.accountNo = accountNo;
this.balance = balance;
}

public String getAccountNo() {
return accountNo;
}

public double getBalance() {
return balance;
}

public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}

public void setBalance(double balance) {
this.balance = balance;
}

public boolean equals(Object obj) {
if (!(obj instanceof Account)) {
return false;
}
Account account = (Account) obj;
return account.accountNo.equals(accountNo) && account.balance == balance;
}
}






AccountService

package com.apress.springrecipes.bank;

public interface AccountService {

public void createAccount(String accountNo);
public void removeAccount(String accountNo);
public void deposit(String accountNo, double amount);
public void withdraw(String accountNo, double amount);
public double getBalance(String accountNo);
}



AccountServiceImpl

package com.apress.springrecipes.bank;

public class AccountServiceImpl implements AccountService {

private AccountDao accountDao;

public AccountServiceImpl(AccountDao accountDao) {
this.accountDao = accountDao;
}

public void createAccount(String accountNo) {
accountDao.createAccount(new Account(accountNo, 0));
}

public void removeAccount(String accountNo) {
Account account = accountDao.findAccount(accountNo);
accountDao.removeAccount(account);
}

public void deposit(String accountNo, double amount) {
Account account = accountDao.findAccount(accountNo);
account.setBalance(account.getBalance() + amount);
accountDao.updateAccount(account);
}

public void withdraw(String accountNo, double amount) {
Account account = accountDao.findAccount(accountNo);
if (account.getBalance() < amount) {
throw new InsufficientBalanceException();
}
account.setBalance(account.getBalance() - amount);
accountDao.updateAccount(account);
}

public double getBalance(String accountNo) {
return accountDao.findAccount(accountNo).getBalance();
}
}



AccountServiceTests

package com.apress.springrecipes.bank;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AccountServiceTests {

private static final String TEST_ACCOUNT_NO = "1234";
private AccountService accountService;

@Before
public void init() {
accountService = new AccountServiceImpl(new InMemoryAccountDao());
accountService.createAccount(TEST_ACCOUNT_NO);
accountService.deposit(TEST_ACCOUNT_NO, 100);
}

@Test
public void deposit() {
accountService.deposit(TEST_ACCOUNT_NO, 50);
assertEquals(accountService.getBalance(TEST_ACCOUNT_NO), 150, 0);
}

@Test
public void withDraw() {
accountService.withdraw(TEST_ACCOUNT_NO, 50);
assertEquals(accountService.getBalance(TEST_ACCOUNT_NO), 50, 0);
}

@After
public void cleanup() {
accountService.removeAccount(TEST_ACCOUNT_NO);
}
}


Thanks!!
14 years ago
I am new to spring!!

I have got the source code of spring recipes book by gary mark.

In an application of bank I am trying to alter the Account class, AccountService interface and the AccountServiceImpl class to allow the setting of a credit limit on accounts and for withdrawals to be allowed up to that credit limit.

Plus I have to Alter the JUnit test in AccountServiceTests to test this new functionality.

How can I do this?

The original files are as under:

Account.java

package com.apress.springrecipes.bank;

public class Account {

private String accountNo;
private double balance;

public Account() {}

public Account(String accountNo, double balance) {
this.accountNo = accountNo;
this.balance = balance;
}

public String getAccountNo() {
return accountNo;
}

public double getBalance() {
return balance;
}

public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}

public void setBalance(double balance) {
this.balance = balance;
}

public boolean equals(Object obj) {
if (!(obj instanceof Account)) {
return false;
}
Account account = (Account) obj;
return account.accountNo.equals(accountNo) && account.balance == balance;
}
}






AccountService

package com.apress.springrecipes.bank;

public interface AccountService {

public void createAccount(String accountNo);
public void removeAccount(String accountNo);
public void deposit(String accountNo, double amount);
public void withdraw(String accountNo, double amount);
public double getBalance(String accountNo);
}



AccountServiceImpl

package com.apress.springrecipes.bank;

public class AccountServiceImpl implements AccountService {

private AccountDao accountDao;

public AccountServiceImpl(AccountDao accountDao) {
this.accountDao = accountDao;
}

public void createAccount(String accountNo) {
accountDao.createAccount(new Account(accountNo, 0));
}

public void removeAccount(String accountNo) {
Account account = accountDao.findAccount(accountNo);
accountDao.removeAccount(account);
}

public void deposit(String accountNo, double amount) {
Account account = accountDao.findAccount(accountNo);
account.setBalance(account.getBalance() + amount);
accountDao.updateAccount(account);
}

public void withdraw(String accountNo, double amount) {
Account account = accountDao.findAccount(accountNo);
if (account.getBalance() < amount) {
throw new InsufficientBalanceException();
}
account.setBalance(account.getBalance() - amount);
accountDao.updateAccount(account);
}

public double getBalance(String accountNo) {
return accountDao.findAccount(accountNo).getBalance();
}
}



AccountServiceTests

package com.apress.springrecipes.bank;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AccountServiceTests {

private static final String TEST_ACCOUNT_NO = "1234";
private AccountService accountService;

@Before
public void init() {
accountService = new AccountServiceImpl(new InMemoryAccountDao());
accountService.createAccount(TEST_ACCOUNT_NO);
accountService.deposit(TEST_ACCOUNT_NO, 100);
}

@Test
public void deposit() {
accountService.deposit(TEST_ACCOUNT_NO, 50);
assertEquals(accountService.getBalance(TEST_ACCOUNT_NO), 150, 0);
}

@Test
public void withDraw() {
accountService.withdraw(TEST_ACCOUNT_NO, 50);
assertEquals(accountService.getBalance(TEST_ACCOUNT_NO), 50, 0);
}

@After
public void cleanup() {
accountService.removeAccount(TEST_ACCOUNT_NO);
}
}




Thanks!
14 years ago
I am new to spring!!

I have got the source code of spring recipes book by gary mark.

In an application of bank I am trying to alter the Account class, AccountService interface and the AccountServiceImpl class to allow the setting of a credit limit on accounts and for withdrawals to be allowed up to that credit limit.

Plus I have to Alter the JUnit test in AccountServiceTests to test this new functionality.

How can I do this?

The original files are as under:

Account.java

14 years ago