Last active
January 2, 2016 06:08
-
-
Save askmeegs/8261189 to your computer and use it in GitHub Desktop.
Flashcards excerpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Piece of Flashcards.java | |
//Final Project, CS 230 Fall '12 | |
//Megan O'Keefe | |
//Last Modified: December 20th, 2012 | |
/*Takes what has been entered in the userEntry JTextField and does a .equalsIgnoreCase() | |
with what is stored in the back of the current card. If it returns true, modifies the | |
correctLabel to "Correct!", otherwise modifies the correctLabel to "Incorrect." | |
Retabulates accuracy and modifies the percentAccuracy label accordingly. | |
@param <code>String</code> representing user's guess | |
*/ | |
public void checkAnswer(String input) { | |
backLabel.setText(currentCard.getBack()); | |
answerPanel.repaint(); | |
answerPanel.revalidate(); | |
if (currentCard.getBack().equalsIgnoreCase(input)) { | |
correctLabel.setText("CORRECT!"); | |
correctLabel.setForeground(Color.GREEN.darker()); | |
numCorrect++; | |
} | |
else { | |
correctLabel.setText("Incorrect."); | |
correctLabel.setForeground(Color.RED); | |
} | |
} | |
/*mandatory override; this method does nothing | |
*/ | |
public void itemStateChanged(ItemEvent event) { | |
return; | |
} | |
/*The showNext method gets the next card in a deck and shows it in the GUI, | |
and waits for the user response. If the current deck is empty, it moves on | |
to the next deck in the list, as delineated in deckInfo. | |
If the list of decks is all used up, the session ends, and calls showStats. | |
*/ | |
public void showNext() { | |
correctLabel.setText(""); | |
if (cardsComplete > 0) //accounts for not putting acc @ 0 before the 1st card is tested | |
accuracy = ((numCorrect / (double)cardsComplete) * 100); | |
percentAccuracy.setText("" + formatAcc.format(accuracy) + "% accuracy"); | |
if(currentDeck.getSize() != 0) { //if current deck is not yet empty... | |
status.setText(currentComplete++ + "/" + currentDeckSize + " completed"); | |
currentCard = currentDeck.getNext(); | |
frontLabel.setText(currentCard.getFront()); | |
questionPanel.repaint(); | |
questionPanel.revalidate(); | |
backLabel.setText(" "); | |
answerPanel.repaint(); | |
answerPanel.revalidate(); | |
userEntry.setText(""); | |
} | |
else { //we are to move on to the next deck in the list... | |
if(decks.size() != 0) { //if the deck list is not empty... | |
currentDeck = decks.get(0); | |
String displayName = currentDeck.getName(); | |
for(int i=0; i <currentDeck.getName().length()-4; i++) { | |
if (currentDeck.getName().charAt(i) == '.') | |
displayName = displayName.substring(0, i); | |
} | |
deckLabel.setText(displayName); | |
decks.remove(0); | |
currentDeckSize = currentDeck.getSize(); | |
currentComplete = 0; | |
status.setText((int)currentComplete + "/" + currentDeckSize + " completed"); | |
currentCard = currentDeck.getNext(); | |
frontLabel.setText(currentCard.getFront()); | |
questionPanel.repaint(); | |
questionPanel.revalidate(); | |
backLabel.setText(" "); | |
answerPanel.repaint(); | |
answerPanel.revalidate(); | |
userEntry.setText(""); | |
} | |
else { //we are done with all of the decks; end session | |
complete = true; | |
status.setText(currentComplete++ + "/" + currentDeckSize + " completed"); | |
showStats(); | |
} | |
} | |
cardsComplete++; | |
} | |
/* | |
Once the session is declared complete, this method displays user statistics where | |
the back of the card is usually displayed, including information for this session, | |
and for all sessions on this machine. | |
*/ | |
public void showStats() { | |
//method variables to calculate user totals across all sessions | |
int totalCardsComplete; | |
int totalCorrect; | |
double totalAccuracy; | |
frontLabel.setText("SESSION COMPLETE!"); | |
Scanner scan = new Scanner(System.in); | |
File statsfile; | |
//try to read in stats file in the directory | |
try { | |
statsfile = new File("stats.txt"); | |
scan = new Scanner(statsfile); | |
} | |
catch (FileNotFoundException ex) { | |
System.out.println(ex); | |
return; | |
} | |
//read in the two pieces of data in stats.txt | |
totalCardsComplete = Integer.parseInt(scan.next()); | |
totalCorrect = Integer.parseInt(scan.next()); | |
//updates method variables based on read info; calculations on accuracy | |
totalCardsComplete += cardsComplete; //updates based on current session | |
totalCorrect += numCorrect; | |
double tempTotalComplete = totalCardsComplete + 0.0; | |
totalAccuracy = totalCorrect/tempTotalComplete; | |
//show current and compiled stats on the back panel | |
userEntry.setText(""); | |
backLabel.setText("User statistics:\n\n" + | |
"cards completed this session: " + cardsComplete + | |
"\ncards correct this session: " + numCorrect + | |
"\ntotal cards completed: " + (int)totalCardsComplete + | |
"\ntotal cards correct: " + (int)totalCorrect + | |
"\npercent accuracy this session: " + formatAcc.format(accuracy) + "%" + | |
"\ntotal percent accuracy: " + formatAcc.format(totalAccuracy*100) + "%"); | |
//write data to stats file to update statistics | |
if(statsfile != null) { | |
try { | |
FileWriter writer = new FileWriter(statsfile.getAbsoluteFile()); | |
BufferedWriter buffer = new BufferedWriter(writer); | |
totalCardsComplete = (int) totalCardsComplete; | |
totalCorrect = (int) totalCorrect; | |
buffer.write("" + totalCardsComplete + "\n"); | |
buffer.write("" + totalCorrect); | |
buffer.close(); | |
} | |
catch(IOException E) { | |
System.out.println(E); | |
return; | |
} | |
} | |
} | |
/* Listens for the pressing of the buttons "QUIT", "Check," and "Next" and | |
calls class methods checkAnswer and showNext appropriately | |
@param <code>ActionEvent</code> "event" representing the source of the action performed | |
*/ | |
public void actionPerformed(ActionEvent event) { | |
Object source = event.getSource(); | |
if (source.equals(quitMenuItem)) System.exit(0); | |
if (source.equals(checkButton)) { | |
//ensures the program doesn't keep running once complete | |
if(complete) correctLabel.setText("PRESS QUIT TO EXIT"); | |
else if(cardsComplete > 0) | |
checkAnswer(userEntry.getText()); | |
else correctLabel.setText("PRESS NEXT TO BEGIN..."); | |
} | |
if (source.equals(nextButton)) { | |
if(complete) correctLabel.setText("PRESS QUIT TO EXIT"); | |
else if (userEntry.getText().equals("") | |
&& !correctLabel.getText().equals("Incorrect.")) | |
checkAnswer(userEntry.getText()); | |
else showNext(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment