/** * A virtual Husky Card Specification. * CSE142 lecture example 1/14/04, 1/16/04, 1/21/04 */ public class HuskyCard { // instance variables private String name; // card holder's name private int id; // ID number private int balance; // account balance in pennies /** * Construct a new HuskyCard with a balance of 0 * @param ownerName the owner's name * @param idNumber the owner's id number */ public HuskyCard(String ownerName, int idNumber) { name = ownerName; id = idNumber; balance = 0; } /** * Return this HuskyCard's owner's name * @return owner's name */ public String getName() { return name; } /** * Return the ID number associated with this HuskyCard * @return the HuskyCard owner's ID number */ public int getID() { return id; } /** * Report the current balance of this HuskyCard * @return the current balance in pennies */ public int getBalance() { return balance; } /** * Change the name on this HuskyCard * @param newName new name for card owner */ public void setName(String newName) { name = newName; } /** * Deposit money into this HuskyCard * @param amount the amount to deposit in pennies * @return true if the deposit was successful, otherwise false */ public boolean deposit(int amount) { if (amount > 0) { balance = balance + amount; return true; } else { return false; } } /** * Withdraw money from this HuskyCard * @param amount the amount to withdraw in pennies * @return true if withdraw was successful, otherwise false */ public boolean withdraw(int amount) { if (balance >= amount && amount > 0) { balance = balance - amount; return true; } else { return false; } } /** * Transfer money from another HuskyCard * @param amount the amount to transfer in pennies * @param otherCard the HuskyCard to transfer the money from * @return true if the transfer was a success, otherwise false */ public boolean transferFrom(int amount, HuskyCard otherCard) { boolean success = otherCard.withdraw(amount); if (success) { deposit(amount); return true; } else { return false; } } /** * Return a String describing this HuskyCard * @return a string with this card's name, id, and balance */ public String toString() { return "HuskyCard[name = " + name + ", id = " + id + ", balance = " + balance + "]"; } }