java - 为特定问题添加评论
问题描述
我正在制作一个问题跟踪系统,但我一直在为您从 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;
}
}
解决方案
为了让你的类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.");
}
}
}
}
推荐阅读
- apache-kafka - 如何使用 kafka 管理客户端获取消费者指标?
- spring - 当joinTable不是实体时,如何使用Spring和JPA从HQL中的多对多关系中检索数据?
- mysql - 如何在 SQL 上创建表的总、费用和净额
- api - Flutter 中即时聊天应用的架构和技术
- html - 具有 3 列固定和灵活宽度的行
- javascript - 在每条路线上放置 csrf 令牌
- layout - 文本未使用 crossaxisalignment 居中
- c# - 为什么第一个图片代码不执行,但第二个却执行?
- assembly - 使用ca65编译一个简单的程序
- android - RecyclerView:没有附加适配器;在 Fragments 中使用 Firebase 跳过布局错误