gpt4 book ai didi

java - 从两个 ArrayList 中提取特定连接的对象,组合这些相关对象并从子类中打印

转载 作者:行者123 更新时间:2023-12-02 11:15:12 24 4
gpt4 key购买 nike

根据下面的信息和代码,我想知道当我在游戏中调用 printAllReview() 时,如何才能显示“评论”以及特定于该评论的所有相关“回复”。

我有两个 ArrayList,一个用于评论,一个用于回复。 ArrayList Review 位于 Content 类中并保存对象。 Comment 类具有 ArrayList Replies 并引用其自身。

addReviews() 方法位于扩展 Content 的 Game 类中。 addReplies() 方法位于 Comment 类中。 printAllReview() 方法位于 Game 类中,是我想用来打印评论及其相应回复的方法。 Comments 中的那个只是一个测试程序,但是在调用它时回复不会全部打印(我想我知道为什么,我相信这是因为 addReply 方法是如何调用的,但必须有一种方法可以解决这个问题而不改变调用方法)。我在使用 Comment 类时遇到了一些问题。

基本上,我不知道如何让我的评论数组列表中的评论与我的回复数组列表中的回复相匹配;然后确保回复与评论相符。此外,评论始终是由特定游戏对象添加的。但回复是由其前面的特定评论对象添加的。我希望这就像一个链接。然而它们都是 Comment 对象。是否存在实例链接或对象继承对象等之类的东西...(听起来很傻,我无法搜索它,但你永远不知道?)

我尝试了很多东西,但过了一段时间就变得令人沮丧。我已经寻找了很长时间。

内容

public abstract class Content {

protected List<Comment> Review = new ArrayList<Comment>();

private String ID;
private String Application_Name;

private int No_of_Downloads = 0;
private double Price = 0;

// constructor to take ID, name and price of app
public Content(String iD, String application_Name, double price) {
super();
ID = iD;
Application_Name = application_Name;
Price = price;
}

// another constructor for free apps
public Content(String iD, String application_Name) {
super();
ID = iD;
Application_Name = application_Name;
}

public String getApplication_Name() {
return Application_Name;
}

public double getPrice() {
return Price;
}

public void Increase_NoD_by1() {
this.No_of_Downloads += 1;
}

public abstract void addReviews(Comment a);

}

评论 - 尽管我在这个类中有一个 print 方法,但我不想在那里有它。而且它并没有真正发挥作用。

public class Comment {

protected List<Comment> reply = new ArrayList<Comment>();

protected User user;
protected String usrComment;

public Comment() {
}

public Comment(User usrDetails, String usrComs) {

this.user = usrDetails;
this.usrComment = usrComs;
}

public void addReply(Comment r) {

this.reply.add(r);
}

@Override
public String toString() {
return this.user.getUsr() + '"' + this.usrComment + '"';
}

public void printAllReview() {
for (Comment a : this.reply) {
System.out.println(a);
}
}
}

游戏 - 我想打印游戏类的所有评论和回复,因为显然它们与特定对象相关。我可以打印所有评论,但我不知道如何将评论链接到回复,反之亦然。我一直在尝试一些事情,但后来我把我的代码弄乱了。

public class Game extends Content {

private boolean isMultiPlayer;
private OS o;
private double Price = 0;

public Game(String iD, String application_Name, double price, boolean isMultiPlayer, OS o) {
super(iD, application_Name, price);
this.isMultiPlayer = isMultiPlayer;
this.o = o;
}

public Game(String iD, String application_Name, boolean isMultiPlayer, OS o) {
super(iD, application_Name);
this.isMultiPlayer = isMultiPlayer;
this.o = o;
}

public OS getO() {
return o;
}

public void printAllReview() {
for (Comment a : this.Review) {
System.out.println(a);
}
}

public void addReviews(Comment a) {

this.Review.add(a);
}
}

主要 - 我认为这很重要,但我也不太了解 java,但评论和回复的工作方式是游戏对象将调用 addReview 进行原始评论。评论对象调用第一个回复的 addReply 方法,第一个回复调用下一个回复等...我的逻辑说,即使评论和回复位于不同的数组列表中,我应该能够将回复与评论相匹配并将它们一起打印出来。

public class Store {

public static void main(String[] args) {
Store admin = new Store();

// adding new games
OS os1 = new OS("Android", 4);
OS os2 = new OS("iOS", 10);
OS os3 = new OS("Android", 3);
Game g1 = new Game("g1", "Pokemon", 5, false, os1);
Game g2 = new Game("g2", "Pokemon", 5, false, os2);
Game g3 = new Game("g3", "MineCraft", 2, true, os1);

// adding new users
User u1 = new User("u1", "Jeremy David", "0412000", 120, os1);
User u2 = new User("u2", "Bradly Josesp", "0412001", 120, os1);
User u3 = new User("u3", "James Wilkie", "0412002", 100, os2);
User u4 = new User("u4", "Will Scobie", "0412003", 50, os3);

// Review 1
Comment cm = new Comment(u1, "Loving this Game");
g1.addReviews(cm);

// replies for review 1
Comment cmr1 = new Comment(u2, "I don't have time for it!");
cm.addReply(cmr1);
Comment cmr2 = new Comment(u1, "Really??");
cmr1.addReply(cmr2);
Comment cmr3 = new Comment(u2, "It's too boring.");
cmr2.addReply(cmr3);

// Review 2
Comment cm2 = new Comment(u3, "Waste of money, so buggy");
g1.addReviews(cm2);

// replies for review 2
Comment cm2r1 = new Comment(u4, "That's becuase games are forever in beta these days.");
cm2.addReply(cm2r1);
Comment cm2r2 = new Comment(u4, "That's becuase games are forever in beta these days.");
cm2r1.addReply(cm2r2);

// Print out all reviews
// In reality I would like this method to show all the reviews and the replies
// for specific reviews.
g1.printAllReview();

// this only shows the first reply - I can mix it up by changing it to cmr2,
// etc..
// but this is not what I want, I want the replies to be printed with the one
// method under the reviews
// I don't know how to link the replies to a specific review
cm.printAllReview();
}
}

最佳答案

我不确定我明白你想做什么,但如果你想显示类似的东西

comment1
reply to comment1
reply to comment1
comment2
reply to comment2

那么最简单的解决办法就是使用递归。当然,您可能想添加一些格式或更多逻辑,但基本思想是:

在 Comment 中创建一个方法,例如。

public void display() {
System.out.println(usrComment);
for (Comment c : reply) {
c.display();
}
}

并将Game中的方法更改为

public void printAllReview() {
for (Comment a : this.Review) {
a.display();
}
}

关于java - 从两个 ArrayList 中提取特定连接的对象,组合这些相关对象并从子类中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50327073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com