001 package com.croftsoft.apps.calc; 002 003 import java.io.*; 004 import java.util.*; 005 006 import com.croftsoft.core.math.FinanceLib; 007 008 /********************************************************************* 009 * Command line financial calculator. 010 * 011 * <p /> 012 * 013 * @version 014 * 2001-10-10 015 * @since 016 * 2001-10-10 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class Calc 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 { 025 026 public static void main ( String [ ] args ) 027 throws Exception 028 ////////////////////////////////////////////////////////////////////// 029 { 030 System.out.println ( "Calc (c) 2001 CroftSoft Inc" ); 031 032 System.out.println ( "https://www.croftsoft.com/" ); 033 034 System.out.println ( "" ); 035 036 System.out.println ( "1 = Net Present Value" ); 037 038 System.out.println ( "2 = Internal Rate of Return" ); 039 040 System.out.println ( "" ); 041 042 String line = prompt ( "Function [Quit]: " ); 043 044 if ( "1".equals ( line ) ) 045 { 046 netPresentValue ( ); 047 } 048 else if ( "2".equals ( line ) ) 049 { 050 internalRateOfReturn ( ); 051 } 052 } 053 054 private static void netPresentValue ( ) 055 throws Exception 056 ////////////////////////////////////////////////////////////////////// 057 { 058 System.out.println ( "Net Present Value (NPV)" ); 059 060 double discountRate 061 = promptDouble ( "DiscountRate (%)", 10.0 ) / 100.0; 062 063 List cashFlowList = new ArrayList ( ); 064 065 int time = 0; 066 067 double value; 068 069 double defaultValue = 0.0; 070 071 while ( true ) 072 { 073 String line = prompt ( 074 "Cash Flow at Time " + time 075 + " (Control-Z or Control-D to end) [" 076 + defaultValue + "]: " ); 077 078 079 if ( line == null ) 080 { 081 break; 082 } 083 084 if ( "".equals ( line ) ) 085 { 086 value = defaultValue; 087 } 088 else 089 { 090 value = Double.parseDouble ( line ); 091 092 defaultValue = value; 093 } 094 095 cashFlowList.add ( new Double ( value ) ); 096 097 time++; 098 } 099 100 double [ ] cashFlows = new double [ cashFlowList.size ( ) ]; 101 102 for ( int i = 0; i < cashFlows.length; i++ ) 103 { 104 cashFlows [ i ] = ( ( Double ) cashFlowList.get ( i ) ).doubleValue ( ); 105 } 106 107 System.out.println ( 108 FinanceLib.netPresentValue ( discountRate, cashFlows ) ); 109 } 110 111 private static void internalRateOfReturn ( ) 112 throws Exception 113 ////////////////////////////////////////////////////////////////////// 114 { 115 System.out.println ( "Internal Rate of Return (IRR)" ); 116 117 double irrGuess 118 = promptDouble ( "Estimated IRR (%)", 10.0 ) / 100.0; 119 120 List cashFlowList = new ArrayList ( ); 121 122 int time = 0; 123 124 double value; 125 126 double defaultValue = 0.0; 127 128 while ( true ) 129 { 130 String line = prompt ( 131 "Cash Flow at Time " + time 132 + " (Control-Z or Control-D to end) [" 133 + defaultValue + "]: " ); 134 135 136 if ( line == null ) 137 { 138 break; 139 } 140 141 if ( "".equals ( line ) ) 142 { 143 value = defaultValue; 144 } 145 else 146 { 147 value = Double.parseDouble ( line ); 148 149 defaultValue = value; 150 } 151 152 cashFlowList.add ( new Double ( value ) ); 153 154 time++; 155 } 156 157 double [ ] cashFlows = new double [ cashFlowList.size ( ) ]; 158 159 for ( int i = 0; i < cashFlows.length; i++ ) 160 { 161 cashFlows [ i ] 162 = ( ( Double ) cashFlowList.get ( i ) ).doubleValue ( ); 163 } 164 165 System.out.println ( ( 100.0 * 166 FinanceLib.internalRateOfReturn ( irrGuess, cashFlows ) ) + "%" ); 167 } 168 169 private static String prompt ( String promptString ) 170 throws IOException 171 ////////////////////////////////////////////////////////////////////// 172 { 173 System.out.print ( promptString ); 174 175 return new BufferedReader ( 176 new InputStreamReader ( System.in ) ).readLine ( ); 177 } 178 179 private static double promptDouble ( 180 String promptString, 181 double defaultValue ) 182 throws IOException, NumberFormatException 183 ////////////////////////////////////////////////////////////////////// 184 { 185 System.out.print ( promptString + " [" + defaultValue + "]: " ); 186 187 String line = new BufferedReader ( 188 new InputStreamReader ( System.in ) ).readLine ( ); 189 190 if ( "".equals ( line ) ) 191 { 192 return defaultValue; 193 } 194 195 return Double.parseDouble ( line ); 196 } 197 198 ////////////////////////////////////////////////////////////////////// 199 ////////////////////////////////////////////////////////////////////// 200 }