# Batting Average Solution

## Batting Average Solution

National Cricket Academy (NCA) wants to monitor the performance of the players registered with them based on the average runs scored in the matches played.

As a software consultant of NCA, help them by writing a java program to meet their requirements.

The application needs to store the runs scored by a player and calculate the average score.

### Requirement 1: Store the runs scored by a player

Whenever a match is over, NCA needs to store the runs scored by the players for future reference.

The addScoreDetails method accepts runScored as argument and adds it to the scoreList.

### Requirement 2: Calculate the average run scored

NCA Academy needs to monitor the performance of a player periodically based on the average runs scored.

The method getAverageRunScored should calculate the average runs scored based on the scoreList.

### The average runs scored needs to be calculated as:

Average runs scored = sum of all runs available in the scoreList / size of the scoreList

Create a Main class with the main method.

Design the menu as described in the Sample Input and Output as:

2. Calculate average runs scored
3. Exit

When the choice is 1, get the runs scored from the user and add it to the scoreList.

When the choice is 2, display the average runs scored.

The entire program should be executed within a loop. It should not terminate after the completion of a functionality. When the choice provided is 3, it should terminate with a message “Thank you for using the Application”.

Please do not use System.exit(0). Instead use break to terminate the program.

Sample Input / Output 1:

Calculate average runs scored

Exit
1
Enter the runs scored
150

Calculate average runs scored

Exit
1
Enter the runs scored
50

Calculate average runs scored

Exit
1
Enter the runs scored
50

Calculate average runs scored

Exit
2
Average runs secured
83.33333333333333

Calculate average runs scored

Exit
3
Thank you for using the Application

## SOLUTION

Program: Batting Average Solution UserInterface.java

``````package com.ui;
import com.utility.Player;
import java.util.ArrayList;
import java.util.Scanner;
public class UserInterface {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Player player = new Player();
player.setScoreList(new ArrayList<>());
boolean flag = true;
while (flag) {
System.out.println("2. Calculate average runs scored");
System.out.println("3. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter the runs scored");
int score = scanner.nextInt();
break;
}
case 2: {
System.out.println("Average runs secured");
System.out.println(player.getAverageRunScored());
break;
}
case 3: {
System.out.println("Thank you for use the application");
flag = false;
break;
}
}
}
}
}``````

Program: Batting Average Solution Player.java

``````package com.utility;
import java.util.List;
public class  Player {
private List<Integer> scoreList;
public List<Integer> getScoreList() {
return scoreList;
}
public void setScoreList(List<Integer> scoreList) {
this.scoreList = scoreList;
}
public double getAverageRunScored() {
if (scoreList.isEmpty()) {
return 0.0;
}
int size = scoreList.size();
int totalScore = 0;
for (int score : scoreList) {
totalScore += score;
}
return (double) totalScore / (double) size;
}