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
40 changes: 40 additions & 0 deletions Solutions/assignment/vishwas/Q1_NotificationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package assignment.vishwas;

public class Q1_NotificationService {

interface NotificationService{
void send(String message);

default void sendWithRetry(String message,int retries){ // Used for Backward Compatibility
if(retries<0){
throw new IllegalArgumentException("Retry count cannot be negative.");
}
for(int attempt=1;attempt<=retries+1;attempt++){ // First Attempt + Additional attempts after failure
try {
send(message);
System.out.println("Message sent on attempt: " + attempt);
return;
}
catch (Exception e) {
if(attempt>retries){
throw new RuntimeException("Failed sending message.");
}
}

}
}
}

public static void main(String[] args) {
NotificationService notificationService = new NotificationService() {
@Override
public void send(String message) {
if(message==null){
throw new RuntimeException("Message cannot be null.");
}
System.out.println("Sending message: " + message);
}
};
notificationService.sendWithRetry("Hello Cloudsufi!",3);
}
}
26 changes: 26 additions & 0 deletions Solutions/assignment/vishwas/Q2_StringValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package assignment.vishwas;

public class Q2_StringValidator {
@FunctionalInterface
public interface stringValidator {
boolean isValid(String str);
}
public static boolean validate(String str, stringValidator validator) { // Utility Method
return validator.isValid(str);
}

public static void main(String[] args) {
String test = "Hello Cloudsufi!";

boolean isNonEmpty = validate(test,s->s!=null && !s.isEmpty());
System.out.println("Is String Non-Empty?: " + isNonEmpty);

boolean isLengthGreaterThan5 = validate(test,s->s!=null && s.length()>5);
System.out.println("Is String Length Greater than 5?: " + isLengthGreaterThan5);

boolean isStartingWithCapitalLetter = validate(test,s->s!=null && !s.isEmpty() && Character.isUpperCase(s.charAt(0)));
System.out.println("Is String starting with a Capital Letter?: " + isStartingWithCapitalLetter);


}
}
28 changes: 28 additions & 0 deletions Solutions/assignment/vishwas/Q3_Stream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package assignment.vishwas;
import java.util.List;

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

static void main(String[] args) {
List<User> users = List.of(
new User("Vishwas",22),
new User("Aakash",21),
new User("Himani",23)
);

List <String> filteredAndSorted = users.stream()
.filter(user->user.age>18)
.map(user -> user.name.toUpperCase())
.sorted()
.toList();
System.out.println(filteredAndSorted);
}
}
19 changes: 19 additions & 0 deletions Solutions/assignment/vishwas/Q4_Predicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package assignment.vishwas;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Q4_Predicates {
static void main(String[] args) {
List<Integer> numbers = Arrays.asList(-5, -2, 0, 1, 2, 3, 4, 6, 9);

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

Predicate<Integer> isPositiveAndEven = isPositive.and(isEven);

numbers.stream()
.filter(isPositiveAndEven)
.forEach(System.out::println);
}
}
28 changes: 28 additions & 0 deletions Solutions/assignment/vishwas/Q5_Optional.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package assignment.vishwas;

import java.util.Optional;

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

static Optional <User> findUser(String id) {
if("101".equals(id)){
return Optional.of(new User("vishwas",true));
} else if ("102".equals(id)) {
return Optional.of(new User("keshav",false));
}
else return Optional.empty();
}
static void userProcessor(String id){
findUser(id)
.filter(User::isActive)
.map(User::username)
.ifPresentOrElse(System.out::println,()-> System.out.println("User not found"));

}
static void main(String[] args) {
userProcessor("101");
userProcessor("102");
userProcessor("104");
}
}
19 changes: 19 additions & 0 deletions Solutions/assignment/vishwas/Q6_Records.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package assignment.vishwas;

public class Q6_Records {
record Employee(Integer id, Integer salary){
public Employee{
if(salary<0){
throw new IllegalArgumentException("Salary must be greater than zero");
}
}
public boolean isHigherEarner(){
return salary>100000;
}
}

static void main(String[] args) {
Employee emp = new Employee(1035, 50000);
System.out.println(emp.salary());
}
}
46 changes: 46 additions & 0 deletions Solutions/assignment/vishwas/Q7_PaymentSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package assignment.vishwas;

public class Q7_PaymentSystem {
sealed interface Payment permits CardPayment, UpiPayment{
}

static final class CardPayment implements Payment{
private final String cardNumber;

CardPayment(String cardNumber){
this.cardNumber=cardNumber;
}

String getCardNumber(){
return cardNumber;
}
}
static final class UpiPayment implements Payment{
private final String upiId;

UpiPayment(String upiId){
this.upiId=upiId;
}

String getUpiId(){
return upiId;
}
}

static void process(Payment pmt){
if(pmt instanceof CardPayment card){
System.out.println("Processing Card Payment for card no.: "+card.getCardNumber());
}
else if(pmt instanceof UpiPayment upi){
System.out.println("Processing UPI Payment for UPI ID: "+upi.getUpiId());
}
else{
throw new IllegalStateException("Invalid Payment Type");
}
}

static void main(String[] args) {
process(new CardPayment("1234-5678-9101-3476"));
process(new UpiPayment("9876776541@upi"));
}
}
21 changes: 21 additions & 0 deletions Solutions/assignment/vishwas/Q8_TextBlocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package assignment.vishwas;

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

String query = """
Select
id,
username,
email,
status
FROM users
WHERE id='%s'
AND status = 'ACTIVE'
ORDER BY username
""".formatted(userId);

System.out.println(query);
}
}