gpt4 book ai didi

Java 数组列表验证

转载 作者:行者123 更新时间:2023-12-02 04:35:17 25 4
gpt4 key购买 nike

当用户将一个人添加到ArrayList时,我需要添加验证。输入名字和姓氏、地址、法定州缩写。等等。我该怎么办?

我想象了一个 IF 语句,但我不确定如何将其合并到 newFriend.setAddress(sc.nextLine()); 类型的行中。

import java.util.*;
import java.io.*;
import javax.swing.*;


public class testAddressBook {

//Personal Friend Array
public static PersonalFriend newFriend = new PersonalFriend(null, null, null, null, null, null, null, null, null, null,0,null);
public static ArrayList<PersonalFriend> FriendList = new ArrayList<PersonalFriend>();

//Business Associate Array
public static BusinessAssociate newBusiness = new BusinessAssociate(null, null, null, null,null, null, null, null,null, null, null, null);
public static ArrayList<BusinessAssociate> salaryList = new ArrayList<BusinessAssociate>();


public static ArrayList<Person> addressBook = new ArrayList<Person>();
private static Scanner sc = new Scanner(System.in);
private static char choice;
private static char type;


public static void main(String[] args) throws IOException {

boolean switcher = true;
do {
System.out.println("\n\tAddress Book Menu");
System.out.println("\n\t\tEnter A to (A)dd Person ");
System.out.println("\t\tEnter R to (R)emove Person");
System.out.println("\t\tEnter M to (M)odify Entry");
System.out.println("\t\tEnter S to (S)earch Address Book ");
System.out.println("\t\tEnter L to (L)ist ALL (sorted) ");
System.out.println("\t\tEnter D to (D)ownload to Disk ");
System.out.println("\t\tEnter U to (U)pload from Disk ");
System.out.println("\t\tEnter P to (P)rint Address Book ");
System.out.println("\t\tEnter Q to Quit");
System.out.print("\n\tPlease enter your choice: ");
choice = sc.nextLine().toUpperCase().charAt(0);


while ((choice != 'A') && (choice != 'R') && (choice != 'M') && (choice != 'S') && (choice != 'L')&& (choice != 'D')&& (choice != 'U')&& (choice != 'P')&&(choice != 'Q')) {
System.out.println("Invalid choice! Please select (A)dd, (D)elete, (M)odify, (S)earch, (L)ist or (Q)uit: ");
choice = sc.nextLine().toUpperCase().charAt(0);
}


switch (choice) {
case 'A' :
if (choice == 'A') {
System.out.print("\nAdd a (F)riend or (B)usiness Associate? ");
type = sc.nextLine().toUpperCase().charAt(0);

while ((type != 'F') && (type != 'B')) {
System.out.print("Invalid choice! Please select (F)riend or (B)usiness Associate: ");
type = sc.nextLine().toUpperCase().charAt(0);
}


if (type == 'F') {

addPersonFriend();

}

else if (type == 'B') {
addBusinessPerson();
}
}


break;

case 'R' :
deletePerson();
break;

case 'M' :
modifyPerson();
break;

case 'S' :

searchPerson();

break;

case 'L' :
listPerson();
break;

case 'D' :
downloadDisk();
break;

case 'U' :
uploadDisk();
break;

case 'P' :
printAddressBook();
break;

case 'Q' :
switcher = false;

break;


}


}
while (switcher != false);
System.out.println("Session Over");

}


private static void addPersonFriend() {
System.out.println("\nFollow the prompts.");
newFriend = new PersonalFriend(null, null, null, null, null, null, null, null, null, null,0,null);
System.out.print("\nEnter First Name: ");
newFriend.setfName(sc.nextLine());
System.out.print("Enter Last Name: ");
newFriend.setlName(sc.nextLine());
System.out.print("Enter Address: ");
newFriend.setAddress(sc.nextLine());
System.out.print("Enter City: ");
newFriend.setCity(sc.nextLine());
System.out.print("Enter State: ");
newFriend.setState(sc.nextLine());
System.out.print("Enter Zip: ");
newFriend.setZip(sc.nextLine());
System.out.print("Enter Phone Number: ");
newFriend.setPhone(sc.nextLine());
System.out.print("Enter Cell Phone Number: ");
newFriend.setCellphone(sc.nextLine());
System.out.print("Enter Email Number: ");
newFriend.setEmail(sc.nextLine());
System.out.print("Enter Birthday: ");
newFriend.setBirthday(sc.nextLine());
System.out.print("Enter Age: ");
newFriend.setAge(sc.nextInt());
System.out.print("Enter Zodiac: ");
newFriend.setZodiac(sc.nextLine());
System.out.print(" ");
newFriend.setZodiac(sc.nextLine());
addressBook.add(newFriend);

System.out.println("\nYou have successfully added a new person!");
}

private static void addBusinessPerson() {
System.out.println("\nFollow the prompts.");
newBusiness = new BusinessAssociate(null, null, null, null, null, null, null, null, null, null, null, null);
System.out.print("\nEnter First Name: ");
newBusiness.setfName(sc.nextLine());
System.out.print("Enter Last Name: ");
newBusiness.setlName(sc.nextLine());
System.out.print("Enter Address: ");
newBusiness.setAddress(sc.nextLine());
System.out.print("Enter City: ");
newBusiness.setCity(sc.nextLine());
System.out.print("Enter State: ");
newBusiness.setState(sc.nextLine());
System.out.print("Enter Zip: ");
newBusiness.setZip(sc.nextLine());
System.out.print("Enter Phone Number: ");
newBusiness.setPhone(sc.nextLine());
System.out.print("Enter Cell Phone Number: ");
newBusiness.setCellphone(sc.nextLine());
System.out.print("Enter Email Number: ");
newBusiness.setEmail(sc.nextLine());
System.out.print("Enter Job Title: ");
newBusiness.setJobtitle(sc.nextLine());
System.out.print("Enter Fax Number: ");
newBusiness.setFax(sc.nextLine());
System.out.print("Enter Company Name: ");
newBusiness.setCompany(sc.nextLine());
addressBook.add(newBusiness);

System.out.println("\nYou have successfully added a new person!");
}

private static void deletePerson() {
System.out.println("Enter first name of person: ");
String deleteName = sc.nextLine();

boolean foundMod = false;
int index2 = 0;

while (index2 < addressBook.size() && !foundMod) {
if ( ((Person)addressBook.get(index2)).getfName().equalsIgnoreCase(deleteName)) {
foundMod = true;
addressBook.remove(index2);

System.out.println("\nYou have successfully deleted this person.");
}

else {
index2++;
}

if (foundMod)
{}
else
System.out.println("Unknown");
}

}

private static void modifyPerson() {

newBusiness = new BusinessAssociate(null, null, null, null, null, null, null, null, null, null, null, null);
newFriend = new PersonalFriend(null, null, null, null, null, null, null, null, null, null,0,null);


System.out.print("Enter first name of person: ");
sc.nextLine();



if (newBusiness instanceof BusinessAssociate) {
System.out.println("Change this business listing: ");
addBusinessPerson();
}

if (newFriend instanceof PersonalFriend) {
System.out.println("This is a Personal Friend: ");
addPersonFriend();
}


}



private static void searchPerson() {

System.out.println("Enter first name of person: ");
String findName = sc.nextLine();

boolean found = false;
int index = 0;

while (index < addressBook.size() && !found) {
if ( ((Person)addressBook.get(index)).getfName().equalsIgnoreCase(findName)) {
found = true;
}

else {
index++;
}

if (found)
System.out.println("Found: " + addressBook.get(index));
else{}

}


}

private static void listPerson() {
System.out.println("\nThere are " + addressBook.size() + " people in this address book.\n");
Collections.sort(addressBook);


for (int i = 0; i < addressBook.size(); i++) {
System.out.println(addressBook.get(i));


}
System.out.println();



}


public static void downloadDisk() {
File file = new File("addressBookContents.dtd");


// Create a file
PrintWriter output = null;
try {
output = new PrintWriter(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Write formatted output to the file
output.println(addressBook);
System.out.println("\nSaved to disk in the default folder for this program. \n");

// Close the file
output.close();

}

private static void uploadDisk() throws IOException {

JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null)
== JFileChooser.APPROVE_OPTION) {
// Get the selected file
java.io.File file = fileChooser.getSelectedFile();

// Create a Scanner for the file
Scanner input = new Scanner(file);

// Read text from the file
while (input.hasNext()) {
System.out.println(input.nextLine());
}

// Close the file
input.close();
}
else {
System.out.println("No file selected");
}


}


@SuppressWarnings("unchecked")
private static void printAddressBook() {

File file = new File("addressBookContents.txt");


// Create a file
PrintWriter output = null;
try {
output = new PrintWriter(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Write formatted output to the file
output.println(addressBook);
System.out.println("\nPrinting Successful! \n");

// Close the file
output.close();

}


}//ends class

最佳答案

确保对象有效的一个好方法是使其不可变,并在其构造函数中检查所有输入是否符合您的要求。

这里的复杂之处在于您在不同的步骤中获取大量信息。在调用带有大量参数的构造函数之前,您可以将每个变量存储在一个变量中,但这有点难以阅读。相反,请考虑 builder pattern

关于Java 数组列表验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com