Add object to an ArrayList using the Scanner

1

I have the following code in my main:

if (opt1>0){

        switch (opt1){

            case 1: 
                System.out.println("New customer");
                Customer.addCustomer();
                customerList.add(new Customer(getName();getSurname());
            break;
            case 2:
                System.out.println("Edit customer");
            break;
            case 3:
                System.out.println("Delete customer");
            break;
            case 4:
                System.out.println("List of customers");

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

                }
                break;
            case 5:
                System.out.println("Invalid Option, Please choose a number between 1 and 3");
                Customer.showMenu();
                break;

        }

        opt1 = 0;

    }else{
        System.out.println("Invalid Option");
        System.out.println("Option has to be a number between 1 and 3");

    }

}

and my addCustomer method is inside the Customer class and is the following

public static void addCustomer(){

    System.out.println("Name:");
    setName(name);
    System.out.println("SurName:");
    setSurname(surname);
    System.out.println("E-mail:");
    setEmail(email);
    System.out.println("Phone Number:");
    setPhone(email);


}

and the method setName and setSurname etc I have them like this:

public void setName() {
    Scanner inputName = new Scanner(System.in);
    String name = inputName.nextLine();

}

public void setSurname(String surname) {
    Scanner inputSurname = new Scanner(System.in);
    surname = inputSurname.nextLine();
}

I believe that up to the Customer.addCustomer () part; The switch works fine but I can not add the data obtained through the keyboard to the Customer type arraylist.

customerList.add(new Customer(getName();getSurname());

could someone show me the correct way to add the data of the object to an arraylist I've been trying for several days and I do not succeed.

    
asked by Edgardo Quintero 21.02.2017 в 20:42
source

1 answer

1

The way to read the data is as follows in the "case 1" part

case 1: 
    System.out.println("New customer");
    Scanner sc = new Scanner(System.in);

    System.out.println("Name:");
    String name = sc.nextLine();

    System.out.println("SurName:");
    String surname = sc.nextLine();

    System.out.println("E-mail:");
    String email = sc.nextLine();

    System.out.println("Phone Number:");
    String phone = sc.nextLine();

    customerList.add(new Customer(name, surname, email, phone));

break;

And your Customer class should look like this:

public class Customer {
private String name;
private String surname;
private String email;
private String phone;
public Customer(String name, String surname, String email, String phone) {
    this.name = name;
    this.surname = surname;
    this.email = email;
    this.phone = phone;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}}
    
answered by 25.02.2017 в 23:06