138 lines
5.0 KiB
Java
138 lines
5.0 KiB
Java
package ch.pizzalink.android.helper;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import ch.pizzalink.android.R;
|
|
import ch.pizzalink.android.activity.BaseActivity;
|
|
import ch.pizzalink.android.model.menu.MenuProductModel;
|
|
import ch.pizzalink.android.model.menu.MenuProductOptionValueModel;
|
|
|
|
/**
|
|
* Created by cimenmus on 12/10/2017.
|
|
*/
|
|
|
|
public class PriceHelper {
|
|
|
|
public static String getPriceWithCurreny(String price){
|
|
price = roundFractions(price);
|
|
return BaseActivity.currentActivity.getString(R.string.chf) + " " + price;
|
|
}
|
|
|
|
public static String getPriceWithCurreny(Double price){
|
|
String priceString = roundFractions(String.valueOf(price));
|
|
return BaseActivity.currentActivity.getString(R.string.chf) + " " + priceString;
|
|
}
|
|
|
|
public static String removeCurrencyFromPrice(String priceWithCurreny){
|
|
/*
|
|
try {
|
|
String currencyText = BaseActivity.currentActivity.getString(R.string.chf);
|
|
String priceWithoutCurrency = priceWithCurreny.replace(currencyText, "");
|
|
priceWithoutCurrency = priceWithoutCurrency.trim();
|
|
return priceWithoutCurrency;
|
|
}catch (Exception e){
|
|
return priceWithCurreny.trim();
|
|
}
|
|
*/
|
|
|
|
String currencyText = BaseActivity.currentActivity.getString(R.string.chf);
|
|
String priceWithoutCurrency = priceWithCurreny.replace(currencyText, "");
|
|
priceWithoutCurrency = priceWithoutCurrency.trim();
|
|
return priceWithoutCurrency;
|
|
}
|
|
|
|
public static String calculatePriceAfterCountChanged(String oldPriceString, int oldCount, int newCount){
|
|
Double oldPrice = stringToDouble(removeCurrencyFromPrice(oldPriceString));
|
|
Double productOldPrice = oldPrice / oldCount;
|
|
Double productNewPrice = productOldPrice * newCount;
|
|
return getPriceWithCurreny(productNewPrice);
|
|
}
|
|
|
|
public static String calculatePriceAfterRadioOptionValueChanged(int count,
|
|
MenuProductModel menuProductModel,
|
|
MenuProductOptionValueModel menuProductOptionValueModel){
|
|
|
|
Double productPrice = stringToDouble(menuProductModel.getPrice());
|
|
|
|
switch (menuProductOptionValueModel.getPrice_prefix()){
|
|
case "+":
|
|
productPrice += stringToDouble(menuProductOptionValueModel.getPrice());
|
|
break;
|
|
case "-":
|
|
productPrice -= stringToDouble(menuProductOptionValueModel.getPrice());
|
|
break;
|
|
}
|
|
|
|
return getPriceWithCurreny(productPrice * count);
|
|
|
|
}
|
|
|
|
public static String calculatePriceAfterCheckboxOptionValueChanged(int count,
|
|
MenuProductModel menuProductModel,
|
|
ArrayList<MenuProductOptionValueModel> menuProductOptionValueModelList){
|
|
|
|
Double productPrice = stringToDouble(menuProductModel.getPrice());
|
|
|
|
for(MenuProductOptionValueModel menuProductOptionValueModel : menuProductOptionValueModelList){
|
|
if(menuProductOptionValueModel.isSelected()){
|
|
switch (menuProductOptionValueModel.getPrice_prefix()){
|
|
case "+":
|
|
productPrice += stringToDouble(menuProductOptionValueModel.getPrice());
|
|
break;
|
|
case "-":
|
|
productPrice -= stringToDouble(menuProductOptionValueModel.getPrice());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return getPriceWithCurreny(productPrice * count);
|
|
|
|
}
|
|
|
|
public static String getProductOptionPriceText(MenuProductOptionValueModel productOptionValueModel){
|
|
return new StringBuilder()
|
|
.append(productOptionValueModel.getPrice_prefix())
|
|
.append(" ")
|
|
.append(BaseActivity.currentActivity.getString(R.string.chf))
|
|
.append(" ")
|
|
.append(roundFractions(productOptionValueModel.getPrice()))
|
|
.toString();
|
|
}
|
|
|
|
private static Double stringToDouble(String string){
|
|
string = addFractions(string);
|
|
Double dbl = Double.valueOf(string);
|
|
return Math.floor(dbl * 100) / 100;
|
|
}
|
|
|
|
public static String roundFractions(String price){
|
|
|
|
if(price.equals("0")){
|
|
price = "0.-";
|
|
}
|
|
if(price.endsWith(".0")){
|
|
price = price.replace(".0", ".-");
|
|
}
|
|
else if(price.endsWith(".00")){
|
|
price = price.replace(".00", ".-");
|
|
}
|
|
else if(price.endsWith(".000")){
|
|
price = price.replace(".000", ".-");
|
|
}
|
|
else if(price.endsWith(".0000")){
|
|
price = price.replace(".0000", ".-");
|
|
}
|
|
return price;
|
|
}
|
|
|
|
private static String addFractions(String price){
|
|
if(price.endsWith(".-")){
|
|
price = price.replace(".-", ".00");
|
|
}
|
|
return price;
|
|
}
|
|
|
|
|
|
}
|