-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemDatabase.java
232 lines (203 loc) · 6.42 KB
/
ProblemDatabase.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import java.io.*;
import java.util.*;
public class ProblemDatabase
{
/**
* ArrayList of ArrayLists of the problems, with the upper ArrayList
* corresponding to the type (0 = kinematics, 1=newton's laws, 2=energy,
* 3=rotation, 4=gravitation, 5=fluids)
*/
ArrayList<ArrayList<Problem>> problemListSorted;
/**
* ArrayList of all the Problems in random order
*/
ArrayList<Problem> problemListUnsorted;
/**
* Holds all of the problems that have been used.
*/
ArrayList<Problem> usedList;
/**
* Constructs a ProblemDatabase by automatically creating problems from the
* problems.txt at the given pathfield.
*
* @param pathfield
* the path for which to look for the problems.txt file
* @author Austin Lei
*/
public ProblemDatabase( String pathfield )
{
problemListSorted = new ArrayList<ArrayList<Problem>>();
problemListUnsorted = new ArrayList<Problem>();
usedList = new ArrayList<Problem>();
Scanner readIn = null;
for ( int i = 0; i < 6; i++ )
{
problemListSorted.add( new ArrayList<Problem>() );
}
try
{
readIn = new Scanner( new File( pathfield ) );
}
catch ( FileNotFoundException exc )
{
System.out.println( "Cannot find problems file" );
System.exit( 0 );
}
while ( readIn.hasNext() )
{
String problemImage = readIn.next();
String solutionImage = readIn.next();
String name = readIn.next();
int category = readIn.nextInt();
String answer = readIn.next();
char ans = answer.charAt( 0 );
Problem prob = new Problem( problemImage, solutionImage, name, category, ans );
addProblem( prob );
}
}
/**
* Moves all of the used problems backed into the two problem lists, both
* sorted and unsorted.
*
* @author Austin Lei
*/
public void reset()
{
while ( !usedList.isEmpty() )
{
Problem pr = usedList.get( 0 );
usedList.remove( pr );
problemListUnsorted.add( pr );
problemListSorted.get( pr.getType() ).add( pr );
}
}
/**
* Gives a random problem of any type. Moves the problem off of the two
* problem lists and onto the used list.
*
* @return the random problem
* @author Austin Lei
*/
public Problem giveRandProblem()
{
if ( problemListUnsorted.size() > 0 )
{
Problem prob = problemListUnsorted
.get( (int)( Math.random() * problemListUnsorted.size() ) );
problemListUnsorted.remove( prob );
problemListSorted.get( prob.getType() ).remove( prob );
usedList.add( prob );
return prob;
}
return null;
}
/**
* Gives a random problem of a specific type. Moves the problem off of the
* two problem lists and onto the used list.
*
* @param type
* the type of problem to give
* @return the random problem of that type
* @author Austin Lei
*/
public Problem giveRandProblem( int type )
{
if ( problemListSorted.get( type ).size() > 0 )
{
Problem prob = problemListSorted.get( type )
.get( (int)( Math.random() * problemListSorted.get( type ).size() ) );
problemListUnsorted.remove( prob );
problemListSorted.get( type ).remove( prob );
usedList.add( prob );
return prob;
}
return null;
}
/**
* Removes problem of the given name from the arraylists. If the type is not
* -1, checks if the found problem also matches the type. If the arraylists
* does not contain a Problem of the name (or of name and type, if given),
* then returns false. Otherwise, removes Problem and returns true.
*
* @param name
* Name of the Problem to remove.
* @param type
* Type of the problem to remove. Precondition: int from -1 to 5
* @return whether or not the problem was removed correctly
* @author Krishnakumar Bhattaram
*/
public boolean removeProblem( String name, int type )
{
if ( type == -1 )
{
Problem toremove = null;
for ( Problem p : problemListUnsorted )
{
if ( p.getName().equals( name ) )
{
toremove = p;
break;
}
}
if ( toremove == null )
{
return false;
}
problemListUnsorted.remove( toremove );
problemListSorted.get( toremove.getType() ).remove( toremove );
return true;
}
else
{
Problem toremove = null;
for ( Problem p : problemListUnsorted )
{
if ( p.getName().equals( name ) && p.getType() == type )
{
toremove = p;
break;
}
}
if ( toremove == null )
{
return false;
}
problemListUnsorted.remove( toremove );
problemListSorted.get( type ).remove( toremove );
return true;
}
}
/**
* By Krishnakumar Bhattaram Checks if the problem to add shares a name with
* one in the database. If it isn't, adds it to the ArrayLists that contain
* Problems and returns true, false otherwise.
*
* @param pr
* Problem to add
* @return True if the problem is added, false if it shares a name
* @author Krishnakumar Bhattaram
*/
public boolean addProblem( Problem pr )
{
for ( Problem p : problemListUnsorted )
{
if ( p.getName().equals( pr.getName() ) )
{
return false;
}
}
problemListSorted.get( pr.getType() ).add( pr );
problemListUnsorted.add( pr );
return true;
}
/**
* Accessor method to all the Problems in this structure.
*
* @return Unsorted ArrayList of all the problems in this structure
* @author Krishnakumar Bhattaram
*/
public ArrayList<Problem> giveAllProblems()
{
return problemListUnsorted;
}
}