Home > Software design >  How to write a method to ask what index and what value to update users information in Array
How to write a method to ask what index and what value to update users information in Array

Time:12-04

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class UpdateUserInfo { 
    public static void main(String[] args) {
        ArrayList<String> userList = new ArrayList<>(Arrays.asList("Name", "LastName", "08/19/1995",
                "123 main Street, mcLean, VA 22102", "7576330954"));

       // To update user's information, please edit value in double quotes, this is a basic way, but can we create our own method for it
//        userList.set(0, "Name"); // name
//        userList.set(1, "LastName");
//        userList.set(2, "08/19/1995");
//        userList.set(3, "123 main Street, mcLean, VA 22102");
//        userList.set(4, "7576330954");

        System.out.print(userList);
       // changeUserInfo();

    }

// Here is my attempt to create my own method

    public static void changeUserInfo(String[] name, String lName, String dob, String address, String phone){
        Scanner scan = new Scanner(System.in);
        System.out.println("Do you wish to update your info? ");
        boolean flag = scan.hasNextBoolean();
        if(flag == true){
            System.out.println("What info do you want to update? Name? LastName?");
        }
    }
}

I was thinking to add boolean to see if they user want to change info, if yes then change it.

CodePudding user response:

I believe you're trying to create an input form for a list of people. You might want to introduce a User class which will have all the fields for the user.

Regarding the update User flow, the part inside the flag boolean check should use some sort of identifier and value mechanism. The identifier selected in one entry will have the value passed as the next input.

As the first statement, you should ask the id or email or any other unique identifier for the user, the info has to be updated.

Next, show the list of fields and a corresponding number which will be entered by the user. People prefer to use a reference instead of typing long words, the trouble of spelling errors is cumbersome. Enums are a great way to manage field name with a corresponding number.

Once the user entered a number (identifier), show the prompt of which field will be updated and show a sample value in brackets. The user can then enter the new value and you can update the information.

You can either prompt next to update a different field for the same user without asking for the id or start the update flow all over again. Cheers!

CodePudding user response:

import java.util.Arrays; import java.util.Scanner;

public class UpdateUserInfo {

public static void main(String[] args) {
    String[] arrayList = { "Name", "LastName", "08/19/1995",
            "123 main Street, mcLean, VA 22102", "7576330954"};
    Scanner scan = new Scanner(System.in);
    System.out.println("to edit the name type: 0, \nto edit the last name type: 1\nto edit birthday type:2\n"  
            "to edit address type:3\nand to edit id type:4");
    int option = scan.nextInt();
    System.out.println("change the value: ");
    String desire = scan.next();
    scan.close();
    System.out.println(" before:");
    System.out.println(Arrays.deepToString(arrayList));
    System.out.println(" after:");
    System.out.println(Arrays.deepToString(changeUserInfo(arrayList,option,desire)));
}

public static String[] changeUserInfo(String[] array,int value, String to){
    array[value] = to;
    return array;
}

}

  •  Tags:  
  • java
  • Related