language: Java (sun-jdk-1.7.0_10)
date: 477 days 3 hours ago
link:
visibility: public
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
 
public class Main {
 
    private static final int NUMBER_OF_QUESTIONS = 10;
    private static final int MIN_QUESTION_ELEMENTS = 2;
    private static final int MAX_QUESTION_ELEMENTS = 4;
    private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
    private static final int MAX_QUESTION_ELEMENT_VALUE = 100;
    private final Random randomGenerator = new Random();
 
    public static void main(String[] args) {
        Main questionGenerator = new Main();
        List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions();
        for (Question question : randomQuestions) {
            System.out.println(question);
        }
    }
 
    public List<Question> getGeneratedRandomQuestions() {
        List<Question> randomQuestions = new ArrayList<Question>(NUMBER_OF_QUESTIONS);
        for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) {
            int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity();
            Question question = new Question(randomQuestionElementsCapacity);
            for (int j = 0; j < randomQuestionElementsCapacity; j++) {
                boolean isLastIteration = j + 1 == randomQuestionElementsCapacity;
 
                QuestionElement questionElement = new QuestionElement();
                questionElement.setValue(getRandomQuestionElementValue());
                questionElement.setOperator(isLastIteration ? null
                        : Operator.values()[randomGenerator.nextInt(Operator.values().length)]);
 
                question.addElement(questionElement);
            }
            randomQuestions.add(question);
        }
        return randomQuestions;
    }
 
    private int getRandomQuestionElementsCapacity() {
        return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS);
    }
 
    private int getRandomQuestionElementValue() {
        return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE);
    }
 
    private int getRandomIntegerFromRange(int min, int max) {
        return randomGenerator.nextInt(max - min + 1) + min;
    }
}
 
class Question {
 
    private List<QuestionElement> questionElements;
 
    public Question(int sizeOfQuestionElemets) {
        questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets);
    }
 
    public void addElement(QuestionElement questionElement) {
        questionElements.add(questionElement);
    }
 
    public List<QuestionElement> getElements() {
        return questionElements;
    }
 
    public int size() {
        return questionElements.size();
    }
 
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (QuestionElement questionElement : questionElements) {
            sb.append(questionElement);
        }
        return sb.toString().trim();
    }
}
 
class QuestionElement {
 
    private int value;
    private Operator operator;
 
    public int getValue() {
        return value;
    }
 
    public void setValue(int value) {
        this.value = value;
    }
 
    public Operator getOperator() {
        return operator;
    }
 
    public void setOperator(Operator operator) {
        this.operator = operator;
    }
 
    @Override
    public String toString() {
        return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " ";
    }
}
 
enum Operator {
 
    PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/");
    private String displayValue;
 
    private Operator(String displayValue) {
        this.displayValue = displayValue;
    }
 
    public String getDisplayValue() {
        return displayValue;
    }
}