-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUserInput.java
48 lines (41 loc) · 1.19 KB
/
UserInput.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
package by.andd3dfx.common;
/**
* <pre>
* User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput,
* which accepts only digits.
*
* Implement the class TextInput that contains:
* - Public method void add(char c) - adds the given character to the current value
* - Public method String getValue() - returns the current value
*
* Implement the class NumericInput that:
* - Inherits from TextInput
* - Overrides the add method so that each non-numeric character is ignored
*
* For example, the following code should output "10":
* TextInput input = new NumericInput();
* input.add('1');
* input.add('a');
* input.add('0');
* System.out.println(input.getValue());
* </pre>
*/
public class UserInput {
public static class TextInput {
private String value = "";
public void add(char c) {
this.value = this.value + c;
}
public String getValue() {
return value;
}
}
public static class NumericInput extends TextInput {
@Override
public void add(char c) {
if (Character.isDigit(c)) {
super.add(c);
}
}
}
}