Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q1_SendWithRetry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.cloudsufi.assignment;

public class Q1_SendWithRetry {

interface NotificationService {
void send(String message);

default void sendWithRetry(String message, int retries) {
for (int i = 0; i <= retries; i++) {
try {
send(message);
return; // success
} catch (RuntimeException e) {
if (i == retries) {
throw e;
}
}
}
}
}

public static void main(String[] args) {

NotificationService service = new NotificationService() {
int attempts = 0;

@Override
public void send(String message) {
attempts++;
if (attempts == 1) {
throw new RuntimeException("Temporary Glitch");
}
System.out.println("Email sent successfully on attempt " + attempts);
}
};

service.sendWithRetry("Important Update", 3);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q2_StringValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cloudsufi.assignment;

public class Q2_StringValidator {

@FunctionalInterface
interface StringValidator {
boolean check(String str);
}

static boolean validate(String value, StringValidator validator) {
return validator.check(value);
}

public static void main(String[] args) {

String input = "Hello";

boolean isNonEmpty = validate(input, s -> s != null && !s.isEmpty());
System.out.println("Is input non-empty: " + isNonEmpty);

boolean isLengthGreaterThanFive = validate(input, s -> s != null && s.length() > 5);
System.out.println("Is input length greater than 5: " + isLengthGreaterThanFive);

boolean isStartsWithCapitalLetter = validate(input, s -> s != null && !s.isEmpty() && Character.isUpperCase(s.charAt(0)));
System.out.println("Is input start with capital letter: " + isStartsWithCapitalLetter);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q3_StreamAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.cloudsufi.assignment;

import java.util.List;

public class Q3_StreamAPI {

static class User {
String name;
int age;

public User(String name, int age) {
this.name = name;
this.age = age;
}
}

public static void main(String[] args) {
List<User> users = List.of(new User("Vedanshu", 21), new User("Harsh", 23), new User("Anushka", 16));

List<String> processedNames = users.stream()
.filter(user -> user.age >= 18)
.map(user -> user.name.toUpperCase())
.sorted()
.toList();

System.out.println(processedNames);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q4_Predicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cloudsufi.assignment;

import java.util.List;
import java.util.function.Predicate;

public class Q4_Predicate {
public static void main(String[] args) {
List<Integer> numbers = List.of(-10, 2, 5, 8, -4);

Predicate<Integer> isPositive = n -> n > 0;
Predicate<Integer> isEven = n -> n % 2 == 0;

numbers.stream()
.filter(isPositive.and(isEven))
.forEach(System.out::println);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q5_Optional.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.cloudsufi.assignment;

import java.util.Optional;

public class Q5_Optional {
record User(String name, boolean isActive) {}

static Optional<User> findUser(String id) {
return switch (id) {
case "1" -> Optional.of(new User("Vedanshu", true)); // Active
case "2" -> Optional.of(new User("Harsh", false)); // Inactive
default -> Optional.empty(); // Not Found
};
}

static void processUser(String id) {
findUser(id)
.filter(User::isActive)
.map(User::name)
.ifPresentOrElse(
System.out::println,
() -> System.out.println("User not found")
);
}

public static void main(String[] args) {
processUser("1");
processUser("2");
processUser("3");
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q6_RecordEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cloudsufi.assignment;

public class Q6_RecordEmployee {
record Employee(int id, double salary) {
public Employee {
if (salary <= 0) {
throw new IllegalArgumentException("Salary should be positive");
}
}

public boolean isHighEarner() {
return salary > 100000;
}
}

public static void main(String[] args) {
Employee e = new Employee(1, 50000);
System.out.println(e.salary());
// e.salary = 60000; // ERROR: Records are immutable (final)
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q7_PaymentSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cloudsufi.assignment;

public class Q7_PaymentSystem {

sealed interface Payment permits CardPayment, UpiPayment {}

static final class CardPayment implements Payment {}
static final class UpiPayment implements Payment {}

static void process(Payment p) {
switch (p) {
case CardPayment c -> System.out.println("Processing Card...");
case UpiPayment u -> System.out.println("Processing UPI...");
}
}

public static void main() {
CardPayment cardPayment = new CardPayment();
process(cardPayment);

UpiPayment upiPayment = new UpiPayment();
process(upiPayment);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/cloudsufi/assignment/Q8_TextBlocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cloudsufi.assignment;

public class Q8_TextBlocks {
public static void main(String[] args) {
int userId = 101;

String query = """
SELECT id, name, email
FROM users
WHERE id = %d
ORDER BY created_at DESC
""".formatted(userId);

System.out.println(query);
}
}