My program is supposed to take words and definitions from the user and display them like flash cards. I've got all the words sorted out into classes and such and now all I need to do is make it so that when a button is pressed by my application, the controller class will execute a method that will go through the arraylist of Card classes and display the word and eventually the definition.
My issue is that I have an object of the reader class which contains all the cards, and I want to be able to call a random card in the getWordClick method. I don't know how I can use that object in another class.
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Scene.fxml"));
primaryStage.setTitle("FlashCards");
Scene scene = new Scene(root, 600, 400, Color.GREY);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main (String[] args){
Reader r = new Reader();
//Initialises the Arraylist and reads the file adding them to arraylist
ArrayList<String> wordList = r.getWordList();
r.OpenFile();
r.readFile(wordList);
r.closeFile();
//Initialises the Definitions Arraylist and reads the file adding them
ArrayList<String> definitionList = r.getDefinitionsList();
r.OpenFile();
r.readFile(definitionList);
r.closeFile();
/* IGNORE IS FOR TESTING PURPOSES
//Wordlist is printed
for (String i : wordList){
System.out.println(i);
}
//Definitions list is printed
for (String i : definitionList){
System.out.println(i);
} */
//Card for each word and def is made
ArrayList<Card> c = r.getCardList();
Main m = new Main();
r.cardSetter(m.addTerms(c, wordList.size(), wordList, definitionList));
//Loops through and displays the word and defs
for (Card i : c){
System.out.printf("%s : %s\n",i.dispWord(),i.dispDef());
}
//Displays the window
launch(args);
}
public ArrayList<Card> addTerms(ArrayList<Card> c, int q, ArrayList<String> word, ArrayList<String> def){
for (int i = 0; i<q; i++){
c.add(new Card(word,def,i));
}
return c;
}
}
Here is the reader class
public class Reader {
private Scanner x;
private Scanner sc;
//ArrayList to store the words
private ArrayList<String> wordList = new ArrayList<>();
//ArrayList to store the definitions
private ArrayList<String> definitionsList = new ArrayList<>();
//ArrayList to store the cards
private ArrayList<Card> cardList = new ArrayList<>();
//Simple scanner collects user input
public String getFileName(){
sc = new Scanner(System.in);
return sc.nextLine();
}
//Method to open the file and throw an exception if failed
public void OpenFile(){
try{
x = new Scanner(new File(getFileName()));
}
catch (Exception e){
System.out.println("could not find file");
}
}
//Assigns each line to a Array
public void readFile(ArrayList<String> e){
while(x.hasNext()){
e.add(x.nextLine());
}
}
//Closes file
public void closeFile(){
x.close();
}
//Returns the wordlist
public ArrayList<String> getWordList(){
return wordList;
}
//Returns Definitionlist
public ArrayList<String> getDefinitionsList(){
return definitionsList;
}
//Returns cardList
public ArrayList<Card> getCardList(){
return cardList;
}
public void cardSetter(ArrayList<Card> c){
c = cardList;
}
}
Here is the card class
public class Card {
private String word;
private String definition;
public Card(ArrayList<String> Word,ArrayList<String> Definition, int i){
word = Word.get(i);
definition = Definition.get(i);
}
public String dispWord(){
return word;
}
public String dispDef(){
return definition;
}
}
Finally here is the controller
public class Controller {
Random rand = new Random();
private int Random;
//Makes the rand instance variable int so that the def class can use it
public Button wordBox;
public Label defBox;
public void getWordClick(){
}
public void goExit(){
}
public void goRand(){
}
public void getDefClick(){
}
public void goNext(){
}
public void goPrev(){
}
}
Sorry I know its really long but the code is just there for reference, my main concern is how to i get the ArrayList<Card>
from Reader r
so that I can use it in the controller in the getWordClick()
method. Literally any help is appreciated, I just need someone to throw me in the right direction as I am stuck.
Update: I now edited the controller class so it looks like this public class Controller {
Random rand = new Random();
private int Random;
//Makes the rand instance variable int so that the def class can use it
public Button wordBox;
public Label defBox;
private Reader mReader = null;
public Controller(Reader reader){
this.mReader = reader;
}
public Reader getReader(){
return this.mReader;
}
public void getWordClick(){
getReader();
}
public void goExit(){
}
public void goRand(){
}
public void getDefClick(){
}
public void goNext(){
}
public void goPrev(){
}
}
But now the concern is that when the fxml file runs and looks for a controller how will it make an object itself or will it use an object I have created, because I made an object where i added the reader in as a constructor. However I do not know how the fxml file will use it for event handling.