Low-Level Design: Building a Cricket Scoring and Commentary System like Cricinfo in Java

In this low-level design, we have meticulously crafted a Cricket Scoring and Commentary System using Java. The system encompasses fundamental cricket entities such as players, teams, matches, innings, and more. The design includes intricate details like ball outcomes, wicket types, and match formats. Notably, the addition of a live commentary system, encapsulating events with timestamps, overs, balls, and messages, enhances the user experience. With an emphasis on clarity and extensibility, this design lays the groundwork for a robust and flexible cricket application.

  1. Player:
    • Responsibility: Represents a cricket player with a name.
    • Usage: Instances of this class are used to model players in a team.
  2. Team:
    • Responsibility: Represents a cricket team with a name and a list of players.
    • Usage: Instances of this class are used to model teams participating in a match.
  3. BallOutcome:
    • Responsibility: Enumerates different outcomes of a cricket ball, including runs, wickets, and extras.
    • Usage: Used to categorize the possible outcomes of each ball bowled during a match.
  4. Runs:
    • Responsibility: Represents the runs scored in a match, including total runs and extras.
    • Usage: Instances of this class are used to track the runs scored in each inning.
  5. WicketType:
    • Responsibility: Enumerates different types of wickets in cricket.
    • Usage: Used to categorize the type of dismissal when a wicket falls.
  6. Wicket:
    • Responsibility: Represents a wicket in cricket, capturing details like the batsman, wicket type, and bowler.
    • Usage: Instances of this class are used to record instances of wickets during a match.
  7. Ball:
    • Responsibility: Represents a ball bowled in cricket, including the outcome, batsman, and bowler.
    • Usage: Instances of this class are used to model each ball bowled during a match.
  8. Over:
    • Responsibility: Represents an over in cricket, consisting of a list of balls.
    • Usage: Instances of this class are used to model the overs bowled in an inning.
  9. CommentaryEvent:
    • Responsibility: Represents a commentary event, capturing details such as the timestamp, over, ball, and commentary message.
    • Usage: Instances of this class are used to record events for live commentary during a match.
  10. Commentary:
    • Responsibility: Represents live commentary for a match, incorporating a list of commentary events.
    • Usage: Instances of this class are used to manage and display live commentary during a match.
  11. Inning:
    • Responsibility: Represents an inning in cricket, comprising overs, runs, and wickets.
    • Usage: Instances of this class are used to model innings in a match.
  12. Match:
    • Responsibility: Represents a cricket match, encompassing match format, innings, teams, winning team, match result, and toss details.
    • Usage: Instances of this class are used to model an entire cricket match.
  13. MatchFormat:
    • Responsibility: Enumerates different formats of cricket matches (e.g., T20, TEST).
    • Usage: Used to categorize the format of a cricket match.
  14. MatchResult:
    • Responsibility: Enumerates different outcomes of a cricket match (e.g., WIN, DRAW, LOSS).
    • Usage: Used to categorize the result of a cricket match.
  15. Toss:
    • Responsibility: Represents the toss in cricket, capturing details like the winning team, choosing team, and decision (bat or bowl).
    • Usage: Instances of this class are used to model the toss details in a match.
  16. LiveScores:
    • Responsibility: Represents live scores for a match.
    • Usage: Instances of this class are used to display live scores during a match.
  17. Tournament:
    • Responsibility: Represents a cricket tournament, consisting of a list of matches.
    • Usage: Instances of this class are used to model an entire cricket tournament.

These classes collectively form a well-organized and modular system for managing cricket-related entities and events, providing a solid foundation for building a comprehensive cricket scoring and commentary application.

import java.time.LocalDateTime;
import java.util.*;

// Enum for different match formats
enum MatchFormat {
    T20,
    TEST
}

// Enum for ball outcomes including extra types and wicket types
enum BallOutcome {
    DOT,
    ONE,
    TWO,
    THREE,
    FOUR,
    SIX,
    WICKET,
    WIDE,
    NO_BALL,
    BYE,
    LEG_BYE
}

// Enum for match result
enum MatchResult {
    WIN,
    DRAW,
    LOSS
}

// Enum for wicket types
enum WicketType {
    BOWLED,
    CAUGHT,
    LBW,
    RUN_OUT,
    STUMPED
}

// Class representing a cricket player
class Player {
    private String name;

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

// Class representing a cricket team
class Team {
    private String name;
    private List<Player> players;

    public Team(String name, List<Player> players) {
        this.name = name;
        this.players = players;
    }

    public String getName() {
        return name;
    }

    public List<Player> getPlayers() {
        return players;
    }
}

// Class representing a ball in cricket
class Ball {
    private BallOutcome outcome;
    private String batsman;  // Name of the batsman
    private String bowler;   // Name of the bowler

    public Ball(BallOutcome outcome, String batsman, String bowler) {
        this.outcome = outcome;
        this.batsman = batsman;
        this.bowler = bowler;
    }

    public BallOutcome getOutcome() {
        return outcome;
    }

    public String getBatsman() {
        return batsman;
    }

    public String getBowler() {
        return bowler;
    }
}

// Class representing runs scored in a match, including extras
class Runs {
    private int totalRuns;
    private int extras;

    public Runs() {
        this.totalRuns = 0;
        this.extras = 0;
    }

    public void addRuns(int runs) {
        totalRuns += runs;
    }

    public void addExtras(int extras) {
        this.extras += extras;
    }

    public int getTotalRuns() {
        return totalRuns;
    }

    public int getExtras() {
        return extras;
    }
}

// Class representing a wicket in cricket
class Wicket {
    private String batsman;  // Name of the batsman
    private WicketType wicketType;
    private String bowler;   // Name of the bowler

    public Wicket(String batsman, WicketType wicketType, String bowler) {
        this.batsman = batsman;
        this.wicketType = wicketType;
        this.bowler = bowler;
    }

    public String getBatsman() {
        return batsman;
    }

    public WicketType getWicketType() {
        return wicketType;
    }

    public String getBowler() {
        return bowler;
    }
}

// Class representing an over in cricket
class Over {
    private List<Ball> balls;

    public Over() {
        this.balls = new ArrayList<>();
    }

    public void addBall(Ball ball) {
        balls.add(ball);
    }

    public List<Ball> getBalls() {
        return balls;
    }
}

// Class representing a commentary event in cricket
class CommentaryEvent {
    private LocalDateTime timeStamp;
    private int over;
    private int ball;
    private String message;

    public CommentaryEvent(LocalDateTime timeStamp, int over, int ball, String message) {
        this.timeStamp = timeStamp;
        this.over = over;
        this.ball = ball;
        this.message = message;
    }

    public LocalDateTime getTimeStamp() {
        return timeStamp;
    }

    public int getOver() {
        return over;
    }

    public int getBall() {
        return ball;
    }

    public String getMessage() {
        return message;
    }
}

// Class representing live commentary for a match
class Commentary {
    private Match match;
    private List<CommentaryEvent> commentaryEvents;

    public Commentary(Match match) {
        this.match = match;
        this.commentaryEvents = new ArrayList<>();
    }

    public void addCommentaryEvent(int over, int ball, String message) {
        LocalDateTime timeStamp = LocalDateTime.now();
        CommentaryEvent event = new CommentaryEvent(timeStamp, over, ball, message);
        commentaryEvents.add(event);
    }

    public List<CommentaryEvent> getCommentaryEvents() {
        return commentaryEvents;
    }
}

// Class representing an inning in cricket
class Inning {
    private List<Over> overs;
    private Runs totalRuns;
    private List<Wicket> wickets;

    public Inning() {
        this.overs = new ArrayList<>();
        this.totalRuns = new Runs();
        this.wickets = new ArrayList<>();
    }

    public void addOver(Over over) {
        overs.add(over);
    }

    public List<Over> getOvers() {
        return overs;
    }

    public Runs getTotalRuns() {
        return totalRuns;
    }

    public void updateTotalRuns(int runs) {
        totalRuns.addRuns(runs);
    }

    public void updateExtras(int extras) {
        totalRuns.addExtras(extras);
    }

    public void addWicket(Wicket wicket) {
        wickets.add(wicket);
    }

    public List<Wicket> getWickets() {
        return wickets;
    }
}

// Class representing a cricket match
class Match {
    private MatchFormat matchFormat;
    private List<Inning> innings;
    private Map<String, Team> teams;  // Map of team names to team objects
    private Team winningTeam;  // The team that won the match
    private MatchResult matchResult;
    private Toss toss;

    public Match(MatchFormat matchFormat, Map<String, Team> teams) {
        this.matchFormat = matchFormat;
        this.innings = new ArrayList<>();
        this.teams = teams;
    }

    public void addInning(Inning inning) {
        innings.add(inning);
    }

    public List<Inning> getInnings() {
        return innings;
    }

    public MatchFormat getMatchFormat() {
        return matchFormat;
    }

    public Map<String, Team> getTeams() {
        return teams;
    }

    public void setWinningTeam(Team winningTeam) {
        this.winningTeam = winningTeam;
    }

    public Team getWinningTeam() {
        return winningTeam;
    }

    public void setMatchResult(MatchResult matchResult) {
        this.matchResult = matchResult;
    }

    public MatchResult getMatchResult() {
        return matchResult;
    }

    public void setToss(Toss toss) {
        this.toss = toss;
    }

    public Toss getToss() {
        return toss;
    }
}

// Class representing a cricket tournament
class Tournament {
    private List<Match> matches;

    public Tournament() {
        this.matches = new ArrayList<>();
    }

    public void addMatch(Match match) {
        matches.add(match);
    }

    public List<Match> getMatches() {
        return matches;
    }
}

// Class representing a toss in cricket
class Toss {
    private Team winningTeam;
    private Team choosingTeam;
    private String decision;  // Decision made by the choosing team - "bat" or "bowl"

    public Toss(Team winningTeam, Team choosingTeam, String decision) {
        this.winningTeam = winningTeam;
        this.choosingTeam = choosingTeam;
        this.decision = decision;
    }

    public Team getWinningTeam() {
        return winningTeam;
    }

    public Team getChoosingTeam() {
        return choosingTeam;
    }

    public String getDecision() {
        return decision;
    }
}

// Class representing live scores for a match
class LiveScores {
    private Match match;

    public LiveScores(Match match) {
        this.match = match;
    }

    public void displayLiveScores() {
        // Logic to display live scores
    }
}

public class Cricinfo {
    public static void main(String[] args) {
        // Sample usage to create a T20 match
        List<Player> teamAPlayers = Arrays.asList(new Player("Player1"), new Player("Player2"), new Player("Player3"));
        List<Player> teamBPlayers = Arrays.asList(new Player("Player4"), new Player("Player5"), new Player("Player6"));

        Team teamA = new Team("TeamA", teamAPlayers);
        Team teamB = new Team("TeamB", teamBPlayers);

        Map<String, Team> teams = new HashMap<>();
        teams.put("TeamA", teamA);
        teams.put("TeamB", teamB);

        Match t20Match = new Match(MatchFormat.T20, teams);

        Inning firstInning = new Inning();
        Over firstOver = new Over();
        firstOver.addBall(new Ball(BallOutcome.SIX, "Player1", "Player4"));
        firstInning.addOver(firstOver);

        Inning secondInning = new Inning();
        Over secondOver = new Over();
        secondOver.addBall(new Ball(BallOutcome.FOUR, "Player2", "Player5"));
        secondInning.addOver(secondOver);

        t20Match.addInning(firstInning);
        t20Match.addInning(secondInning);

        Toss toss = new Toss(teamA, teamA, "bat");  // TeamA wins the toss and chooses to bat
        t20Match.setToss(toss);

        t20Match.setMatchResult(MatchResult.WIN);  // TeamA wins the match
        t20Match.setWinningTeam(teamA);

        Tournament tournament = new Tournament();
        tournament.addMatch(t20Match);

        // Sample usage to create live commentary and scores
        Commentary commentary = new Commentary(t20Match);
        commentary.addCommentaryEvent(1, 1, "Exciting match in progress!");
        commentary.addCommentaryEvent(1, 1, "First inning: 6 runs in the first over!");
        commentary.addCommentaryEvent(2, 3, "TeamA wins the match!");

        LiveScores liveScores = new LiveScores(t20Match);
        liveScores.displayLiveScores();
    }
}

Add a match

The sequence diagram for adding a match should have the following actors and objects that will interact with each other:

  • Actors: AdminUmpire, and Commentator
  • Objects: Match and Stadium

The steps involved in adding a match are listed below:

  1. The admin creates a new match of a specific match type.
  2. The admin adds the playing teams for the match.
  3. The admin assigns a stadium to the match.
  4. The admin assigns an umpire to the match.
  5. The admin assigns a commentator to the match.
Admin Stadium Umpire Commentator sd add match Match createMatch(MatchType) match created addTeam(Playing11) team added addTeam(Playing11) team added assignStatdium(Match) stadium assigned assignUmpire(Match) umpire assigned assignCommentator(Match) commentator assigned

You may also like...