I need the name Validation in its own method. My professor said \"Name validatio
ID: 3558070 • Letter: I
Question
I need the name Validation in its own method. My professor said "Name validation should be in a method instead of repeated twice." So I need a new method with just the name Validation in it.
private static void createNewAccount(ArrayList<BankAccount> list) {
int number = list.size() + 1;
String first = "", last = "";
int error = 0;
do {
error = 0;
System.out.print(" Enter the first name: ");
first = input.nextLine();
first = first.trim();
if (first.length() < 3) {
System.out.println("First name must have at least 3 characters");
error = 1;
}
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) >= 'A' && first.charAt(i) <= 'Z')
continue;
else if (first.charAt(i) >= 'a' && first.charAt(i) <= 'z')
continue;
else if (first.charAt(i) == '.')
continue;
else if (first.charAt(i) == ''')
continue;
else if (first.charAt(i) == ' ')
continue;
else if (first.charAt(i) == '-')
continue;
else {
System.out.println("Please enter your first name!");
error = 1;
break;
}
}
}
while (error == 1);
do {
error = 0;
System.out.print("Enter the last name: ");
last = input.nextLine();
last = last.trim();
if (last.length() < 3) {
System.out.println("Last name must have at least 3 characters.");
error = 1;
}
for (int i = 0; i < last.length(); i++) {
if (last.charAt(i) >= 'A' && last.charAt(i) <= 'Z')
continue;
else if (last.charAt(i) >= 'a' && last.charAt(i) <= 'z')
continue;
else if (last.charAt(i) == '.')
continue;
else if (last.charAt(i) == ''')
continue;
else if (last.charAt(i) == ' ')
continue;
else if (last.charAt(i) == '-')
continue;
else {
System.out.println("Please enter your last name!");
error = 1;
break;
}
}
}
while (error == 1);
System.out.print("Enter the initial balance: $");
BigDecimal initialBal = ammount();
BankAccount acc = new BankAccount(number, first, last, initialBal);
list.add(acc);
System.out.println("Thank You, " + first + " " + last + " for joining our bank today! Have a great Day!");
System.out.println("Your account no is " + number + " and initial balance is $" + new DecimalFormat("0.00").forma(initialBal));
}
Explanation / Answer
// I need the name Validation in its own method.
// My professor said "Name validation should be in a method instead of repeated twice."
// So I need a new method with just the name Validation in it.
private static String Validation(String str)
{
int error;
String first;
Scanner input = new Scanner(System.in);
do {
error = 0;
System.out.print(" Enter the "+str+" name: ");
first = input.nextLine();
first = first.trim();
if (first.length() < 3) {
System.out.println(str+" name must have at least 3 characters");
error = 1;
}
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) >= 'A' && first.charAt(i) <= 'Z')
continue;
else if (first.charAt(i) >= 'a' && first.charAt(i) <= 'z')
continue;
else if (first.charAt(i) == '.')
continue;
else if (first.charAt(i) == ''')
continue;
else if (first.charAt(i) == ' ')
continue;
else if (first.charAt(i) == '-')
continue;
else {
System.out.println("Please enter your "+str+" name!");
error = 1;
break;
}
}
}
while (error == 1);
return fisrt;
}
private static void createNewAccount(ArrayList<BankAccount> list)
{
int number = list.size() + 1;
String first = "", last = "";
first = Validation("first");
last = Validation("last");
System.out.print("Enter the initial balance: $");
BigDecimal initialBal = ammount();
BankAccount acc = new BankAccount(number, first, last, initialBal);
list.add(acc);
System.out.println("Thank You, " + first + " " + last + " for joining our bank today! Have a great Day!");
System.out.println("Your account no is " + number + " and initial balance is $" + new DecimalFormat("0.00").forma(initialBal));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.