generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #133 from Arquisoft/Question_Generation_General
Fixed small problems with the generation
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
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
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
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
62 changes: 62 additions & 0 deletions
62
...r/src/main/java/questionGenerator/question/answers/formatAnswers/SetAmountOfDecimals.java
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,62 @@ | ||
package main.java.questionGenerator.question.answers.formatAnswers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import main.java.questionGenerator.question.answers.AbstractFormater; | ||
import main.java.questionGenerator.question.answers.AnswerFormater; | ||
|
||
public class SetAmountOfDecimals extends AbstractFormater { | ||
|
||
private int amount; | ||
|
||
public SetAmountOfDecimals(AnswerFormater formater, int amount) { | ||
super(formater); | ||
this.amount = amount; | ||
} | ||
|
||
@Override | ||
public List<String> format(List<String> answers) { | ||
List<String> result = new ArrayList<>(); | ||
for(String answer : answers) { | ||
String[] splitted = split(answer); | ||
if(splitted.length==0) | ||
result.add(answer); | ||
else { | ||
String decimalPart = formatDecimalPart(splitted[1]); | ||
result.add(splitted[0] + '.' + decimalPart); | ||
} | ||
} | ||
return end(result); | ||
} | ||
|
||
private String[] split(String answer) { | ||
String[] result = {"", ""}; | ||
int position = 0; | ||
for(char c : answer.toCharArray()) { | ||
if(c=='.') | ||
position = 1; | ||
else | ||
result[position] += c; | ||
} | ||
return result; | ||
} | ||
|
||
private String formatDecimalPart(String decimalPart) { | ||
if(decimalPart.length()<amount) { | ||
int diference = amount - decimalPart.length(); | ||
for(int i=0; i<diference; i++) { | ||
decimalPart += "0"; | ||
} | ||
} | ||
else if(decimalPart.length()>amount) { | ||
char[] aux = decimalPart.toCharArray(); | ||
decimalPart = ""; | ||
for(int i=0; i<amount; i++) { | ||
decimalPart += aux[i]; | ||
} | ||
} | ||
return decimalPart; | ||
} | ||
|
||
} |