EASY Process Notifications

Textio now has a system to process different types of notifications: direct messages, group messages, and system alerts. Each notification type has a unique way of calculating its importance score based on user interaction and content.

Assignment

1

Implement importance methods for each message type

2

Complete processNotification function

Solution

main.go
package main

type notification interface {
	importance() int
}

type directMessage struct {
	senderUsername string
	messageContent string
	priorityLevel  int
	isUrgent       bool
}

func (m directMessage) importance() int{
	priortyScore := m.priorityLevel
	urgent:= m.isUrgent

	if urgent{
		return 50
	}else{
		return priortyScore
	}
}

type groupMessage struct {
	groupName      string
	messageContent string
	priorityLevel  int
}

func (g groupMessage) importance() int{
	return g.priorityLevel
}

type systemAlert struct {
	alertCode      string
	messageContent string
}

func (s systemAlert) importance() int{
	return 100
}

func processNotification(n notification) (string, int) {
	switch v := n.(type){
	case directMessage:
		return v.senderUsername, v.importance()
	case groupMessage:
		return v.groupName, v.importance()
	case systemAlert:
		return v.alertCode, v.importance()
	default:
		return "",0
	}
}

πŸ” Concept Breakdown

1. The notification Interface

  • This defines a contract: Any type (struct) that wants to be considered a β€œnotification” must have an importance() method that returns an int.

  • This allows polymorphism β€” meaning, you can treat different types (directMessage, groupMessage, systemAlert) the same way as long as they satisfy this interface.


2. The directMessage Struct

  • Represents a direct message between users.

  • The fields store who sent it, the message text, its priority, and whether it’s urgent.

Its method:

  • Implements the importance() method required by the notification interface.

  • If urgent β†’ returns a fixed score of 50.

  • Otherwise β†’ returns its priority level.

Because it defines importance(), this struct satisfies the notification interface.


3. The groupMessage Struct

  • Represents messages sent to a group.

  • Its importance() method simply returns priorityLevel.


4. The systemAlert Struct

  • Represents system-generated alerts (like errors or warnings).

  • Always has the maximum importance:


5. The processNotification Function

  • This function accepts any value that implements notification.

  • It uses a type switch (switch v := n.(type)) to detect what specific struct the notification is.

  • Depending on the type:

    • directMessage β†’ return username + importance score

    • groupMessage β†’ return group name + importance score

    • systemAlert β†’ return alert code + importance score

    • Unknown β†’ return empty string and 0


βš™οΈ Example Usage

🧩 Output


🧠 Summary of How the Types Work Together

Type
Implements
importance() Logic
processNotification Output

directMessage

notification

50 if urgent, else priorityLevel

Username + importance

groupMessage

notification

Returns priorityLevel

Group name + importance

systemAlert

notification

Always 100

Alert code + importance

All three structs satisfy the same interface, which allows you to pass them to a single function (processNotification) that can handle them differently based on their concrete type β€” that’s Go polymorphism in action.