首页 > 解决方案 > 为特定问题添加评论

问题描述

我正在制作一个问题跟踪系统,但我一直在为您从 ID 收到的特定问题添加评论。我希望能够从已经存储在 ArrayList(done) 中的唯一 ID 中获取特定问题,然后写一个将保存在该特定问题上的评论(可能在它自己的 ArrayList 中?),所以当我稍后在显示有关该特定问题的信息时,评论也将与时间戳一起显示为发表评论。

public class Class2 {
    Scanner input = new Scanner(System.in);
    ArrayList<Issue> issues = new ArrayList<Issue>();

    public void retrieveIssue() {

        System.out.println("Write the ID of the issue you want to show:");
        String inputNewID = input.nextLine();
        boolean isIssueFound = false;
        for (Issue issue : issues) {
            if (inputNewID.equals(issue.getID())) {
                isIssueFound = true;
                System.out.println("ID: " + issue.getID() + "\nName: " + issue.getIssueName() + "\nDate: " + issue.getLocalDate() + "\nComment: " + issue.getIssueComment() + "\nStatus: " + issue.getIssueStatus());
            }
        }

        if (!isIssueFound) {
            System.out.println("False ID. Try again.");
        }
    }

    public void commentIssue() {
        System.out.println("Write the ID of the issue you want to comment:");
        String inputNewID = input.nextLine();
        boolean isIssueFound = false;
        for (Issue issue : issues) {
            if (inputNewID.equals(issue.getID())) {
                isIssueFound = true;
            }
            System.out.println("Vad vill du kommentera?");
            String issueComment = input.nextLine();
            Issue comment = new Issue(issueComment);
        }

        if (!isIssueFound) {
            System.out.println("False ID. Try again.");
        }
    }
}
public class Issue {
    private String ID;
    private String issueName;
    private String localDate;
    private String issueStatus;
    private String issueComment;

    public Issue (String ID, String issueName, String issueStatus) {
        this.ID = ID;
        this.issueName = issueName;
        this.localDate = new Date().toString();
        this.issueStatus = issueStatus;
    }

    public Issue(String issueComment) {
        this.issueComment = issueComment;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public void setIssueName(String issueName) {
        this.issueName = issueName;
    }

    public void setIssueStatus(String issueStatus) {
        this.issueStatus = issueStatus;
    }

    public void setLocalDate(String localDate) {
        this.localDate = localDate;
    }

    public void setIssueComment(String issueComment) {
        this.issueComment = issueComment;
    }

    public String getID() {
        return this.ID;
    }

    public String getIssueName() {
        return this.issueName;
    }

    public String getLocalDate() {
        return this.localDate;
    }

    public String getIssueStatus() {
        return this.issueStatus;
    }

    public String getIssueComment() {
        return this.issueComment;
    }
}

标签: java

解决方案


为了让你的类Issue能够存储多条评论,你必须为评论提供合适的数据结构,单条String是不够的。如果您想添加评论,请提供一种方法。看到类Issue略有变化:

public class Issue {
    private String ID;
    private String issueName;
    private String localDate;
    private String issueStatus;
    private List<String> issueComments; // this is the first change

    public Issue (String ID, String issueName, String issueStatus) {
        this.ID = ID;
        this.issueName = issueName;
        this.localDate = new Date().toString();
        this.issueStatus = issueStatus;
        this.issueComments = new ArrayList<>(); // the list needs to be initialized
    }

    // this getter gives you all the comments of an instance of Issue
    public List<String> getIssueComments() {
        return issueComments;
    }

    // omitted other getters and setters for brevity

    /**
     * Adds a new comment to the collection of issue comments
     */
    public void addComment(String comment) {
        issueComments.add(comment);
    }
}

在您的commentIssue()方法中Class2,您必须Issue在增强for循环中使用当前并添加新注释。请参阅下面类中的代码注释:

public class Class2 {
    Scanner input = new Scanner(System.in);
    ArrayList<Issue> issues = new ArrayList<Issue>();

    public void retrieveIssue() {
        System.out.println("Write the ID of the issue you want to show:");
        String inputNewID = input.nextLine();
        boolean isIssueFound = false;
        for (Issue issue : issues) {
            if (inputNewID.equals(issue.getID())) {
                isIssueFound = true;
                System.out.println("ID: " + issue.getID() 
                        + "\nName: " + issue.getIssueName() 
                        + "\nDate: " + issue.getLocalDate() 
                        + "\nComment: " + issue.getIssueComment() 
                        + "\nStatus: " + issue.getIssueStatus());
            }
        }

        if (!isIssueFound) {
            System.out.println("False ID. Try again.");
        }
    }

    public void commentIssue() {
        System.out.println("Write the ID of the issue you want to comment:");
        String inputNewID = input.nextLine();
        boolean isIssueFound = false;
        for (Issue issue : issues) {
            if (inputNewID.equals(issue.getID())) {
                issueFound = true;
                System.out.println("Vad vill du kommentera?");
                String issueComment = input.nextLine();
                /*
                 * No need for a new instance of Issue here,
                 * just store the comment in the collection of comments 
                 * of this specific instance of Issue
                 */
                issue.add(issueComment);
            }

            if (!issueFound) {
                System.out.println("False ID. Try again.");
            }
        }
    }
}

推荐阅读