实验内容:
信用卡号是一串长度介于13和16之间的一串数字。卡号必须是以4、5、6、或37开头,并遵循下面模式:
1) 从卡号最后一位数字开始,偶数位乘以2,如果乘以2的结果是两位数,那么就将这两位数加在一起得到一位数。
2) 将第一步所得到的所有一位数相加
3) 从卡号最后一位数字开始,奇数位数字相加
4) 将第二步和第三步的到的结果相加
5) 如果第四步得到的结果能被10整除,那么卡号是合法的,否则,卡号是不合法的。
package org.test; import java.util.Scanner; public class CreditCard { /** Return true if the card number is valid */ public boolean isValid(long number) { return (getSize(number) >= 13) && (getSize(number) <= 16) && (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 6) || prefixMatched(number, 37)) && (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0; } /** Get the result from Step 2 */ public int sumOfDoubleEvenPlace(long number) { int result = 0; number = number / 10; // Starting from the second digit from left while (number != 0) { result += getDigit((int)((number % 10) * 2)); number = number / 100; // Move to the next even place } return result; } /** Return this number if it is a single digit, otherwise, return * the sum of the two digits */ public int getDigit(int number) { return number % 10 + (number / 10); } /** Return sum of odd place digits in number */ //3) 从卡号最后一位数字开始,奇数位数字相加 public int sumOfOddPlace(long number) { int result = 0; while (number != 0) { result += (int)(number % 10); number = number / 100; // Move two positions to the left } return result; } /** Return true if the digit d is a prefix for number */ public boolean prefixMatched(long number, int d) { return getPrefix(number, getSize(d)) == d; } /** Return the number of digits in d */ public int getSize(long d) { int numberOfDigits = 0; while (d != 0) { numberOfDigits++; d = d / 10; } return numberOfDigits; } /** Return the first k number of digits from number. If the number * of digits in number is less than k, return number. */ public long getPrefix(long number, int k) { long result = number; for (int i = 0; i < getSize(number) - k; i++) result /= 10; return result; } }
参数化 && 覆盖率100% 测试设计:
package org.test; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class CreditCardTest { CreditCard creditCard; private long input; private int sumOfOddPlaceExpected; private int digitExpected; private int sumOfDoubleEvenPlaceExpected; private boolean isValidExpected; @Parameters @SuppressWarnings("unchecked") public static Collection prepareData1(){ Object[][] object = { {4111111111111111L,8,8,22,true}, {38520000023237L,21,3,19,false}, {6011000000000004L,5,5,5,true}, {100000000L,1,1,0,false}, {10000000000000000L,1,1,0,false}, {371449635398431L,33,6,47,true}, {5111111111111198L,15,6,22,false} }; return Arrays.asList(object); } public CreditCardTest(long input,int sumOfOddPlaceExpected,int digitExpected, int sumOfDoubleEvenPlaceExpected,boolean isValidExpected){ this.input = input; this.sumOfOddPlaceExpected = sumOfOddPlaceExpected; this.digitExpected = digitExpected; this.sumOfDoubleEvenPlaceExpected = sumOfDoubleEvenPlaceExpected; this.isValidExpected = isValidExpected; } @Before public void setUp() throws Exception { creditCard = new CreditCard(); } //检验信用卡号 @Test public void testIsValid() { boolean result = creditCard.isValid(input); Assert.assertEquals(isValidExpected,result); } //从卡号最后一位数字开始,偶数位乘以2,如果乘以2的结果是两位数,那么就将这两位数加在一起得到一位数。 @Test public void testSumOfDoubleEvenPlace() { int result = creditCard.sumOfDoubleEvenPlace(input); Assert.assertEquals(result, sumOfDoubleEvenPlaceExpected); } //如果两位数,那么就将这两位数加在一起得到一位数,否则返回本身 @Test public void testGetDigit() { int result = creditCard.getDigit(sumOfOddPlaceExpected); Assert.assertEquals(digitExpected, result); } //3)从卡号最后一位数字开始,奇数位数字相加 @Test public void testSumOfOddPlace() { int result = creditCard.sumOfOddPlace(input); Assert.assertEquals(sumOfOddPlaceExpected,result); } }
最新评论