forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from B1G-SAM/Math
Add Math package
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package Math; | ||
import java.util.Random; | ||
import java.util.ArrayList; | ||
|
||
public class MathPool { | ||
private ArrayList<MathQuestion> poolOfQuestions; | ||
private final Random random; | ||
|
||
|
||
public MathPool(){ | ||
random = new Random(); | ||
} | ||
|
||
public void addMathQuestion(String wordProblem, int solution, int difficulty){ | ||
MathQuestion problem = new MathQuestion(wordProblem, solution, difficulty); | ||
poolOfQuestions.add(problem); | ||
} | ||
|
||
public MathQuestion getQuestionByDifficulty(int targetDifficulty){ | ||
ArrayList<MathQuestion> filteredQuestions = new ArrayList<>(); | ||
for (MathQuestion question : poolOfQuestions) { | ||
if (question.getDifficulty() == targetDifficulty) { | ||
filteredQuestions.add(question); | ||
} | ||
} | ||
if (!filteredQuestions.isEmpty()) { | ||
int index = random.nextInt(filteredQuestions.size()); | ||
return filteredQuestions.get(index); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package Math; | ||
|
||
public class MathQuestion { | ||
private String question; | ||
private int answer; | ||
private int difficulty; | ||
|
||
public MathQuestion(String qn, int ans, int diff){ | ||
this.question = qn; | ||
this.answer = ans; | ||
this.difficulty = diff; | ||
} | ||
|
||
public String getQuestion(){ | ||
return question; | ||
} | ||
|
||
public int getDifficulty() { | ||
return difficulty; | ||
} | ||
|
||
public boolean checkAns(int userAns){ | ||
return answer == userAns; | ||
} | ||
} |