import java.text.Format;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/*
 * PresentValue.java
 *
 * Created on August 11, 2006, 9:18 PM
 *
 * COP 6007 Examples
 * 
 */

/**
 *
 * @author Bill Kraynek
 */
public class PresentValue {
    ArrayList<Double> cashFlowTimes = new ArrayList<Double>();
    ArrayList<Double> cashFlowAmounts= new ArrayList<Double>();
    ArrayList<CashFlow> cashFlows = new ArrayList<CashFlow>();
    Format fmt = NumberFormat.getCurrencyInstance();
    
    /** Creates a new instance of PresentValue */
    public PresentValue() {
        for( int t = 0; t < 12; t++ ) cashFlowTimes.add((double)t);
        for( int t = 0; t < 12; t++ ) cashFlowAmounts.add(100.0);
        String out = "Continuous Compounding: ";
        out += fmt.format(cashFlowPresentValue(cashFlowTimes,cashFlowAmounts,.05)) + "\n";
        out += "Discrete Compounding: ";
        out += fmt.format(cashFlowPresentValueDiscrete(cashFlowTimes,cashFlowAmounts,.05)) + "\n";
        JOptionPane.showMessageDialog(null,out);
        for( int t = 0; t < 12; t++ ) cashFlows.add(new CashFlow(t,100));
        out = "Using the CashFlow class\n\nContinuous Compounding: ";
        out += fmt.format(cashFlowPresentValue(cashFlows,.05)) + "\n";
        out += "Discrete Compounding: ";
        out += fmt.format(cashFlowPresentValueDiscrete(cashFlows,.05)) + "\n";
        JOptionPane.showMessageDialog(null,out);
    }
    
    public double cashFlowPresentValue(ArrayList<Double> cashFlowTimes, ArrayList<Double> cashFlowAmounts, double r) {
        double presentValue = 0.0;
        for( int t = 0; t < cashFlowTimes.size(); t++ ) {
            presentValue += cashFlowAmounts.get(t) * Math.exp(-r*cashFlowTimes.get(t));
        }// end for
        return presentValue;
    }
    
    public double cashFlowPresentValueDiscrete(ArrayList<Double> cashFlowTimes, ArrayList<Double> cashFlowAmounts, double r) {
        double presentValue = 0.0;
        for( int t = 0; t < cashFlowTimes.size(); t++ ) {
            presentValue += cashFlowAmounts.get(t) / Math.pow(1+r,cashFlowTimes.get(t));
        }// end for
        return presentValue;
    }
    
    class CashFlow {
        double time;
        double amount;
        public CashFlow(double t, double a) {
            time = t;
            amount =a;
        }
    }// end CashFlow
    
    public double cashFlowPresentValue(ArrayList<CashFlow> cashFlows, double r) {
        double presentValue = 0.0;
        for( CashFlow cashFlow : cashFlows ) {
            presentValue += cashFlow.amount * Math.exp(-r*cashFlow.time);
        }// end for
        return presentValue;
    }
    
    public double cashFlowPresentValueDiscrete(ArrayList<CashFlow> cashFlows, double r) {
        double presentValue = 0.0;
        for( CashFlow cashFlow : cashFlows ) {
            presentValue += cashFlow.amount / Math.pow(1+r,cashFlow.time);
        }// end for
        return presentValue;
    }    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new PresentValue();
    }
    
}

