The code below will go through an excel spread sheet taken from kaggle, is named "SteamGames (71k games)" and the creator is "MEXWELL".
inside the for
loop I take only the required information, create a Game object which is very straight forward with only gets
, sets
and a compareTo
function for sorting later also a score
variable that I can assign and sort by.
I create ArrayList for the list of all games and one for only the name. This is important because I realized that there are duplicates inside the spreadsheet I downloaded so I have to check to see if the the game I’m adding is already on the list or not. This causes a bit of performance issue as it takes about 5 seconds on my machine to run the code but I don't see a better solution aside from finding another dataset.
as for my second part this is where I choose games that will get a score and then assign it, I included games that cost at least five dollars and have at least 20 reviews in total. Then I assign the score based on positive negative reviews and the price the higher amount of positive reviews will result in higher score while higher price and negative review count will result in lower score.
Also important to note that some games have some missing information hence the try
and catch
and the counter of "corrupted" games. In total there were about 250 out of 71k corrupted games.
Then I just sorted using a compareTo function implemented in the Game class chose the number of games that will get written into the txt file, got it written and thats it.
I deleted the titles from the excel sheet to make it easier go through as I knew in which places the names prices and reviews were.
If you have any feedback I would appreciate it, I’m not too disappointed with the performance issue but if you have some insight feel free to share and maybe where to go next with this project
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main{
public static void main(String[] args) throws Exception{
//first part
String line = "";
String splitBy = ",";
List<String[]> games=new ArrayList<String[]>();
try {
BufferedReader br = new BufferedReader(new FileReader("games.csv"));
while ((line = br.readLine()) != null) {
String[] game = line.split(splitBy); // use comma as separator
games.add(game);
}
List<String> listOfNames = new ArrayList<String>();
listOfNames.add(games.get(0)[1]);
List<Game> bestGames = new ArrayList<Game>();
for (String[] game : games) {
String name = game[1];
String price = game[7];
String positive = game[23];
String negative = game[24];
Game newGame = new Game(name, price, positive, negative);
if(listOfNames.contains(name) && !name.equals(games.get(0)[1])){
continue;
}
listOfNames.add(name);
bestGames.add(newGame);
}
//second part
int numberOfFails = 0;
for (int i = 1; i < bestGames.size(); i++) {
try {
double price = Double.parseDouble(bestGames.get(i).getPrice());
int positive = Integer.parseInt(bestGames.get(i).getPositive());
int negative = Integer.parseInt(bestGames.get(i).getNegative());
if(price >= 5.0 && (positive + negative >= 20)){
double score = (positive)/(price + negative);
bestGames.get(i).setScore(score);
}
} catch (Exception e) {
numberOfFails++;
}
}
Collections.sort(bestGames);
File bestGamesFile = new File("C:\\Users\\guybo\\Java_Data_Projects\\Main.java\\bestGames.txt");
FileWriter myWriter = new FileWriter("bestGames.txt");
int numberOfBestGames = 50;
for(int i = 0; i < numberOfBestGames; i++){
String name = bestGames.get(i).getName();
String price = bestGames.get(i).getPrice();
myWriter.write(name + "\n" + price + "$" + "\n" + "\n");
}
myWriter.close();
} catch (Exception e) {
e.getMessage();
}
}
}
public class Game implements Comparable<Game>{
private String name;
private String price;
private String positive;
private String negative;
private double score;
public Game(String name, String price, String positive, String negative){
this.name = name;
this.price = price;
this.positive = positive;
this.negative = negative;
this.score = 0;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getName() {
return name;
}
public String getNegative() {
return negative;
}
public String getPositive() {
return positive;
}
public String getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setNegative(String negative) {
this.negative = negative;
}
public void setPositive(String positive) {
this.positive = positive;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public int compareTo(Game game) {
return -Double.compare(this.score, game.getScore());
}
}