CASE 1. 한줄이고 콤마로 읽어버릴수 있는 경우
public class 맞는 이름 {
private static final String promotionPath = "src/main/resources/promotions.md";
public 반환형태 매서드 이름(String 경로 이름) {
try {
Path path = Paths.get(promotionPath);
List<String> lines = Files.readAllLines(path);
String line = lines.getFirst();
String[] data = line.split(" ");
return new 형태;
} catch (IOException e) {
throw new IllegalArgumentException(에러 메시지);
}
}
}
CASE2. 여러 줄이고 공백으로 구분된 경우 (Try-Catch 넣어서 디벨롭 하도록)
public class 맞는 이름 {
public 형태 매서드명(String promotionPath) throws IOException {
List<Promotion> promotions = new ArrayList<>();
Path path = Paths.get(promotionPath);
List<String> lines = Files.readAllLines(path);
for (int i = 1; i < lines.size(); i++) {
extractPromotions(lines.get(i), promotions);
}
return promotions;
}
private void extractPromotions(String line, List<Promotion> promotions) {
String[] data = line.split(",");
String name = data[0].trim();
int buy = Integer.parseInt(data[1].trim());
int get = Integer.parseInt(data[2].trim());
LocalDateTime startDate = LocalDate.parse(data[3].trim()).atStartOfDay();
LocalDateTime endDate = LocalDate.parse(data[4].trim()).atStartOfDay();
promotions.add(new Promotion(name, startDate, endDate, buy, get));
}
}