Skip to content

Instantly share code, notes, and snippets.

@rshepherd
Last active August 29, 2015 14:10
Show Gist options
  • Save rshepherd/b3e4f3f2845b890ac58f to your computer and use it in GitHub Desktop.
Save rshepherd/b3e4f3f2845b890ac58f to your computer and use it in GitHub Desktop.
Solution to practice midterm
public class DigitalWallet {
private static double maxWithdrawalPct = .5;
private long accountId;
private long balance = 0;
private TransactionHistory transactions = new TransactionHistory();
public DigitalWallet(long accountId) {
this.accountId = accountId;
}
public DigitalWallet(long accountId, int initialDeposit) {
this(accountId);
deposit(initialDeposit);
}
public long getAccountId() {
return accountId;
}
public void deposit(long amount) {
if(amount > 0) {
balance += amount;
transactions.addTransaction(amount);
} else {
System.out.println("Invalid deposit amount.");
}
}
public void withdraw(long amount) {
if(canWithdraw(amount)) {
balance -= amount;
transactions.addTransaction(-amount);
} else {
System.out.println("Invalid withdrawal amount");
}
}
public boolean canWithdraw(double amount) {
return amount > 0 && amount <= balance * DigitalWallet.maxWithdrawalPct;
}
public static double getMaxWithdrawalPct() {
return maxWithdrawalPct;
}
public static void setMaxWithdrawalPct(double maxWithdrawalPct) {
DigitalWallet.maxWithdrawalPct = maxWithdrawalPct;
}
//Question #3
public void transferFunds(DigitalWallet other, long amount) {
if(this.canWithdraw(amount)) {
this.withdraw(amount);
other.deposit(amount);
}
}
// Needed for question #2. Existence assumed for test.
public long getBalance() {
return balance;
}
public TransactionHistory getTransactionHistory() {
return this.transactions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment