# HG changeset patch # User Andrew # Date 1661660178 14400 # Sun Aug 28 00:16:18 2022 -0400 # Node ID 7c6675c4840b714813a1e17c659055bf4960da5a # Parent ff51b2d7a71d472e256085d5d9b354d0d337e9b3 Progress towards interpolating exchange rates; for now it just uses the "before" figure. Still, that allows calculation of approximate values. Which does much good for Spain's recovering economy from 1940 to 1950. 1940 is now enabled. diff --git a/src/main/java/DBObjects/CalculatedObjects/CurrencyFigure.java b/src/main/java/DBObjects/CalculatedObjects/CurrencyFigure.java --- a/src/main/java/DBObjects/CalculatedObjects/CurrencyFigure.java +++ b/src/main/java/DBObjects/CalculatedObjects/CurrencyFigure.java @@ -36,7 +36,10 @@ 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<>()); } diff --git a/src/main/java/DBObjects/ExchangeRates.java b/src/main/java/DBObjects/ExchangeRates.java --- a/src/main/java/DBObjects/ExchangeRates.java +++ b/src/main/java/DBObjects/ExchangeRates.java @@ -90,6 +90,37 @@ 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. diff --git a/src/main/java/com/ajtjp/gearCity/Main.java b/src/main/java/com/ajtjp/gearCity/Main.java --- a/src/main/java/com/ajtjp/gearCity/Main.java +++ b/src/main/java/com/ajtjp/gearCity/Main.java @@ -53,7 +53,7 @@ //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");