/* * Copyright (C) 2021 Ardi Madadi. All rights reserved. Permission is * hereby granted to students registered for University of Washington * CSE 331 for use solely during Summer Quarter 2021 for purposes of * the course. No other use, copying, distribution, or modification * is permitted without prior written consent. Copyrights for * third-party components of this work must be honored. Instructors * interested in reusing these course materials should contact the * author. */ // DataSet from https://www.kaggle.com/rajatrc1705/english-premier-league202021 import java.io.*; import java.util.List; import java.util.ArrayList; import java.util.stream.Collectors; /** * Parser utility to load the and display premier league's players and their goal tally in 2020-2021 season. */ public class LeagueReader { /** * English Premier League(2020-21) dataset. Each line of the input file contains a player and their information * standard stats such as Goals, Assists, xG, xA, Passes Attempted, Pass Accuracy and more * @param filename the file that will be read * @spec.requires filename is a valid file in the resources/data folder. * @return A list of strings formatted in the format "{player_name} + scored + {number_of_goals}" */ public static ArrayList parseData(String filename) { List lines = readLines(filename); ArrayList result = new ArrayList(); // A trivial loop. Reads each line in the file and saves the player name and the number of goals they have // scored in an instance of StringBuilder for (var line : lines) { StringBuilder newLine = new StringBuilder(); String playerName = line.split(",")[0]; String goals = line.split(",")[8]; newLine.append(playerName); newLine.append(" scored "); newLine.append(goals); result.add(newLine.toString()); } return result; } /** * Reads all lines contained within the provided data file, which is located * in this folder in this parser's classpath. * * @param filename The file to read. * @return A new {@link List} containing all lines in the file. * @throws IllegalArgumentException if the file doesn't exist, has an invalid name, or can't be read */ private static List readLines(String filename) { InputStream stream = LeagueReader.class.getResourceAsStream(filename); if (stream == null) { // stream is null if the file doesn't exist. // We want to handle this case so we don't try to call // readLines and have a null pointer exception. throw new IllegalArgumentException("No such file: " + filename); } return new BufferedReader(new InputStreamReader(stream)).lines().collect(Collectors.toList()); } // The main method running the methods public static void main(String[] args) { var playersGoals = parseData("EPL_20_21.csv"); for (var player : playersGoals) { System.out.println(player); } } }