java 8 - 1
java 8 - 1
java
package MyPack;
// Constructor
public BankAccount(String accountHolder, double initialBalance) {
this.accountHolder = accountHolder;
this.balance = initialBalance;
}
// Deposit method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Invalid deposit amount.");
}
}
// Withdraw method
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrew: $" + amount);
} else {
System.out.println("Invalid withdrawal amount.");
}
}
package MyPack;
// Constructor
public SavingsAccount(String accountHolder, double initialBalance, double
interestRate) {
super(accountHolder, initialBalance);
this.interestRate = interestRate;
}
// Apply interest
public void applyInterest() {
double interest = getBalance() * (interestRate / 100);
deposit(interest);
System.out.println("Interest applied: $" + interest);
}
import MyPack.BankAccount;
import MyPack.SavingsAccount;
System.out.println();