简体   繁体   中英

Add objects in an ArrayList through constructor

I am a beginner, and I am building a classical NimGame. Before, I used to save the project using an array. Now, I modify it to apply the ArrayList to this time. It seems no problem, though, the functions I've made are not working without any errors. I couldn't figure out why.

For now, I tried to add the NimPlayer type into the new playerList , which is the ArrayList. I put the ArrayList in the NimModel , and use the constructor from the NimPlayer to create new players. The Nimsys is the main panel to give commands and receive user inputs. That's why I separate them into three classes.

The command is like this $addplayer userName,familyName,givenName . And the scanner should process the string and go through the constructor to be a new object.

Any help is highly appreciated, and thank you for your kindness and patience.

Here is my related code Nimsys :

public class Nimsys {

private NimModel nimModel;

public static void main(String[] args) {
    Nimsys nimsys = new Nimsys();
    nimsys.processCommands();
}

private void processCommands() {
    this.nimModel = new NimModel();
    Scanner in = new Scanner(System.in);

    System.out.println("Welcome to Nim\n");
    while (true) {
        System.out.print('$');
        String commandin = in.nextLine().trim();

        if (commandin.equalsIgnoreCase("addplayer")) {
            addplayer(in);
        }
        if (commandin.equalsIgnoreCase("removeplayer")) {
            removeplayer(in);
        }

}

private String[] splitName(String inName) {
    String[] splittedLine = inName.split(",");
    String[] name = null;
    if (splittedLine.length == 3) {
        String userName = splittedLine[0].trim();
        String familyName = splittedLine[1].trim();
        String givenName = splittedLine[2].trim();
        name = new String[3];
        name[0] = userName;
        name[1] = familyName;
        name[2] = givenName;
    }
    return name;
}
private void addplayer(Scanner in) {
    String inName = in.nextLine().trim();
    String[] name = splitName(inName);

    if (name != null && name.length == 3) {
        ArrayList<NimPlayer> playerList = nimModel.getPlayerList();
        for (NimPlayer player: playerList) {
            if (player.getUserName().contains(name[0])) {
                System.out.println("The player already exists.");
                return;
            } else {
                nimModel.createPlayer(name[0], name[1], name[2]);
                System.out.println("The player has been created.");
            }
        }        
    } 
private void removeplayer(Scanner in) {
    String removeUserName = in.nextLine().trim();
    NimPlayer player = nimModel.removePlayer(removeUserName);
    if (player == null) {
        System.out.println("The player does not exist");
    } else {
        System.out.println("Player " + player.getUserName() + 
                " removed successfully!");
    }
}

And the NimModel :

public class NimModel {

private NimPlayer nimplayer;

private ArrayList<NimPlayer> playerList = new ArrayList<>();

public void createPlayer(String userName, String familyName, String givenName) {
    NimPlayer player = new NimPlayer(userName, familyName, givenName);
    playerList.add(player);     

}

public ArrayList<NimPlayer> getPlayerList() {
    return playerList;
}
public NimPlayer removePlayer(String userName) {
    for (NimPlayer player: playerList) {
        String nameCheck = nimplayer.getUserName();
        String playerName = player.getUserName();
        if (playerName.equals(nameCheck)) {
            playerList.remove(player);
            break;
        } 
    }
    return null;

Lastly, NimPlayer class

public class NimPlayer {


private final String userName;
private String familyName;
private String givenName;

private int gamesPlayed;
private int gamesWon;
private int winRatio;



public NimPlayer(String userName, String familyName, String givenName) {

    this.userName = userName;
    this.familyName = familyName;
    this.givenName = givenName;
    this.gamesPlayed = 0;
    this.gamesWon = 0;
}
//getters and setters
}

in a nutschell:

private void addplayer(Scanner in) {
String inName = in.nextLine().trim();
String[] name = splitName(inName);
if (name != null && name.length == 3) {
    ArrayList<NimPlayer> playerList = nimModel.getPlayerList();
    for (NimPlayer player: playerList) {
        if (player.getUserName().contains(name[0])) {
            System.out.println("The player already exists.");
            return;
        }
    }
    nimModel.createPlayer(name[0], name[1], name[2]);
    System.out.println("The player has been created.");         
}

Furthermore, your addPlayer() given in Nimsys is defined in your While(true) but I think it's more a typing error. Personally I would also give a constructor to your model:

import java.util.ArrayList;

public class NimModel {

private NimPlayer nimplayer;

private ArrayList<NimPlayer> playerList;
public NimModel()
{
   this.playerList =  new ArrayList<NimPlayer>();
}
public void createPlayer(String userName, String familyName, String givenName) {
    NimPlayer player = new NimPlayer(userName, familyName, givenName);
    playerList.add(player);

}

public ArrayList<NimPlayer> getPlayerList() {
    return playerList;
}

public NimPlayer removePlayer(String userName) {
    for (NimPlayer player : playerList) {
        String nameCheck = nimplayer.getUserName();
        String playerName = player.getUserName();
        if (playerName.equals(nameCheck)) {
            playerList.remove(player);
            break;
        }
    }
    return null;
}
}

When you use scanner.nextLine() you are asking for a new input to the user. So if you want the format: $addplayer user,firstName,lastName you have to fetch it into a string and use this string:

while (true) {
        System.out.print('$');
        String commandin = in.nextLine().trim();

        if (commandin.split(" ")[0].equalsIgnoreCase("addplayer")) {
            addplayer(commandin);
        }
    }
}

private void addplayer(String commandin) {
    String inName = commandin.split(" ")[1];
    String[] name = splitName(inName);
      ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM