Question: Java Program: Using the following program, modify the program to allow the user to enter the BALANCE, DEPOSIT and WITHDRAWAL amounts. Then create a CUSTOM
Java Program:
Using the following program, modify the program to allow the user to enter the BALANCE, DEPOSIT and WITHDRAWAL amounts. Then create a CUSTOM EXCEPTION CLASS for when a user enters a WITHDRAWAL AMOUNT more than the current BALANCE.
public class Assignment6 {
public static void main (String[] args) {
Account account = new Account(1122, 20000); //account id, balance
Account.setAnnualInterestRate(4.5);
account.withdraw(2500); //withdraw
account.deposit(3000); //deposit
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created on " + account.getDateCreated()); //date of account creation
}
}
class Account {
private int id;
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance; // method for getting balance
}
public static double getAnnualInterestRate() {
return annualInterestRate; //for annual interest rate
}
public void setId(int newId) {
id = newId; // custom id
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
