import java.io.File;  // Import the File class
import java.io.FileNotFoundException;  // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
import java.util.ArrayList; // import the ArrayList class
/**
 * This project contains the file reading exercises.
 * 
 * This reads in a file called vgsales.csv.
 * 
 * This is a csv file that contains data on best selling games and how many units are sold.
 * Note that the number are in MILLIONS.
 * 
 */
public class GameSales{
    
    ArrayList<String> items = new ArrayList<String>(); //This is an arrayList of strings


    /**
     * This is a demo algorithm that loads in a file called shopping.txt and prints it out.
     * Try changing the items in shopping.txt and see what happens!
     */
    public void run(){
        try {
            File file = new File("shopping.txt");
            Scanner scan = new Scanner(file);
            while (scan.hasNextLine()) {
                String nextLine = scan.nextLine();
                System.out.println(nextLine);
            }
            scan.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found. Check the spelling and location of your file.");
        }
    }
    
    /**
     * Load in the data from shopping.txt into an Array.
     * Sort the array and print out the new list.
     */
    public void sortList(){
        //
    }
    
    /**
     * Load shopping.txt into an array and print out how many items are in the list
     */
    public void howMany(){
        //
    }
    
    //Following functions are related to vgsales.csv
    /**
     * Load the file vgsales.csv. Create an object for each Game. Then print the first 10 things in the list.
     * 
     * This current versions prints them all out!
     * 
     */
    public void mostPopular(){
        ArrayList<VideoGame> games = new ArrayList<VideoGame>();
        try {
            File file = new File("vgsales.csv");
            Scanner scan = new Scanner(file);
            String nextLine = scan.nextLine();//this removes the first line of the .csv file. It is often called a schema if you study databases.
            while (scan.hasNextLine()) {
                nextLine = scan.nextLine();
                String[] temp = nextLine.split(",");
                //index 1 is the name
                //index 10 is the global sales
                String gameName = temp[1];
                double gameSales = Double.parseDouble(temp[10]);
                VideoGame g = new VideoGame(gameName,gameSales);
                games.add(g);
            }
            scan.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found. Check the spelling and location of your file.");
        }
        
        for(VideoGame v:games){
            //YOUR CODE HERE - This is currently broken!
            System.out.println(v.getName());
        }
    }
    
    /**
     * Load the file and list all the games that have sold over 10 million
     */
    public void over10(){
        //YOUR CODE HERE
    }
    
    /**
     * Load the file and list all the games that have sold under 2 million
     */
    public void under2(){
        //YOUR CODE HERE
    }
    
    /**
     * Pick your favourite console and list all the popular games with that console. 
     * Extension: Let the user type a console name instead.
     */
    public void bestOf(){
        //YOUR CODE HERE
    }
}
