博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
测试 | 信用卡号验证
阅读量:4961 次
发布时间:2019-06-12

本文共 4483 字,大约阅读时间需要 14 分钟。

 

实验内容:

信用卡号是一串长度介于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);  	}}

  

  

转载于:https://www.cnblogs.com/jj81/p/9896745.html

你可能感兴趣的文章
20180104-高级特性-Slice
查看>>
6个SQL Server 2005性能优化工具介绍
查看>>
nginx启动、关闭命令、重启nginx报错open() "/var/run/nginx/nginx.pid" failed
查看>>
BZOJ 3097 Hash Killer I
查看>>
UINavigationController的视图层理关系
查看>>
html阴影效果怎么做,css 内阴影怎么做
查看>>
宏观经济
查看>>
综合练习:词频统计
查看>>
BZOJ1026: [SCOI2009]windy数
查看>>
样板操作数
查看>>
64位UBUNTU下安装adobe reader后无法启动
查看>>
iTextSharp带中文转换出来的PDF文档显示乱码
查看>>
组件:slot插槽
查看>>
走进C++程序世界------异常处理
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1988 Cube Stacking
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
Android------三种监听OnTouchListener、OnLongClickListener同时实现即其中返回值true或者false的含义...
查看>>
MATLAB实现多元线性回归预测
查看>>