JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
21 days ago1 views
👨‍💻Programming
public class SavingsAccount {

    // Variables
    private double balance;
    public static double interestRate = 0;

    // Constructor
    public SavingsAccount() {
        balance = 0;
    }

    // Static setter method
    public static void setInterestRate(double newRate) {
        interestRate = newRate;
    }

    // Static getter method
    public static double getInterestRate() {
        return interestRate;
    }

    // Non-static getter method
    public double getBalance() {
        return balance;
    }

    // Deposit method
    public void deposit(double amount) {
        balance += amount;
    }

    // Withdraw method
    public double withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            return amount;
        } else {
            return 0;
        }
    }

    // Add interest
    public void addInterest() {
        double interest = balance * interestRate;
        balance += interest;
    }

    // Static showBalance method
    public static void showBalance(SavingsAccount account) {
        System.out.printf("Your balance is: %.2f%n", account.getBalance());
    }
}

import java.util.Scanner;

public class RunSavingsAccount {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Create SavingsAccount object
        SavingsAccount savings = new SavingsAccount();

        // Ask for interest rate
        System.out.print("Enter interest rate: ");
        double rate = input.nextDouble();

        // Set interest rate
        SavingsAccount.setInterestRate(rate);

        // Ask for amount to deposit
        System.out.print("Enter amount to deposit: ");
        double amount = input.nextDouble();

        savings.deposit(amount);

        // Display balance
        SavingsAccount.showBalance(savings);

        // Ask user to press D or W
        System.out.print("Enter D for Deposit or W for Withdraw: ");
        char choice = input.next().charAt(0);

        if (choice == 'D' || choice == 'd') {

            System.out.print("Enter amount to deposit: ");
            amount = input.nextDouble();

            savings.deposit(amount);

        } else if (choice == 'W' || choice == 'w') {

            System.out.print("Enter amount to withdraw: ");
            amount = input.nextDouble();

            double withdrawn = savings.withdraw(amount);

            if (withdrawn == 0) {
                System.out.println("Insufficient balance.");
            }
        } else {
            System.out.println("Invalid choice.");
        }

        // Show balance after transaction
        SavingsAccount.showBalance(savings);

        // Apply interest if balance is greater than 1000
        if (savings.getBalance() > 1000) {
            savings.addInterest();

            System.out.println("Balance after applying interest:");
            SavingsAccount.showBalance(savings);
        }

        input.close();
    }
}
← Back to timeline