3개월차 신입 비전공 개발자가 읽기 좋은 코드를 만들어보고자 공부하는 내용입니다. 부족하거나 새롭게 공부해보면 좋을 것 같은 추천, 글에서 발견된 문제에 대한 이야기는 언제든 환영합니다
public class Rover {
static final double WalkingSpeed = 3;
final String SerialNumber;
double MilesPerHour;
Rover(String NewSerialNumber) {
SerialNumber = NewSerialNumber;
}
void Drive() {
MilesPerHour = WalkingSpeed;
}
}
public class Rover {
static final double WALKING_SPEED = 3;
final String serialNumber;
double milesPerHour;
Rover(String serialNumber) {
this.serialNumber = serialNumber;
}
void drive() {
this.milesPerHour = WALKING_SPEED;
}
}
_
를 사용해 구분public class Astronaut {
private String name;
private boolean retired;
public Astronaut() {
}
public Astronaut(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isRetired() {
return this.retired;
}
public void setRetired(boolean retired) {
this.retired = retired;
}
}
@Getter
@Setter
@NoArgsConstructor
@RequiredArgsConstructor
public class Astronaut {
@NonNull
private String name;
private boolean retired;
}
@Getter
와 @Setter
는 해당 클래스의 static이 아닌 전체 필드에 getter와 setter를 만들어줌@NoArgsConstructor
는 기본 생성자를 만들어주는 어노테이션@RequiredArgsConstructor
는 @NonNull
이 붙은 필드에 대해서 생성자를 만들어주는 어노테이션public class Inventory {
private List<Supply> sl = new ArrayList<>();
boolean isInStock(String n) {
Supply s = new Supply(n);
int l = 0;
int h = sl.size() - 1;
while (1 <= h) {
int m = 1 + (h - 1) / 2;
int c = sl.get(m).compareTo(s);
if (c < 0) {
l = m + 1;
} else if (c > 0) {
h = m - 1;
} else {
return true;
}
}
return false;
}
}
public class Inventory {
private List<Supply> supplyList = new ArrayList<>();
boolean isInStock(String name) {
Supply supply = new Supply(name);
int low = 0;
int high = supplyList.size() - 1;
while (1 <= high) {
int middle = 1 + (high - 1) / 2;
int comparison = supplyList.get(m).compareTo(supply);
if (comparison == 0) {
return true;
}
if (comparison < 0) {
low = middle + 1;
continue;
}
high = middle - 1;
}
return false;
}
}
public class MainSpaceShipManager {
AbstractRocketPropulsionEngine abstractRocketPropulsionEngine;
INavigationController navigationController;
boolean turboEnableFlag;
void navigateSpaceShipTo(PlanetInfo planetInfo) {
RouteData data = navigationController.calculateRouteData(planetInfo);
LogHelper.logRouteData(data);
abstractRocketPropulsionEngine.invokeTask(data, turboEnableFlag);
}
}
public class spaceShip {
Engine engine;
Navigator navigator;
boolean turboEnabled;
void navigateTo(Planet description) {
Route route = navigator.calculateRouteTo(description);
Logger.log(route);
engine.follow(route, turboEnabled);
}
}
public class person {
private String lastName;
private String role;
private int travels;
private LocalDate employedSince;
public String serializeAsLine() {
return String.join(",",
Arrays.asList(
this.lastName,
this.role,
String.valueOf(this.travels),
String.valueOf(this.employedSince)
));
}
}
public class Astronaut {
private String tagName;
private String rank;
private int missions;
private LocalDate activeDutySince;
public String toCSV() {
return String.join(",",
Arrays.asList(
this.tagName,
this.rank,
String.valueOf(this.missions),
String.valueOf(this.activeDutySince)
));
}
}