package cse131x; /** * Represents a bank account with an owner and an integer balance. * @author kjg * */ public class Account { String owner; int balance; public Account(String owner, int initialBalance) { this.owner = owner; balance = initialBalance; } public String toString() { return owner + ": $"+balance+".00"; } /** * Adds the given amount to the balance. * REQUIRES: the amount is not negative. * @param amount dollar amount to be deposited */ public void deposit(int amount) { if (amount < 0) throw new IllegalArgumentException("amount can't be negative"); balance = balance + amount; } /** * Withdraws the given amount from this account. * The withdrawl succeeds if there are sufficient funds. * @param amount the dollar amount to withdraw * @return true if the withdrawl was successful */ public boolean withdraw(int amount) { if (amount < 0) throw new IllegalArgumentException("amount can't be negative"); if (balance >= amount) { balance = balance - amount; return true; } else return false; } /** * Get the balance of this account. * @return the current balance */ public int getBalance() { return balance; } }