From 88c82455871cd03e0a0b6f32591f9bee74a2dc34 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期二, 05 三月 2024 14:52:22 +0800 Subject: [PATCH] 1.修改卡标识的bug 2.优化卡结构的代码 --- app/src/main/java/com/dayu/recharge/card/UserCard.java | 82 +++++++++++++---------------------------- 1 files changed, 26 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/dayu/recharge/card/UserCard.java b/app/src/main/java/com/dayu/recharge/card/UserCard.java index df69beb..93a8656 100644 --- a/app/src/main/java/com/dayu/recharge/card/UserCard.java +++ b/app/src/main/java/com/dayu/recharge/card/UserCard.java @@ -2,7 +2,6 @@ import com.dayu.recharge.tools.BcdUtil; import com.dayu.recharge.tools.HexUtil; -import com.dayu.recharge.utils.MornyUtil; import com.dayu.recharge.utils.MyCommon; import java.io.Serializable; @@ -15,7 +14,7 @@ * Date: 2023-11-07 9:37 * Description: 鐢ㄦ埛鍗$粨鏋� */ -public class UserCard implements Serializable { +public class UserCard extends BaseCard implements Serializable { public String cardType = MyCommon.USER_CARD_TYPE_1;//鍐欏崱鏍囧織 A1鍒峰崱寮�娉靛墠 A8鍒峰崱寮�娉靛悗 A2鍙犲姞鍏呭�� public int rechargeTimes;//鍏呭�兼鏁� @@ -53,7 +52,7 @@ /** * 閫氳繃byte杞琤ean * - * @param data + * @param data 璇诲崱鐨勬暟鎹� */ public static UserCard getBean(List<byte[]> data) { try { @@ -65,29 +64,22 @@ userCard.cardType = HexUtil.byteToHex(zero[0]); userCard.rechargeTimes = HexUtil.get16to10(HexUtil.byteToHex(zero[1])); byte[] swipeNumberBytes = new byte[2]; - swipeNumberBytes[0] = zero[2]; - swipeNumberBytes[1] = zero[3]; + System.arraycopy(zero,2,swipeNumberBytes,0,swipeNumberBytes.length); String swipeNumberHex = HexUtil.bytesToHex(swipeNumberBytes); userCard.swipeNumber = (short) HexUtil.get16to10(swipeNumberHex); byte[] electricityPriceBytes = new byte[4]; - for (int i = 0; i < 4; i++) { - electricityPriceBytes[i] = zero[i + 4]; - } + System.arraycopy(zero,4,electricityPriceBytes,0,electricityPriceBytes.length); + userCard.electricityPrice = HexUtil.hexToFloat(HexUtil.bytesToHex(electricityPriceBytes)); userCard.state = BcdUtil.bcdToStr(zero[8]); byte[] balanceBytes = new byte[4]; - for (int i = 0; i < 4; i++) { - balanceBytes[i] = zero[i + 9]; - } + System.arraycopy(zero,9,balanceBytes,0,balanceBytes.length); + userCard.balance = HexUtil.get16to10LowHigh(HexUtil.bytesToHex(balanceBytes)); byte[] addressCodeBytes = new byte[6]; - addressCodeBytes[0] = zero[13]; - addressCodeBytes[1] = zero[14]; - addressCodeBytes[2] = two[11]; - addressCodeBytes[3] = two[12]; - addressCodeBytes[4] = two[13]; - addressCodeBytes[5] = two[14]; + System.arraycopy(zero,13,addressCodeBytes,0,2); + System.arraycopy(two,11,addressCodeBytes,0,4); userCard.addressCode = BcdUtil.bcdToStr(addressCodeBytes); int year = 0; @@ -122,22 +114,8 @@ Calendar calendar = Calendar.getInstance(); calendar.set(2000 + year, month, day, hour, minute, second); byte[] initPeasantCodeBytes = new byte[16]; - initPeasantCodeBytes[0] = one[10]; - initPeasantCodeBytes[1] = one[11]; - initPeasantCodeBytes[2] = one[12]; - initPeasantCodeBytes[3] = one[13]; - initPeasantCodeBytes[4] = one[14]; - initPeasantCodeBytes[5] = two[0]; - initPeasantCodeBytes[6] = two[1]; - initPeasantCodeBytes[7] = two[2]; - initPeasantCodeBytes[8] = two[3]; - initPeasantCodeBytes[9] = two[4]; - initPeasantCodeBytes[10] = two[5]; - initPeasantCodeBytes[11] = two[6]; - initPeasantCodeBytes[12] = two[7]; - initPeasantCodeBytes[13] = two[8]; - initPeasantCodeBytes[14] = two[9]; - initPeasantCodeBytes[15] = two[10]; + System.arraycopy(one,10,initPeasantCodeBytes,0,5); + System.arraycopy(two,0,initPeasantCodeBytes,0,11); userCard.initPeasantCode = HexUtil.bytesToHex(initPeasantCodeBytes); return userCard; } @@ -157,7 +135,7 @@ /** * 鐢ㄦ埛鍗�0鍧� */ - public class Zero extends BaseCard { + public class Zero { public byte[] toByte() { byte[] data = new byte[16]; data[0] = HexUtil.hexToByte(cardType); @@ -167,25 +145,22 @@ e.printStackTrace(); } byte[] swipeNumbers = HexUtil.hexToByteArray(HexUtil.get10to16(swipeNumber)); - for (int i = 0; i < swipeNumbers.length; i++) { - data[i + 2] = swipeNumbers[i]; - } + + System.arraycopy(swipeNumbers, 0, data, 2, swipeNumbers.length); + byte[] electricityPrices = HexUtil.hexToByteArray(HexUtil.folatToHexString(electricityPrice)); - for (int i = 0; i < electricityPrices.length; i++) { - data[i + 4] = electricityPrices[i]; - } + + System.arraycopy(electricityPrices, 0, data, 4, electricityPrices.length); try { data[8] = BcdUtil.strToBcd(state)[0]; } catch (Exception e) { e.printStackTrace(); } byte[] balances = HexUtil.hexToByteArray(HexUtil.get10to16LowHigh(balance, 8)); - for (int i = 0; i < balances.length; i++) { - data[i + 9] = balances[i]; - } + + System.arraycopy(balances, 0, data, 9, balances.length); byte[] addresscodes = BcdUtil.strToBcd(addressCode); - data[13] = addresscodes[0]; - data[14] = addresscodes[1]; + System.arraycopy(addresscodes, 0, data, 13, 2); data[15] = getByteSum(data); return data; } @@ -195,14 +170,12 @@ /** * 鐢ㄦ埛鍗�1鍧� */ - public class One extends BaseCard { + public class One { public byte[] toBytes() { byte[] data = new byte[16]; byte[] balances = HexUtil.hexToByteArray(HexUtil.get10to16LowHigh(balance, 8)); - for (int i = 0; i < balances.length; i++) { - data[i] = balances[i]; - } + System.arraycopy(balances, 0, data, 0, balances.length); if (rechargeDate != null) { // 鑾峰彇骞淬�佹湀銆佹棩銆佹椂銆佸垎銆佺 @@ -221,6 +194,7 @@ byte bcdHour = (byte) ((hour / 10) << 4 | (hour % 10)); byte bcdMinute = (byte) ((minute / 10) << 4 | (minute % 10)); byte bcdSecond = (byte) ((second / 10) << 4 | (second % 10)); + data[4] = bcdYear; data[5] = bcdMonth; data[6] = bcdDay; @@ -229,9 +203,7 @@ data[9] = bcdSecond; } byte[] initPeasantCodes = HexUtil.hexToByteArray(initPeasantCode); - for (int i = 0; i < 5; i++) { - data[i + 10] = initPeasantCodes[i]; - } + System.arraycopy(initPeasantCodes, 5, data, 0, 5); data[15] = getByteSum(data); return data; } @@ -242,13 +214,11 @@ /** * 鐢ㄦ埛鍗�2鍧� */ - public class Two extends BaseCard { + public class Two { public byte[] toBytes() { byte[] data = new byte[16]; byte[] initPeasantCodes = HexUtil.hexToByteArray(initPeasantCode); - for (int i = 5; i < 16; i++) { - data[i - 5] = initPeasantCodes[i]; - } + System.arraycopy(initPeasantCodes, 5, data, 0, 11); byte[] addresscodes = BcdUtil.strToBcd(addressCode); data[11] = addresscodes[2]; data[12] = addresscodes[3]; -- Gitblit v1.8.0