Skip to content

Instantly share code, notes, and snippets.

@kunxin-chor
Created August 7, 2024 13:17
Show Gist options
  • Save kunxin-chor/5c274a9dff1f2e8031e27ce2f9e9f7cf to your computer and use it in GitHub Desktop.
Save kunxin-chor/5c274a9dff1f2e8031e27ce2f9e9f7cf to your computer and use it in GitHub Desktop.
Interface Example
// Tradable interface
interface Tradable {
double getCurrentMarketPrice();
void executeTransaction(boolean isBuy, int quantity);
}
// Insurable interface
interface Insurable {
double calculatePremium();
void fileClaim(String claimDescription);
}
// Concrete class for Tradable (not a financial product)
class Commodity implements Tradable {
private String name;
private String unit;
private double pricePerUnit;
public Commodity(String name, String unit, double pricePerUnit) {
this.name = name;
this.unit = unit;
this.pricePerUnit = pricePerUnit;
}
@Override
public double getCurrentMarketPrice() {
return pricePerUnit;
}
@Override
public void executeTransaction(boolean isBuy, int quantity) {
System.out.println((isBuy ? "Bought " : "Sold ") + quantity + " " + unit + " of " + name);
}
}
// Concrete class for Insurable (not a financial product)
class RealEstate implements Insurable {
private String address;
private double propertyValue;
private boolean isCommercial;
public RealEstate(String address, double propertyValue, boolean isCommercial) {
this.address = address;
this.propertyValue = propertyValue;
this.isCommercial = isCommercial;
}
@Override
public double calculatePremium() {
// Base premium is 0.5% of property value
double basePremium = propertyValue * 0.005;
// Commercial properties have a 20% higher premium
return isCommercial ? basePremium * 1.2 : basePremium;
}
@Override
public void fileClaim(String claimDescription) {
System.out.println("Filed claim for property at " + address + ": " + claimDescription);
}
}
// Example of how to add these interfaces to FinancialProducts
class EnhancedMutualFund extends MutualFund implements Tradable, Insurable {
private double currentPrice;
public EnhancedMutualFund(String productName, double historicalReturn, double minimumInvestment,
String fundType, double expenseRatio, double currentPrice) {
super(productName, historicalReturn, minimumInvestment, fundType, expenseRatio);
this.currentPrice = currentPrice;
}
@Override
public double getCurrentMarketPrice() {
return currentPrice;
}
@Override
public void executeTransaction(boolean isBuy, int quantity) {
System.out.println((isBuy ? "Bought " : "Sold ") + quantity + " units of " + productName);
}
@Override
public double calculatePremium() {
return minimumBalance * 0.02; // 2% of minimum balance as premium
}
@Override
public void fileClaim(String claimDescription) {
System.out.println("Filed claim for " + productName + ": " + claimDescription);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment