fix formatting for price

This commit is contained in:
Manfred Karrer 2014-08-30 02:42:10 +02:00
parent 454dc94965
commit 4fd06ac161
11 changed files with 50 additions and 89 deletions

View File

@ -155,10 +155,9 @@ class CreateOfferPM extends PresentationModel<CreateOfferModel> {
model.minAmountAsCoin.set(orderBookFilter.getAmount());
}
// TODO use Fiat in orderBookFilter
// apply only if valid
if (orderBookFilter.getPrice() != 0 && isBtcInputValid(String.valueOf(orderBookFilter.getPrice())).isValid)
model.priceAsFiat.set(parseToFiatWith2Decimals(String.valueOf(orderBookFilter.getPrice())));
if (orderBookFilter.getPrice() != null && isBtcInputValid(orderBookFilter.getPrice().toPlainString()).isValid)
model.priceAsFiat.set(parseToFiatWith2Decimals(orderBookFilter.getPrice().toPlainString()));
}

View File

@ -194,7 +194,7 @@ public class OrderBookController extends CachedViewController {
});
price.textProperty().addListener((observable, oldValue, newValue) -> {
orderBookFilter.setPrice(textInputToNumber(oldValue, newValue));
orderBookFilter.setPrice(BSFormatter.parseToFiat(newValue));
updateVolume();
});

View File

@ -128,7 +128,7 @@ public class BSFormatter {
public static Coin parseToCoin(String input) {
try {
return coinFormat.parse(cleanedInput(input));
return coinFormat.parse(cleanInput(input));
} catch (Throwable t) {
log.warn("Exception at parseToBtc: " + t.toString());
return Coin.ZERO;
@ -145,7 +145,7 @@ public class BSFormatter {
*/
public static Coin parseToCoinWith4Decimals(String input) {
try {
return Coin.valueOf(new BigDecimal(parseToCoin(cleanedInput(input)).value).setScale(-scale - 1,
return Coin.valueOf(new BigDecimal(parseToCoin(cleanInput(input)).value).setScale(-scale - 1,
BigDecimal.ROUND_HALF_UP).setScale(scale + 1).toBigInteger().longValue());
} catch (Throwable t) {
log.warn("Exception at parseToCoinWith4Decimals: " + t.toString());
@ -192,7 +192,7 @@ public class BSFormatter {
public static Fiat parseToFiat(String input) {
try {
return Fiat.parseFiat(currencyCode, cleanedInput(input));
return Fiat.parseFiat(currencyCode, cleanInput(input));
} catch (Exception e) {
return Fiat.valueOf(currencyCode, 0);
}
@ -208,7 +208,7 @@ public class BSFormatter {
*/
public static Fiat parseToFiatWith2Decimals(String input) {
try {
return parseToFiat(new BigDecimal(cleanedInput(input)).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
return parseToFiat(new BigDecimal(cleanInput(input)).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
} catch (Throwable t) {
log.warn("Exception at parseCoinTo4Decimals: " + t.toString());
return Fiat.valueOf(currencyCode, 0);
@ -219,6 +219,8 @@ public class BSFormatter {
public static boolean hasFiatValidDecimals(String input) {
return parseToFiat(input).equals(parseToFiatWith2Decimals(input));
}
///////////////////////////////////////////////////////////////////////////////////////////
// Other
///////////////////////////////////////////////////////////////////////////////////////////
@ -228,6 +230,7 @@ public class BSFormatter {
* " are supported. Thousands separator is not supported.
* @return Returns a double value. Any invalid value returns Double.NEGATIVE_INFINITY.
*/
@Deprecated //TODO use Fiat or Btc if possible
public static double parseToDouble(String input) {
try {
checkNotNull(input);
@ -247,22 +250,6 @@ public class BSFormatter {
return result;
}
public static String formatDouble(Fiat value) {
return formatDouble(value, 4);
}
public static String formatDouble(Fiat value, int fractionDigits) {
DecimalFormat decimalFormat = getDecimalFormat(fractionDigits);
return decimalFormat.format(value);
}
public static DecimalFormat getDecimalFormat(int fractionDigits) {
DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(locale);
decimalFormat.setMinimumFractionDigits(fractionDigits);
decimalFormat.setMaximumFractionDigits(fractionDigits);
decimalFormat.setGroupingUsed(false);
return decimalFormat;
}
public static String countryLocalesToString(List<Country> countries) {
String result = "";
@ -290,7 +277,6 @@ public class BSFormatter {
return result;
}
public static String arbitrationMethodsToString(List<Arbitrator.METHOD> items) {
String result = "";
int i = 0;
@ -325,25 +311,18 @@ public class BSFormatter {
}
public static String formatCollateralPercent(long collateral) {
return getDecimalFormat(1).format(collateral / 10) + " %";
DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(locale);
decimalFormat.setMinimumFractionDigits(1);
decimalFormat.setMaximumFractionDigits(1);
decimalFormat.setGroupingUsed(false);
return decimalFormat.format(collateral / 10) + " %";
}
public static String formatVolumeWithMinVolume(Fiat volume, Fiat minVolume) {
return formatFiat(volume) + " (" + formatFiat(minVolume) + ")";
}
/*
@Deprecated
public static String formatCoin(Coin coin) {
return coin != null ? coin.toPlainString() : "";
}
@Deprecated
public static String formatCoinWithCode(Coin coin) {
return coin != null ? coin.toFriendlyString() : "";
}*/
private static String cleanedInput(String input) {
private static String cleanInput(String input) {
input = input.replace(",", ".");
// don't use String.valueOf(Double.parseDouble(input)) as return value as it gives scientific
// notation (1.0E-6) which screw up coinFormat.parse

View File

@ -20,8 +20,6 @@ package io.bitsquare.settings;
import io.bitsquare.arbitrator.Arbitrator;
import io.bitsquare.locale.Country;
import com.google.bitcoin.core.Coin;
import java.io.Serializable;
import java.util.ArrayList;
@ -99,25 +97,14 @@ public class Settings implements Serializable {
return acceptedArbitrators;
}
public List<Locale> getAcceptedLanguageLocales() {
return acceptedLanguageLocales;
}
public List<Country> getAcceptedCountries() {
return acceptedCountryLocales;
}
//TODO
public Arbitrator getRandomArbitrator(Coin amount) {
List<Arbitrator> candidates = new ArrayList<>();
for (Arbitrator arbitrator : acceptedArbitrators) {
candidates.add(arbitrator);
}
return !candidates.isEmpty() ? candidates.get((int) (Math.random() * candidates.size())) : null;
}
public void setCollateral(long collateral) {
this.collateral = collateral;
}

View File

@ -22,12 +22,11 @@ import io.bitsquare.bank.BankAccountType;
import io.bitsquare.locale.Country;
import com.google.bitcoin.core.Coin;
import com.google.bitcoin.utils.ExchangeRate;
import com.google.bitcoin.utils.Fiat;
import java.io.Serializable;
import java.math.BigDecimal;
import java.security.PublicKey;
import java.util.Currency;
@ -60,7 +59,7 @@ public class Offer implements Serializable {
private final List<Country> acceptedCountries;
private final List<Locale> acceptedLanguageLocales;
private final String bankAccountUID;
private final Arbitrator arbitrator;
private final List<Arbitrator> arbitrators;
private String offerFeePaymentTxID;
@ -78,7 +77,7 @@ public class Offer implements Serializable {
Currency currency,
Country bankAccountCountry,
String bankAccountUID,
Arbitrator arbitrator,
List<Arbitrator> arbitrators,
long collateral,
List<Country> acceptedCountries,
List<Locale> acceptedLanguageLocales) {
@ -92,7 +91,7 @@ public class Offer implements Serializable {
this.currency = currency;
this.bankAccountCountry = bankAccountCountry;
this.bankAccountUID = bankAccountUID;
this.arbitrator = arbitrator;
this.arbitrators = arbitrators;
this.collateral = collateral;
this.acceptedCountries = acceptedCountries;
@ -156,10 +155,11 @@ public class Offer implements Serializable {
}
public Fiat getVolumeForCoin(Coin coin) {
BigDecimal amountBD = BigDecimal.valueOf(coin.longValue());
BigDecimal volumeBD = amountBD.multiply(BigDecimal.valueOf(price.longValue() / 10000));
long fiatAsDouble = volumeBD.divide(BigDecimal.valueOf(Coin.COIN.value)).longValue();
return Fiat.valueOf("EUR", fiatAsDouble);
if (price != null && coin != null && !coin.isZero() && !price.isZero()) {
return new ExchangeRate(price).coinToFiat(coin);
}
else
return null;
}
public Fiat getOfferVolume() {
@ -178,8 +178,8 @@ public class Offer implements Serializable {
this.offerFeePaymentTxID = offerFeePaymentTxID;
}
public Arbitrator getArbitrator() {
return arbitrator;
public List<Arbitrator> getArbitrators() {
return arbitrators;
}
public long getCollateral() {
@ -208,7 +208,7 @@ public class Offer implements Serializable {
", acceptedLanguageLocales=" + acceptedLanguageLocales +
", offerFeePaymentTxID='" + offerFeePaymentTxID + '\'' +
", bankAccountUID='" + bankAccountUID + '\'' +
", arbitrator=" + arbitrator +
", arbitrator=" + arbitrators +
'}';
}

View File

@ -177,7 +177,7 @@ public class TradeManager {
user.getCurrentBankAccount().getCurrency(),
user.getCurrentBankAccount().getCountry(),
user.getCurrentBankAccount().getUid(),
settings.getRandomArbitrator(amount),
settings.getAcceptedArbitrators(),
settings.getCollateral(),
settings.getAcceptedCountries(),
settings.getAcceptedLanguageLocales());

View File

@ -25,6 +25,7 @@ import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.listeners.OrderBookListener;
import io.bitsquare.settings.Settings;
import io.bitsquare.trade.Direction;
import io.bitsquare.trade.Offer;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.user.User;
@ -135,20 +136,16 @@ public class OrderBook implements OrderBookListener {
// Apply applyFilter only if there is a valid value set
boolean priceResult = true;
if (orderBookFilter.getPrice() > 0) {
//TODO
/* if (offer.getDirection() == Direction.SELL) {
priceResult = orderBookFilter.getPrice() //>= offer.getPrice();
}
else {
priceResult = orderBookFilter.getPrice() <= offer.getPrice();
}*/
if (orderBookFilter.getPrice() != null) {
if (offer.getDirection() == Direction.SELL)
priceResult = orderBookFilter.getPrice().compareTo(offer.getPrice()) >= 0;
else
priceResult = orderBookFilter.getPrice().compareTo(offer.getPrice()) <= 0;
}
// The arbitrator defined in the offer must match one of the accepted arbitrators defined in the settings
// (1 to n)
boolean arbitratorResult = arbitratorInList(offer.getArbitrator(), settings.getAcceptedArbitrators());
boolean arbitratorResult = arbitratorsInList(offer.getArbitrators(), settings.getAcceptedArbitrators());
boolean result = currencyResult && countryResult && languageResult && amountResult && directionResult &&
priceResult && arbitratorResult;
@ -250,15 +247,11 @@ public class OrderBook implements OrderBookListener {
return false;
}
private boolean arbitratorInList(Arbitrator arbitratorToMatch, List<Arbitrator> list) {
if (arbitratorToMatch != null) {
for (Arbitrator arbitrator : list) {
try {
if (arbitrator.getId().equals(arbitratorToMatch.getId())) {
return true;
}
} catch (Exception e) {
log.error(e.toString());
private boolean arbitratorsInList(List<Arbitrator> list1, List<Arbitrator> list2) {
for (Arbitrator arbitrator1 : list2) {
for (Arbitrator arbitrator2 : list1) {
if (arbitrator1.getId().equals(arbitrator2.getId())) {
return true;
}
}
}

View File

@ -20,6 +20,7 @@ package io.bitsquare.trade.orderbook;
import io.bitsquare.trade.Direction;
import com.google.bitcoin.core.Coin;
import com.google.bitcoin.utils.Fiat;
import javafx.beans.property.SimpleBooleanProperty;
@ -27,7 +28,7 @@ public class OrderBookFilter {
// TODO use ObjectProperty<Direction> instead
private final SimpleBooleanProperty directionChangedProperty = new SimpleBooleanProperty();
private double price;
private Fiat price;
private Coin amount;
private Direction direction;
@ -58,11 +59,11 @@ public class OrderBookFilter {
directionChangedProperty.set(!directionChangedProperty.get());
}
public double getPrice() {
public Fiat getPrice() {
return price;
}
public void setPrice(double price) {
public void setPrice(Fiat price) {
this.price = price;
}

View File

@ -38,7 +38,7 @@ public class VerifyOffer {
checkNotNull(offer.getAcceptedCountries(), "AcceptedCountries is null");
checkNotNull(offer.getAcceptedLanguageLocales(), "AcceptedLanguageLocales is null");
checkNotNull(offer.getAmount(), "Amount is null");
checkNotNull(offer.getArbitrator(), "Arbitrator is null");
checkNotNull(offer.getArbitrators(), "Arbitrator is null");
checkNotNull(offer.getBankAccountCountry(), "BankAccountCountry is null");
checkNotNull(offer.getBankAccountId(), "BankAccountId is null");
checkNotNull(offer.getCollateral(), "Collateral is null");

View File

@ -156,7 +156,8 @@ public class BuyerAcceptsOfferProtocol {
tradeId = trade.getId();
offer = trade.getOffer();
arbitratorPubKey = offer.getArbitrator().getPubKeyAsHex();
//TODO use first for now
arbitratorPubKey = offer.getArbitrators().get(0).getPubKeyAsHex();
bankAccount = user.getBankAccount(trade.getOffer().getBankAccountId());
accountId = user.getAccountId();

View File

@ -150,7 +150,8 @@ public class SellerTakesOfferProtocol {
tradeId = trade.getId();
tradeAmount = trade.getTradeAmount();
collateral = trade.getCollateralAmount();
arbitratorPubKey = trade.getOffer().getArbitrator().getPubKeyAsHex();
//TODO use 1. for now
arbitratorPubKey = trade.getOffer().getArbitrators().get(0).getPubKeyAsHex();
peersMessagePublicKey = offer.getMessagePublicKey();