Skip to content

Instantly share code, notes, and snippets.

@helospark
Last active October 9, 2019 19:24
Show Gist options
  • Save helospark/9145d3f6fd6afb7f93707b626a1f6ed8 to your computer and use it in GitHub Desktop.
Save helospark/9145d3f6fd6afb7f93707b626a1f6ed8 to your computer and use it in GitHub Desktop.
Simulates return and results on the SunExchange
package com.helospark.invest;
import java.util.ArrayList;
import java.util.List;
/**
* Simulates return and results on the SunExchange.
*/
public class SunExchangeCalculator {
private static final double ZAR_TO_USD = 0.066;
private static final int CELL_LIFETIME = 20;
private static final double PRICE_PER_CELL_ZAR = 78.0;
private static final double WATT_PER_CELL = 5;
private static double INITIAL_INVESTEMENT_USD = 1000.0;
private static double ADDITIONAL_INVESTEMENT_PER_YEAR = 0.0; // add this investement every year
private static int SIMULATION_YEARS = 30;
private static int REINVESTEMENT_UNTIL_YEAR = 30; // reinvest all earnings from the investement back into thesunexchange until this years
private static double START_EARNINGS_IN_ZAR_PER_CELL = 5;
private static final double INTEREST_INCREASE_PER_YEAR = 1.075;
static double money = 0.0;
static List<Integer> numberOfCellsPerYear = new ArrayList<>();
public static void main(String[] args) {
int numberOfCellsForFirstYear = (int) ((INITIAL_INVESTEMENT_USD / ZAR_TO_USD) / PRICE_PER_CELL_ZAR);
numberOfCellsPerYear.add(numberOfCellsForFirstYear);
System.out.printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\n", "Year", "Sum", "Earning", "# Cells", "Power capacity");
for (int year = 0; year < SIMULATION_YEARS; ++year) {
double fullEarningPerYear = 0.0;
int startIndex = numberOfCellsPerYear.size() < CELL_LIFETIME ? 0 : numberOfCellsPerYear.size() - CELL_LIFETIME;
for (int i = startIndex; i < numberOfCellsPerYear.size(); ++i) {
double pricePerCell = (START_EARNINGS_IN_ZAR_PER_CELL * Math.pow(INTEREST_INCREASE_PER_YEAR, (year - i)));
double earning = numberOfCellsPerYear.get(i) * pricePerCell;
money += earning;
fullEarningPerYear += earning;
}
money += (ADDITIONAL_INVESTEMENT_PER_YEAR / ZAR_TO_USD);
if (year < REINVESTEMENT_UNTIL_YEAR) {
int numberOfNewCells = (int)(money / PRICE_PER_CELL_ZAR);
money -= numberOfNewCells * PRICE_PER_CELL_ZAR;
numberOfCellsPerYear.add(numberOfNewCells);
} else {
numberOfCellsPerYear.add(0);
}
int numberOfCells = calculateNumberOfCells(numberOfCellsPerYear);
System.out.printf("%d\t\t$%.1f\t\t$%.1f\t\t%d\t\t%.2fKW\n", (year + 1), (money * ZAR_TO_USD), (fullEarningPerYear * ZAR_TO_USD), numberOfCells, (numberOfCells * WATT_PER_CELL) / 1000.0);
}
}
private static int calculateNumberOfCells(List<Integer> numberOfCellsPerYear) {
int startIndex = numberOfCellsPerYear.size() < CELL_LIFETIME ? 0 : numberOfCellsPerYear.size() - CELL_LIFETIME;
int sum = 0;
for (int i = startIndex; i < numberOfCellsPerYear.size(); ++i) {
sum += numberOfCellsPerYear.get(i);
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment