Day 18 - Java

Problem Statement: El Classico - CSK vs MI

The Indian Premier League (IPL) is at its peak with two of the most formidable teams, Chennai Super Kings (CSK) and Mumbai Indians (MI), set to clash in the highly anticipated El Classico match. As a cricket enthusiast, you've been tasked with creating a program to simulate player selections for both teams to ensure a fair and exciting competition.

The rivalry between CSK and MI is legendary, often referred to as El Classico in the IPL. In this match, both teams aim to field their best players across various roles to outshine their opponent. Your task is to create a program that validates the player selections for both teams based on specific criteria.

Player Information:

Below are the player details along with their unique IDs for CSK and MI:

CSK Players:

MS Dhoni - 1

Ambati Rayudu - 2

Ruturaj Gaikwad - 3

Murali Vijay - 4

Subramaniya Badrinath - 5

Abhinav Mukund - 6

Ajinkya Rahane - 7

Deepak Chahar - 8

Shardul Thakur -  9

Mohit Sharma - 10

Suresh Raina - 11

Ravichandran Ashwin - 12

Ravindra Jadeja - 13

Faf du Plessis  - 14

Matthew Hayden - 15

Michael Hussey - 16

Devon Conway - 17

Stephen Fleming - 18

Brendon McCullum - 19

Shane Watson - 20

Mustafizur Rahman - 21

Doug Bollinger - 22

Matheesha Pathirana - 23

Dwayne Bravo - 24

Albie Morkel - 25

MI Players:

Rohit Sharma - 1

Sachin Tendulkar - 2

Surya Kumar Yadav -  3

Ishan Kishan - 4

Jasprit Bumrah - 5

Zaheer Khan -  6

Munaf Patel -  7

Praveen Kumar - 8

Hardik Pandya -  9

Tilak Varma - 10

Krunal Pandya -  11

Yuvraj Singh - 12

Corey Anderson - 13

Dewald Brewis - 14

Quinton de Cock - 15

Tim David - 16

Ricky Ponting - 17

Aaron Finch - 18

Luke Wood - 19

Lasith Malinga - 20

Mitchell McClenaghan - 21

Lendi Simmons - 22

JP Duminy - 23

Sanath Jayasuriya -  24

Glenn Maxwell -  25

Input Format:

The input consists of two lines:

The first line contains space-separated 11 IDs of selected CSK players.

The second line contains space-separated 11 IDs of selected MI players.

Output Format:

If the selection is valid, print "El Classico!" indicating the match can proceed. Otherwise, print the corresponding validation message.

HashMap Description:

To facilitate player selection and validation, a hashmap structure is employed to store player information for both Chennai Super Kings (CSK) and Mumbai Indians (MI). This hashmap allows efficient access to player details based on their roles.

For instance, the CSK hashmap, cskPlayers, is organized as follows:

Map<String, List<String>> cskPlayers = new HashMap<>();

Each key in the hashmap represents a player role (e.g., "Wicketkeeper", "Batsman", "Bowler", etc.), while the corresponding value is a list containing the names of players assigned to that role.

Example Entry:

cskPlayers.put("Wicketkeeper", Arrays.asList("MS Dhoni"));

cskPlayers.put("Batsman", Arrays.asList("Ambati Rayudu", "Ruturaj Gaikwad", "Murali Vijay", "Subramaniya Badrinath", "Abhinav Mukund", "Ajhinkya Rahane"));

Similarly, the MI hashmap, miPlayers, follows the same structure to store player details for Mumbai Indians.

This hashmap organization streamlines the process of accessing player information based on their roles, ensuring efficient player selection and validation mechanisms within the program.

Sample Input and Output:

Input:

1 3 4 7 8 10 11 13 14 19 23 24

1 2 3 4 5 9 12 14 18 19 25

Output:

El Classico!

Input:

1 3 4 7 8 10 11 13 14 19 23 24

1 2 3 5 9 12 14 18 19 25

Output:

MI must have exactly 1 wicketkeeper.

Input:

1 3 4 7 8 10 11 13 14 19

1 4 9 12 14 18 19 25 24 23 22

Output:

Each team should have 11 Players.

Input:

1 3 4 7 8 10 11 13 14 19 23

1 4 9 12 14 18 19 25 24 23 22

Output:

MI cannot have more than 4 foreign players.

Input:

1 2 3 4 5 6 7 8 10 11 13

1 2 3 4 5 9 12 14 18 19 25

Output:

CSK cannot have more than 5 batsmen.


SOLUTION:

import java.util.*;

public class IPLMatchSimulation {

    public static void main(String[] args) {

        Scanner sc= new Scanner(System.in);


        Map<String, List<String>> cskPlayers = new HashMap<>();

        cskPlayers.put("Wicketkeeper", Arrays.asList("MS Dhoni"));  

        cskPlayers.put("Batsman", Arrays.asList("Ambati Rayudu", "Ruturaj Gaikwad", "Murali Vijay", "Subramaniya Badrinath","Abhinav Mukund","Ajhinkya Rahane")); 

        cskPlayers.put("Bowler", Arrays.asList("Deepak Chahar", "Shardul Thakur", "Mohit Sharma" ));    

        cskPlayers.put("All Rounders", Arrays.asList("Suresh Raina", "Ravichandran Ashwin", "Ravindra Jadeja"));

        cskPlayers.put("Foreign Batsman", Arrays.asList("Faf du Plessis", "Matthew Hayden", "Michael Hussey", "Devon Conway", "Stephen Fleming","Brendon McCullum", "Shane Watson")); 

        cskPlayers.put("Foreign Bowler", Arrays.asList("Mustafizur Rahman", "Doug Bollinger","Matheesha Pathirana"));

        cskPlayers.put("Foreign All Rounders", Arrays.asList("Dwayne Bravo","Albie Morkel"));    


        Map<String, List<String>> miPlayers = new HashMap<>();

        miPlayers.put("Batsman", Arrays.asList("Rohit Sharma", "Sachin Tendulkar", "Surya Kumar Yadav"));

        miPlayers.put("Wicketkeeper", Arrays.asList("Ishan Kishan"));  

        miPlayers.put("Bowler", Arrays.asList("Jasprit Bumrah","Zaheer Khan","Munaf Patel","Praveen Kumar"));    

        miPlayers.put("All Rounders", Arrays.asList("Hardik Pandya","Tilak Varma" ,"Krunal Pandya", "Yuvraj Singh"));    

        miPlayers.put("Foreign Batsman", Arrays.asList("Corey Anderson","Dewald Brewis","Quinton de Cock","Tim David","Ricky Ponting","Aaron Finch"));

        miPlayers.put("Foreign Bowler", Arrays.asList("Luke Wood","Lasith Malinga","Mitchell McClenaghan")); 

        miPlayers.put("Foreign All Rounders", Arrays.asList("Lendi Simmons","JP Duminy","Sanath Jayasuriya","Glenn Maxwell"));    


        String[] cskPlayerIds = sc.nextLine().split("\\s+");

        String[] miPlayerIds = sc.nextLine().split("\\s+");

        if (cskPlayerIds.length != 11 || miPlayerIds.length != 11)

        {

          System.out.println("Each team should have 11 Players.");

          return;

        }


        String result = validateSelection(cskPlayerIds, miPlayerIds);

        if (result != null) {

            System.out.println(result);

            return;

        }


        System.out.println("El Classico!");

    }


    public static String validateSelection(String[] cskPlayerIds, String[] miPlayerIds) {

        int cskForeignCount = 0;

        int miForeignCount = 0;

        int cskWicketkeeperCount = 0;

        int miWicketkeeperCount = 0;

        int cskBatsmanCount = 0;

        int miBatsmanCount = 0;

        int cskBowlerCount = 0;

        int miBowlerCount = 0;


        for (String id : cskPlayerIds) {

            int playerId = Integer.parseInt(id);

            if (playerId >= 20 && playerId <= 25) {

                cskForeignCount++;

            }

            if (playerId == 1) {

                cskWicketkeeperCount++;

            }

            if (playerId >= 2 && playerId <= 7) {

                cskBatsmanCount++;

            }

            if (playerId >= 8 && playerId <= 13) {

                cskBowlerCount++;

            }

        }


        for (String id : miPlayerIds) {

            int playerId = Integer.parseInt(id);

            if (playerId >= 18 && playerId <= 24) {

                miForeignCount++;

            }

            if (playerId == 4) {

                miWicketkeeperCount++;

            }

            if (playerId >= 1 && playerId <= 3) {

                miBatsmanCount++;

            }

            if (playerId >= 5 && playerId <= 8) {

                miBowlerCount++;

            }

        }

       

        if (cskForeignCount > 4) {

            return "CSK cannot have more than 4 foreign players.";

        }

        if (miForeignCount > 4) {

            return "MI cannot have more than 4 foreign players.";

        }

        if (cskWicketkeeperCount != 1) {

            return "CSK must have exactly 1 wicketkeeper.";

        }

        if (miWicketkeeperCount != 1) {

            return "MI must have exactly 1 wicketkeeper.";

        }

        if (cskBatsmanCount > 5) {

            return "CSK cannot have more than 5 batsmen.";

        }

        if (miBatsmanCount > 5) {

            return "MI cannot have more than 5 batsmen.";

        }

        if (cskBowlerCount > 5) {

            return "CSK cannot have more than 5 bowlers.";

        }

        if (miBowlerCount > 5) {

            return "MI cannot have more than 5 bowlers.";

        }

        return null;

    }

}


Insights:

  • The program checks if both teams have exactly 11 players selected. This ensures adherence to the rules of cricket where each team must have 11 players on the field. If the number of selected players for either team is not 11, the program displays a message indicating the requirement for 11 players per team.
  • Players for both teams are selected based on their unique player IDs input by the user. Each player ID corresponds to a specific player in the respective team's lineup, allowing for accurate player selection.
  • The program tracks the number of foreign players selected for each team. It ensures that neither CSK nor MI exceeds the limit of 4 foreign players, which is a common rule in IPL matches to maintain balance and encourage local talent.
  • The program verifies that each team has exactly one wicketkeeper selected. This ensures that there is a player designated to fulfill the crucial role of wicketkeeping during the match.
  • The program checks that the number of selected batsmen and bowlers for each team does not exceed 5. This restriction ensures a balanced composition of the team, with an adequate number of players for batting and bowling roles.
  • Player information for both CSK and MI is stored in HashMaps. Each HashMap uses player roles (e.g., "Wicketkeeper", "Batsman", "Bowler") as keys and corresponding lists of player names as values. This organization facilitates easy access to player details based on their roles during the validation process.
  • The program splits the input string containing player IDs using whitespace as the delimiter.
  • The validateSelection method iterates through the player IDs of both teams. It checks various criteria such as foreign player count, wicketkeeper count, batsman count, and bowler count to ensure compliance with IPL regulations.
  • If the player selection meets all criteria, the program prints "El Classico!", indicating that the match can proceed. In case of any violation of selection criteria, the program displays a validation message specifying the particular criterion that has been breached.
Hey Java aficionados! The El Classico is live today! Don't miss out on the excitement, catch MS Dhoni in action, and keep up the Java practice for your own winning streak!
YelLOVE Coding :) 💛


Comments

Post a Comment