-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubject.java
More file actions
119 lines (103 loc) · 2.88 KB
/
Subject.java
File metadata and controls
119 lines (103 loc) · 2.88 KB
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
package flashcards.model;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* A class which models a subject or topic to be filled with FlashCards.
*
* @author Daniel Nedrow
*/
public class Subject {
private String title;
private ArrayList<FlashCard> flashcards;
private int numFlashcards;
/**
* Create new subject with given title.
*
* @param title subject's title
*/
public Subject(String title) {
this.title = title;
flashcards = new ArrayList<>();
numFlashcards = 0;
}
/**
* @return the subject's title
*/
public String getTitle() {
return title;
}
/**
* @return the subject's ArrayList of FlashCards
*/
public ArrayList<FlashCard> getFlashCards() {
return flashcards;
}
/**
* Add FlashCard to subject with given question and answer.
*
* @param question flash card question
* @param answer flash card answer
*/
public void addFlashCard(String question, String answer) {
flashcards.add(new FlashCard(question, answer));
numFlashcards++;
}
/**
* Add given FlashCard object to this subject.
*
* @param flashcard FlashCard to add to subject
*/
public void addFlashCard(FlashCard flashcard) {
flashcards.add(flashcard);
numFlashcards++;
}
/**
* @return the number of flash cards in this subject
*/
public int getNumFlashcards() {
return numFlashcards;
}
/**
* cycles through the array list clearing each card's score
*/
public void resetFlashcards() {
for (FlashCard card : flashcards) {
card.resetScore();
}
}
public void removeFlashcard(FlashCard card) {
flashcards.remove(card);
numFlashcards--;
}
public void shuffleFlashcards() {
Collections.shuffle(flashcards);
}
/**
* Creates a new subject filled with flash cards by reading a properly-
* formatted file (specified as the parameter).
*
* @param subjectFile the specified file to read
* @return the new subject created by reading the file
*/
public static Subject createSubjectFromFile(File subjectFile) {
String[] questionAnswer;
Subject subject;
String title;
try {
Scanner input = new Scanner(subjectFile);
title = input.nextLine();
subject = new Subject(title);
while (input.hasNextLine()) {
questionAnswer = input.nextLine().split("@DL");
subject.addFlashCard(questionAnswer[0].trim(),
questionAnswer[1].trim());
}
return subject;
} catch (FileNotFoundException ex) {
return null;
}
}
}