@GetMapping("/ror")
public String getRor(Model model, @RequestParam double buyPrice, @RequestParam double sellPrice, @RequestParam long quantity) {
model.addAttribute("ror", (sellPrice - buyPrice) / (double) buyPrice * 100);
model.addAttribute("profit", (long) ((sellPrice - buyPrice) / (double) buyPrice * 100 * quantity));
model.addAttribute("buyPrice", buyPrice);
model.addAttribute("sellPrice", sellPrice);
model.addAttribute("quantity", quantity);
return "calculator";
}
@GetMapping("/welfare")
public String getWelfare(Model model,
@RequestParam(defaultValue = "0.0") String price,
@RequestParam(defaultValue = "0") String term,
@RequestParam(defaultValue = "0.0") String welfareRor) {
try {
double priceDouble = Double.parseDouble(price);
long termLong = Long.parseLong(term);
double welfareRorDouble = Double.parseDouble(welfareRor);
model.addAttribute("price", price);
model.addAttribute("term", term);
model.addAttribute("welfareRor", welfareRor);
List<Double> rorList = RorCalculator.getWelfareRorList(welfareRorDouble, termLong);
List<Double> revenueList = new ArrayList<>();
List<Double> priceList = new ArrayList<>();
revenueList.add(priceDouble * (rorList.get(0) / 100));
priceList.add(priceDouble + revenueList.get(0));
for(int i = 1 ; i < rorList.size() ; i++){
revenueList.add((priceDouble * (rorList.get(i) / 100)) - (priceDouble * (rorList.get(i - 1) / 100)));
priceList.add(priceList.get(i - 1) + revenueList.get(i));
}
model.addAttribute("totalProfit", (long) (priceDouble * (rorList.get(rorList.size() - 1) / 100)));
model.addAttribute("finalAmount", (long) (priceDouble * (1 + rorList.get(rorList.size() - 1) / 100)));
model.addAttribute("rorList", rorList);
model.addAttribute("revenueList", revenueList);
model.addAttribute("priceList", priceList);
} catch (NumberFormatException e) {
// 잘못된 입력 형식에 대한 처리
return "calculator";
}
return "calculator";
}
public static List<Double> getWelfareRorList(double welfareRor, long term){
List<Double> welfareRorList = new ArrayList<>();
welfareRorList.add(welfareRor);
for(int i = 1; i < term; i++){
double calWelfare = (welfareRorList.get(i - 1) / 100 + 1) * (1 + welfareRor / 100) - 1;
welfareRorList.add(calWelfare * 100);
}
return welfareRorList;
}
복리 수익률을 계산하는 함수를 추가하였다.