瀏覽代碼

creditcard

Seth 7 年之前
父節點
當前提交
de041c041a

+ 8
- 0
pom.xml 查看文件

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
     <properties>
11 19
         <maven.compiler.source>1.8</maven.compiler.source>
12 20
         <maven.compiler.target>1.8</maven.compiler.target>

+ 48
- 0
src/main/java/com/zipcoder/payment/CreditCard.java 查看文件

@@ -0,0 +1,48 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment {
4
+    long id;
5
+    String payerName;
6
+    String number;
7
+    int expiredMonth;
8
+    int expiredYear;
9
+
10
+    public CreditCard(int id, String payerName, String number, int expiredMonth, int expiredYear) {
11
+        this.id = id;
12
+        this.payerName = payerName;
13
+        this.number = number;
14
+        this.expiredMonth = expiredMonth;
15
+        this.expiredYear = expiredYear;
16
+    }
17
+
18
+    public long getId() { return id; }
19
+
20
+    public void setId(long id) { this.id = id; }
21
+
22
+    public String getPayerName() { return payerName; }
23
+
24
+    public void setPayerName(String payerName) { this.payerName = payerName; }
25
+
26
+    public String getNumber() { return number; }
27
+
28
+    public void setNumber(String number) { this.number = number; }
29
+
30
+    public int getExpiredMonth() { return expiredMonth; }
31
+
32
+    public void setExpiredMonth(int expiredMonth) { this.expiredMonth = expiredMonth; }
33
+
34
+    public int getExpiredYear() { return expiredYear; }
35
+
36
+    public void setExpiredYear(int expiredYear) { this.expiredYear = expiredYear; }
37
+
38
+    public String getLastFourDigits(String number) {
39
+        int length = number.length();
40
+        return number.substring(length - 4, length);
41
+    }
42
+
43
+    public String getShortDescription() {
44
+
45
+        return "CC " + payerName + " " + getLastFourDigits(number) + " " + expiredMonth + "/" + expiredYear ;}
46
+
47
+
48
+}

+ 11
- 0
src/main/java/com/zipcoder/payment/Payment.java 查看文件

@@ -0,0 +1,11 @@
1
+package com.zipcoder.payment;
2
+
3
+public interface Payment {
4
+
5
+    public long getId();
6
+
7
+    public String getPayerName();
8
+
9
+    public String getShortDescription();
10
+
11
+}

+ 88
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java 查看文件

@@ -0,0 +1,88 @@
1
+package com.zipcoder.payment;
2
+
3
+
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+public class CreditCardTest {
9
+    CreditCard testCard;
10
+
11
+
12
+    @Before
13
+    public void initialize() {
14
+        testCard = new CreditCard(10, "John Hurt", "0043783590454335", 03,2020);
15
+
16
+    }
17
+
18
+    @Test
19
+    public void getSetIdTest() {
20
+        long expected = 100;
21
+        testCard.setId(100);
22
+        long actual = testCard.getId();
23
+
24
+        Assert.assertEquals(expected, actual);
25
+
26
+    }
27
+
28
+    @Test
29
+    public void getSetPayerNameTest() {
30
+        String expected = "Bob";
31
+        testCard.setPayerName("Bob");
32
+
33
+        String actual = testCard.getPayerName();
34
+
35
+        Assert.assertEquals(expected, actual);
36
+
37
+    }
38
+
39
+    @Test
40
+    public void getSetNumberTest() {
41
+        String expected = "4536";
42
+        testCard.setNumber("4536");
43
+
44
+        String actual = testCard.getNumber();
45
+
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test
50
+    public void getSetExpiredMonthTest() {
51
+        int expected = 10;
52
+        testCard.setExpiredMonth(10);
53
+
54
+        int actual = testCard.getExpiredMonth();
55
+
56
+        Assert.assertEquals(expected, actual);
57
+
58
+    }
59
+
60
+    @Test
61
+    public void getSetExpiredYearTest() {
62
+        int expected = 2021;
63
+        testCard.setExpiredYear(2021);
64
+
65
+        int actual = testCard.getExpiredYear();
66
+
67
+        Assert.assertEquals(expected, actual);
68
+    }
69
+
70
+    @Test
71
+    public void getLastFourDigitsTest() {
72
+        String expected = "4556";
73
+        String fullNumber = "230598904556";
74
+
75
+        String actual = testCard.getLastFourDigits(fullNumber);
76
+
77
+        Assert.assertEquals(expected, actual);
78
+
79
+    }
80
+
81
+    @Test
82
+    public void getShortDescriptionTest() {
83
+        String expected = "CC John Hurt 4335 3/2020";
84
+        String actual = testCard.getShortDescription();
85
+
86
+        Assert.assertEquals(expected, actual);
87
+    }
88
+}