EASY Process Notifications
Assignment
Solution
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
notification Interface2. The directMessage Struct
directMessage Struct3. The groupMessage Struct
groupMessage Struct4. The systemAlert Struct
systemAlert Struct5. The processNotification Function
processNotification FunctionβοΈ Example Usage
π§© Output
π§ Summary of How the Types Work Together
Type
Implements
importance() Logic
processNotification Output