I've found an open source Java code that does IRR calculation. I would like to integrate this into my program. The idea is to input this java program some amounts and dates it then calculates IRR and returns a single number (double). The program excepts collection class as input (combination of numbers and dates) then returns the number. It can take as many number and dates as user wants. There are some sample code within the documentation, but all of them shows how this program gets parameters within the code hard coded. I'm trying to change it, so the program will get user input parse it into numbers and dates then ideally converts them into collection and pass it to the java program . I couldn't do it. I couldn't create collection object from user input and passed it to the program. I'm attaching sample code that does it values hardcodeded in the code, all I want to write a class that will dynamically capture user input (combination value and date, ideally one value, one date and so on) and pass it to the XIRR method.
public double xirr_issue5b() {
double rate = new Xirr(
new Transaction(-2610, "2001-06-22"),
new Transaction(-2589, "2001-07-03"),
new Transaction(-5110, "2001-07-05"),
new Transaction(-2550, "2001-07-06"),
new Transaction(-5086, "2001-07-09"),
new Transaction(-2561, "2001-07-10"),
new Transaction(-5040, "2001-07-12"),
new Transaction(-2552, "2001-07-13"),
new Transaction(-2530, "2001-07-16"),
new Transaction(-9840, "2001-07-17"),
new Transaction(38900, "2001-07-18")
).xirr();
return rate;
}
One thing to note is that the XIRR implementation in the open source package you refer to has public Xirr(Transaction... tx){
which (if you are not familiar with var args) means you can have any number of elements of transactions. What it also allows is for you to enter in an array. XIRR also can take in Collections (such as ArrayLists) So what I do in the following code is:
Scanner
to read in the user inputArrayList
that holds the transactionsfor
loop that loops the iterations
amount predefined by the user and adds a new Transaction
to the ArrayList
each iteration taking in the user's next int and next String (converted to a date via date formatter).Try this:
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.Scanner;
public double xirr_issue5b() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Scanner sc = new Scanner(System.in);
ArrayList<Transaction> trans = new ArrayList<Transaction>();
int iterations = sc.nextInt();
for(int k = 0; k < iterations; k++) {
trans.add(new Transaction(sc.nextInt(), format.parse(sc.next())));
}
double rate = Xirr(trans).xirr();
sc.close();
return rate;
}