import java.util.*;
import java.io.*;

public class CourseWork {

    private static final String store_filename = "coursework_store.txt";
    private static final String separator      = "-------------------------------------\n";
    private static final String end_of_file    = "=====================================";

    private static ArrayList<Course> courses = new ArrayList<>();

    public static void main (String[] args) throws FileNotFoundException, IOException{
        Scanner console = new Scanner(System.in);
        boolean done = false;
        while (! done) {
            menu();
            String choice = console.nextLine();
            choice        = choice.trim();
            if (choice.equals("1")){
                add_course(console);
            } else if (choice.equals("q")) {
                store_state();
                done = true;
            } else {
                System.out.println(choice + " is not a valid entry");
            }
        }
    }

    private static void menu(){
        String menu = "";
        menu += "1 : Add Course\n";
        menu += "q : Quit\n";
        menu += "? ";
        System.out.print(menu);
    }

    private static void store_state() throws IOException{
        String store_text = "";
        for (int i = 0; i < courses.size(); i++){
            Course c = courses.get(i);
            store_text += c.store_string();
            store_text += separator;
        }
        store_text += end_of_file;    
        FileWriter output = new FileWriter(store_filename);
        output.write(store_text);
        output.close();
    }

    private static void add_course(Scanner console) throws FileNotFoundException {
        Course c = Course.add_course(console);
        System.out.println(c); //debug
        courses.add(c);
    }
}
