refactoring : repeated switches
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.example.refactoring._12_repeated_switches;
|
||||
|
||||
public class SwitchImprovements {
|
||||
|
||||
public int vacationHours(String type) {
|
||||
return switch (type) {
|
||||
case "full-time" -> 120;
|
||||
case "part-time" -> 80;
|
||||
case "temporal" -> 32;
|
||||
default -> 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.refactoring._12_repeated_switches._before;
|
||||
|
||||
public class SwitchImprovements {
|
||||
|
||||
public int vacationHours(String type) {
|
||||
int result;
|
||||
switch (type) {
|
||||
case "full-time": result = 120; break;
|
||||
case "part-time": result = 80; break;
|
||||
case "temporal": result = 32; break;
|
||||
default: result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.refactoring._12_repeated_switches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SwitchImprovementsTest {
|
||||
|
||||
@Test
|
||||
void vacationHours() {
|
||||
SwitchImprovements si = new SwitchImprovements();
|
||||
assertEquals(120, si.vacationHours("full-time"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user