Skip to content

Instantly share code, notes, and snippets.

@EronWright
Last active June 28, 2016 22:17
Show Gist options
  • Save EronWright/0e6992fb126af4359a24784a5aa86893 to your computer and use it in GitHub Desktop.
Save EronWright/0e6992fb126af4359a24784a5aa86893 to your computer and use it in GitHub Desktop.
Account.java - Sample code for instructional purposes
public class Account {
private String accountNumber;
public Account(String accountNumber) {
// Constructor
this.accountNumber = accountNumber;
}
public String getAccountNumber() {
return accountNumber; // return the account number
}
public ArrayList getTransactions() throws Exception {
try {
// Get the list of transactions
List dbTransactionList = Db.getTransactions(accountNumber.trim());
ArrayList transactionList = new ArrayList();
int i;
for(i = 0; i < dbTransactionList.size(); i++) {
DbRow row = (DbRow) dbTransactionList.get(i);
Transaction trans = makeTransactionFromDbRow(row);
transactionList.add(trans);
}
return transactionList;
} catch (SQLException ex) {
// There was a database error
throw new Exception("Can't retrieve transactions from the database");
}
}
public Transaction makeTransactionFromDbRow(DbRow row) {
double currencyAmountInPounds = Double.parseDouble(row.getValueForField("amt"));
String description = row.getValueForField("desc");
return new Transaction(description, currencyAmountInPounds); // return the new Transaction object
}
// Override the equals method
public boolean equals(Account o) {
return o.getAccountNumber() == getAccountNumber(); // check account numbers are the same
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment