Bank Management - Part 8
Goal: Set up Main
.
Task 1
Inside Main
, create a new Bank
object.
static Bank bank = new Bank();
Task 2
Define a method based on the Doc comment.
/** * Name: createObject * @param values (String[] values) * @return Account * * Inside the function: * 1. Dynamically creates a Chequing, Loan, or Savings object based on the values array. */
A values
parameter would have four elements. For example:
{"Chequing","f84c43f4-a634-4c57-a644-7602f8840870","Michael Scott","1524.51"};
You can approach this task in one of two ways:
- Use a
switch
statement that creates an object based on the first element invalues
. - Use the following syntax to dynamically create an object based on the first element in
values
.
Class.forName("src.main.model.account." + first_element)
.getConstructor(String.class, String.class, double.class)
.newInstance(second_element, third_element, fourth_element);
You can choose either option. Option 2 is better than option 1 because it's dynamic whereas option 1 involves code duplication. If you choose option 2, it throws a lot of checked exceptions. Instead of catching each one separately, consider that Exception
is the base class for all exceptions (think of polymorphism).
catch (Exception e) { System.out.println(e.getMessage()); }
Whether you choose option 1 or 2, call createObject()
from main()
using the values
array and confirm that an object gets created.
String[] values = new String[] {"Chequing","f84c43f4-a634-4c57-a644-7602f8840870","Michael Scott","1524.51"};
After you confirm the method works, clear main()
.
Task 3
Define a method based on the Doc Comment.
/** * Name: returnAccounts() * @return ArrayList<Account> * @throws FileNotFoundException * * Inside the function: * 1. Creates a Scanner object and reads the data from accounts.txt. * 2. Creates an Account object for every line in accounts.txt. * 3. Returns an ArrayList of Account objects. */
Call the method from main()
and confirm the resulting ArrayList
has 16 elements.
Task 4
Define a method based on the Doc Comment.
/** * Name: loadAccounts * @param accounts (ArrayList<Account>) * * Inside the function: * 1. Loads every account into the Bank object. * */
Task 5
Define a method based on the Doc Comment.
/** * Name: returnTransactions() * @return ArrayList<Transaction> * @throws FileNotFoundException * * Inside the function: * 1. Creates a Scanner object and reads the data from transactions.txt. * 2. Populates an ArrayList with transaction objects. * 3. Sorts the ArrayList. */
Call the method from main()
and confirm the resulting ArrayList
has 352 sorted transactions.
Task 6
Define a method based on the following Doc Comment.
/** * Name: runTransactions * @param transactions ArrayList<Transaction> * * Inside the function: * 1. Executes every transaction using bank.execute. */
Task 7
Define a method that prints the transaction history of an account.
/** * Name: transactionHistory * @param id (String) * * Inside the function * 1. Print: \t\t\t\t TRANSACTION HISTORY\n\t * 2. Print every transaction that corresponds to the id. (Waits 300 milliseconds before printing the next one) * - Use this format "\t"+transaction+"\n" * 3. Print: \n\t\t\t\t\tAFTER TAX\n * 4. Print: "\t" + account that corresponds to id +"\n\n\n\n" */
Task 8
Replace your main()
method with the following and run your code.
public static void main(String[] args) {
try {
ArrayList<Account> accounts = returnAccounts();
loadAccounts(accounts);
ArrayList<Transaction> transactions = returnTransactions();
runTransactions(transactions);
bank.deductTaxes();
for (Account account : accounts) {
System.out.println("\n\t\t\t\t\t ACCOUNT\n\n\t"+account+"\n\n");
transactionHistory(account.getId());
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
Final Output: