This commit is contained in:
ds.731
2025-01-02 14:10:20 -06:00
commit a453916341
8 changed files with 350 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package bankAcctApp;
import java.util.ArrayList;
import java.util.List;
public class BankAcctApp {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
boolean moreCustomers = true;
while (moreCustomers) {
Customer customer = new Customer();
System.out.println("Enter details for new customer:\n");
customer.setID(DataEntry.inputStringWithLimit("Customer ID (max 5 chars): ", 5));
customer.setSSN(DataEntry.inputNumericString("SSN (9 numeric chars): ", 9));
customer.setLastName(DataEntry.inputStringWithLimit("Last Name (max 20 chars): ", 20));
customer.setFirstName(DataEntry.inputStringWithLimit("First Name (max 15 chars): ", 15));
customer.setStreet(DataEntry.inputStringWithLimit("Street (max 20 chars): ", 20));
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));
customers.add(customer);
String more = DataEntry.inputStringWithLimit("\nAdd another customer? (y/n): ", 1);
if (!more.equalsIgnoreCase("y")) {
moreCustomers = false;
}
}
System.out.println("\nCustomer Information:");
System.out.println("========================================");
for (Customer c : customers) {
System.out.println(c);
}
}
}

View File

@@ -0,0 +1,95 @@
package bankAcctApp;
public class Customer {
private String id;
private String ssn;
private String lastName;
private String firstName;
private String street;
private String city;
private String state;
private String zip;
private String phone;
// Getter and Setter for Customer ID info.
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
// Getter and Setter for Customer SSN info.
public String getSSN() {
return ssn;
}
public void setSSN(String ssn) {
this.ssn = ssn;
}
// Getter and Setter for Customer Last Name.
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
// Getter and Setter for Customer First Name.
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// Getter and Setter for Customer Street Address.
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
// Getter and Setter for Customer City.
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
// Getter and Setter for Customer State.
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
// Getter and Setter for Customer ZIP.
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
// Getter and Setter for Customer Phone Number.
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
// 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
@Override
public String toString() {
return String.format(
"ID: %s, SSN: %s, Name: %s %s, Address: %s, %s, %s %s, Phone: %s",
id, ssn, firstName, lastName, street, city, state, zip, phone
);
}
}

View File

@@ -0,0 +1,116 @@
package bankAcctApp;
import java.util.Scanner;
import java.util.regex.*;
public class DataEntry {
private static Scanner in = new Scanner(System.in);
// Static method to validate a string without a limit.
public static String inputString(String prompt) {
System.out.print(prompt);
return in.nextLine();
}
// Static method to validate a string with a limit.
public static String inputStringWithLimit(String prompt, int maxLength) {
String input;
do {
System.out.print(prompt);
input = in.nextLine();
if (input == "" || input.length() > maxLength) {
System.out.println("Invalid input. Must be non-blank and up to "
+ maxLength + " characters.");
}
} while (input == "" || input.length() > maxLength);
return input;
}
// Static method to validate a numeric-only string without a limit.
public static String inputNumericString(String prompt, int length) {
String input;
do {
System.out.print(prompt);
input = in.nextLine();
if (!input.matches("\\d+")) {
System.out.println("Invalid input. Must only be numeric characters.");
}
} while (!input.matches("\\d+"));
return input;
}
// Static method to validate Integers.
public static int inputInteger(String prompt) {
int input = 0;
boolean isValid = false;
System.out.print(prompt);
do {
if (in.hasNextInt()) {
input = in.nextInt();
isValid = true;
} else {
System.out.print("Invalid entry. Try again: ");
in.nextLine();
}
} while (!isValid);
return input;
}
// Static method to validate Integers within a range.
public static int inputIntegerInRange(String prompt, int min, int max) {
int input = (min - 1);
do {
input = inputInteger(prompt);
if (input < min || input > max) {
System.out.print("Invalid input. Try again: ");
}
} while (input < min || input > max);
return input;
}
// Static method to validate decimals.
public static double inputDecimal(String prompt) {
boolean isValid = false;
double decimalValue = 0.0;
System.out.print(prompt);
do {
if (in.hasNextDouble()) {
decimalValue = in.nextDouble();
isValid = true;
} else{
System.out.println("Invalid input. Please enter a valid decimal number.");
in.next();
}
} while (!isValid);
return decimalValue;
}
// Static method to validate decimals within a range.
public static double inputDecimalInRange(String prompt, double min, double max) {
double value;
do {
value = inputDecimal(prompt);
if (value < min || value > max) {
System.out.println("Invalid input. Must be between "
+ min + " and " + max + ".");
}
} while (value < min || value > max);
return value;
}
// Static method to validate a date in MM/DD/YYYY format.
public static String inputDate(String prompt) {
Pattern patternDate = Pattern.compile("^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/(\\d{4})$");
String date = "";
System.out.print(prompt);
do {
String input = in.nextLine();
if (patternDate.matcher(input).matches()) {
return input;
} else {
System.out.print("Invalid date. Please try again: ");
}
} while (date == "");
return date;
}
}