While developing my code, the calculations functioned correctly until I introduced the
feature allowing users to specify their preferred payment date. This led to a problem
where if a user selects a payment date earlier than the program’s start date, the
calculations become inaccurate. Additionally, I’m facing difficulty implementing the
functionality for users to input the start date and the staking duration (e.g., 24 months).
Does anyone have any advice on resolving these issues?
import java.time.LocalDate;
import java.util.Scanner;
import java.io.FileWriter;
import java.time.format.DateTimeFormatter;
import java.text.DecimalFormat;
public class EtheriumCalcBonus2 {
// Method to append data to StringBuilder
public static void appendData(StringBuilder stringBuilder, int line, LocalDate date, double investmentAmount,
double rewardAmount, double totalRewardAmount, double stakingRewardRate) {
DecimalFormat df = new DecimalFormat("#.######"); // Limiting to 6 decimal places
stringBuilder.append(line).append(" , ").append(date).append(" , ").append(df.format(investmentAmount))
.append(" , ").append(df.format(rewardAmount)).append(" , ").append(df.format(totalRewardAmount))
.append(" , ").append(stakingRewardRate * 100).append("%n");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder stringBuilder = new StringBuilder();
// Header for CSV file
stringBuilder.append("Line").append(" , ").append("Reward Date").append(" , ").append("Investment Amount")
.append(" , ").append("Reward Amount").append(" , ").append("Total Reward Amount Till that date")
.append(" , ").append("Stacking Reward Rate").append("n");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String startDate = "2024-04-15";
LocalDate date = LocalDate.parse(startDate, formatter);
LocalDate checkDate = LocalDate.parse(startDate, formatter);
String lastDate = "2026-04-15";
LocalDate endDate = LocalDate.parse(lastDate, formatter);
int line = 1;
double investmentAmount = 0;
double rewardAmount = 0;
double totalRewardAmount = 0;
double stakingRewardRate = 0;
int rewardPaymentDay = 0;
boolean reinvest = true;
System.out.print("Welcome to the investor center.n");
// Get investment amount from user
System.out.print("Please enter the amount you would like to invest: ");
while (!scanner.hasNextDouble()) {
System.out.print("Invalid input. Please enter a number: ");
scanner.next(); // Discard invalid input
}
investmentAmount = scanner.nextDouble();
// Get staking reward rate from user
System.out.print("What is the staking reward rate? ");
while (!scanner.hasNextDouble()) {
System.out.print("Invalid input. Please enter a number: ");
scanner.next(); // Discard invalid input
}
stakingRewardRate = scanner.nextDouble() / 100; // we divide by 100 , because RewardRate is percentages
// Get reward payment day from user
System.out.print("What is your desired payment day? ");
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Please enter an integer: ");
scanner.next(); // Discard invalid input
}
rewardPaymentDay = scanner.nextInt();
try (FileWriter writer = new FileWriter("desired location") {
while ((!date.isAfter(endDate))) {
if (line == 1) {
if (date.getDayOfMonth() < rewardPaymentDay) {
// interest=(actual/365)*(days that we are counting) * (investment)
rewardAmount = (stakingRewardRate / 365) * ((rewardPaymentDay - date.getDayOfMonth()))
* investmentAmount;
totalRewardAmount += rewardAmount;
} else {
// interest=(actual/365)*(days that we are counting) * (investment)
LocalDate firstDayMonth = date.withDayOfMonth(1);
LocalDate lastDayMonth1 = firstDayMonth.minusDays(1);
int lastDayMonth = lastDayMonth1.getDayOfMonth();
rewardAmount = ((stakingRewardRate / 365)
* ((lastDayMonth - checkDate.getDayOfMonth()) + rewardPaymentDay)
* investmentAmount);
totalRewardAmount += rewardAmount;
date = date.plusDays(1);
continue;
}
line++;
} else {
if (date.getDayOfMonth() != rewardPaymentDay) {
date = date.plusDays(1);
continue;
} else {
// Reinvest check
System.out.print("for " + date + " the rewards are " + rewardAmount + "n"
+ "Would you like to reinvest your earnings? (yes/no): ");
while (true) {
String reinvestChoice = scanner.next().toLowerCase(); // Convert input to lowercase for
// case-insensitive comparison
switch (reinvestChoice) {
case "yes":
reinvest = true;
break;
case "no":
reinvest = false;
break;
default:
System.out.print("Invalid input. Please enter 'yes' or 'no': ");
continue;
}
break;
}
appendData(stringBuilder, line, date, investmentAmount, rewardAmount, totalRewardAmount,
stakingRewardRate);
// Calculating last day of month , because we`re counting days after the 23rd
// from last month, plus days until the 23 from this month
LocalDate currentDate = LocalDate.now();
LocalDate firstDayOfCurrentMonth = currentDate.withDayOfMonth(1);
LocalDate lastDayOfLastMonth = firstDayOfCurrentMonth.minusDays(1);
int dayOfLastMonth = lastDayOfLastMonth.getDayOfMonth();
// interest=(actual/365)*(days that we are counting) * (investment)
rewardAmount = (stakingRewardRate / 365) * dayOfLastMonth * investmentAmount;
totalRewardAmount += rewardAmount;
if (reinvest) {
investmentAmount += rewardAmount;
}
date = date.plusDays(1);
line++;
}
}
}
// Write data to file
writer.write(stringBuilder.toString());
System.out.println("The file has been created");
scanner.close();
} catch (Exception e) {
System.out.println("An error occurred.");
}
}
}