Skip to content

Commit ff20953

Browse files
committedMay 1, 2020
Initial commit
0 parents  commit ff20953

15 files changed

+756
-0
lines changed
 

‎ATM.class

4.85 KB
Binary file not shown.

‎ATM.java

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package ATMBankManager;
2+
3+
import java.util.Scanner;
4+
5+
public class ATM {
6+
public static void main(String[] args) {
7+
// Init Scanner.
8+
Scanner in = new Scanner(System.in);
9+
10+
// Init Bank.
11+
Bank theBank = new Bank("Bank of Tom");
12+
13+
// Add a new User, called 'aUser', to the Bank, and also create a savings account for User.
14+
User aUser = theBank.addUser("John", "Doe", "1234");
15+
16+
// Add a new "Checking" Account for our User.
17+
Account newAccount = new Account("Checking", aUser, theBank);
18+
aUser.addAccount(newAccount);
19+
theBank.addAccount(newAccount);
20+
21+
User curUser;
22+
while (true) {
23+
// Stay inside login prompt until succesffuly login. Hence indefinite loop.
24+
curUser = ATM.mainMenuPrompt(theBank, in);
25+
26+
// Stay inside main menu until user quits. Hence indefinite loop.
27+
ATM.printUserMenu(curUser, in);
28+
}
29+
}
30+
/**
31+
*
32+
* @param theBank
33+
* @param in
34+
* @return
35+
*/
36+
// Interface.
37+
public static User mainMenuPrompt(Bank theBank, Scanner in) { // Static keyword can be used here, because there's no data in this (ATM) class?
38+
// Inits.
39+
String userID;
40+
String pin;
41+
User authUser;
42+
43+
// Prompt user for userID and Pin combo, until a set is resolved.
44+
do {
45+
System.out.printf("\n\nWelcome to %s\n\n", theBank.getName());
46+
System.out.print("Enter user ID: ");
47+
userID = in.nextLine();
48+
System.out.print("Enter pin: ");
49+
pin = in.nextLine();
50+
51+
// Try to get a match for UserID and Pin combo entered.
52+
authUser = theBank.userLogin(userID, pin);
53+
if (authUser == null) {
54+
System.out.println("Incorrect user ID and/or Pin entered. Try Again.");
55+
}
56+
57+
} while(authUser == null); // Loop until 'authUser' has found a successful User login, i.e. Now has a value, (so isn't null).
58+
59+
return authUser; // Return the User that has been stored in authUser, after being validated.
60+
}
61+
62+
public static void printUserMenu(User theUser, Scanner in) { // "Static" methods allow for self reference, i.e. 'ATM.<something>();'.
63+
// Print a summary of the user's accounts.
64+
theUser.printAccountsSummary();
65+
66+
// Init.
67+
int choice;
68+
69+
// User menu.
70+
do {
71+
System.out.printf("Welcome %s, what action would you like to take?\nPress the corresponding number to choose an item from the menu below.\n", theUser.getFirstName());
72+
System.out.println(" 1) Show Account Transaction history.");
73+
System.out.println(" 2) Make a Withdrawel.");
74+
System.out.println(" 3) Make a Deposit.");
75+
System.out.println(" 4) Transfer.");
76+
System.out.println(" 5) QUIT.");
77+
System.out.println();
78+
System.out.print("Enter your choice (1-5): ");
79+
choice = in.nextInt();
80+
81+
if (choice < 1 || choice > 5) {
82+
System.out.println("Invalid input. Please input 1-5.");
83+
}
84+
} while(choice < 1 || choice > 5);
85+
86+
// Process the choice.
87+
switch(choice) {
88+
case 1:
89+
ATM.showTransactionHistory(theUser, in);
90+
break;
91+
case 2:
92+
ATM.withdrawFunds(theUser, in);
93+
break;
94+
case 3:
95+
ATM.depositFunds(theUser, in);
96+
break;
97+
case 4:
98+
ATM.transferFunds(theUser, in);
99+
break;
100+
case 5:
101+
// Grab/or/Remove rest of the previous input line. - (Gobble up rest of previous input?). Prevents bad formatting.
102+
in.nextLine();
103+
//System.exit(1); // Exit app.
104+
break;
105+
default:
106+
System.out.println("Invalid input. Please input 1-5.");
107+
ATM.printUserMenu(theUser, in); // Recursive call (Recursion). Brings up menu again.
108+
}
109+
110+
// Redisplay this menu unless the user quits.
111+
if (choice !=5) {
112+
ATM.printUserMenu(theUser, in); // Recursive call (Recursion). Brings up menu again.
113+
}
114+
}
115+
116+
/**
117+
*
118+
* @param theUser
119+
* @param in
120+
*/
121+
// Show the Transaction history for an Account.
122+
public static void showTransactionHistory(User theUser, Scanner in) {
123+
int theAcct;
124+
125+
// Choose Account transaction history to view.
126+
do {
127+
System.out.printf("Enter the number (1-%d) of the account " + "for which the transactions you would like to view: ", theUser.numAccounts());
128+
theAcct = in.nextInt()-1;
129+
if (theAcct < 0 || theAcct <= theUser.numAccounts()) {
130+
System.out.println("Invalid Account. Please try again.");
131+
}
132+
} while(theAcct < 0 || theAcct >= theUser.numAccounts());
133+
134+
// Print the transaction history.
135+
theUser.printAcctTransHistory(theAcct);
136+
}
137+
138+
/**
139+
*
140+
* @param theUser
141+
* @param in
142+
*/
143+
// Process the transferring of funds from one Account to another Account.
144+
public static void transferFunds(User theUser, Scanner in) {
145+
// Inits.
146+
int fromAcct;
147+
int toAcct;
148+
double amount;
149+
double acctBal;
150+
151+
// Get the account to transfer from.
152+
do {
153+
System.out.printf("Enter the number (1-%d) of the account\n" + "to transfer from: ", theUser.numAccounts());
154+
fromAcct = in.nextInt()-1;
155+
if (fromAcct < 0 || fromAcct >= theUser.numAccounts()) {
156+
System.out.println("Invalid Account. Please try again.");
157+
}
158+
} while(fromAcct < 0 || fromAcct >= theUser.numAccounts());
159+
acctBal = theUser.getAcctBalance(fromAcct);
160+
161+
// Get account to transfer to.
162+
do {
163+
System.out.printf("Enter the number (1-%d) of the account\n" + "to transfer to: ", theUser.numAccounts());
164+
toAcct = in.nextInt()-1;
165+
if (toAcct < 0 || toAcct >= theUser.numAccounts()) {
166+
System.out.println("Invalid Account. Please try again.");
167+
}
168+
} while(toAcct < 0 || toAcct >= theUser.numAccounts());
169+
170+
// Get the amount to transfer.
171+
do {
172+
System.out.printf("Enter the amount to Transfer (max. £%.02f): £", acctBal);
173+
amount = in.nextDouble();
174+
if (amount < 0) {
175+
System.out.println("Amount must be greater than 0.");
176+
} else if (amount > acctBal) {
177+
System.out.printf("Amount must not be greater than\n" + "balance of £%.02f.\n", acctBal);
178+
}
179+
} while(amount < 0 || amount > acctBal);
180+
181+
// Perform the Transfer.
182+
theUser.addAcctTransaction(fromAcct, -1*amount, String.format("Transfer to Account %s", theUser.getAcctUUID(toAcct))); // Subtract Transfer amount fromAcct.
183+
theUser.addAcctTransaction(toAcct, amount, String.format("Transfer to Account %s", theUser.getAcctUUID(fromAcct))); // Add Transfer amount toAcct.
184+
185+
}
186+
187+
/**
188+
*
189+
* @param theUser
190+
* @param in
191+
*/
192+
// Process a withdrawel of funds from an Account.
193+
public static void withdrawFunds(User theUser, Scanner in) {
194+
195+
// Inits.
196+
int fromAcct;
197+
double amount;
198+
double acctBal;
199+
String memo;
200+
201+
// Get the account to withdraw from.
202+
do {
203+
System.out.printf("Enter the number (1-%d) of the account\n" + "to Withdraw from: ", theUser.numAccounts());
204+
fromAcct = in.nextInt()-1;
205+
if (fromAcct < 0 || fromAcct >= theUser.numAccounts()) {
206+
System.out.println("Invalid Account. Please try again.");
207+
}
208+
} while(fromAcct < 0 || fromAcct >= theUser.numAccounts());
209+
acctBal = theUser.getAcctBalance(fromAcct);
210+
211+
// Get the amount to withdraw.
212+
do {
213+
System.out.printf("Enter the amount to Withdraw (max. £%.02f): £", acctBal);
214+
amount = in.nextDouble();
215+
if (amount < 0) {
216+
System.out.println("Amount must be greater than 0.");
217+
} else if (amount > acctBal) {
218+
System.out.printf("Amount must not be greater than\n" + "balance of £%.02f.\n", acctBal);
219+
}
220+
} while(amount < 0 || amount > acctBal);
221+
222+
// Grab/or/Remove rest of the previous input line. - (Gobble up rest of previous input?). Prevents bad formatting.
223+
in.nextLine();
224+
225+
// Get a Memo.
226+
System.out.print("Enter a memo: ");
227+
memo = in.nextLine();
228+
229+
// Do the Withdrawel.
230+
theUser.addAcctTransaction(fromAcct, -1*amount, memo); // Decrease amount being withdrawn.
231+
}
232+
233+
/**
234+
*
235+
* @param theUser
236+
* @param in
237+
*/
238+
// Process a deposit of funds into an Account.
239+
public static void depositFunds(User theUser, Scanner in) {
240+
// Inits.
241+
int toAcct;
242+
double amount;
243+
String memo;
244+
245+
// Get the account to depsosit into.
246+
do {
247+
System.out.printf("Enter the number (1-%d) of the account\n" + "to deposit into: ", theUser.numAccounts());
248+
toAcct = in.nextInt()-1;
249+
if (toAcct < 0 || toAcct >= theUser.numAccounts()) {
250+
System.out.println("Invalid Account. Please try again.");
251+
}
252+
} while(toAcct < 0 || toAcct >= theUser.numAccounts());
253+
254+
// Get the amount to deposit.
255+
do {
256+
System.out.print("Enter the amount to Deposit: £");
257+
amount = in.nextDouble();
258+
if (amount < 0) {
259+
System.out.println("Amount must be greater than 0.");
260+
}
261+
} while(amount < 0);
262+
263+
// Grab/or/Remove rest of the previous input line. - (Gobble up rest of previous input?). Prevents bad formatting.
264+
in.nextLine();
265+
266+
// Get a Memo.
267+
System.out.print("Enter a memo: ");
268+
memo = in.nextLine();
269+
270+
// Do the Deposit.
271+
theUser.addAcctTransaction(toAcct, amount, memo); // Increase amount being deposited.
272+
}
273+
274+
}

‎Account.class

2.11 KB
Binary file not shown.

‎Account.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package ATMBankManager;
2+
3+
import java.util.ArrayList;
4+
5+
public class Account {
6+
// Account details.
7+
private String name;
8+
private String uuid; // Account's uuid (ID). Different from user's.
9+
private User holder; // Account holder (user who owns account).
10+
private ArrayList<Transaction> transactions; // List of transactions. ArrayList (imported).
11+
12+
/**
13+
*
14+
* @param name
15+
* @param holder
16+
* @param theBank
17+
*/
18+
// Create a new Account. @param name (String Name of Account), @param holder (User object that owns/hold this account), @param theBank (bank that issues this Account).
19+
public Account(String name, User holder, Bank theBank) {
20+
21+
// Set account name and holder.
22+
this.name = name;
23+
this.holder = holder;
24+
25+
// Get new account uuid.
26+
this.uuid = theBank.getNewAccountUUID();
27+
28+
// init transactions.
29+
this.transactions = new ArrayList<Transaction>();
30+
}
31+
32+
/**
33+
*
34+
* @return
35+
*/
36+
// "String" is returned. @return uuid (account's uuid). Public method available to get access to a Private value.
37+
public String getUUID() {
38+
return this.uuid;
39+
}
40+
41+
/**
42+
*
43+
* @return
44+
*/
45+
// Get Summary line for the account
46+
public String getSummaryLine() {
47+
// Get balance.
48+
double balance = this.getBalance();
49+
50+
// Format the sumamry line, dependant on negative or positive balance.
51+
if (balance >= 0) {
52+
return String.format("%s : £%.02f : %s", this.uuid, balance, this.name); // Similar to 'printf()'. "%.02f" Wildcard for a double with 2 decimal places.
53+
} else {
54+
return String.format("%s : £(%.02f) : %s", this.uuid, balance, this.name); // Similar to 'printf()'. "%.02f" Wildcard for a double with 2 decimal places.
55+
}
56+
}
57+
58+
/**
59+
*
60+
* @return
61+
*/
62+
// Get and return balance amount of Account.
63+
public double getBalance() {
64+
double balance = 0;
65+
for (Transaction t: this.transactions) {
66+
balance += t.getAmount();
67+
}
68+
return balance;
69+
}
70+
71+
// Print the Transaction history for the Account.
72+
public void printAcctTransHistory() {
73+
System.out.printf("\nTransaction history for account %s\n", this.uuid);
74+
for (int i = this.transactions.size()-1; i >= 0; i--) {
75+
System.out.println(this.transactions.get(i).getSummaryLine());
76+
}
77+
System.out.println();
78+
}
79+
80+
/**
81+
*
82+
* @param amount
83+
* @param memo
84+
*/
85+
// Create a new Transaction object and add it to our list.
86+
public void addTransaction(double amount, String memo) {
87+
Transaction newTrans = new Transaction(amount, memo, this); //
88+
this.transactions.add(newTrans);
89+
}
90+
}

‎Bank.class

2.78 KB
Binary file not shown.

‎Bank.java

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package ATMBankManager;
2+
3+
import java.util.ArrayList;
4+
import java.util.Random;
5+
6+
public class Bank {
7+
// Bank's details.
8+
private String name;
9+
private ArrayList<User> users; // Each element of type 'User', in the ArrayList 'users'.
10+
private ArrayList<Account> accounts; // Each element of type 'Account', in the ArrayList 'accounts'.
11+
12+
/**
13+
*
14+
* @param name
15+
*/
16+
// Bank Constructor, to create a new Bank object, with empty users and accounts lists. @param name (the name of the bank).
17+
public Bank(String name) {
18+
this.name = name;
19+
this.users = new ArrayList<User>();
20+
this.accounts = new ArrayList<Account>();
21+
}
22+
23+
/**
24+
*
25+
* @return
26+
*/
27+
// "public" = scope of accessibility of this func/method. "String" = returned data type from this function/method.
28+
// Generate new uuid for user, and check to ensure it doesn't exist in the users list. @return uuid.
29+
public String getNewUserUUID() {
30+
31+
// Inits.
32+
String uuid;
33+
Random rng = new Random();
34+
int len = 6; // Characters length.
35+
boolean nonUnique; // A flag.
36+
37+
// Loop until nonUnique flag is true.
38+
do {
39+
// Generate number.
40+
uuid = "";
41+
for (int i = 0; i < len; i++) {
42+
43+
// Generate a number between 0 and <10, cast as integer object (primitive int to non-primitive Integer), to allow access to number methods, such as 'toString()', (since uuid is expecting a String, and we're generating a number to append to uuid).
44+
uuid += ((Integer)rng.nextInt(10)).toString();
45+
}
46+
47+
// Check uuid is unique.
48+
nonUnique = false;
49+
// Iterate through this class' 'users' ArrayList, through all the elements of type 'User', storing the current interated element in 'u'.
50+
for (User u: this.users) {
51+
if (uuid.compareTo(u.getUUID()) == 0) {
52+
nonUnique = true;
53+
break;
54+
}
55+
}
56+
} while (nonUnique); // loop to do once, check condition, and then decide if to iterate again.
57+
58+
return uuid;
59+
}
60+
61+
/**
62+
*
63+
* @return
64+
*/
65+
// "String" = returned data type from this function/method. Generate new uuid for account, and check to ensure it doesn't exist in the users list. @return uuid.
66+
public String getNewAccountUUID() {
67+
68+
// Inits.
69+
String uuid;
70+
Random rng = new Random();
71+
int len = 10; // Characters length.
72+
boolean nonUnique; // A flag.
73+
74+
// Loop until nonUnique flag is true.
75+
do {
76+
// Generate number.
77+
uuid = "";
78+
for (int i = 0; i < len; i++) {
79+
// Generate a number between 0 and <10, cast as integer object (primitive int to non-primitive Integer), to allow access to number methods, such as 'toString()', (since uuid is expecting a String, and we're generating a number to append to uuid).
80+
uuid += ((Integer)rng.nextInt(10)).toString();
81+
}
82+
83+
// Check uuid is unique.
84+
nonUnique = false;
85+
// Iterate through this class' 'accounts' ArrayList, through all the elements of type 'Account', storing the current interated element into 'a'.
86+
for (Account a: this.accounts) {
87+
if (uuid.compareTo(a.getUUID()) == 0) {
88+
nonUnique = true;
89+
break;
90+
}
91+
}
92+
} while (nonUnique); // loop to do once, check condition, and then decide if to iterate again.
93+
94+
return uuid;
95+
}
96+
97+
/**
98+
*
99+
* @param anAcct
100+
*/
101+
// "void" keyword signifies no return values. Encapsulation - this public encapsulated (self-contained) method allows an account to be added to the private ArrayList 'accounts', form an outside class (using this method). There are two of these public methods for each list, which are mirrors (a user's list and bank's list).
102+
// Adds account to accounts list. @param anAcct (the account to add to Bank's list).
103+
public void addAccount(Account anAcct) {
104+
this.accounts.add(anAcct);
105+
}
106+
107+
/**
108+
*
109+
* @param firstName
110+
* @param lastName
111+
* @param pin
112+
* @return
113+
*/
114+
// "User" indicates a returned value of type 'User'. Create a new user, and add to users list. @param firstName (user's first name), @param lastName (user's last name), @param pin (user's pin code). @return (the new User, as an object).
115+
public User addUser(String firstName, String lastName, String pin) {
116+
User newUser = new User(firstName, lastName, pin, this); // 'this' is the Bank object.
117+
this.users.add(newUser);
118+
119+
// Create a default savings account for the new user.
120+
Account newAccount = new Account("Savings", newUser, this); // 'this' is the Bank object.
121+
122+
// Add to holder/user list and bank list. Add the same Account (not copies of Account) we're currently making, to accounts lists for Bank and User classes.
123+
newUser.addAccount(newAccount);
124+
this.addAccount(newAccount);
125+
126+
return newUser;
127+
}
128+
129+
/**
130+
*
131+
* @param userID
132+
* @param pin
133+
* @return
134+
*/
135+
// Login with some correct credentials, by associating the input with a particular userID and pin (if valid). User Login. @param userID (uuid of user logging in), @param pin (user's pin code). @return User object (Object, and only if correct login details, else null returned).
136+
public User userLogin(String userID, String pin) {
137+
138+
// Search users list for userID.
139+
for (User u: this.users) {
140+
// Check userID is correct.
141+
if (u.getUUID().compareTo(userID) == 0 && u.validatePin(pin)) {
142+
return u; // return user with correct id and pin provided.
143+
}
144+
}
145+
return null; // return null is userID or Pin is incorrect.
146+
}
147+
148+
/**
149+
*
150+
* @return
151+
*/
152+
// Get and return name of the Bank. @return name (the name of the bank).
153+
public String getName() {
154+
return this.name;
155+
}
156+
}

‎README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ATM-like Bank Account Manager
2+
3+
***
4+
5+
### A Java object-oriented (OOP) console application, which will take a user's account ID and corresponding secure 4-digit Pin number (stored as a hash (MD5)), in order to authenticate and successfully login. Once logged in users can view their account summaries, get a full transactions history, as well as withdraw, deposit and transfer funds between the user's open accounts.
6+
7+
***
8+
9+
##### A great repo to learn some of the fundamentals and advanced aspects of OOP with Java.

‎Screenshots/Banking Actions #1.png

138 KB
Loading
Loading

‎Screenshots/Login & Main Menu.png

81.1 KB
Loading
115 KB
Loading

‎Transaction.class

1.06 KB
Binary file not shown.

‎Transaction.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package ATMBankManager;
2+
3+
import java.util.Date;
4+
5+
public class Transaction {
6+
private double amount;
7+
private Date timestamp;
8+
private String memo; // A note attached to a transaction, explaining nature of transaction.
9+
private Account inAccount; // Account transaction was performed.
10+
11+
// Overloading contructors: Two constructors for a new Transaction. The correct one is deduced by the arguments passed with a call, done by Java/JVM.
12+
13+
/**
14+
*
15+
* @param amount
16+
* @param inAccount
17+
*/
18+
// Transaction constructor in case of a transaction with No optional Memo attached to it. Set all properties, and Memo to "".
19+
// Create a new Transaction. @param amount (amount involved in transaction), @param inAccount (the account the transaction belongs to).
20+
public Transaction(double amount, Account inAccount) {
21+
this.amount = amount;
22+
this.inAccount = inAccount;
23+
this.timestamp = new Date(); // Current date.
24+
this.memo = ""; // There is no memo, so set to empty string.
25+
}
26+
27+
/**
28+
*
29+
* @param amount
30+
* @param memo
31+
* @param inAccount
32+
*/
33+
// Transaction constructor in case of a transaction WITH an optional memo attached. Set the attached Memo.
34+
// Create a new Transaction. @param amount (amount involved in transaction), @param memo (memo for the transaction), @param inAccount (the account the transaction belongs to).
35+
public Transaction(double amount, String memo, Account inAccount) {
36+
37+
// Call the other constructor first. (The two argument Contructor, where NO Memo is in argument or with the Transaction).
38+
this(amount, inAccount);
39+
40+
// With all the properties now set (by calling prev. constructor), but without the Memo (set as ""), we can set the Memo here, (from "", to the argument value), in this Constructor.
41+
this.memo = memo;
42+
}
43+
44+
/**
45+
*
46+
* @return
47+
*/
48+
// Get the amount of a transaction.
49+
public double getAmount() {
50+
return this.amount;
51+
}
52+
53+
/**
54+
*
55+
* @return
56+
*/
57+
// Get a string with the summary of a Transaction.
58+
public String getSummaryLine() {
59+
if(this.amount >= 0) {
60+
return String.format("%s : £%.02f : %s", this.timestamp.toString(), this.amount, this.memo);
61+
} else {
62+
return String.format("%s : £%.02f : %s", this.timestamp.toString(), -this.amount, this.memo);
63+
}
64+
}
65+
}

‎User.class

2.77 KB
Binary file not shown.

‎User.java

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package ATMBankManager;
2+
3+
import java.util.ArrayList;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
7+
public class User {
8+
// User's bank account details.
9+
private String firstName;
10+
private String lastName;
11+
private String uuid; // ID.
12+
private byte pinHash[]; // Stored Pin code, in a byte Array called pinHash (empty). Will be stored hashed in MD5.
13+
14+
// An array with a layer that adds easy list-like functions, or easier in adding new elements etc. Instead of Array.
15+
private ArrayList<Account> accounts; // List of user's accounts. ArrayList (imported).
16+
17+
/**
18+
*
19+
* @param firstName
20+
* @param lastName
21+
* @param pin
22+
* @param theBank
23+
*/
24+
// @param firstName, @param lastName, @param pin, @param theBank.
25+
public User(String firstName, String lastName, String pin, Bank theBank) {
26+
27+
// Set user's name.
28+
this.firstName = firstName;
29+
this.lastName = lastName;
30+
31+
// Store the original pin as MD5 hash. A Try Catch, to catch any errors in an unrecognised getInstance argument string, (if "MD5" was invalid, it would throw the error).
32+
try {
33+
// pinHash is a byte array. Use digest function ' getBytes()', from our md MessageDigest hashing object, and use on user's pin, to get the memeory/bytes of the pin object to digest the bytes through our MD5 algorith, to return pin hash bytes. In short, we are taking pin string and returning a MD5 hashed value.
34+
35+
MessageDigest md = MessageDigest.getInstance("MD5"); // MD5 Hashing algorithm.
36+
this.pinHash = md.digest(pin.getBytes()); // Pass pin through algo, to hash.
37+
38+
} catch (NoSuchAlgorithmException e) {
39+
40+
System.err.println("error: caught NoSuchAlgorithmException");
41+
e.printStackTrace(); //
42+
System.exit(1); // Exit app.
43+
}
44+
45+
// Generate a new uuid for user.
46+
this.uuid = theBank.getNewUserUUID();
47+
48+
// Create empty list of accounts. ArrayList constructor.
49+
this.accounts = new ArrayList<Account>();
50+
51+
// Print log message.
52+
System.out.printf("New user %s, %s with ID %s created.\n", lastName, firstName, this.uuid); // Print user information.
53+
}
54+
55+
/**
56+
*
57+
* @param anAcct
58+
*/
59+
// "void" keyword signifies no return values. Encapsulation - this public encapsulated (self-contained) method allows an account to be added to the private ArrayList 'accounts', form an outside class (using this method). There are two of these public methods for each list, which are mirrors (a user's list and bank's list).
60+
// @param anAcct (the account to add to User's list).
61+
public void addAccount(Account anAcct) {
62+
this.accounts.add(anAcct);
63+
}
64+
65+
/**
66+
*
67+
* @return
68+
*/
69+
// "String" is returned. @return uuid (user's uuid). Public method made available, to get access to a Private value.
70+
public String getUUID() {
71+
return this.uuid;
72+
}
73+
74+
/**
75+
*
76+
* @param aPin
77+
* @return
78+
*/
79+
// Check whether given pin matches the particular User's pin. @param aPin (inputted pin to check). @return boolean (whether pin is valid or not).
80+
public boolean validatePin(String aPin) {
81+
try {
82+
// pinHash is a byte array. Use digest function ' getBytes()', from our md MessageDigest hashing object, and use on user's pin, to get the memeory/bytes of the pin object to digest the bytes through our MD5 algorith, to return pin hash bytes. In short, we are taking pin string and returning a MD5 hashed value.
83+
84+
MessageDigest md = MessageDigest.getInstance("MD5"); // MD5 Hashing algorithm.
85+
return MessageDigest.isEqual(md.digest(aPin.getBytes()), this.pinHash); // Static method 'isDigest()', to return a boolean value, after comparing this inputted pin (hashed) with our targetted User's pin. True if a matching hash is found, else false.
86+
87+
} catch (NoSuchAlgorithmException e) {
88+
89+
System.err.println("error: caught NoSuchAlgorithmException");
90+
e.printStackTrace(); //
91+
System.exit(1); // Exit app.
92+
}
93+
return false; // If somehow the execution stream gets here, by getInstance error catch, then we still need to return a boolean value, so we dont get a Java error.
94+
}
95+
96+
/**
97+
*
98+
* @return firstName (the user's first name)
99+
*/
100+
// Method to access private firstName, to return the User's firstName.
101+
public String getFirstName() {
102+
return this.firstName;
103+
}
104+
105+
// Print summaries for the accounts of the user.
106+
public void printAccountsSummary() {
107+
System.out.printf("\n\n%s's accounts summary\n", this.firstName);
108+
for (int i = 0; i < this.accounts.size(); i++) {
109+
System.out.printf(" %d) %s\n", i+1, this.accounts.get(i).getSummaryLine()); // "%d" Wildcard for integers. "%s" Wildcard for strings.
110+
}
111+
System.out.println();
112+
}
113+
114+
/**
115+
*
116+
* @return
117+
*/
118+
// Get and return number of accounts User has.
119+
public int numAccounts() {
120+
return this.accounts.size();
121+
}
122+
123+
/**
124+
*
125+
* @param acctIdx
126+
*/
127+
// Print the Transaction history for a particular Account.
128+
public void printAcctTransHistory(int acctIdx) {
129+
this.accounts.get(acctIdx).printAcctTransHistory();
130+
}
131+
132+
/**
133+
*
134+
* @param acctIdx
135+
* @return
136+
*/
137+
// Get the balance of a particular account.
138+
public double getAcctBalance(int acctIdx) {
139+
return this.accounts.get(acctIdx).getBalance();
140+
}
141+
142+
/**
143+
*
144+
* @param acctIdx
145+
* @return
146+
*/
147+
// Get the UUID of a particular Account.
148+
public String getAcctUUID(int acctIdx) {
149+
return this.accounts.get(acctIdx).getUUID();
150+
}
151+
152+
/**
153+
*
154+
* @param acctIdx
155+
* @param amount
156+
* @param memo
157+
*/
158+
// Add a Transaction to a particular Account
159+
public void addAcctTransaction(int acctIdx, double amount, String memo) {
160+
this.accounts.get(acctIdx).addTransaction(amount, memo);
161+
}
162+
}

0 commit comments

Comments
 (0)
Please sign in to comment.