M src/main/java/DBObjects/CalculatedObjects/CurrencyFigure.java +4 -1
@@ 36,7 36,10 @@ public class CurrencyFigure {
public CurrencyFigure convertTo(String resultCurrency) throws CurrencyConversionException {
ExchangeRates rates = ExchangeRates.getExchangeOptions(new ExchangeRateKey(currency, year));
if (rates == null) {
- throw new CurrencyConversionException(currency, resultCurrency, year, "Could not find applicable rates data");
+ rates = ExchangeRates.getInterpolatedExchangeOptions(new ExchangeRateKey(currency, year));
+ if (rates == null) { //if they're still null, they're very null indeed!
+ throw new CurrencyConversionException(currency, resultCurrency, year, "Could not find applicable rates data");
+ }
}
return rates.convertTo(resultCurrency, this, new ArrayList<>());
}
M src/main/java/DBObjects/ExchangeRates.java +31 -0
@@ 90,6 90,37 @@ public class ExchangeRates {
return exchangeRateOptions.get(key);
}
+ public static ExchangeRates getInterpolatedExchangeOptions(ExchangeRateKey key) {
+ if (exchangeRateOptions.containsKey(key)) {
+ return exchangeRateOptions.get(key);
+ }
+ //Find before
+ int olderYear = key.getYear();
+ ExchangeRateKey olderKey = key;
+ do {
+ olderYear--;
+ olderKey = new ExchangeRateKey(key.getCurrency(), olderYear);
+ } while (!exchangeRateOptions.containsKey(olderKey) && olderYear > 1895);
+
+ //Find after
+ int newerYear = key.getYear();
+ ExchangeRateKey newerKey = key;
+ do {
+ newerYear--;
+ newerKey = new ExchangeRateKey(key.getCurrency(), newerYear);
+ } while (!exchangeRateOptions.containsKey(olderKey) && newerYear < 2050);
+
+ if (olderYear <= 1896 || newerYear >= 2050) {
+ return null;
+ }
+
+ //If we got here, we can interpolate
+ ExchangeRates olderRates = exchangeRateOptions.get(olderKey);
+ ExchangeRates newerRates = exchangeRateOptions.get(newerKey);
+ //TODO: Interpolate
+ return olderRates;
+ }
+
//Gets the next level deep of conversion options.
//E.g. we started with NOK in 1900, and got a list of options, which contains GBP, BEF, FRF, SEK, NLG, and DEM.
//But we want USD. So we get the next level down.
M src/main/java/com/ajtjp/gearCity/Main.java +1 -1
@@ 53,7 53,7 @@ public class Main {
//Prior to that, it might decrease when we load EE1 cities from the DB
static int nextXMLID = 125;
- final static int[] years = { 1900, 1910, 1920, 1930 };
+ final static int[] years = { 1900, 1910, 1920, 1930, 1940 };
private static DecimalFormat percentFormat = new DecimalFormat("0.00");