Exercise Overview
This advanced exercise demonstrates how to work with complex data structures in Dart, specifically usingMap<String, List<int>> to create a comprehensive student grade management system.
What You’ll Build
A system that manages student grades by subject and provides:Grade Tracking
Store multiple grades for each subject
Average Calculation
Calculate the average grade for each subject
Performance Analysis
Find the best and worst performing subjects
Complete Reports
Generate comprehensive grade reports
Problem Statement
Given a Map containing subjects and their corresponding grades:1
Calculate subject averages
Compute the average grade for each subject
2
Find best performance
Identify the subject with the highest average
3
Find worst performance
Identify the subject with the lowest average
4
Generate complete report
Display all subjects with their grades and averages
Solution Architecture
Data Structure
We use two Maps: one to store the raw grades (
Map<String, List<int>>) and another to store the calculated averages (Map<String, double>).Function 1: Calculate Subject Averages
Understanding forEach with Maps
Understanding forEach with Maps
The
forEach method on a Map provides two parameters:materia: The key (subject name)calificaciones: The value (list of grades)
Function 2: Find Best Performance
Function 3: Find Worst Performance
We initialize
peorPromedio to 100.0 (the maximum possible grade) so any actual grade will be lower.Function 4: Display Complete Report
Function 5: Display Averages
Complete Program
Sample Output
Key Concepts
Map Data Structure
Maps store key-value pairs for efficient data lookup
Nested Collections
Map<String, List<int>> combines Maps and ListsforEach Method
Iterate through Map entries with key-value pairs
Data Aggregation
Calculate statistics from collections of data
Advanced Concepts
Why use two separate Maps?
Why use two separate Maps?
We use two Maps because:
- calificacionesPorMateria: Stores the raw data (all individual grades)
- promedios: Stores the calculated averages
- Keep the original data intact
- Access both raw grades and averages when needed
- Recalculate averages if grades change
Alternative: Using Map methods
Alternative: Using Map methods
Dart provides functional programming methods that can simplify the code:
Handling edge cases
Handling edge cases
In a production system, consider:
Practice Challenges
Challenge 1: Overall Average
Add a function to calculate the overall average across all subjects.
Challenge 2: Grade Distribution
Create a function that shows how many grades fall into different ranges (90-100, 80-89, etc.).
Challenge 3: Student Class
Expand the system to manage multiple students, each with their own subject grades.
Challenge 4: Grade Input
Add functionality to let users input new subjects and grades interactively.
Enhanced Version: Multiple Students
Click to see an enhanced version managing multiple students
Click to see an enhanced version managing multiple students
Best Practices
Type Safety
Always specify types:
Map<String, List<int>>Immutability
Consider using final for maps that won’t be reassigned
Null Safety
Check for empty collections before operations
Clear Naming
Use descriptive names like
calificacionesPorMateria