Skip to content

Commit e0d9248

Browse files
committed
code i created so far from beginning to prediate
1 parent 68f4d29 commit e0d9248

File tree

11 files changed

+426
-0
lines changed

11 files changed

+426
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.noah</groupId>
8+
<artifactId>javafunctional</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>23</maven.compiler.source>
13+
<maven.compiler.target>23</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package functionalinterface;
2+
3+
import java.util.function.BiConsumer;
4+
import java.util.function.Consumer;
5+
6+
public class _Consumer {
7+
public static void main(String[] args) {
8+
//creating customer
9+
Customer maria = new Customer("Maria", "99999");
10+
System.out.println("//Imperative normal Java Function");
11+
//Imperative
12+
greetCustomer(maria);
13+
14+
System.out.println("//Declarative");
15+
System.out.println("//Consumer functional Interface");
16+
//Consumer accepts single input and returns no result
17+
greetCustomer.accept(maria);
18+
19+
System.out.println("//BiConsumer functional Interface takes 2 inputs");
20+
//BiConsumer functional Interface takes 2 inputs
21+
System.out.println("//greetCustomerV2 true");
22+
greetCustomerV2.accept(maria, true);
23+
System.out.println("//greetCustomerV2 false");
24+
greetCustomerV2.accept(maria, false);
25+
}
26+
27+
//Declarative
28+
static Consumer<Customer> greetCustomer = customer ->
29+
System.out.println("hello " + customer.customerName + " thanks for registering phonenumber " + customer.customerPhoneNumber);
30+
31+
static BiConsumer<Customer, Boolean> greetCustomerV2 = (customer, showPhonenumber) ->
32+
System.out.println("hello " + customer.customerName + " thanks for registering phonenumber " + (showPhonenumber ? customer.customerPhoneNumber : "*******") );
33+
34+
//Imperative
35+
static void greetCustomer(Customer customer) {
36+
System.out.println("hello " + customer.customerName + " thanks for registering phonenumber " + customer.customerPhoneNumber);
37+
}
38+
39+
static class Customer {
40+
private String customerName;
41+
private String customerPhoneNumber;
42+
43+
public Customer(String customerName, String customerPhoneNumber) {
44+
this.customerName = customerName;
45+
this.customerPhoneNumber = customerPhoneNumber;
46+
}
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package functionalinterface;
2+
3+
import java.util.function.BiFunction;
4+
import java.util.function.Function;
5+
6+
public class _Function {
7+
public static void main (String[] args) {
8+
System.out.println("//Imperative");
9+
//Imperative
10+
int increment = incrementByOne(0);
11+
System.out.println(increment);
12+
13+
System.out.println(incrementByOneAndMultiply(1, 10));
14+
15+
System.out.println("//Declarative");
16+
System.out.println("//Function takes 1 argument and returns result");
17+
//Declarative
18+
//Function takes 1 argument and returns result
19+
int increment2 = incrementByOneFunction.apply(1);
20+
System.out.println(increment2);
21+
22+
System.out.println("//Chain two functions together with andThen");
23+
//Use two functions together with andThen
24+
Function<Integer, Integer> addBy1AndThenMultiplyBy10 =
25+
incrementByOneFunction.andThen(multiplyBy10Function);
26+
System.out.println(addBy1AndThenMultiplyBy10.apply(2));
27+
28+
System.out.println("//BiFunction takes two inputs and returns 1 output");
29+
//BiFunction takes two inputs and returns 1 output
30+
System.out.println(incrementByOneAndMultiplyBiFunction.apply(4, 100));
31+
32+
33+
34+
35+
}
36+
37+
//Declarative Functions
38+
static Function<Integer, Integer> incrementByOneFunction =
39+
number -> number + 1;
40+
41+
static Function<Integer, Integer> multiplyBy10Function = number -> number * 10;
42+
43+
static BiFunction<Integer, Integer, Integer> incrementByOneAndMultiplyBiFunction =
44+
(numberToIncrementByOne, numberToMultiplyBy) -> (numberToIncrementByOne + 1) * numberToMultiplyBy;
45+
46+
//Imperative
47+
static int incrementByOne(int number){
48+
49+
return number + 1;
50+
}
51+
52+
static int incrementByOneAndMultiply(int number, int numToMultiplyBy){
53+
54+
return (number + 1) * numToMultiplyBy;
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package functionalinterface;
2+
3+
import javax.swing.*;
4+
import java.util.function.BiPredicate;
5+
import java.util.function.Predicate;
6+
7+
public class _Predicate {
8+
public static void main(String[] args) {
9+
System.out.println("//Imperative normal Java Function");
10+
//Imperative
11+
System.out.println(isPhoneNumberValid("07000000000"));
12+
System.out.println(isPhoneNumberValid("08000000000"));
13+
14+
System.out.println("//Declarative");
15+
System.out.println("//Predicate functional Interface");
16+
//Predicate represents a boolean valued function of one argument
17+
System.out.println("is phonenumber valid = " + isPhonenumberValidPredicate.test("07000000000"));
18+
System.out.println("is phonenumber valid = " + isPhonenumberValidPredicate.test("08000000000"));
19+
20+
System.out.println("//Multiple Predicates");
21+
//Multiple Predicates
22+
System.out.println("is phonenumber valid and contains number 3 = " + isPhonenumberValidPredicate.and(containsNumber3).test("07300000000"));
23+
System.out.println("is phonenumber valid and contains number 3 = " + isPhonenumberValidPredicate.and(containsNumber3).test("08400000000"));
24+
25+
System.out.println("//BiPredicates take two inputs return one boolean");
26+
//BiPredicates take two inputs return one boolean
27+
System.out.println("phonenumber 1 matches phonenumber 2 = " + phonenumberEqualsSecondPhonenumber.test("0784039365", "0784039354"));
28+
System.out.println("phonenumber 1 matches phonenumber 2 = " + phonenumberEqualsSecondPhonenumber.test("0784039365", "0784039365"));
29+
}
30+
31+
//Declarative
32+
static Predicate<String> isPhonenumberValidPredicate = phoneNumber ->
33+
phoneNumber.startsWith("07") && phoneNumber.length() == 11;
34+
35+
static Predicate<String> containsNumber3 = phoneNumber ->
36+
phoneNumber.contains("3");
37+
38+
static BiPredicate<String, String> phonenumberEqualsSecondPhonenumber = (phoneNumber1, phoneNumber2) ->
39+
phoneNumber1.equals(phoneNumber2);
40+
41+
//Imperative
42+
static boolean isPhoneNumberValid(String phoneNumber){
43+
return phoneNumber.startsWith("07") && phoneNumber.length() == 11;
44+
}
45+
}

0 commit comments

Comments
 (0)