Thursday, 1 July 2021

Java Program to add Football player details and sort them by number of players

 

A university records data of its Football players. Drawthe class diagram and develop the class FootballPlayerwith name, numberOfGoals and numberOfMatches.Code the constructor, toString() method. The main()method in Demo class must store data of footballplayers in a collection and is menu driven with thefollowing options: a) Add new player b)sort by numberof goals.

Class diagram                                                                                               

 

FootballPlayer

-name:String

-numberOfGoals:int

-numberOfMatches:int

+FootballPlayer(name: String, numberOfGoals:int, numberOfMatches:int)

+getNumberOfGoals():int

 

SortByNumberOfGoals

 

+compare(f1:FootballPlayer, f2: FootballPlayer):int

 

 

Demo

 

+main(args:String[]):void

 

 

 

FootballPlayer.java                                                                                        

package key;

 

public class FootballPlayer

{

            private String name;

            private int numberOfGoals,numberOfMatches;

           

            public int getNumberOfGoals() {

                        return numberOfGoals;

            }

 

            public void setNumberOfGoals(int numberOfGoals) {

                        this.numberOfGoals = numberOfGoals;

            }

 

            public FootballPlayer(String name, int numberOfGoals, int numberOfMatches)

            {

                        this.name = name;

                        this.numberOfGoals = numberOfGoals;

                        this.numberOfMatches = numberOfMatches;

            }

           

            public String toString()

            {

                        return "name=" + name + ", numberOfGoals=" + numberOfGoals + ", numberOfMatches="

                                                + numberOfMatches;

            }

           

           

}

 

Demo.java

package key;

 

import java.util.*;

class SortByNumberOfGoals implements Comparator<FootballPlayer>                    

{

            public int compare(FootballPlayer f1,FootballPlayer f2 )

            {

                        return (int)(f1.getNumberOfGoals() -f2.getNumberOfGoals());

            }

}

public class Demo                                                                                          

{

            public static void main(String[] args)

            {

                        List<FootballPlayer> fp = new ArrayList<FootballPlayer>();

                        Scanner sc= new Scanner(System.in);

                       

                        while(true)

                        {

                                    System.out.println("enter 1. Add new player 2.sort by numberof goals 3. exit");

                                    int choice=sc.nextInt();

                                    switch(choice)

                                    {

                                    case 1:

                                                System.out.println("enter player name, number of goals, numbe of matches");

                                                fp.add(new FootballPlayer(sc.next(),sc.nextInt(),sc.nextInt()));

                                                break;

                                    case 2:

                                                System.out.println("sorted players list based on number of goals");

                                                Collections.sort(fp,new SortByNumberOfGoals());

                                                for(FootballPlayer ob:fp)

                                                            System.out.println(ob);

                                                break;

                                    case 3:

                                                return;

                                    default:

                                                System.out.println("invalid choice");

                                               

                                    }

                        }

                       

            }

 

}

 

Output:

enter 1. Add new player 2.sort by numberof goals 3. exit

1

enter player name, number of goals, numbe of matches

sarvani

34

20

enter 1. Add new player 2.sort by numberof goals 3. exit

1

enter player name, number of goals, numbe of matches

lakshmi

12

40

enter 1. Add new player 2.sort by numberof goals 3. exit

2

sorted players list based on number of goals

name=lakshmi, numberOfGoals=12, numberOfMatches=40

name=sarvani, numberOfGoals=34, numberOfMatches=20

enter 1. Add new player 2.sort by numberof goals 3. exit

3

No comments:

Post a Comment