简体   繁体   English

问题跟踪程序-ArrayList中的对象

[英]Issue Tracking program - objects in an ArrayList

Sorry if I'm making some silly mistakes. 抱歉,如果我犯了一些愚蠢的错误。 I'm working on a school assignment and I'm supposed to create a issue tracking program. 我正在做学校作业,应该创建一个问题跟踪程序。 Each issue must be represented by an object and the objects must be stored in an ArrayList. 每个问题必须由一个对象表示,并且这些对象必须存储在ArrayList中。 There must be a menu where the user has the option to create a new issue, mark a created issue as solved, view solved and unsolved issues. 必须有一个菜单,用户可以在其中选择创建新问题,将已创建问题标记为已解决,查看已解决和未解决的问题。 The ArrayList I've made doesn't store the objects correctly whenever I enter a new object all the other objects are changed into the one I enter and I'm stuck at this... Any help will be much appreciated! 每当我输入一个新对象时,我制作的ArrayList都无法正确存储对象,所有其他对象都变成了我输入的对象,并且我被困在此...任何帮助将不胜感激! Thanks! 谢谢!

Here's my code (I think the problem begins at "if (option == 1)"): 这是我的代码(我认为问题始于“ if(option == 1)”):

public static void main(String[] args) {
    int option = 0;
    Scanner input = new Scanner(System.in);

    ArrayList<IssueTracking> myIssueList = new ArrayList<>();
    IssueTracking issueNum = new IssueTracking();  

    mainMenu();
    option = input.nextInt();

    while (option != 1 && option != 2 && option != 3 && option != 4 && option != 0){
        System.out.println("Invalid option, please choose 1, 2, 3, 4 or 0 in the menu.");
        mainMenu();
        option = input.nextInt();
     }

    while(option !=0){

        while (option != 1 && option != 2 && option != 3 && option != 4 && option != 0){
        System.out.println("Invalid option, please choose 1, 2, 3, 4 or 0 in the menu.");
        mainMenu();
        option = input.nextInt();
     }


        if (option == 1){
            System.out.println("Please describe the issue bellow: ");
            String issueDescription = input.nextLine();
            issueDescription = input.nextLine();

            issueNum.setIssueDescription(issueDescription);
            issueNum.setIsSolved(Boolean.FALSE );
            myIssueList.add(issueNum);
            System.out.println(myIssueList.toString().replace("[", "").replace("]", "").replace(",", ""));   

            returnMenu();
            mainMenu();
            option = input.nextInt();
        }
        if (option == 2){
            System.out.println("Please press 1 to mark the issue as solved: ");
            Boolean isSolved = input.nextBoolean();

            issueNum.setIsSolved(isSolved);

            myIssueList.add(issueNum);
            //System.out.println("\n"+myIssueList.toString().replace("[", "").replace("]", ""));  

            returnMenu();
            mainMenu();
            option = input.nextInt();
        }    

        if (option == 3){
            //View unsolved issues.
            System.out.println(myIssueList);

            returnMenu();

            mainMenu();
            option = input.nextInt();

        }
        if (option == 4){
            //View solved issues.
            System.out.println(issueNum.getIssueDeString()+issueNum.getIsSolved());

            returnMenu();

            mainMenu();
            option = input.nextInt();
        }
    }                       
}    

} }

The issues class: 问题类别:

public class IssueTracking {
private String issueDescription;
private Boolean issueSolved;

public void setIssueDescription(String issueDescription){
    this.issueDescription = issueDescription;
}

public String getIssueDeString(){
    return issueDescription;
} 

public void setIsSolved (Boolean issueSolved){
    this.issueSolved = issueSolved;
}

public Boolean getIsSolved() {
    return issueSolved;
}

public String toString(){
    return "The issue is: " + getIssueDeString() +"\n"+ "The issue is solved: " + getIsSolved()+"\n"; // 
}

Basically you are trying to reuse same object again and again and hence you see changing field in one object impacts the other as well. 基本上,您试图一次又一次地重用同一对象,因此,您看到一个对象中更改字段也会影响另一个对象。 What you should be doing is: 您应该做的是:

if (option == 1) {
      IssueTracking issueNum = new IssueTracking();
      ...//get or set values
      myIssueList.add(issueNum);
}

And similarly you should be asking for issue number which has been solved and should update only that issue (I would have used Map for this where key would be issue number and value would be IssueTracking) 同样,您应该询问已解决的问题编号,并且仅应更新该问题(我将为此使用Map,其中键将是问题编号,而值将是IssueTracking)

The problem is that you're reusing the issueNum object - when you enter the first entry this is added to the list, when you enter a second one, you edit the existing entry, then add another copy of the same object, and so on. 问题在于您正在重用issueNum对象-当您输入第一个条目时,它会添加到列表中,当您输入第二个条目时,您将编辑现有条目,然后添加同一对象的另一个副本,依此类推。 。

Creating a new instance will fix this: 创建一个新实例将解决此问题:

    if (option == 1){
        System.out.println("Please describe the issue bellow: ");
        String issueDescription = input.nextLine();
        issueDescription = input.nextLine();

        issueNum=new IssueTracking();
        issueNum.setIssueDescription(issueDescription);
        issueNum.setIsSolved(Boolean.FALSE );
        myIssueList.add(issueNum);
        System.out.println(myIssueList.toString().replace("[", "").replace("]", "").replace(",", ""));   

        returnMenu();
        mainMenu();
        option = input.nextInt();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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