Expanded application functionality by adding Account class and enhanced data validation using try/catch blocks for exception handling

This commit is contained in:
ds.731 2025-01-02 15:19:13 -06:00
parent a453916341
commit 65963450ab
8 changed files with 289 additions and 41 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,80 @@
/* Phase II */
package bankAcctApp;
public class Account {
private String accountNumber;
private String accountType;
private Double svcFee;
private Double interestRate;
private Double overDraftFee;
private Double balance = 0.00;
// Getter and Setter for Account Number info.
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
// Getter and Setter for Customer ID info.
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
// Getter and Setter for Service Fee info.
public Double getSvcFee() {
return svcFee;
}
public void setSvcFee(Double svcFee) {
this.svcFee = svcFee;
}
// Getter and Setter for Interest Rate info.
public Double getInterestRate() {
return interestRate;
}
public void setInterestRate(Double interestRate) {
this.interestRate = interestRate;
}
// Getter and Setter for Overdraft Fee info.
public Double getOverDraftFee() {
return overDraftFee;
}
public void setOverDraftFee(Double overDraftFee) {
this.overDraftFee = overDraftFee;
}
// Getter and Setter for Balance info.
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
// Override the toString() method that is inherited by default in Java.
// Then use the custom-written toString() method to return Account Info
@Override
public String toString() {
return String.format(
"Acct#: Type: Svc Fee: Int Rate: Ovdft Fee: Balance: \n" +
"------ ----- -------- --------- ---------- -------- \n" +
"%-8s %-8s $%-10.2f %-12.1f $%-12.2f $%-9.2f",
accountNumber, accountType, svcFee, interestRate, overDraftFee, balance
);
}
}

View File

@ -1,40 +1,182 @@
/* Phase II */
package bankAcctApp; package bankAcctApp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.InputMismatchException;
public class BankAcctApp { public class BankAcctApp {
public static void main(String[] args) { public static void main(String[] args) {
// Create ArrayLists for the Customer and Account classes. Each ArrayList will store the instances
// created of its respective class, appending each new instance to the end of the list.
ArrayList<Customer> customers = new ArrayList<>(); ArrayList<Customer> customers = new ArrayList<>();
ArrayList<Account> accounts = new ArrayList<>();
boolean moreCustomers = true; boolean moreCustomers = true;
// Create the loop that keeps adding Customer and Account instances until
// the user chooses to quit.
while (moreCustomers) { while (moreCustomers) {
Customer customer = new Customer();
// Create instances of the both the Customer and Account classes.
// These will be stored in their respective class' ArrayList.
Customer customer = new Customer();
Account account = new Account();
// Customer Information:
System.out.println("Enter details for new customer:\n"); System.out.println("Enter details for new customer:\n");
customer.setID(DataEntry.inputStringWithLimit("Customer ID (max 5 chars): ", 5)); // Collect and validate user input for Customer ID info.
customer.setSSN(DataEntry.inputNumericString("SSN (9 numeric chars): ", 9)); try {
customer.setLastName(DataEntry.inputStringWithLimit("Last Name (max 20 chars): ", 20)); customer.setID(DataEntry.inputStringWithLimit("Customer ID (max 5 chars): ", 5));
customer.setFirstName(DataEntry.inputStringWithLimit("First Name (max 15 chars): ", 15)); } catch (IllegalArgumentException e){
customer.setStreet(DataEntry.inputStringWithLimit("Street (max 20 chars): ", 20)); System.out.println("Customer ID must be 5 alphanumeric characters only. Try again.");
customer.setCity(DataEntry.inputStringWithLimit("City (max 20 chars): ", 20)); }
customer.setState(DataEntry.inputStringWithLimit("State (2 chars): ", 2));
customer.setZip(DataEntry.inputNumericString("Zip (5 numeric chars): ", 5));
customer.setPhone(DataEntry.inputNumericString("Phone (10 numeric chars): ", 10));
// Collect and validate user input for Social Security Number (SSN).
try {
customer.setSSN(DataEntry.inputNumericString("SSN (9 numeric chars): ", 9));
} catch (IllegalArgumentException e) {
System.out.println("SSN must be exactly 9 digits. Try again.");
}
// Collect and validate user input for Last Namee.
try {
customer.setLastName(DataEntry.inputStringWithLimit("Last Name (max 20 chars): ", 20));
} catch (IllegalArgumentException e) {
System.out.println("Last Name must not contain numbers (0-9) or special characters "
+ "(!@#$%^&*(){}[]|). Try again.");
}
// Collect and validate user input for First Name.
try {
customer.setFirstName(DataEntry.inputStringWithLimit("First Name (max 15 chars): ", 15));
} catch (IllegalArgumentException e) {
System.out.println("First Name must not contain numbers (0-9) "
+ "or special characters (!@#$%^&*(){}[]|). Try again.");
}
// Collect and validate user input for Street Address.
try {
customer.setStreet(DataEntry.inputStringWithLimit("Street (max 20 chars): ", 20));
} catch (IllegalArgumentException e) {
System.out.println("Street must be no more than 20 characters consisting of "
+ "numbers, letters, spaces, and \" . , - ' \". Try again.");
}
// Collect and validate user input for City.
try {
customer.setCity(DataEntry.inputStringWithLimit("City (max 20 chars): ", 20));
} catch (IllegalArgumentException e) {
System.out.println("City must not contain numbers (0-9) or special characters "
+ "(!@#$%^&*(){}[]|). Try again.");
}
// Collect and validate user input for State.
try {
customer.setState(DataEntry.inputStringWithLimit("State (2 chars): ", 2));
} catch (InputMismatchException e) {
System.out.println("State must be 2 letters only. Try again.");
}
// Collect and validate user input for Zip Code.
try {
customer.setZip(DataEntry.inputNumericString("Zip (5 numeric chars): ", 5));
} catch (NumberFormatException e) {
System.out.println("Zip Code must only contain 5 digits. Try again.");
}
// Collect and validate user input for Phone Number.
try {
customer.setPhone(DataEntry.inputNumericString("Phone (10 numeric chars): ", 10));
} catch (NumberFormatException e) {
System.out.println("Phone Number must only contain 9 digits.");
}
// Store Customer information to the Customer instance.
customers.add(customer); customers.add(customer);
String more = DataEntry.inputStringWithLimit("\nAdd another customer? (y/n): ", 1); // Account Information:
if (!more.equalsIgnoreCase("y")) { // Collect and validate user input for Account Number.
try {
account.setAccountNumber(DataEntry.inputNumericString("Account Number (5 numeric chars): ", 5));
} catch (NumberFormatException e) {
System.out.println("Account Number can only be 5 digits. Try again.");
}
// Collect and validate user input for Account Type.
try {
boolean validAcctType = false;
while (!validAcctType) {
String input = DataEntry.inputStringWithLimit("Account type ('CHK' or 'SVG' only): ", 3);
if (input.equalsIgnoreCase("CHK") || input.equalsIgnoreCase("SAV")) {
account.setAccountType(input.toUpperCase());
validAcctType = true;
} else {
System.out.println("Input for Account Type can only be 'CHK' or 'SVG'. Try again.");
}
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please try again.");
}
// Collect and validate user input for Service Fee.
try {
account.setSvcFee(DataEntry.inputDecimalInRange("Service Fee Amount (in dollars and cents): $", 0.00, 10.00));
} catch (InputMismatchException e) {
System.out.println("Input must be a dollar amount between $0.00 - $10.00. Try again.");
}
// Collect and validate user input for Interest Rate.
try {
account.setInterestRate(DataEntry.inputDecimalInRange("Interest Rate (percent between 0.0% and 10.0%: ", 0.0, 10.0));
} catch (InputMismatchException e) {
System.out.println("Interest Rate must be entered as a decimial between 0.0% - 10.0%. Try again.");
}
// Collect and validate user input for Overdraft Fee.
try {
account.setOverDraftFee(DataEntry.inputDecimal("Overdraft Fee Amount (in dollars and cents): $"));
} catch (InputMismatchException e) {
System.out.println("Input must be a dollar amount between $0.00 and $10.00");
}
// Collect and validate user input for Current Balance.
try {
account.setBalance(DataEntry.inputDecimal("Starting Balance (in dollars and cents): $"));
} catch (InputMismatchException e) {
System.out.println("Current Balance must be in dollars and cents. Try again.");
}
// Add the Account class instance to the accounts ArrayList
accounts.add(account);
// Prompt user to add additional customers or quit.
String more = DataEntry.inputStringWithLimit("\nAdd another customer? (y/n): ", 1);
if (more.equalsIgnoreCase("n")) {
moreCustomers = false; moreCustomers = false;
} }
} }
System.out.println("\nCustomer Information:"); // Print out the results so that each Customer instance and its corresponding Account instance are
System.out.println("========================================"); // printed together, and so the program iterates through all of the instance pairs in their respective ArrayLists.
for (Customer c : customers) { System.out.println("\n========================================\n");
System.out.println(c); for (int i = 0; i < customers.size(); i++) {
// Use [int i] for both ArrayLists to ensure the correct Accounts instance is
// printed with the corresponding Customer instance.
Customer customer = customers.get(i);
Account account = accounts.get(i);
System.out.println(customer + "\n");
System.out.println(account);
System.out.println("---------------------------------------------------------"
+ "----------------------------------------------------------------------------");
} }
} }
} }

View File

@ -1,6 +1,10 @@
/* Phase II */
package bankAcctApp; package bankAcctApp;
public class Customer { public class Customer {
private String id;
private String id;
private String ssn; private String ssn;
private String lastName; private String lastName;
private String firstName; private String firstName;
@ -10,6 +14,7 @@ public class Customer {
private String zip; private String zip;
private String phone; private String phone;
// Getter and Setter for Customer ID info. // Getter and Setter for Customer ID info.
public String getID() { public String getID() {
return id; return id;
@ -18,6 +23,7 @@ public class Customer {
this.id = id; this.id = id;
} }
// Getter and Setter for Customer SSN info. // Getter and Setter for Customer SSN info.
public String getSSN() { public String getSSN() {
return ssn; return ssn;
@ -26,6 +32,7 @@ public class Customer {
this.ssn = ssn; this.ssn = ssn;
} }
// Getter and Setter for Customer Last Name. // Getter and Setter for Customer Last Name.
public String getLastName() { public String getLastName() {
return lastName; return lastName;
@ -34,6 +41,7 @@ public class Customer {
this.lastName = lastName; this.lastName = lastName;
} }
// Getter and Setter for Customer First Name. // Getter and Setter for Customer First Name.
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
@ -42,6 +50,7 @@ public class Customer {
this.firstName = firstName; this.firstName = firstName;
} }
// Getter and Setter for Customer Street Address. // Getter and Setter for Customer Street Address.
public String getStreet() { public String getStreet() {
return street; return street;
@ -50,6 +59,7 @@ public class Customer {
this.street = street; this.street = street;
} }
// Getter and Setter for Customer City. // Getter and Setter for Customer City.
public String getCity() { public String getCity() {
return city; return city;
@ -67,6 +77,7 @@ public class Customer {
this.state = state; this.state = state;
} }
// Getter and Setter for Customer ZIP. // Getter and Setter for Customer ZIP.
public String getZip() { public String getZip() {
return zip; return zip;
@ -75,6 +86,7 @@ public class Customer {
this.zip = zip; this.zip = zip;
} }
// Getter and Setter for Customer Phone Number. // Getter and Setter for Customer Phone Number.
public String getPhone() { public String getPhone() {
return phone; return phone;
@ -83,13 +95,18 @@ public class Customer {
this.phone = phone; this.phone = phone;
} }
// Override the toString() method that is inherited by default from Java's Object class. // Override the toString() method that is inherited by default from Java's Object class.
// Then use the custom-written toString() method to return Customer Info // Then use the custom-written toString() method to return Customer Info
@Override @Override
public String toString() { public String toString() {
return String.format( return String.format(
"ID: %s, SSN: %s, Name: %s %s, Address: %s, %s, %s %s, Phone: %s", "Customer Information:\n" +
id, ssn, firstName, lastName, street, city, state, zip, phone "---------------------\n" +
); "ID: Last Name: First Name: SSN: Phone: Street: City: ST: ZIP: \n" +
} "--- ---------- ----------- ---- ------ ------- ----- --- ---- \n" +
"%-7s %-22s %-17s %-11s %-11s %-22s %-17s %-6s %-7s",
id, lastName, firstName, ssn, phone, street, city, state, zip
);
}
} }

View File

@ -1,3 +1,5 @@
/* Phase II */
package bankAcctApp; package bankAcctApp;
import java.util.Scanner; import java.util.Scanner;
@ -18,11 +20,11 @@ public class DataEntry {
do { do {
System.out.print(prompt); System.out.print(prompt);
input = in.nextLine(); input = in.nextLine();
if (input == "" || input.length() > maxLength) { if (input.isBlank() || input.length() > maxLength) {
System.out.println("Invalid input. Must be non-blank and up to " System.out.println("Invalid input. Must be non-blank and up to "
+ maxLength + " characters."); + maxLength + " characters.");
} }
} while (input == "" || input.length() > maxLength); } while (input.isBlank() || input.length() > maxLength);
return input; return input;
} }
@ -32,10 +34,10 @@ public class DataEntry {
do { do {
System.out.print(prompt); System.out.print(prompt);
input = in.nextLine(); input = in.nextLine();
if (!input.matches("\\d+")) { if (!input.matches("\\d{" + length + "}")) {
System.out.println("Invalid input. Must only be numeric characters."); System.out.println("Invalid input. Must be exactly " + length + " numbers.");
} }
} while (!input.matches("\\d+")); } while (!input.matches("\\d{" + length + "}"));
return input; return input;
} }
@ -50,20 +52,23 @@ public class DataEntry {
isValid = true; isValid = true;
} else { } else {
System.out.print("Invalid entry. Try again: "); System.out.print("Invalid entry. Try again: ");
in.nextLine(); in.next();
} }
in.nextLine();
} while (!isValid); } while (!isValid);
return input; return input;
} }
// Static method to validate Integers within a range. // Static method to validate Integers are within a range.
public static int inputIntegerInRange(String prompt, int min, int max) { public static int inputIntegerInRange(String prompt, int min, int max) {
int input = (min - 1); int input = (min - 1);
do { do {
input = inputInteger(prompt); input = inputInteger(prompt);
if (input < min || input > max) { if (input < min || input > max) {
System.out.print("Invalid input. Try again: "); System.out.print("Invalid input. Try again: ");
in.next();
} }
in.nextLine();
} while (input < min || input > max); } while (input < min || input > max);
return input; return input;
} }
@ -81,11 +86,12 @@ public class DataEntry {
System.out.println("Invalid input. Please enter a valid decimal number."); System.out.println("Invalid input. Please enter a valid decimal number.");
in.next(); in.next();
} }
in.nextLine();
} while (!isValid); } while (!isValid);
return decimalValue; return decimalValue;
} }
// Static method to validate decimals within a range. // Static method to validate decimals are within a range.
public static double inputDecimalInRange(String prompt, double min, double max) { public static double inputDecimalInRange(String prompt, double min, double max) {
double value; double value;
do { do {
@ -93,6 +99,7 @@ public class DataEntry {
if (value < min || value > max) { if (value < min || value > max) {
System.out.println("Invalid input. Must be between " System.out.println("Invalid input. Must be between "
+ min + " and " + max + "."); + min + " and " + max + ".");
in.next();
} }
} while (value < min || value > max); } while (value < min || value > max);
return value; return value;
@ -109,8 +116,10 @@ public class DataEntry {
return input; return input;
} else { } else {
System.out.print("Invalid date. Please try again: "); System.out.print("Invalid date. Please try again: ");
in.next();
} }
} while (date == ""); in.nextLine();
} while (date.isEmpty());
return date; return date;
} }
} }