Skip to content

Instantly share code, notes, and snippets.

@jcaddel
Last active April 30, 2018 17:33
Show Gist options
  • Save jcaddel/11a8ac44383084e5cd74747207e98312 to your computer and use it in GitHub Desktop.
Save jcaddel/11a8ac44383084e5cd74747207e98312 to your computer and use it in GitHub Desktop.
RuleBook
RuleBook<String, String> messages = DefaultRuleBook.<String, String>builder()
.withDefaultResult("?")
.addRule(rule -> rule.when(code -> code.contains("hello"))
.then(message -> "Hello World"))
.addRule(rule -> rule.when(code -> code.contains("goodbye"))
.then(message -> "Goodbye Cruel World"))
.build();
RuleBook<Applicant, Double> rates = DefaultRuleBook.<Applicant, Double>builder()
// default interest rate is 4.5%
.withDefaultResult(4.5)
// if they have a credit score below 600 they must pay 4x the default rate and we are done
.addRule(rule -> rule.when(applicant -> applicant.getCreditScore() < 600)
.then(rate -> rate * 4)
.stop())
// 600-700 pays one point higher
.addRule(rule -> rule.when(applicant -> applicant.getCreditScore() >= 600 && applicant.getCreditScore() < 700)
.then(rate -> rate + 1))
// above 700 + $25k cash on hand gets a 1/4 point reduction
.addRule(rule -> rule.when(applicant -> applicant.getCreditScore() >= 700 && applicant.getCashOnHand() >= 25000)
.then(rate -> rate - 0.25))
// first time home buyers get 20% off of their rate (unless they have a credit score < 600)
.addRule(rule -> rule.when(applicant -> applicant.isFirstTimeHomeBuyer())
.then(rate -> rate * 0.80))
.build();
==================================================================================
Applicant{creditScore=599, cashOnHand=20000.0, firstTimeHomeBuyer=true} -> 18.0%
Applicant{creditScore=650, cashOnHand=20000.0, firstTimeHomeBuyer=true} -> 4.4%
Applicant{creditScore=650, cashOnHand=20000.0, firstTimeHomeBuyer=false} -> 5.5%
Applicant{creditScore=700, cashOnHand=20000.0, firstTimeHomeBuyer=true} -> 3.6%
Applicant{creditScore=700, cashOnHand=20000.0, firstTimeHomeBuyer=false} -> 4.5%
Applicant{creditScore=700, cashOnHand=25000.0, firstTimeHomeBuyer=false} -> 4.25%
Applicant{creditScore=700, cashOnHand=25000.0, firstTimeHomeBuyer=true} -> 3.40%
==================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment