如何在 ArrayList 类中添加新的用户数据

发布于 2025-01-17 04:58:49 字数 3830 浏览 0 评论 0原文

抱歉,如果我没有正确解释这一点,但是我将如何添加新的联系信息。

我正在开发一个个人项目,并尝试将数据添加到数组列表中。我创建了供用户输入信息的输入,但如何将信息添加到数组列表中?

ArrayList contactLists = new ArrayList<>();

主类:

package com.ContactList;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    ArrayList<ContactList> contactLists = new ArrayList<>();

    public static void main(String[] args) {
        displayMen();
    }


    public static void displayMen() {

        do {

            System.out.println("Please choose from the following selection \n press 2 add a business contact \n press 2 to a personal contact \n press 3 to display your contact list");

            Scanner sc = new Scanner(System.in);
            int select = sc.nextInt();

            switch (select) {
                case 1:
                    //use this to add a business contact
                    // System.out.println("This is a test ot see the code is working");
                    addContact();
                    break;
                case 2:
                    //use this to add a personal contact

                    break;
                case 3:
                    //allow the user to display the contact information
                    break;
                case 4:
                    //this is to quit the program
            }

        } while (true);
    }

    public static void addContact() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the user first name");
        String fNmae = sc.next();
        System.out.println("Please enter the user last name");
        String lName = sc.next();
        System.out.println("Please enter the user address");
        String address = sc.next();
        System.out.println("Please enter the user phoneNumber");
        String phoneNumber = sc.next();
        System.out.println("Please enter the user email");
        String email = sc.next();
        String data = (lName + lName + address + phoneNumber + email);





    }


}

ContactList 类:

package com.ContactList;

public class ContactList {

    String firstName;
    String lastName;
    String address;
    String phoneNumber;
    String email;

    public void contactList(String firstName, String lastName, String address, String phoneNumber, String email){
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }


    //Getter Methods
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getAddress() {
        return address;
    }

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public String getEmail() {
        return email;
    }
}

Business Contact 类:

package com.ContactList;

public class BusinessContact extends ContactList{

    String jobTitle;
    String organization;


    public void businessContact(String firstName, String lastName, String address, String phoneNumber, String email) {
        super.contactList(firstName, lastName, address, phoneNumber, email);
        this.jobTitle = jobTitle;
        this.organization = organization;
    }
}

PersonalContact 类:

public class PersonalContact extends ContactList{

    int dateOfBirth;


    public void PersonalContact(String firstName, String lastName, String address, int phoneNumber, String email, int dateOfBirth) {
        super.contactList(firstName, lastName, address, phoneNumber, email);

        this.dateOfBirth = dateOfBirth;
    }

    public int getDateOfBirth() {
        return dateOfBirth;
    }
}

Sorry if I'm not explaining this correctly, but how would I add new contact information into.

I'm working on a personal project and trying to add data into an arraylist list. I created input for the user to enter the information but how do I go about adding the information into the array list?

ArrayList contactLists = new ArrayList<>();

Main Class:

package com.ContactList;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    ArrayList<ContactList> contactLists = new ArrayList<>();

    public static void main(String[] args) {
        displayMen();
    }


    public static void displayMen() {

        do {

            System.out.println("Please choose from the following selection \n press 2 add a business contact \n press 2 to a personal contact \n press 3 to display your contact list");

            Scanner sc = new Scanner(System.in);
            int select = sc.nextInt();

            switch (select) {
                case 1:
                    //use this to add a business contact
                    // System.out.println("This is a test ot see the code is working");
                    addContact();
                    break;
                case 2:
                    //use this to add a personal contact

                    break;
                case 3:
                    //allow the user to display the contact information
                    break;
                case 4:
                    //this is to quit the program
            }

        } while (true);
    }

    public static void addContact() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the user first name");
        String fNmae = sc.next();
        System.out.println("Please enter the user last name");
        String lName = sc.next();
        System.out.println("Please enter the user address");
        String address = sc.next();
        System.out.println("Please enter the user phoneNumber");
        String phoneNumber = sc.next();
        System.out.println("Please enter the user email");
        String email = sc.next();
        String data = (lName + lName + address + phoneNumber + email);





    }


}

ContactList Class:

package com.ContactList;

public class ContactList {

    String firstName;
    String lastName;
    String address;
    String phoneNumber;
    String email;

    public void contactList(String firstName, String lastName, String address, String phoneNumber, String email){
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }


    //Getter Methods
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getAddress() {
        return address;
    }

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public String getEmail() {
        return email;
    }
}

Business Contact class:

package com.ContactList;

public class BusinessContact extends ContactList{

    String jobTitle;
    String organization;


    public void businessContact(String firstName, String lastName, String address, String phoneNumber, String email) {
        super.contactList(firstName, lastName, address, phoneNumber, email);
        this.jobTitle = jobTitle;
        this.organization = organization;
    }
}

PersonalContact class:

public class PersonalContact extends ContactList{

    int dateOfBirth;


    public void PersonalContact(String firstName, String lastName, String address, int phoneNumber, String email, int dateOfBirth) {
        super.contactList(firstName, lastName, address, phoneNumber, email);

        this.dateOfBirth = dateOfBirth;
    }

    public int getDateOfBirth() {
        return dateOfBirth;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2025-01-24 04:58:49

看起来您可以在 addContact() 中创建一个新的 PersonalContact/BusinessContact 对象,并使用刚刚从用户那里获得的数据进行初始化,并使用 add() 方法将其添加到 ArrayList 的末尾。

对于 BusinessContact,构造函数参数列表中缺少职务和组织字段,因此您需要添加这些字段。

对于 PersonalContact,您已选择将 phoneNumber 和 dateOfBirth 字段存储为整数,因此您需要使用适当的 Scanner 方法来获取该数据并进行适当的转换。

也许您还应该向 addContact 方法指示联系人是 BusinessContact 还是 PersonalContact,以便您知道要构造什么类型的对象结尾:

contactLists.add(new BusinessContact(fName, lName, 地址, 电话号码, 电子邮件, jobTitle, 组织);

https ://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#add(E)

Looks like you can just create a new PersonalContact/BusinessContact object inside your addContact(), initialized with the data you just got from the user, and add that to the end of the ArrayList using the add() method.

For BusinessContact, the job title and organization fields are missing from the constructor parameter list, so you will need to add those.

For PersonalContact, you've chosen to store the phoneNumber and dateOfBirth fields as integers, so you'll need to use the appropriate Scanner methods to get that data and cast it appropriately.

Perhaps you should also indicate to the addContact method whether the contact will be a BusinessContact or a PersonalContact so you know what type of object to construct at the end:

contactLists.add(new BusinessContact(fName, lName, address, phoneNumber, email, jobTitle, organization);

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#add(E)

⊕婉儿 2025-01-24 04:58:49

从 OOP 的角度来看,您的代码存在多个问题。但是尝试直接回答你的问题,你必须做两件事:

  1. ContactList 类中,你缺少类私有字段的所有 SETTER (现在是默认的,你应该将它们设为私有)。如果您想更改与这些字段相关的任何值,这些可以帮助您。这将允许您为每个类属性设置一个值,这将我们带到下一件事。
  2. Main 类中:

字符串数据 = (lName + lName + 地址 + 电话号码 + 电子邮件);

您应该通过其构造函数创建一个新的 ClientList 对象,并使用您刚刚从用户读取的值填充其属性:

contactLists.add(new ContactList(fName, lName, address, phoneNumber, email));

There are multiple problems with your code from OOP perspective. But tring dirrectlly to answer your question you have to do 2 things:

  1. In ContactList class you are missing all the SETTERs for the class private fields (which right now are default, you should make them private). Those could help you if you want to change any values related to these fields. This will allow you to set a value for each of the class atributes, which brings us to the next thing.
  2. In Main class at the line:

String data = (lName + lName + address + phoneNumber + email);

You should create a new ClientList object via it's constructor and populate it's attributes with the values that you have just read from the user:

contactLists.add(new ContactList(fName, lName, address, phoneNumber, email));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文