Explorar el Código

got the lambdas working

Nicholas Maidanos hace 8 años
padre
commit
d8cb4c8026

+ 12
- 0
pom.xml Ver fichero

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 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 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>org.junit.jupiter</groupId>

+ 12
- 0
src/main/java/com/zipcoder/Comparators/Id.java Ver fichero

@@ -6,6 +6,18 @@ import java.util.Comparator;
6 6
 
7 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 21
     public int compare(Payment o1, Payment o2) {
10 22
         String id1 = Long.toString(o1.getId());
11 23
         String id2 = Long.toString(o2.getId());

+ 7
- 0
src/main/java/com/zipcoder/Comparators/PaymentComparators.java Ver fichero

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

+ 8
- 2
src/main/java/com/zipcoder/payment/PaymentPresenter.java Ver fichero

@@ -35,12 +35,18 @@ public class PaymentPresenter {
35 35
     public void orderBy(Payment[] payments){
36 36
         switch(this.order){
37 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 41
                 break;
40 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 46
                 break;
43 47
             case ID:
48
+                Comparator<Payment> idComparatorLamda =
49
+                        (Payment s1, Payment s2) -> {return new Long(s1.getId()).compareTo(s2.getId());};
44 50
                 this.bubbleSort(payments, new Id());
45 51
                 break;
46 52
             default:

+ 23
- 0
src/test/java/com/zipcoder/payment/PaymentOrderByPayerTest.java Ver fichero

@@ -11,6 +11,29 @@ import java.util.Comparator;
11 11
 public class PaymentOrderByPayerTest {
12 12
 
13 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 37
     public void compairTest1() {
15 38
         //When
16 39
         Comparator ps = new Payer();