Object-Oriented Programming with Java, part I – Exercise 24

This program is an extension of Exercise 13 and uses a if else statements to output NHL statistics.

My Code

import java.util.Scanner;
import nhlstats.NHLStatistics;

public class NhlStatisticsPart2 {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        System.out.println("NHL statistics service");

        while (true) {
            System.out.println("");
            System.out.print("command (points, goals, assists, penalties, player, club, quit): ");
            String command = reader.nextLine();

            if (command.equals("quit")) {
                break;
            }

            if (command.equals("points")) {
                // Print the top ten players sorted by points.
                NHLStatistics.sortByPoints();
                NHLStatistics.top(10);
            } else if (command.equals("goals")) {
                // Print the top ten players sorted by goals.
                NHLStatistics.sortByGoals();
                NHLStatistics.top(10);
            } else if (command.equals("assists")) {
                //  Print the top ten players sorted by assists.
                NHLStatistics.sortByAssists();
                NHLStatistics.top(10);
            } else if (command.equals("penalties")) {
                //  Print the top ten players sorted by penalties.
                NHLStatistics.sortByPenalties();
                NHLStatistics.top(10);
            } else if (command.equals("player")) {
                // Ask the user first which player's statistics are needed and then print them.
                System.out.print("Which player?: ");
                String playerName = reader.nextLine();
                NHLStatistics.searchByPlayer(playerName);
            } else if (command.equals("club")) {
                // Ask the user first which club's statistics are needed and then print them.
                // Note: When printing statistics they should be ordered by points (so the players with the most points come first).
                System.out.print("Which club?: ");
                String clubName = reader.nextLine();
                NHLStatistics.sortByPoints();
                NHLStatistics.teamStatistics(clubName);
            }

        }
    }
}

Model Code

import java.util.Scanner;

import nhlstats.NHLStatistics;

 

public class NhlStatisticsPart2 {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

 

        System.out.println("NHL statistics service");

 

        while (true) {

            System.out.println("");

            System.out.print("command (points, goals, assists, penalties, player, club, quit): ");

            String command = reader.nextLine();

 

            if (command.equals("quit")) {

                break;

            }

 

            if (command.equals("points")) {

                // Print the top ten players sorted by points.

                NHLStatistics.sortByPoints();

                NHLStatistics.top(10);

            } else if (command.equals("goals")) {

                // Print the top ten players sorted by goals.

                NHLStatistics.sortByGoals();

                NHLStatistics.top(10);

            } else if (command.equals("assists")) {

                //  Print the top ten players sorted by assists.

                NHLStatistics.sortByAssists();

                NHLStatistics.top(10);

            } else if (command.equals("penalties")) {

                //  Print the top ten players sorted by penalties.

                NHLStatistics.sortByPenalties();

                NHLStatistics.top(10);

            } else if (command.equals("player")) {

                // Ask the user first which player's statistics are needed and then print them.

                System.out.print("who: ");

                String pelaaja = reader.nextLine();

                nhlstats.NHLStatistics.searchByPlayer(pelaaja);

            } else if (command.equals("club")) {

                // Ask the user first which club's statistics are needed and then print them.

                // Note: When printing statistics they should be ordered by points (so the players with the most points come first).

                System.out.print("team: ");

                String team = reader.nextLine();

                NHLStatistics.sortByPoints();

                nhlstats.NHLStatistics.teamStatistics(team);

            }

 

        }

    }

}

Comments

There are some minor differences but mine does basically the same as the model code.

Leave a comment