/** * This class models a HuskyCard as it is used for * debit card transactions. *

Note that the balance and all transaction amounts are * handled as pennies in an integer variable. This * limits the account to a maximum balance of * 2,147,483,647¢ or $21,474,836.47.

*

This version was written in the CSE 142 Wi04 9:30 lecture.

*/ public class HuskyCard { /** the name of the owner of this account */ private String name; /** the account number for this account */ private int id; /** the balance, stated as integer pennies */ private int balance; /** * Create a new HuskyCard object using the given * parameters to initialize. Balance defaults to 0. * @param ownerName the String name of the student * @param idNumber the int student id associated with * this account */ public HuskyCard(String ownerName, int idNumber) { name = ownerName; id = idNumber; balance = 0; } /** * Deposit the given amount into the account and add * it to the balance. * @param amount the amount to add to the balance, * stated as integer pennies */ public void deposit(int amount) { balance = balance + amount; } /** * Withdraw the requested amount from the account. * If the amount given is less than zero, or the amount * given is greater than the balance, don't do anything. * @param amount the amount to withdraw, stated as integer * pennies * @return true if withdrawal okay, false if error */ public boolean withdraw(int amount) { if ((amount < 0) || (amount > balance)) { return false; } else { balance = balance - amount; return true; } } /** * Get the name of the student who owns this account. * @return the student's name as a String */ public String getName() { return name; } /** * Get the integer ID number associated with this account. * @return the account ID as an integer */ public int getID() { return id; } /** * Get the current balance (stated in pennies). * @return the current account balance, stated in pennies. */ public int getBalance() { return balance; } /** * Change the name of the student who owns this account. * @param newName the new name as a String */ public void setName(String newName) { name = newName; } /** * Transfer an amount from one HC to another. * @param amount amount the transfer stated in integer pennies * @param other the account from which to get the money * @return true if transaction okay, false if error */ public boolean transferFrom(int amount,HuskyCard other) { if (other.withdraw(amount)) { this.deposit(amount); return true; } else { return false; } } /** * Return a string that describes this HuskyCard. * @return a short string description of this HuskyCard */ public String toString() { return "HuskyCard: "+name +", id: "+id+", bal: " +balance; } }