Commit 7e9efdd5 authored by sunye's avatar sunye
Browse files

Add new exercises

parent 52f74988
package fr.unantes.l3.construction.refactorings;
import java.util.Date;
public class AdvisedLineStrategy extends CapitalStrategy {
public AdvisedLineStrategy(Loan loan) {
super(loan);
}
public double duration() {
return 0.0; // Advised Line
}
public double capital() {
return (getLoan().outstandingRiskAmount() * duration() * getLoan().riskFactor())
+ (getLoan().unusedRiskAmount() * duration() * getLoan().unusedRiskFactor());
}
}
package fr.unantes.l3.construction.refactorings;
import java.util.Date;
import java.util.Iterator;
public abstract class CapitalStrategy {
private Loan loan;
public CapitalStrategy() {
}
public CapitalStrategy(Loan loan) {
this.loan = loan;
}
Loan getLoan() {
return loan;
}
double yearsTo(Date endDate) {
Date beginDate = (getLoan().getToday() == null ? getLoan().getStart() : getLoan().getToday());
return ((endDate.getTime() - beginDate.getTime()) / Loan.MILLIS_PER_DAY) / Loan.DAYS_PER_YEAR;
}
public abstract double duration();
public abstract double capital();
}
package fr.unantes.l3.construction.refactorings;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class Loan {
private static String TERM_LOAN = "TL";
private static String REVOLVER = "RC";
private static String RCTL = "RCTL";
private static final int MILLIS_PER_DAY = 86400000;
private static final int DAYS_PER_YEAR = 365;
private String type;
private float notional;
private float outstanding;
private int customerRating;
private Date maturity;
private Date expiry;
private double commitment;
private List<Payment> payments;
private CapitalStrategy strategy;
protected static String TERM_LOAN = "TL";
protected static String REVOLVER = "RC";
protected static String RCTL = "RCTL";
protected static final int MILLIS_PER_DAY = 86400000;
protected static final int DAYS_PER_YEAR = 365;
protected String type;
protected float notional;
protected float outstanding;
protected int customerRating;
protected Date maturity;
protected Date expiry;
protected double commitment;
protected List<Payment> payments;
private Date today;
private Date start;
private int riskRating;
protected int riskRating;
private int rating;
public double capital() {
if (expiry == null && maturity != null) // Term Loan
return commitment * duration() * riskFactor();
if (expiry != null && maturity == null) {
if (getUnusedPercentage() != 1.0) // Revolver
return commitment * getUnusedPercentage() * duration() * riskFactor();
else // Advised Line
return (outstandingRiskAmount() * duration() * riskFactor())
+ (unusedRiskAmount() * duration() * unusedRiskFactor());
private Loan(CapitalStrategy capitalStrategy, double commitment, int riskRating, Date maturity, Date expiry) {
this(capitalStrategy, commitment, 0.00f, riskRating, maturity, expiry);
}
private Loan(CapitalStrategy capitalStrategy, double commitment, float outstanding, int riskRating, Date maturity,
Date expiry) {
this.commitment = commitment;
this.outstanding = outstanding;
this.riskRating = riskRating;
this.maturity = maturity;
this.expiry = expiry;
this.strategy = capitalStrategy;
if (capitalStrategy == null) {
if (expiry == null)
this.strategy = new TermLoanStrategy(this);
else if (maturity == null)
this.strategy = new RevolverStrategy(this);
else
this.strategy = new AdvisedLineStrategy(this);
}
return 0.0;
}
public double duration() {
if (expiry == null && maturity != null)
return weightedAverageDuration();
else if (expiry != null && maturity == null)
return yearsTo(expiry);
return 0.0;
private Loan(Date expiry, Date maturity) {
this.expiry = expiry;
this.maturity = maturity;
if (expiry == null && maturity != null) {
strategy = new TermLoanStrategy(this);
} else if (expiry != null && maturity == null) {
strategy = new RevolverStrategy(this);
} else {
strategy = new AdvisedLineStrategy(this);
}
}
private double outstandingRiskAmount() {
return outstanding;
public static Loan createTermLoan(Date maturity) {
return new Loan(null, 0, 0.00f, 0, maturity, null);
}
private double unusedRiskAmount() {
return (commitment - outstanding);
public static Loan createRevolverLoan(double commitment, int riskRating, Date maturity, Date expiry) {
return new Loan(null, commitment, 0.00f, riskRating, maturity, expiry);
}
public static Loan createAdvisedLineLoan(double commitment, float outstanding,
int riskRating, Date maturity, Date expiry) {
return new Loan(null, commitment, outstanding, riskRating, maturity, expiry);
}
public double capital() {
return strategy.capital();
}
private double weightedAverageDuration() {
double duration = 0.0;
double weightedAverage = 0.0;
double sumOfPayments = 0.0;
Iterator loanPayments = payments.iterator();
while (loanPayments.hasNext()) {
Payment payment = (Payment) loanPayments.next();
sumOfPayments += payment.amount();
weightedAverage += yearsTo(payment.date()) * payment.amount();
}
if (commitment != 0.0)
duration = weightedAverage / sumOfPayments;
return duration;
protected double outstandingRiskAmount() {
return outstanding;
}
private double yearsTo(Date endDate) {
Date beginDate = (today == null ? start : today);
return ((endDate.getTime() - beginDate.getTime()) / MILLIS_PER_DAY) / DAYS_PER_YEAR;
protected double unusedRiskAmount() {
return (commitment - outstanding);
}
private double riskFactor() {
protected double riskFactor() {
return RiskFactor.getFactors().forRating(riskRating);
}
private double unusedRiskFactor() {
protected double unusedRiskFactor() {
return UnusedRiskFactors.getFactors().forRating(riskRating);
}
private double getUnusedPercentage() {
protected double getUnusedPercentage() {
return 0.0;
}
Date getToday() {
return today;
}
Date getStart() {
return start;
}
}
package fr.unantes.l3.construction.refactorings;
public class PermissionGranted extends PermissionState{
public PermissionGranted() {
super(("GRANTED"));
}
}
package fr.unantes.l3.construction.refactorings;
public class PermissionState {
public final static PermissionState REQUESTED = new PermissionState("REQUESTED");
public final static PermissionState CLAIMED = new PermissionState("REQUESTED");
public final static PermissionState GRANTED = new PermissionGranted();
public final static PermissionState DENIED = new PermissionState("DENIED");
private final String name;
public PermissionState(String name) {
this.name = name;
}
}
package fr.unantes.l3.construction.refactorings;
import java.util.Date;
public class RevolverStrategy extends CapitalStrategy {
public RevolverStrategy() {
super();
}
public RevolverStrategy(Loan loan) {
super(loan);
}
public double duration() {
return yearsTo(getLoan().expiry);
}
public double capital() {
return getLoan().commitment * getLoan().getUnusedPercentage() * duration() * getLoan().riskFactor();
}
}
package fr.unantes.l3.construction.refactorings;
public class SystemAdmin {
}
package fr.unantes.l3.construction.refactorings;
/**
* Chapter 7 - Simplification
* Replace state-altering conditionals with state
*
* http://www.industriallogic.com/xp/refactoring/alteringConditionalsWithState.
* html
* http://www.informit.com/articles/article.aspx?p=1398607&seqNum=4
*/
public class SystemPermission {
private SystemProfile profile;
private SystemUser requestor;
private SystemAdmin admin;
private boolean isGranted;
private PermissionState state;
public SystemPermission(SystemUser requestor, SystemProfile profile) {
this.requestor = requestor;
this.profile = profile;
state = PermissionState.REQUESTED;
isGranted = false;
notifyAdminOfPermissionRequest();
}
public void claimedBy(SystemAdmin admin) {
if (!state.equals(PermissionState.REQUESTED)) {
return;
}
willBeHandledBy(admin);
state = PermissionState.CLAIMED;
}
public void deniedBy(SystemAdmin admin) {
if (!state.equals(PermissionState.CLAIMED)) {
return;
}
if (!admin.equals(this.admin)) {
return;
}
isGranted = false;
state = PermissionState.DENIED;
notifyUserOfPermissionRequestResult();
}
public void grantedBy(SystemAdmin admin) {
if (!state.equals(PermissionState.CLAIMED)) {
return;
}
if (!admin.equals(this.admin)) {
return;
}
state = PermissionState.GRANTED;
isGranted = true;
notifyUserOfPermissionRequestResult();
}
private void willBeHandledBy(SystemAdmin admin) {
this.admin = admin;
}
private void notifyUserOfPermissionRequestResult() {
}
private void notifyAdminOfPermissionRequest() {
}
public PermissionState state() {
return state;
}
public boolean isGranted() {
return isGranted;
}
}
package fr.unantes.l3.construction.refactorings;
public class SystemProfile {
}
package fr.unantes.l3.construction.refactorings;
public class SystemUser {
}
package fr.unantes.l3.construction.refactorings;
import java.util.Date;
import java.util.Iterator;
public class TermLoanStrategy extends CapitalStrategy {
public TermLoanStrategy(Loan loan) {
super(loan);
}
public TermLoanStrategy() {
super();
}
public double duration() {
return weightedAverageDuration();
}
public double capital() {
return getLoan().commitment * duration() * getLoan().riskFactor();
}
private double weightedAverageDuration() {
double duration = 0.0;
double weightedAverage = 0.0;
double sumOfPayments = 0.0;
Iterator loanPayments = getLoan().payments.iterator();
while (loanPayments.hasNext()) {
Payment payment = (Payment) loanPayments.next();
sumOfPayments += payment.amount();
weightedAverage += yearsTo(payment.date()) * payment.amount();
}
if (getLoan().commitment != 0.0)
duration = weightedAverage / sumOfPayments;
return duration;
}
}
package fr.unantes.l3.construction.refactorings;
import org.junit.jupiter.api.Test;
import java.util.Date;
class LoanTest {
@Test
public void testTermLoanNoPayments() {
Date maturity = new Date();
Loan termLoan = Loan.createTermLoan(maturity);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment