public static void main(String[] args) { Team redSox = new RedSox(); Team cubs = new Cubs(); Game game = new BaseballGame(redSox, cubs); System.out.println(game.playGame()); } }
public String toString() { return getName(); } }
public String toString() { return getName(); } }
public BigDecimal deposit(Long id, BigDecimal amount) { Account account = repository.findOne(id); BigDecimal newBalance = account.getBalance().add(amount); account.setBalance(newBalance); repository.save(account); return newBalance; }
@Override public void updateAccount(Account account) { String sqlTxt = "update account set balance = ? where id = ?"; template.update(sqlTxt, account.getBalance(), account.getId()); }
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public BigDecimal getBalance(Long id) { return repository.getAccount(id).getBalance(); }
@Override public Long createAccount(BigDecimal initialBalance) { long id = nextId++; entityManager.persist(new Account(id, initialBalance)); return id; }
@Test public void testPlayGame() throws Exception { String home = game.getHomeTeam().getName(); String away = game.getAwayTeam().getName(); String result = game.playGame(); assertTrue(result.contains(home) || result.contains(away)); }
@Override public void updateAccount(Account account) { accountMap.put(account.getId(), account); } }
public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Game game = context.getBean("game", Game.class); Team royals = context.getBean("royals", Team.class); Team redSox = context.getBean("redSox", Team.class); Team cubs = context.getBean("cubs", Team.class); game.setHomeTeam(royals); game.setAwayTeam(cubs); game.playGame(); game.setHomeTeam(cubs); game.setAwayTeam(redSox); game.playGame(); context.close(); } }
@Test public void testGetAndSetHomeTeam() throws Exception { Team royals = ctx.getBean("royals", Team.class); game.setHomeTeam(royals); assertEquals(royals.getName(), game.getHomeTeam().getName()); } }
@Override public String playGame() { return Math.random() < 0.5 ? getHomeTeam().getName() : getAwayTeam().getName(); }
public String toString() { return getName(); } }
public BigDecimal deposit(Long id, BigDecimal amount) { Account account = repository.getAccount(id); BigDecimal newBalance = account.getBalance().add(amount); account.setBalance(newBalance); repository.updateAccount(account); return newBalance; }
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public BigDecimal getBalance(Long id) { return repository.findOne(id).getBalance(); }
@Override public Long createAccount(BigDecimal initialBalance) { Long id = nextId++; accountMap.put(id, new Account(id, initialBalance)); return id; }
public BigDecimal deposit(Long id, BigDecimal amount) { Account account = repository.getAccount(id); BigDecimal newBalance = account.getBalance().add(amount); account.setBalance(newBalance); repository.updateAccount(account); return newBalance; }
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public BigDecimal getBalance(Long id) { return repository.getAccount(id).getBalance(); }
@Override public Account mapRow(ResultSet resultSet, int i) throws SQLException { return new Account(resultSet.getLong("id"), resultSet.getBigDecimal("balance")); } }