fixed loan quote Comparable implementation (for "best" quote)

This commit is contained in:
Mark Fisher
2010-06-18 15:13:25 +00:00
parent 1ef2d6a460
commit 75d2add40c
13 changed files with 74 additions and 87 deletions

View File

@@ -1,8 +1,8 @@
Loan Broker sample is distributed as valid Eclipse/Maven project, so you can run it in two ways.
The Loan Broker sample is distributed as a valid Eclipse/Maven project, so you can run it in two ways.
Maven (command line):
1. Execute 'mvn install'
You should see the output that looks similar to this:
You should see output that looks similar to this:
. . . . .
INFO : org.springframework.integration.loanbroker.demo.LoanBrokerDemo -
********* Best Quote:
@@ -17,5 +17,5 @@ Maven (command line):
. . . . .
Eclipse/STS (with m2eclipse plug-in)
1. Import project into Eclipse/STS. If m2eclipse plugin is installed the dependency will be downloaded automatically
2. Run 'org.springframework.integration.loanbroker.demo.LoanBrokerDemo' class located in 'src/test/java'
1. Import project into Eclipse/STS. If the m2eclipse plugin is installed, the dependencies will be downloaded automatically.
2. Run the 'org.springframework.integration.loanbroker.demo.LoanBrokerDemo' class located in 'src/test/java'.

View File

@@ -22,8 +22,8 @@ import org.springframework.integration.loanbroker.domain.LoanQuote;
import org.springframework.integration.loanbroker.domain.LoanRequest;
/**
* POJI Gateway which connects method invocations to the request-channel
* completely isolating SI developer from SI API details.
* POJI Gateway that connects method invocations to the request-channel.
* This completely isolates the developer from Spring Integration API details.
*
* @author Oleg Zhurakousky
*/
@@ -32,7 +32,7 @@ public interface LoanBrokerGateway {
/**
* Will return the best {@link LoanQuote} for the given request.
*/
public LoanQuote getLoanQuote(LoanRequest loanRequest);
public LoanQuote getBestLoanQuote(LoanRequest loanRequest);
/**
* Will return all {@link LoanQuote}s for the given request.

View File

@@ -24,30 +24,30 @@ import org.springframework.integration.core.Message;
import org.springframework.integration.loanbroker.domain.LoanQuote;
/**
* Will aggregate {@link LoanQuote}s based on the value of the 'RESPONSE_TYPE' message header.
* The value of the 'RESPONSE_TYPE' header is set by the 'gateway' and is based on the type of
* {@link LoanBrokerGateway} method that was invoked by the client.
* <p>
* Will return the best {@link LoanQuote} if 'RESPONSE_TYPE' header value is 'BEST' else it will
* return all quotes.
* Aggregates {@link LoanQuote}s based on the value of the 'RESPONSE_TYPE' message header.
* When only the <i>best</i> quote is desired, the 'RESPONSE_TYPE' header should have a value
* of 'BEST'. In this example, that value is set by the 'gateway' when the
* {@link LoanBrokerGateway#getBestLoanQuote(org.springframework.integration.loanbroker.domain.LoanRequest)}
* method is invoked by the client.
*
* @author Oleg Zhurakousky
*/
public class LoanQuoteAggregator {
/**
* Aggregates LoanQuote Messages to return a single reply Message.
*
* @param messages Messages received from upstream lenders.
* @return the best {@link LoanQuote} if the 'RESPONSE_TYPE' header value is 'BEST' else all quotes
*/
public Object aggregateQuotes(List<Message<LoanQuote>> messages) {
ArrayList<LoanQuote> payloads = new ArrayList<LoanQuote>(messages.size());
for (Message<LoanQuote> message : messages) {
payloads.add(message.getPayload());
}
Collections.sort(payloads);
String responseType = messages.get(0).getHeaders().get("RESPONSE_TYPE", String.class);
if ("BEST".equals(responseType)) {
Collections.sort(payloads);
return payloads.get(0);
}
else {
return payloads;
}
return ("BEST".equals(responseType)) ? payloads.get(0) : payloads;
}
}

View File

@@ -27,10 +27,10 @@ public class LoanQuote implements Comparable<LoanQuote>{
private String lender;
private Date quoteDate;
private Date expirationDate;
private double loanAmount;
private int loanTerm;
private double amount;
private int term;
private float rate;
public String getLender() {
return lender;
}
@@ -38,7 +38,7 @@ public class LoanQuote implements Comparable<LoanQuote>{
public void setLender(String lender) {
this.lender = lender;
}
public Date getQuoteDate() {
return quoteDate;
}
@@ -46,56 +46,55 @@ public class LoanQuote implements Comparable<LoanQuote>{
public void setQuoteDate(Date quoteDate) {
this.quoteDate = quoteDate;
}
public Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public double getLoanAmount() {
return loanAmount;
public double getAmount() {
return amount;
}
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
public void setAmount(double amount) {
this.amount = amount;
}
public int getLoanTerm() {
return loanTerm;
public int getTerm() {
return term;
}
public void setLoanTerm(int loanTerm) {
this.loanTerm = loanTerm;
public void setTerm(int term) {
this.term = term;
}
public float getRate() {
return rate;
}
public void setRate(float rate) {
this.rate = rate;
}
public String toString(){
return "\n====== Loan Quote =====\n" +
"Lender: " + lender + "\n" +
"Loan amount: " + new DecimalFormat("$###,###.###").format(loanAmount) + "\n" +
"Quotation Date: " + quoteDate + "\n" +
"Expiration Date: " + expirationDate + "\n" +
"Term: " + loanTerm + " years" + "\n" +
"Rate: " + rate + "%\n" +
"Lender: " + this.lender + "\n" +
"Loan amount: " + new DecimalFormat("$###,###.###").format(this.amount) + "\n" +
"Quote Date: " + this.quoteDate + "\n" +
"Expiration Date: " + this.expirationDate + "\n" +
"Term: " + this.term + " years" + "\n" +
"Rate: " + this.rate + "%\n" +
"=======================\n";
}
public int compareTo(LoanQuote loanQuote) {
if (loanQuote.rate > this.rate) {
public int compareTo(LoanQuote other) {
if (this.rate > other.rate) {
return 1;
}
else if (loanQuote.rate < this.rate) {
else if (this.rate < other.rate) {
return -1;
}
return 0;

View File

@@ -31,7 +31,7 @@ public class BankStub {
* @param loanRequest the loan request
* @return a LoanQuote for the given request
*/
public LoanQuote quote(LoanRequest loanRequest){
public LoanQuote quote(LoanRequest loanRequest) {
Calendar calendar = Calendar.getInstance();
LoanQuote loanQuote = new LoanQuote();
Random random = new Random();
@@ -39,8 +39,8 @@ public class BankStub {
calendar.add(Calendar.DAY_OF_YEAR, random.nextInt(25));
loanQuote.setExpirationDate(calendar.getTime());
loanQuote.setRate(random.nextFloat() + 5);
loanQuote.setLoanTerm(10 + random.nextInt(10));
loanQuote.setLoanAmount(250000 + random.nextInt(40000));
loanQuote.setTerm(10 + random.nextInt(10));
loanQuote.setAmount(250000 + random.nextInt(40000));
loanQuote.setLender("StubBank-" + random.nextInt(30));
return loanQuote;
}

View File

@@ -36,7 +36,7 @@ public class CreditBureauStub {
*/
public CreditScore getCreditScore(LoanRequest loanRequest){
Random random = new Random();
int creditScore = 750 + random.nextInt(100);
int creditScore = 700 + random.nextInt(150);
logger.info("Credit Score: " + creditScore);
return new CreditScore(creditScore);
}

View File

@@ -2,8 +2,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:map id="banks">
<entry key="abcBankChannel" value="premier" />
@@ -12,5 +12,5 @@
<entry key="xyzBankChannel" value="secondary" />
<entry key="fooBankChannel" value="secondary" />
</util:map>
</beans>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:stub-services-config.xml" />
<import resource="classpath:loan-broker-config.xml" />

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:stub-services-config.xml" />
<import resource="classpath:loan-broker-config.xml" />

View File

@@ -13,7 +13,7 @@
<gateway id="loanBrokerGateway"
default-request-channel="loanBrokerPreProcessingChannel"
service-interface="org.springframework.integration.loanbroker.LoanBrokerGateway">
<method name="getLoanQuote">
<method name="getBestLoanQuote">
<header name="RESPONSE_TYPE" value="BEST"/>
</method>
</gateway>

View File

@@ -2,8 +2,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<bean id="creditBureau" class="org.springframework.integration.loanbroker.stubs.CreditBureauStub" />

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.loanbroker.demo;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.loanbroker.LoanBrokerGateway;
import org.springframework.integration.loanbroker.domain.Customer;
@@ -28,39 +30,28 @@ import org.springframework.integration.loanbroker.domain.LoanRequest;
/**
* @author Oleg Zhurakousky
*
*/
public class LoanBrokerDemo {
private static Logger logger = Logger.getLogger(LoanBrokerDemo.class);
/**
* @param args
*/
public static void main(String[] args) {
new LoanBrokerDemo().runDemo();
}
/**
*
*/
@Test
public void runDemo() {
ConfigurableApplicationContext ac =
new ClassPathXmlApplicationContext("bootstrap-config/stubbed-loan-broker.xml");
LoanBrokerGateway broker = ac.getBean("loanBrokerGateway", LoanBrokerGateway.class);
ApplicationContext context = new ClassPathXmlApplicationContext("bootstrap-config/stubbed-loan-broker.xml");
LoanBrokerGateway broker = context.getBean("loanBrokerGateway", LoanBrokerGateway.class);
LoanRequest loanRequest = new LoanRequest();
loanRequest.setCustomer(new Customer());
LoanQuote loan = broker.getLoanQuote(loanRequest);
LoanQuote loan = broker.getBestLoanQuote(loanRequest);
logger.info("\n********* Best Quote: " + loan);
List<LoanQuote> loanQuotes = broker.getAllLoanQuotes(loanRequest);
logger.info("\n********* All Quotes: ");
for (LoanQuote loanQuote : loanQuotes) {
logger.info(loanQuote);
}
ac.close();
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.integration.loanbroker.domain.LoanRequest;
* @author Gary Russell
*/
public class LoanBrokerSharkDetectorDemo {
private static Logger logger = Logger.getLogger(LoanBrokerSharkDetectorDemo.class);
@Test
@@ -39,19 +40,15 @@ public class LoanBrokerSharkDetectorDemo {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("bootstrap-config/stubbed-loan-broker-multicast.xml");
LoanBrokerGateway broker = context.getBean("loanBrokerGateway", LoanBrokerGateway.class);
LoanRequest loanRequest = new LoanRequest();
loanRequest.setCustomer(new Customer());
LoanQuote loan = broker.getLoanQuote(loanRequest);
LoanQuote loan = broker.getBestLoanQuote(loanRequest);
logger.info("\n********* Best Quote: " + loan);
List<LoanQuote> loanQuotes = broker.getAllLoanQuotes(loanRequest);
logger.info("\n********* All Quotes: ");
for (LoanQuote loanQuote : loanQuotes) {
logger.info(loanQuote);
}
context.close();
}
}