Notification system

mail@pastecode.io avatar
unknown
java
5 months ago
5.9 kB
1
Indexable
import java.util.HashMap;
import java.util.Map;

// Interfaces

interface Notification {
    String getContent();
    String getType();  // e.g., "EMAIL", "SMS", "IN_APP"
}

interface User {
    String getUsername();
}

interface Notifier {
    void sendNotification(Notification notification, User user) throws Exception;
}

interface NotificationService {
    void send(Notification notification, User user);
}

// Implementations

class EmailNotification implements Notification {
    private String content;

    public EmailNotification(String content) {
        this.content = content;
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public String getType() {
        return "EMAIL";
    }
}

class SmsNotification implements Notification {
    private String content;

    public SmsNotification(String content) {
        this.content = content;
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public String getType() {
        return "SMS";
    }
}

class InAppNotification implements Notification {
    private String content;

    public InAppNotification(String content) {
        this.content = content;
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public String getType() {
        return "IN_APP";
    }
}

class UserImpl implements User {
    private String username;

    public UserImpl(String username) {
        this.username = username;
    }

    @Override
    public String getUsername() {
        return username;
    }
}

abstract class AbstractNotifier implements Notifier {
    private static final int MAX_RETRIES = 3;

    protected abstract void sendNotificationInternal(Notification notification, User user) throws Exception;

    @Override
    public void sendNotification(Notification notification, User user) throws Exception {
        int attempt = 0;
        while (attempt < MAX_RETRIES) {
            try {
                sendNotificationInternal(notification, user);
                System.out.println("Notification sent successfully.");
                return;
            } catch (Exception e) {
                attempt++;
                System.out.println("Failed to send notification. Attempt " + attempt + " of " + MAX_RETRIES);
                if (attempt >= MAX_RETRIES) {
                    System.out.println("All attempts failed. Could not send notification.");
                    throw e;  // Re-throw exception if all retries fail
                }
            }
        }
    }
}

class EmailNotifier extends AbstractNotifier {
    @Override
    protected void sendNotificationInternal(Notification notification, User user) throws Exception {
        if ("EMAIL".equals(notification.getType())) {
            // Logic to send email
            System.out.println("Sending email to " + user.getUsername() + ": " + notification.getContent());
            // Simulate success or failure
            // throw new Exception("Simulated failure"); // Uncomment to simulate failure
        }
    }
}

class SmsNotifier extends AbstractNotifier {
    @Override
    protected void sendNotificationInternal(Notification notification, User user) throws Exception {
        if ("SMS".equals(notification.getType())) {
            // Logic to send SMS
            System.out.println("Sending SMS to " + user.getUsername() + ": " + notification.getContent());
            // Simulate success or failure
            // throw new Exception("Simulated failure"); // Uncomment to simulate failure
        }
    }
}

class InAppNotifier extends AbstractNotifier {
    @Override
    protected void sendNotificationInternal(Notification notification, User user) throws Exception {
        if ("IN_APP".equals(notification.getType())) {
            // Logic to display in-app notification
            System.out.println("In-app notification for " + user.getUsername() + ": " + notification.getContent());
            // Simulate success or failure
            // throw new Exception("Simulated failure"); // Uncomment to simulate failure
        }
    }
}

class NotificationServiceImpl implements NotificationService {
    private Map<String, Notifier> notifiers = new HashMap<>();

    public NotificationServiceImpl() {
        // Register notifiers
        notifiers.put("EMAIL", new EmailNotifier());
        notifiers.put("SMS", new SmsNotifier());
        notifiers.put("IN_APP", new InAppNotifier());
    }

    @Override
    public void send(Notification notification, User user) {
        Notifier notifier = notifiers.get(notification.getType());
        if (notifier != null) {
            try {
                notifier.sendNotification(notification, user);
            } catch (Exception e) {
                System.out.println("Error sending notification: " + e.getMessage());
            }
        } else {
            System.out.println("No notifier found for type: " + notification.getType());
        }
    }
}

// Main Application

public class NotificationSystem {
    public static void main(String[] args) {
        // Create users
        User alice = new UserImpl("Alice");
        User bob = new UserImpl("Bob");

        // Create notifications
        Notification emailNotification = new EmailNotification("Welcome to our service!");
        Notification smsNotification = new SmsNotification("Your verification code is 123456.");
        Notification inAppNotification = new InAppNotification("You have a new message.");

        // Create notification service
        NotificationService notificationService = new NotificationServiceImpl();

        // Send notifications
        notificationService.send(emailNotification, alice);
        notificationService.send(smsNotification, bob);
        notificationService.send(inAppNotification, alice);
    }
}
Leave a Comment