Skip to content

Commit f6735b7

Browse files
Arpit AggarwalArpit Aggarwal
authored andcommitted
Initial Commit
0 parents  commit f6735b7

18 files changed

+331
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bin
2+
.project
3+
.classpath
4+
.settings/

src/com/test/factory/Add.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.test.factory;
2+
3+
public class Add implements Operation {
4+
5+
@Override
6+
public int execute(final int x, final int y) {
7+
return x + y;
8+
}
9+
10+
}

src/com/test/factory/Divide.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.test.factory;
2+
3+
public class Divide implements Operation {
4+
5+
@Override
6+
public int execute(final int x, final int y) {
7+
return x / y;
8+
}
9+
10+
}

src/com/test/factory/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.test.factory;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
String operation = "add";
7+
String nextOperation = "subtract";
8+
Operation add = OperationFactory.getOperation(operation);
9+
System.out.println(add.execute(20, 10));
10+
Operation subtract = OperationFactory.getOperation(nextOperation);
11+
System.out.println(subtract.execute(20, 10));
12+
}
13+
14+
}

src/com/test/factory/Multiply.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.test.factory;
2+
3+
public class Multiply implements Operation {
4+
5+
@Override
6+
public int execute(final int x, final int y) {
7+
return x * y;
8+
}
9+
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.test.factory;
2+
3+
@FunctionalInterface
4+
public interface Operation {
5+
int execute(final int x, final int y);
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.test.factory;
2+
3+
public class OperationFactory {
4+
5+
public static Operation getOperation(String operation) {
6+
switch (operation) {
7+
case "add":
8+
return new Add()::execute;
9+
case "subtract":
10+
return new Subtract()::execute;
11+
case "multiply":
12+
return new Multiply()::execute;
13+
case "divide":
14+
return new Divide()::execute;
15+
default:
16+
return null;
17+
}
18+
}
19+
}

src/com/test/factory/Subtract.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.test.factory;
2+
3+
public class Subtract implements Operation {
4+
5+
@Override
6+
public int execute(final int x, final int y) {
7+
return x - y;
8+
}
9+
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.test.programs;
2+
3+
import java.util.function.IntPredicate;
4+
import java.util.stream.IntStream;
5+
6+
public class FizzBuzz {
7+
8+
private static final IntPredicate fizz = (x) -> x % 3 == 0;
9+
private static final IntPredicate buzz = (x) -> x % 5 == 0;
10+
11+
public static void main(String[] args) {
12+
IntStream.range(1, 100).boxed().map(element -> {
13+
if (fizz.and(buzz).test(element)) {
14+
return "Fizz Buzz";
15+
} else if (fizz.test(element)) {
16+
return "Fizz";
17+
} else if (buzz.test(element)) {
18+
return "Buzz";
19+
}
20+
return element;
21+
}).forEach(System.out::println);
22+
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.test.so.solutions;
2+
3+
import java.util.stream.IntStream;
4+
5+
public class AlphabetsLooping {
6+
static final String input = "hello";
7+
8+
public static void main(String[] args) {
9+
StringBuilder builder = new StringBuilder("");
10+
input.chars().mapToObj(i -> builder.append((char) i))
11+
.forEach(System.out::println);
12+
13+
System.out.println("********");
14+
15+
IntStream.range(0, input.length())
16+
.mapToObj(index -> input.substring(0, index + 1))
17+
.forEach(System.out::println);
18+
}
19+
}

0 commit comments

Comments
 (0)