I need help adding a lot more validations for user input for this code. import j
ID: 3557523 • Letter: I
Question
I need help adding a lot more validations for user input for this code.
import java.io.*;
import java.util.*;
/**
* Returns a users bank account information and allows the user to deposit, withdraw and check his/her balance.
* Also, returns the highest and lowest account balances that have been saved. This is done by the data being saved in
* an array list.
*
*
@author t59360
*
*/
public
class TestBankAccount {
static Scanner input = new Scanner(System.in);
/**
* The main method provides the menu options and invokes the methods per the user input.
*
*
@param args
*
@throws FileNotFoundException when checkBalance method is selected and the file is not found.
*/
public static void main(String[] args) throws FileNotFoundException {
ArrayList<BankAccount> list =
new ArrayList<BankAccount>();
int choice = 0;
do {
while (true) {
System.
out.println("1. Deposit money");
System.
out.println("2. Withdraw money");
System.
out.println("3. Check balance");
System.
out.println("4. Create new account");
System.
out.print("Your choice, 0 to quit: ");
try {
String line =
input.nextLine();
choice = Integer.parseInt(line);
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid entry! Please Try Again!");
input = new Scanner(System.in);
continue;
}
}
switch (choice) {
case 1:
depositMoney(list);
break;
case 2:
withdrawMoney(list);
break;
case 3:
checkBalance(list);
break;
case 4:
createNewAccount(list);
break;
case 0:
System.
out.println("Thank you for trusting us with your banking needs!");
break;
default:
System.
out.println("Invalid option is selected! Please try again");
}
System.
out.println();
}
while (choice != 0);
if (list.size() > 0) {
displayResults(list);
}
}
/**
* When invoked this method asks the user their account number and the
* amount they want to deposit and then checks against the array list to
* make sure that their is an account that matches. If their is an account
* that matches then the program will add the deposited amount to what is
* currently in the account.
*
*
@param list
*/
private static void depositMoney(ArrayList<BankAccount> list) {
int number;
while (true) {
System.
out.println(" Enter account number: ");
try {
number =
input.nextInt();
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid Entry! Please try only Integers");
input = new Scanner(System.in);
}
}
System.
out.print("Enter money to deposit: $");
double money = input.nextDouble();
boolean isFound = false;
for (int i = 0; i < list.size() && !isFound; i++) {
if (list.get(i).getAccountNumber() == number) {
list.get(i).Deposit(money);
System.
out.println("Account Number: " + list.get(i).getAccountNumber());
System.
out.println("Current Balance: $" + list.get(i).getBalance());
isFound =
true;
}
}
if (!isFound)
System.
out.println("This account number is not found!");
}
/**
* When invoked this method asks the user for his/her account number
* and then the amount they want to withdraw. The program then checks the array
* list for the account number and checks to make sure their is enough money
* in the account to withdraw the users request. If everything checks out
* then the program displays the account number and the remaining balance.
*
*
@param list
*/
private static void withdrawMoney(ArrayList<BankAccount> list) {
int number;
while (true) {
System.
out.print(" Enter account number: ");
try {
number =
input.nextInt();
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid Entry! Please try only Integers");
input = new Scanner(System.in);
}
}
System.
out.print("Enter amount to withdraw: $");
double money = input.nextDouble();
boolean isFound = false;
for (int i = 0; i < list.size() && !isFound; i++) {
if (list.get(i).getAccountNumber() == number) {
list.get(i).withdraw(money);
System.
out.println("Account Number: " + list.get(i).getAccountNumber());
System.
out.println("Current Balance: $" + list.get(i).getBalance());
isFound =
true;
}
}
if (!isFound)
System.
out.println("This account number is not found!");
}
/**
* This method when invoked asks the user for their account number and then
* checks the account number against the array list to make sure it exists
* and get the account balance. Then gives the user their account number and
* the balance in which exists in their account.
*
*
@param list
*/
private static void checkBalance(ArrayList<BankAccount> list) {
int number;
while (true) {
System.
out.print(" Enter account number: ");
try {
number =
input.nextInt();
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid Entry! Please try only Integers");
input = new Scanner(System.in);
}
}
boolean isFound = false;
for (int i = 0; i < list.size() && !isFound; i++) {
if (list.get(i).getAccountNumber() == number) {
System.
out.println("Account Number: " + list.get(i).getAccountNumber());
System.
out.println("Current Balance: $" + list.get(i).getBalance());
isFound =
true;
}
}
if (!isFound)
System.
out.println("This account number is not found!");
}
/**
* This method when invoked starts the process of creating a new account by
* asking the user his/her first name and then last name. The program
* validates that the user enters a name that consists of at least 3 characters.
* Then asks the user their initial deposit. When all of the data is entered the
* data gets stored in the array list for retrieval later.
*
*
@param list
*/
private static void createNewAccount(ArrayList<BankAccount> list) {
int number = list.size() + 1;
System.
out.print(" Enter the first name: ");
String first =
input.nextLine();
if (first.length() < 3) {
System.
out.println("First name must have at least 3 characters and should be only letters!");
System.
out.print("Re-enter the first name: ");
first =
input.next();
}
System.
out.print(" Enter the last name: ");
String last =
input.nextLine();
if (last.length() < 3) {
System.
out.println("Last name must have at least 3 characters.");
System.
out.print("Re-enter the last name: ");
last =
input.next();
}
System.
out.print("Enter the initial balance: $");
Double initialBal =
input.nextDouble();
System.
out.println("Thank You, " + first + " " + last + " for joining our bank today! Have a great Day!");
BankAccount acc =
new BankAccount(number, first, last, initialBal);
list.add(acc);
}
/**
* This method when invoked checks the Array List for the highest number in the list
* and the lowest number in the list and displays them in the console. Then prints
* to a file on the H drive called MyBank.txt
*
*
@param list
*
@throws FileNotFoundException when a file cannot be written to.
*/
private static void displayResults(ArrayList<BankAccount> list)throws FileNotFoundException {
double high = list.get(0).getBalance();
double low = list.get(0).getBalance();
int highNumber = 1;
int lowNumber = 1;
for (int i = 0; i < list.size(); i++) {
if (high < list.get(i).getBalance()) {
high = list.get(i).getBalance();
highNumber = i + 1;
}
if (low > list.get(i).getBalance()) {
low = list.get(i).getBalance();
lowNumber = i + 1;
}
}
System.
out.println("Highest balance: Account " + highNumber + " Amount $" + high);
System.
out.println("Lowest balance: Account " + lowNumber + " Amount $" + low);
PrintWriter out =
new PrintWriter(new File("H:/MyBank.txt"));
for (int i = 0; i < list.size(); i++) {
out.println(list.get(i));
}
out.close();
}
}
-What i am looking for is when someone creates a new account and enters a number instead of a letter for their name I need it to give an error message and ask again until they get it right.
-When the user enters their initial deposit I need the program to only accept numbers and not letters.
I need validation for as much as one can think of on this. I got a lot of it done just stuck on the two big ones above
Explanation / Answer
import java.io.*;
import java.util.*;
public
class TestBankAccount {
static Scanner input = new Scanner(System.in);
/**
* The main method provides the menu options and invokes the methods per the user input.
*
*
@param args
*
@throws FileNotFoundException when checkBalance method is selected and the file is not found.
*/
public static void main(String[] args) throws FileNotFoundException {
ArrayList<BankAccount> list =
new ArrayList<BankAccount>();
int choice = 0;
do {
while (true) {
System.
out.println("1. Deposit money");
System.
out.println("2. Withdraw money");
System.
out.println("3. Check balance");
System.
out.println("4. Create new account");
System.
out.print("Your choice, 0 to quit: ");
try {
String line =
input.nextLine();
choice = Integer.parseInt(line);
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid entry! Please Try Again!");
input = new Scanner(System.in);
continue;
}
}
switch (choice) {
case 1:
depositMoney(list);
break;
case 2:
withdrawMoney(list);
break;
case 3:
checkBalance(list);
break;
case 4:
createNewAccount(list);
break;
case 0:
System.
out.println("Thank you for trusting us with your banking needs!");
break;
default:
System.
out.println("Invalid option is selected! Please try again");
}
System.
out.println();
}
while (choice != 0);
if (list.size() > 0) {
displayResults(list);
}
}
/**
* When invoked this method asks the user their account number and the
* amount they want to deposit and then checks against the array list to
* make sure that their is an account that matches. If their is an account
* that matches then the program will add the deposited amount to what is
* currently in the account.
*
*
@param list
*/
private static void depositMoney(ArrayList<BankAccount> list) {
int number;
while (true) {
System.
out.println(" Enter account number: ");
try {
number =
input.nextInt();
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid Entry! Please try only Integers");
input = new Scanner(System.in);
}
}
System.
out.print("Enter money to deposit: $");
try{
Double money = input.nextDouble();
boolean isFound = false;
if(money.equals("[a-zA-Z]+"))
{
System.out.println("only numbers valid");
}
for (int i = 0; i < list.size() && !isFound; i++) {
if (list.get(i).getAccountNumber() == number) {
list.get(i).Deposit(money);
System.
out.println("Account Number: " + list.get(i).getAccountNumber());
System.
out.println("Current Balance: $" + list.get(i).getBalance());
isFound =
true;
break;
}
}
if (!isFound)
System.
out.println("This account number is not found!");
}
catch(Exception e)
{
System.
out.println("Error: Invalid Entry! Please try only Integers");
}
}
/**
* When invoked this method asks the user for his/her account number
* and then the amount they want to withdraw. The program then checks the array
* list for the account number and checks to make sure their is enough money
* in the account to withdraw the users request. If everything checks out
* then the program displays the account number and the remaining balance.
*
*
@param list
*/
private static void withdrawMoney(ArrayList<BankAccount> list) {
int number;
while (true) {
System.
out.print(" Enter account number: ");
try {
number =
input.nextInt();
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid Entry! Please try only Integers");
input = new Scanner(System.in);
}
}
System.
out.print("Enter amount to withdraw: $");
double money = input.nextDouble();
boolean isFound = false;
for (int i = 0; i < list.size() && !isFound; i++) {
if (list.get(i).getAccountNumber() == number) {
list.get(i).withdraw(money);
System.
out.println("Account Number: " + list.get(i).getAccountNumber());
System.
out.println("Current Balance: $" + list.get(i).getBalance());
isFound =
true;
}
}
if (!isFound)
System.
out.println("This account number is not found!");
}
/**
* This method when invoked asks the user for their account number and then
* checks the account number against the array list to make sure it exists
* and get the account balance. Then gives the user their account number and
* the balance in which exists in their account.
*
*
@param list
*/
private static void checkBalance(ArrayList<BankAccount> list) {
int number;
while (true) {
System.
out.print(" Enter account number: ");
try {
number =
input.nextInt();
break;
}
catch (Exception e) {
System.
out.println("Error: Invalid Entry! Please try only Integers");
input = new Scanner(System.in);
}
}
boolean isFound = false;
for (int i = 0; i < list.size() && !isFound; i++) {
if (list.get(i).getAccountNumber() == number) {
System.
out.println("Account Number: " + list.get(i).getAccountNumber());
System.
out.println("Current Balance: $" + list.get(i).getBalance());
isFound =
true;
}
}
if (!isFound)
System.
out.println("This account number is not found!");
}
/**
* This method when invoked starts the process of creating a new account by
* asking the user his/her first name and then last name. The program
* validates that the user enters a name that consists of at least 3 characters.
* Then asks the user their initial deposit. When all of the data is entered the
* data gets stored in the array list for retrieval later.
*
*
@param list
*/
private static void createNewAccount(ArrayList<BankAccount> list) {
int number = list.size() + 1;
System.
out.print(" Enter the first name: ");
String first =
input.nextLine();
if (first.length() < 3||!first.matches("[a-zA-Z]+")) {
System.
out.println("First name must have at least 3 characters and should be only letters!");
System.
out.print("Re-enter the first name: ");
first =
input.next();
}
System.
out.print(" Enter the last name: ");
String last =
input.nextLine();
if (last.length() < 3||!last.matches("[a-zA-Z]+")) {
System.
out.println("Last name must have at least 3 characters and should only be letters.");
System.
out.print("Re-enter the last name: ");
last =
input.next();
}
System.
out.print("Enter the initial balance: $");
try
{
Double initialBal =
input.nextDouble();
if(initialBal.equals("[a-zA-Z]+"))
{
System.out.println("only numbers valid");
}
System.
out.println("Thank You, " + first + " " + last + " for joining our bank today! Have a great Day!");
BankAccount acc =
new BankAccount(number, first, last, initialBal);
list.add(acc);
}
catch(Exception e)
{
System.out.println("Invalid entry");
}
}
/**
* This method when invoked checks the Array List for the highest number in the list
* and the lowest number in the list and displays them in the console. Then prints
* to a file on the H drive called MyBank.txt
*
*
@param list
*
@throws FileNotFoundException when a file cannot be written to.
*/
private static void displayResults(ArrayList<BankAccount> list)throws FileNotFoundException {
double high = list.get(0).getBalance();
double low = list.get(0).getBalance();
int highNumber = 1;
int lowNumber = 1;
for (int i = 0; i < list.size(); i++) {
if (high < list.get(i).getBalance()) {
high = list.get(i).getBalance();
highNumber = i + 1;
}
if (low > list.get(i).getBalance()) {
low = list.get(i).getBalance();
lowNumber = i + 1;
}
}
System.
out.println("Highest balance: Account " + highNumber + " Amount $" + high);
System.
out.println("Lowest balance: Account " + lowNumber + " Amount $" + low);
PrintWriter out =
new PrintWriter(new File("H:/MyBank.txt"));
for (int i = 0; i < list.size(); i++) {
out.println(list.get(i));
}
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.