Recap - Custom Collection
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 75 of 110.
Challenge
EasyLet's build a ScoreBoard system that tracks player scores and demonstrates how special methods work together to create a polished, professional class!
You'll organize your code into two files:
score_board.dart: Create aPlayerclass and aScoreBoardclass that work together to manage game scores.Your
Playerclass should have aname(String) andscore(int). It needs to:- Implement
Comparable<Player>so players can be sorted by score in descending order (highest first) - Override
==andhashCodeso two players with the same name are considered equal (regardless of score) - Override
toString()to return:[name]: [score] points
Your
ScoreBoardclass should store a list of players and provide:- An
addPlayer(Player player)method that adds a player only if they don't already exist (use your equality implementation!) - A
call()method that takes no arguments and returns a sorted copy of the players list (sorted by score, highest first) - Override
toString()to display all players (unsorted), one per line, with the headerScoreBoard:on the first line
- Implement
main.dart: Import your score board file and demonstrate how all these special methods work together:- Create a
ScoreBoard - Add these players:
Alicewith150,Bobwith200,Charliewith175 - Try adding
Aliceagain with score999(it should be rejected as a duplicate) - Print the scoreboard using its
toString() - Print an empty line
- Print
Rankings: - Call the scoreboard like a function to get sorted rankings, then print each player
- Create a
This challenge brings together toString() for display, == and hashCode for duplicate detection, compareTo() for sorting, and call() for a clean callable interface!
Expected output:
ScoreBoard:
Alice: 150 points
Bob: 200 points
Charlie: 175 points
Rankings:
Bob: 200 points
Charlie: 175 points
Alice: 150 pointsTry it yourself
import 'score_board.dart';
void main() {
// TODO: Create a ScoreBoard
// TODO: Add players: Alice (150), Bob (200), Charlie (175)
// TODO: Try adding Alice again with score 999 (should be rejected as duplicate)
// TODO: Print the scoreboard using its toString()
// TODO: Print an empty line
// TODO: Print "Rankings:"
// TODO: Call the scoreboard like a function to get sorted rankings, then print each player
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor