4040import org .gnucash .android .model .Commodity ;
4141import org .gnucash .android .model .Money ;
4242import org .gnucash .android .model .Price ;
43- import org .gnucash .android .ui .transaction .TransactionFormFragment ;
4443import org .gnucash .android .ui .transaction .TransactionsActivity ;
4544import org .gnucash .android .ui .util .OnTransferFundsListener ;
4645
4746import java .math .BigDecimal ;
4847import java .text .DecimalFormat ;
4948import java .text .NumberFormat ;
5049import java .text .ParseException ;
50+ import java .text .ParsePosition ;
5151import java .util .Currency ;
5252
5353import butterknife .Bind ;
@@ -201,13 +201,10 @@ private void transferFunds() {
201201 String originCommodityUID = commoditiesDbAdapter .getCommodityUID (mOriginAmount .getCurrency ().getCurrencyCode ());
202202 String targetCommodityUID = commoditiesDbAdapter .getCommodityUID (mTargetCurrencyCode );
203203
204- if (mExchangeRateRadioButton .isChecked ()){
204+ if (mExchangeRateRadioButton .isChecked ()) {
205205 BigDecimal rate ;
206- String exchangeRateString = mExchangeRateInput .getText ().toString ();
207- DecimalFormat formatter = (DecimalFormat ) NumberFormat .getNumberInstance ();
208- formatter .setParseBigDecimal (true );
209206 try {
210- rate = ( BigDecimal ) formatter . parse ( exchangeRateString );
207+ rate = parseAmount ( mExchangeRateInput . getText (). toString () );
211208 } catch (ParseException e ) {
212209 mExchangeRateInputLayout .setError (getString (R .string .error_invalid_exchange_rate ));
213210 return ;
@@ -218,14 +215,14 @@ private void transferFunds() {
218215 mConvertedAmount = mOriginAmount .multiply (rate ).withCurrency (targetCommodity );
219216 }
220217
221- if (mConvertedAmountRadioButton .isChecked ()){
222- String convertedAmount = mConvertedAmountInput .getText ().toString ();
223- if (convertedAmount .isEmpty ()){
224- mConvertedAmountInputLayout .setError (getString (R .string .error_converted_amount_required ));
218+ if (mConvertedAmountRadioButton .isChecked ()) {
219+ BigDecimal amount ;
220+ try {
221+ amount = parseAmount (mConvertedAmountInput .getText ().toString ());
222+ } catch (ParseException e ) {
223+ mConvertedAmountInputLayout .setError (getString (R .string .error_invalid_amount ));
225224 return ;
226225 }
227-
228- BigDecimal amount = TransactionFormFragment .parseInputToDecimal (convertedAmount );
229226 mConvertedAmount = new Money (amount , Commodity .getInstance (mTargetCurrencyCode ));
230227
231228 price = new Price (originCommodityUID , targetCommodityUID );
@@ -243,6 +240,19 @@ private void transferFunds() {
243240 dismiss ();
244241 }
245242
243+ private BigDecimal parseAmount (String amount ) throws ParseException {
244+ DecimalFormat formatter = (DecimalFormat ) NumberFormat .getNumberInstance ();
245+ formatter .setParseBigDecimal (true );
246+ ParsePosition parsePosition = new ParsePosition (0 );
247+ BigDecimal parsedAmount = (BigDecimal ) formatter .parse (amount , parsePosition );
248+
249+ // Ensure any mistyping by the user is caught instead of partially parsed
250+ if (parsePosition .getIndex () < amount .length ())
251+ throw new ParseException ("Parse error" , parsePosition .getErrorIndex ());
252+
253+ return parsedAmount ;
254+ }
255+
246256 /**
247257 * Hides the error message from mConvertedAmountInputLayout and mExchangeRateInputLayout
248258 * when the user edits their content.
0 commit comments