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;
}
}
CodePudding user response:
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)