Trouble with Payment Date Selection and Staking Duration Input Handling in Code Calculations

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.");
            }
        }
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật