-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuizCardPlayer.java
127 lines (108 loc) · 3.88 KB
/
QuizCardPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*A payback engine that can load a flashcard set and play it for a user*/
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class QuizCardPlayer {
private JTextArea display;
private JTextArea answer;
private ArrayList<QuizCard> cardList;
private QuizCard currentCard;
private int currentCardIndex;
private JFrame frame;
private JButton nextButton;
private boolean isShowAnswer;
public static void main(String[]args){
QuizCardPlayer reader =new QuizCardPlayer();
reader.go();
}
public void go(){
//build and display gui
frame =new JFrame("Quiz Card Builder");
JPanel mainPanel =new JPanel();
Font bigFont =new Font("sanserif",Font.BOLD,24);
display=new JTextArea(10,20);
display.setFont(bigFont);
display.setLineWrap(true);
display.setEditable(false);
JScrollPane qScroller =new JScrollPane(display);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
nextButton =new JButton("Show Question");
mainPanel.add(qScroller);
mainPanel.add(nextButton);
nextButton.addActionListener(new NextCardListener());
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu=new JMenu("File");
JMenuItem loadMenuItem =new JMenuItem("Load card set");
loadMenuItem.addActionListener(new OpenMenuListener());
fileMenu.add(loadMenuItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
frame.setSize(640,500);
frame.setVisible(true);
}
class NextCardListener implements ActionListener{
public void actionPerformed(ActionEvent ev ){
//if this is question, show the answer,otherwise show next question
//set a flag for whether we're viewing a question or answer
if (isShowAnswer){
//show the answer bc they've seen the question
display.setText(currentCard.getAnswer());
nextButton.setText("Next Card");
isShowAnswer=false;
}else{
//show the next question
if (currentCardIndex<cardList.size()){
showNextCard();
}else{
//there are no more cards
display.setText("That was last card");
nextButton.setEnabled(false);
}}}}
class OpenMenuListener implements ActionListener{
public void actionPerformed(ActionEvent ev ){
//bring up a file dialog box
//let the user navigate to and choose a card set to open
JFileChooser fileOpen=new JFileChooser();
fileOpen.showOpenDialog(frame);
loadFile(fileOpen.getSelectedFile()); }}
private void loadFile(File file){
//must build and array of cards, by reading them from a text file
//called from the OpenMenuListener event handler.reads the file one line at a time
//and tells the makeCard() method to make a new card out of the line
//one line in the file holds both the question and answer,separated by a "/"
cardList =new ArrayList<QuizCard>();
try{
BufferedReader reader =new BufferedReader(new FileReader(file));
String line =null;
while((line=reader.readLine())!=null){
makeCard(line);
}
reader.close();
}catch(Exception ex){
System.out.println("couldn't read the card line");
ex.printStackTrace();
}
//now time to start by showing the first card
showNextCard();
}
private void makeCard(String lineToParse){
//called by the loadFile method, takes a line from a text file
//and parses into two pieces-question & answer-and creates a new QuizCard
//and adds it to the ArrayList called CardList
String[] result =lineToParse.split("/");
QuizCard card =new QuizCard(result[0],result[1]);
cardList.add(card);
System.out.println("made a card");
}
private void showNextCard(){
currentCard=cardList.get(currentCardIndex);
currentCardIndex++;
display.setText(currentCard.getQuestion());
nextButton.setText("Show Answer");
isShowAnswer=true;
}
}