ソースを参照

got the lambdas working

Nicholas Maidanos 8 年 前
コミット
d8cb4c8026

+ 12
- 0
pom.xml ファイルの表示

7
     <groupId>com.zipcoder</groupId>
7
     <groupId>com.zipcoder</groupId>
8
     <artifactId>payment</artifactId>
8
     <artifactId>payment</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
     <dependencies>
22
     <dependencies>
11
         <dependency>
23
         <dependency>
12
             <groupId>org.junit.jupiter</groupId>
24
             <groupId>org.junit.jupiter</groupId>

+ 12
- 0
src/main/java/com/zipcoder/Comparators/Id.java ファイルの表示

6
 
6
 
7
 public class Id implements Comparator<Payment> {
7
 public class Id implements Comparator<Payment> {
8
 
8
 
9
+    public static void main(String[] args){
10
+
11
+
12
+        Comparator<String> stringComparatorLamda =
13
+                (String s1, String s2) -> {return s1.compareTo(s2);};
14
+
15
+        int lambdaComparison = stringComparatorLamda.compare("Hello", "World");
16
+
17
+        System.out.println(lambdaComparison);
18
+
19
+    }
20
+
9
     public int compare(Payment o1, Payment o2) {
21
     public int compare(Payment o1, Payment o2) {
10
         String id1 = Long.toString(o1.getId());
22
         String id1 = Long.toString(o1.getId());
11
         String id2 = Long.toString(o2.getId());
23
         String id2 = Long.toString(o2.getId());

+ 7
- 0
src/main/java/com/zipcoder/Comparators/PaymentComparators.java ファイルの表示

1
+package com.zipcoder.Comparators;
2
+
3
+public class PaymentComparators {
4
+
5
+
6
+
7
+}

+ 8
- 2
src/main/java/com/zipcoder/payment/PaymentPresenter.java ファイルの表示

35
     public void orderBy(Payment[] payments){
35
     public void orderBy(Payment[] payments){
36
         switch(this.order){
36
         switch(this.order){
37
             case SHORTDESCRIPTION:
37
             case SHORTDESCRIPTION:
38
-                this.bubbleSort(payments, new ShortDescription());
38
+                Comparator<Payment> shortnameComparatorLamda =
39
+                        (Payment s1, Payment s2) -> {return s1.getShortDescription().compareTo(s2.getShortDescription());};
40
+                this.bubbleSort(payments, shortnameComparatorLamda);
39
                 break;
41
                 break;
40
             case PAYERNAME:
42
             case PAYERNAME:
41
-                this.bubbleSort(payments, new Payer());
43
+                Comparator<Payment> paynameComparatorLamda =
44
+                        (Payment s1, Payment s2) -> {return s1.getPayerName().compareTo(s2.getPayerName());};
45
+                this.bubbleSort(payments, paynameComparatorLamda);
42
                 break;
46
                 break;
43
             case ID:
47
             case ID:
48
+                Comparator<Payment> idComparatorLamda =
49
+                        (Payment s1, Payment s2) -> {return new Long(s1.getId()).compareTo(s2.getId());};
44
                 this.bubbleSort(payments, new Id());
50
                 this.bubbleSort(payments, new Id());
45
                 break;
51
                 break;
46
             default:
52
             default:

+ 23
- 0
src/test/java/com/zipcoder/payment/PaymentOrderByPayerTest.java ファイルの表示

11
 public class PaymentOrderByPayerTest {
11
 public class PaymentOrderByPayerTest {
12
 
12
 
13
     @Test
13
     @Test
14
+    public void lambda() {
15
+        //When
16
+        Comparator ps = new Payer();
17
+        Payment cc = new CreditCard(12, 4111131,"Jimmy Maidanos", 4, 11);
18
+        Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
19
+
20
+        Comparator<Payment> stringComparatorLamda =
21
+                (Payment s1, Payment s2) -> {return s1.getShortDescription().compareTo(s2.getShortDescription());};
22
+
23
+        int lambdaComparison = stringComparatorLamda.compare(pp, cc);
24
+
25
+        System.out.println(lambdaComparison);
26
+
27
+//        //Expect
28
+//        int expect = -1;
29
+//
30
+//        //Actual
31
+//        int actual = ps.compare(cc, pp);
32
+//
33
+//        assertEquals(expect, actual);
34
+    }
35
+
36
+    @Test
14
     public void compairTest1() {
37
     public void compairTest1() {
15
         //When
38
         //When
16
         Comparator ps = new Payer();
39
         Comparator ps = new Payer();