-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor : Number Class 정적팩토리로 표현, 메모리에 저장
- Loading branch information
1 parent
d65b03c
commit b8c5830
Showing
5 changed files
with
60 additions
and
50 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package calculator; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Number { | ||
private static final Pattern NUMERIC_PATTERN = Pattern.compile("[+]?\\d+"); | ||
private static final Map<Integer, Number> positiveNumbers = new HashMap<>(); | ||
private int value; | ||
|
||
private Number() { | ||
this.value = 0; | ||
} | ||
|
||
private Number(int num) { | ||
this.value = num; | ||
} | ||
|
||
public static Number sumOf(List<String> strings) { | ||
Number number = new Number(); | ||
for (String string : strings) { | ||
number.add(ofPositive(string).value); | ||
} | ||
return number; | ||
} | ||
|
||
private static Number ofPositive(String stringNumber) { | ||
int num = parsePositiveInt(stringNumber); | ||
Number number = positiveNumbers.getOrDefault(num, new Number(num)); | ||
positiveNumbers.put(num, number); | ||
return number; | ||
} | ||
|
||
private static int parsePositiveInt(String string) { | ||
Matcher isNumeric = NUMERIC_PATTERN.matcher(string); | ||
if (isNumeric.matches()) { | ||
return Integer.parseInt(string); | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
private void add(int number) { | ||
this.value += number; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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