From 8e70c7458927f64ff5e269f035223fd4a63c98ff Mon Sep 17 00:00:00 2001 From: jorge Date: Fri, 26 Apr 2024 14:06:08 +0200 Subject: [PATCH] The answers with decimals now always show two decimals --- .../answersAreNotEntites/SizeGenerator.java | 3 +- .../formatAnswers/SetAmountOfDecimals.java | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/SetAmountOfDecimals.java diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java index 3e321036..1e9b7997 100644 --- a/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java @@ -12,6 +12,7 @@ import main.java.questionGenerator.question.answers.formatAnswers.AddUnitsFormater; import main.java.questionGenerator.question.answers.formatAnswers.EmbellishNumbersFormater; import main.java.questionGenerator.question.answers.formatAnswers.RemoveEFromNumber; +import main.java.questionGenerator.question.answers.formatAnswers.SetAmountOfDecimals; public class SizeGenerator extends AnswersAreNotEntites { @@ -45,7 +46,7 @@ private String getRightAnswerEntity(String url) { @Override public List decorateAnswers(List answers) { AnswerFormater formater = new RemoveEFromNumber(new EmbellishNumbersFormater( - new AddUnitsFormater(null, "km^2"))); + new SetAmountOfDecimals(new AddUnitsFormater(null, "km^2"), 2))); return formater.format(answers); } diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/SetAmountOfDecimals.java b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/SetAmountOfDecimals.java new file mode 100644 index 00000000..9c0a1430 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/SetAmountOfDecimals.java @@ -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 format(List answers) { + List 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) { + char[] aux = decimalPart.toCharArray(); + decimalPart = ""; + for(int i=0; i