游戏自定义金币单位换算管理类

https://blog.csdn.net/QuVi_God/article/details/83382628

根据这篇游戏金币管理类文章,所改写的Java版本的游戏金币管理类。

---------------------------------

金币单位枚举类:

public enum GoldUnit {//自定义的金币单位类型
	N(0), 
	K(1), 
	M(2), 
	B(3), 
	T(4),
	aa(5),
	ab(6),
	ac(7),
	ad(8),
	ae(9),
	af(10),
	ag(11),
	ah(12),
	ai(13),
	aj(14),
	ak(15),
	al(16),
	am(17),
	an(18),
	ao(19),
	ap(20),
	aq(21),
	ar(22),
	as(23),
	at(24),
	au(25),
	av(26),
	aw(27),
	ax(28),
	ay(29),
	az(30),
	;
	
    private int value;

    private GoldUnit(int value)
    {
        this.value = value;
    }

    public int getValue()
    {
        return value;
    }

    public static GoldUnit parse(int value)
    {
    	GoldUnit[] states = GoldUnit.values();
        for (GoldUnit s : states)
        {
            if (s.getValue() == value)
                return s;
        }
        return N;
    }
}

金币单位管理实现类:

public class GoldNum {
	/**
	 * 金币+单位
	 */
	private String goldCountText;
	/**
	 * 单位枚举
	 */
	private Integer goldUnit;
	/**
	 * 金币金额
	 */
	private Double goldValue;
	public String GoldCountText;
	public String value;

	// 构造函数
	public GoldNum(String value) {
		value = value.trim();// 删除字符串首部和尾部的空格
		boolean isSingleDigit = true;
		//遍历金币单位枚举
		for (int i = 30; i >= 0; i--) {
			if (value.endsWith(GoldUnit.parse(i).toString())) {
				value = value.replace(GoldUnit.parse(i).toString(), "");
				isSingleDigit = false;
				goldUnit = i;
				goldValue = Double.parseDouble(value);
				break;
			}
		}
		//没有单位
		if (isSingleDigit) {
			goldUnit = 0;
			goldValue = Double.parseDouble(value);
		}
		//值大于500  进一位
		while (goldValue > 500 && goldUnit < 30) {
			goldUnit++;
			goldValue /= 1000;
		}
		//值大于0 小于 1 且 拥有金币单位  退一位
		while (goldValue > 0 && goldValue < 1 && goldUnit > 0) {
			goldUnit--;
			goldValue *= 1000;
		}
	}
	
	// 构造函数
	public GoldNum(Double value) {
		goldUnit = 0;
		goldValue = (double) Math.round(value);
		while (goldValue > 500 && goldUnit < 30) {
			goldUnit++;
			goldValue /= 1000;
		}
		while (goldValue > 0 && goldValue < 1 && goldUnit > 0) {
			goldUnit--;
			goldValue *= 1000;
		}
	}

	public String getGoldCountText() {
		String p="";
		if (goldUnit == 0) {
			goldCountText = Math.round(goldValue) + "";
		} else {
			BigDecimal bg = new BigDecimal(goldValue);
			float tempValue = bg.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
			if (Math.round(tempValue) - tempValue == 0) {
				p=String.valueOf(Math.round(tempValue));
			}
			else{
				//保留小数点后两位
				DecimalFormat decimalFormat = new DecimalFormat(".00");
				p = decimalFormat.format(tempValue);
			}
			goldCountText = p + (GoldUnit.parse(goldUnit)).toString();
		}
		return goldCountText;
	}

	public void setGoldCountText(String value) {
		if (value != null) {
			if (!value.equals(this.value)) {
				this.value = value;
			}
		} else {
			if (value != this.value) {
				this.value = value;
			}
		}
		goldCountText = value;
	}

	public Integer getGoldUnit() {
		return goldUnit;
	}

	public void setGoldUnit(Integer goldUnit) {
		this.goldUnit = goldUnit;
	}

	public Double getGoldValue() {
		return goldValue;
	}

	public void setGoldValue(Double goldValue) {
		this.goldValue = goldValue;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}


	public static GoldNum GoldNum(String value) {
		GoldNum goldNum = null;
		int length = value.length();
		if (length <= 3) {
			goldNum = new GoldNum(value);
		}
		return goldNum;
	}

	/**
	 * 加法
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static GoldNum add(GoldNum A, GoldNum B) {
		int limit = 4;
		int tempUnit = 0;
		Double tempValue = 0.0;

		if (A.goldUnit == B.goldUnit) {
			tempUnit = A.goldUnit;
			tempValue = A.goldValue + B.goldValue;
		} else if (A.goldUnit > B.goldUnit) {
			if (A.goldUnit - B.goldUnit <= limit) {
				tempUnit = A.goldUnit;
				tempValue = (Double) (A.goldValue + B.goldValue / Math.pow(1000, A.goldUnit - B.goldUnit));
			} else if (A.goldUnit - B.goldUnit > limit) {
				tempUnit = A.goldUnit;
				tempValue = A.goldValue;
			}
		} else if (A.goldUnit < B.goldUnit) {
			if (B.goldUnit - A.goldUnit <= limit) {
				tempUnit = B.goldUnit;
				tempValue = (Double) (A.goldValue / Math.pow(1000, B.goldUnit - A.goldUnit) + B.goldValue);
			} else if (B.goldUnit - A.goldUnit > limit) {
				tempUnit = B.goldUnit;
				tempValue = B.goldValue;
			}
		}
		GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());
		return result;
	}

	/**
	 * 减法
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static GoldNum sub(GoldNum A, GoldNum B) {
		int limit = 4;
		int tempUnit = 0;
		Double tempValue = 0.0;

		if (A.goldUnit == B.goldUnit) {
			tempUnit = A.goldUnit;
			tempValue = A.goldValue - B.goldValue;
			if(tempValue==0){
				tempUnit=0;
			}
		} else if (A.goldUnit > B.goldUnit) {
			if (A.goldUnit - B.goldUnit <= limit) {
				tempUnit = A.goldUnit;
				tempValue = (Double) (A.goldValue - B.goldValue / Math.pow(1000, A.goldUnit - B.goldUnit));
			} else if (A.goldUnit - B.goldUnit > limit) {
				tempUnit = A.goldUnit;
				tempValue = A.goldValue;
			}
		} else if (A.goldUnit < B.goldUnit) {
			if (B.goldUnit - A.goldUnit <= limit) {
				tempUnit = B.goldUnit;
				tempValue = (Double) (A.goldValue / Math.pow(1000, B.goldUnit - A.goldUnit) - B.goldValue);
			} else if (B.goldUnit - A.goldUnit > limit) {
				tempUnit = B.goldUnit;
				tempValue = -B.goldValue;
			}
		}
		GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());
		return result;
	}

	/**
	 * 乘法
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static GoldNum mul(GoldNum A, float B) {
		int tempUnit = A.goldUnit;
		Double tempValue = A.goldValue * B;
		GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());
		return result;
	}

	/**
	 * 乘法
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static GoldNum mul(GoldNum A, GoldNum B) {
		int tempUnit = A.goldUnit + B.goldUnit;
		Double tempValue = A.goldValue * B.goldValue;
		GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());
		return result;
	}

	/**
	 * 除法
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static GoldNum div(GoldNum A, float B) {
		int tempUnit = A.goldUnit;
		Double tempValue = A.goldValue / B;
		GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());
		return result;
	}

	/**
	 * 大于
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static boolean than(GoldNum A, GoldNum B) {
		GoldNum result = GoldNum.sub(A, B);
		return result.getGoldCountText().startsWith("-");
	}

	/**
	 * 小于
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static boolean less(GoldNum A, GoldNum B) {
		GoldNum result = GoldNum.sub(A, B);
		return result.getGoldCountText().startsWith("-");
	}

	/**
	 * 大于并且等于
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static boolean thanAndEqual(GoldNum A, GoldNum B) {
		GoldNum result = GoldNum.sub(A, B);
		return !result.getGoldCountText().startsWith("-") || result.getGoldCountText() == "0";
	}

	/**
	 * 小于并且等于
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static boolean lessAndEqual(GoldNum A, GoldNum B) {
		GoldNum result = GoldNum.sub(A, B);
		return result.getGoldCountText().startsWith("-") || result.GoldCountText == "0";
	}

	/**
	 * 随机数
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static GoldNum DoubleRandom(GoldNum A, GoldNum B) {
		int limit = 4;
		int tempUnit = 0;
		Double tempValue = 0.0;
		GoldNum result = null;
		ThreadLocalRandom random = ThreadLocalRandom.current();
		if (A.goldUnit == B.goldUnit) {
			tempUnit = A.goldUnit;
			if(A.getGoldValue().equals(B.getGoldValue())){
				tempValue=A.getGoldValue();
			}
			else{
				tempValue = random.nextDouble(A.getGoldValue(), B.getGoldValue());
			}
			result = new GoldNum(tempValue + (GoldUnit.parse(A.getGoldUnit())).toString());
		} else if (B.goldUnit > A.goldUnit) {
			if (B.goldUnit - A.goldUnit <= limit) {
				tempValue = (Double) (B.goldValue * Math.pow(1000, B.goldUnit - A.goldUnit));
				tempValue = random.nextDouble(A.getGoldValue(), tempValue);
				if (tempValue > 1000) {
					result = new GoldNum(tempValue + (GoldUnit.parse(B.getGoldUnit()-1)).toString());
				} else {
					result = new GoldNum(tempValue + (GoldUnit.parse(A.getGoldUnit())).toString());
				}
			} else if (A.goldUnit - B.goldUnit > limit) {
				tempUnit = A.goldUnit;
				tempValue = A.goldValue;
			}
		}
		return result;
	}

	@Override
	public String toString() {
		return "GoldNum [goldCountText=" + goldCountText + ", goldUnit=" + goldUnit + ", goldValue=" + goldValue
				+ ", GoldCountText=" + GoldCountText + ", value=" + value + "]";
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值