3개월차 신입 비전공 개발자가 읽기 좋은 코드를 만들어보고자 공부하는 내용입니다. 부족하거나 새롭게 공부해보면 좋을 것 같은 추천, 글에서 발견된 문제에 대한 이야기는 언제든 환영합니다!
@Setter
public class ExampleService {
private double targetSpeed;
public void setPreset(int speedPreset) {
if (speedPreset == 2) {
setTargetSpeed(17944);
}
if (speedPreset == 1) {
setTargetSpeed(6767);
}
if (speedPreset == 0) {
setTargetSpeed(0);
}
}
}
@Setter
public class ExampleService {
static final int STOP_SPEED_PRESET = 0;
static final int PLANETARY_SPEED_PRESET = 1;
static final int CRUISE_SPEED_PRESET = 2;
static final int STOP_SPEED_KMH = 0;
static final int PLANETARY_SPEED_KMH = 6767;
static final int CRUISE_SPEED_KMH = 17944;
private double targetSpeed;
public void setPreset(int speedPreset) {
if (speedPreset == CRUISE_SPEED_PRESET) {
setTargetSpeed(CRUISE_SPEED_KMH);
}
if (speedPreset == PLANETARY_SPEED_PRESET) {
setTargetSpeed(PLANETARY_SPEED_KMH);
}
if (speedPreset == STOP_SPEED_PRESET) {
setTargetSpeed(STOP_SPEED_KMH);
}
}
}
@Setter
public class ExampleService {
private double targetSpeed;
public void setPreset(SpeedPreset speedPreset) {
Objects.requireNonNull(speedPreset);
setTargetSpeed(speedPreset.speedKmh);
}
enum SpeedPreset {
STOP(0),
PLANETARY_SPEED(6767),
CRUISE_SPEED_KMH(17944);
final double speedKmh;
SpeedPreset(double speedKmh) {
this.speedKmh = speedKmh;
}
}
}
List<Supply> supplies = new ArrayList<>();
public void dispose() {
for (Supply supply : supplies) {
if (supply.isContamiated()) {
supplies.remove(supply);
}
}
}
public void dispose() {
Iterator<Supply> iterator = supplies.iterator();
while (iterator.hasNext()) {
if (iterator.next().isContamiated()) {
iterator.remove();
}
}
}
public void dispose(String regex) {
List<Supply> result = new ArrayList<>();
for (Supply supply : supplies) {
if (Pattern.matches(regex, supply.toString())) {
result.add(supply);
}
}
}
public void dispose(String regex) {
List<Supply> result = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
for (Supply supply : supplies) {
if (pattern.matcher(supply.toString()).matches()) {
result.add(supply);
}
}
}
public int countFrequency(TestAVo aVo) {
if (aVo == null) {
throw new NullPointerException();
}
int frequency = 0;
for (TestAVo eleAVo : aVoList) {
if (aVo.equals(eleAVo)) {
frequency++;
}
}
return frequency;
}
public int countFrequency(TestAVo aVo) {
Objects.requireNonNull(aVo);
return Collections.frequency(aVoList, aVo);
}