package designingclasses2; /** * This class stores data about a bank account. */ public class BankAccount { // instance variables private double balance; private String owner; // class variable private static double interestRate = 5; /** * This constructor initializes the balance and owner fields to the passed values. * @param initialBalance Account's initial balance. * @param ownerName Account's owner name. */ public BankAccount(double initialBalance, String ownerName) { balance = (initialBalance >= 0) ? initialBalance : 0; this.owner = ownerName; } /** * This constructor initializes the balance and owner fields to default values. */ public BankAccount() { this.balance = 0; this.owner = ""; } /** * The getBalance method returns a BankAccount object's balance. * @return The value in the balance field. */ public double getBalance() { return this.balance; } /** * The getOwner method returns a BankAccount object's owner. * @return The value in the owner field. */ public String getOwner() { return this.owner; } /** * The getInterestRate method returns the class's interestRate field. * @return The value in the interestRate static field. */ public static double getInterestRate() { return interestRate; } /** * The setBalance method stores a value in the balance field. * @param balance the value to store in balance */ public void setBalance(double balance) { this.balance = (balance >= 0) ? balance : 0; } /** * The setOwner method stores a value in the owner field. * @param owner the value to store in owner */ public void setOwner(String owner) { this.owner = owner; } /** * The setInterestRate method resets the value in the class's interestRate field. * @param newRate The new value to store in the interestRate static field. */ public static void setInterestRate(double newRate) { interestRate = newRate; } /** * The deposit method adds the given amount to the account balance * @param amount amount to be added */ public void deposit(double amount) { this.setBalance(this.balance + amount); } /** * The withdraw method subtracts the given amount from the account balance if amount is not * greater than the balance. * @param amount amount to be withdrawn */ public void withdraw(double amount) { if( amount <= this.balance ) this.setBalance(this.balance - amount); } /** * The addMonthlyInterest method adds interest to the account's current balance. */ public void addMonthlyInterest() { double interestAmount = this.balance * interestRate / 100; this.setBalance(this.balance + interestAmount); } }