Skip to content

Instantly share code, notes, and snippets.

@nanduzira
Created June 5, 2018 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanduzira/655a11dea13ffec17fdc5535bc8a89bd to your computer and use it in GitHub Desktop.
Save nanduzira/655a11dea13ffec17fdc5535bc8a89bd to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
public class CricketMatch {
public static void main(String... args) throws Exception {
List<Matches> matches = readMatchesCSV("matches.csv");
List<Deliveries> deliveries = readDeliveriesCSV("deliveries.csv");
List<Matches> winners = new ArrayList<>();
for(Matches m : matches) {
if(m.getWinner().equals(m.getTossWinner()) && m.getTossDecision().equals("field") && m.getSeason().equals("2016")) {
System.out.println(m);
winners.add(m);
}
}
}
private static List<Matches> readMatchesCSV(String fileName) {
List<Matches> matches = new ArrayList<>();
File file = new File(fileName);
try(Scanner in = new Scanner(file)) {
String line = in.nextLine();
while(in.hasNextLine()) {
line = in.nextLine();
String[] attributes = line.split(",", 10);
Matches match = createMatch(attributes);
matches.add(match);
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
return matches;
}
private static List<Deliveries> readDeliveriesCSV(String fileName) {
List<Deliveries> deliveries = new ArrayList<>();
File file = new File(fileName);
try(Scanner in = new Scanner(file)) {
String line = in.nextLine();
while(in.hasNextLine()) {
line = in.nextLine();
String[] attributes = line.split(",");
Deliveries delivery = createDelivery(attributes);
deliveries.add(delivery);
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
return deliveries;
}
private static Matches createMatch(String[] metadata) {
int MATCH_ID = Integer.parseInt(metadata[0]);
int SEASON = Integer.parseInt(metadata[1]);
String CITY = metadata[2];
String DATE = metadata[3];
String TEAM1 = metadata[4];
String TEAM2 = metadata[5];
String TOSS_WINNER = metadata[6];
String TOSS_DECISION = metadata[7];
String RESULT = metadata[8];
String WINNER = metadata[9];
return new Matches(MATCH_ID, SEASON, CITY, DATE, TEAM1, TEAM2, TOSS_WINNER, TOSS_DECISION, RESULT, WINNER);
}
private static Deliveries createDelivery(String[] metadata) {
int MATCH_ID = Integer.parseInt(metadata[0]);
int INNING = Integer.parseInt(metadata[1]);
String BATTING_TEAM = metadata[2];
String BOWLING_TEAM = metadata[3];
int OVER = Integer.parseInt(metadata[4]);
int BALL = Integer.parseInt(metadata[5]);
String BATSMAN = metadata[6];
String BOWLER = metadata[7];
int WIDE_RUNS = Integer.parseInt(metadata[8]);
int BYE_RUNS = Integer.parseInt(metadata[9]);
int LEGBYE_RUNS = Integer.parseInt(metadata[10]);
int NOBALL_RUNS = Integer.parseInt(metadata[11]);
int PENALTY_RUNS = Integer.parseInt(metadata[12]);
int BATSMAN_RUNS = Integer.parseInt(metadata[13]);
int EXTRA_RUNS = Integer.parseInt(metadata[14]);
int TOTAL_RUNS = Integer.parseInt(metadata[15]);
return new Deliveries(MATCH_ID, INNING, BATTING_TEAM, BOWLING_TEAM, OVER, BALL, BATSMAN, BOWLER, WIDE_RUNS, BYE_RUNS, LEGBYE_RUNS, NOBALL_RUNS, PENALTY_RUNS, BATSMAN_RUNS, EXTRA_RUNS, TOTAL_RUNS);
}
}
class Deliveries {
private int MATCH_ID;
private int INNING;
private String BATTING_TEAM;
private String BOWLING_TEAM;
private int OVER;
private int BALL;
private String BATSMAN;
private String BOWLER;
private int WIDE_RUNS;
private int BYE_RUNS;
private int LEGBYE_RUNS;
private int NOBALL_RUNS;
private int PENALTY_RUNS;
private int BATSMAN_RUNS;
private int EXTRA_RUNS;
private int TOTAL_RUNS;
public Deliveries(int MATCH_ID, int INNING, String BATTING_TEAM, String BOWLING_TEAM, int OVER, int BALL,
String BATSMAN, String BOWLER, int WIDE_RUNS, int BYE_RUNS, int LEGBYE_RUNS,
int NOBALL_RUNS, int PENALTY_RUNS, int BATSMAN_RUNS, int EXTRA_RUNS, int TOTAL_RUNS) {
this.MATCH_ID = MATCH_ID;
this.INNING = INNING;
this.BATTING_TEAM = BATTING_TEAM;
this.BOWLING_TEAM = BOWLING_TEAM;
this.OVER = OVER;
this.BALL = BALL;
this.BATSMAN = BATSMAN;
this.BOWLER = BOWLER;
this.WIDE_RUNS = WIDE_RUNS;
this.BYE_RUNS = BYE_RUNS;
this.LEGBYE_RUNS = LEGBYE_RUNS;
this.NOBALL_RUNS = NOBALL_RUNS;
this.PENALTY_RUNS = PENALTY_RUNS;
this.BATSMAN_RUNS = BATSMAN_RUNS;
this.EXTRA_RUNS = EXTRA_RUNS;
this.TOTAL_RUNS = TOTAL_RUNS;
}
@Override
public String toString() {
return "Deliveries [MATCH_ID=" + MATCH_ID + ", INNING=" + INNING + ", BATTING_TEAM=" + BATTING_TEAM + ", BOWLING_TEAM=" + BOWLING_TEAM +
", OVER=" + OVER + ", BALL=" + BALL + ", BATSMAN=" + BATSMAN + ", BOWLER=" + BOWLER + ", WIDE_RUNS=" + WIDE_RUNS +
", BYE_RUNS=" + BYE_RUNS + ", LEGBYE_RUNS=" + LEGBYE_RUNS + ", NOBALL_RUNS=" + NOBALL_RUNS + ", PENALTY_RUNS=" + PENALTY_RUNS +
", BATSMAN_RUNS=" + BATSMAN_RUNS + ", EXTRA_RUNS=" + EXTRA_RUNS + ", TOTAL_RUNS=" +TOTAL_RUNS + "]";
}
}
class Matches {
private int MATCH_ID;
private int SEASON;
private String CITY;
private String DATE;
private String TEAM1;
private String TEAM2;
private String TOSS_WINNER;
private String TOSS_DECISION;
private String RESULT;
private String WINNER;
public Matches(int MATCH_ID, int SEASON, String CITY, String DATE, String TEAM1, String TEAM2, String TOSS_WINNER,
String TOSS_DECISION, String RESULT, String WINNER) {
this.MATCH_ID = MATCH_ID;
this.SEASON = SEASON;
this.CITY = CITY;
this.DATE = DATE;
this.TEAM1 = TEAM1;
this.TEAM2 = TEAM2;
this.TOSS_WINNER = TOSS_WINNER;
this.TOSS_DECISION = TOSS_DECISION;
this.RESULT = RESULT;
this.WINNER = WINNER;
}
public int getMatchId() {
return MATCH_ID;
}
public String getSeason() {
return String.valueOf(SEASON);
}
public String getTossWinner() {
return TOSS_WINNER;
}
public String getTossDecision() {
return TOSS_DECISION;
}
public String getWinner() {
return WINNER;
}
@Override
public String toString() {
return "Matches [MATCH_ID=" + MATCH_ID + ", SEASON=" + SEASON + ", CITY=" + CITY + ", DATE=" + DATE + ", TEAM1=" + TEAM1 + ", TEAM2=" + TEAM2 +
", TOSS_WINNER=" + TOSS_WINNER + ", TOSS_DECISSION=" + TOSS_DECISION + ", RESULT=" + RESULT + ", WINNER=" + WINNER + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment