Repository navigation
Expand file tree
/
Copy pathNumber.java
More file actions
76 lines (60 loc) · 2.06 KB
/
Copy pathNumber.java
File metadata and controls
76 lines (60 loc) · 2.06 KB
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
public class Number {
private int value;
private int lenght;
private String strValue;
public Number(int value) {
this.value = value;
this.lenght = Integer.toString(value).length();
this.strValue = Integer.toString(value);
}
public int getPointsNeeded(int divider){
int mod = this.value % divider;
if(mod == 0){
return 0;
}else{
return divider - mod;
}
}
public boolean isAddable(int points){
char[] chars = this.strValue.toCharArray();
int availblePoints = 0;
for (char c : chars) {
availblePoints += (9 - Integer.parseInt(String.valueOf(c)));
}
return points <= availblePoints;
}
public void add(int points, int position){
int pointsLeft = points;
while(position < this.lenght && pointsLeft > 0){
int digit = this.getDigitFromPosition(position);
while(digit < 9 && pointsLeft > 0){
digit++;
pointsLeft--;
}
this.setDigit(digit, position);
position++;
}
}
private void setDigit(int newDigit, int position){
char[] chars = this.strValue.toCharArray(); //string value to char array
chars[position] = Integer.toString(newDigit).charAt(0); //swap digit at index
this.setValue(Integer.parseInt(String.valueOf(chars))); //char array back to string back to integer to set new value xD
}
public int getDigitFromPosition(int position){
return Character.getNumericValue(this.strValue.charAt(position));
}
public int getValue() {
return this.value;
}
public int getLenght(){
return this.lenght;
}
public String getStringValue(){
return this.strValue;
}
public void setValue(int value){
this.value = value;
this.lenght = Integer.toString(value).length();
this.strValue = Integer.toString(value);
}
}