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

时间:2018-05-14 09:28:21

标签: java object arraylist instance

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

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

addReviews()方法在Game类中,它扩展了Content。 addReplies()方法位于Comment类中。 printAllReview()方法在Game类中,是我想用来打印评论及其相应回复的方法。注释中的那个只是一个测试人员,但是当调用它时,回复并不是全部打印(我想我知道为什么,我相信这是因为如何调用addReply方法,但是必须有一种解决方法而不改变调用方法)。我在评论课上遇到了一些麻烦。

基本上,我不知道如何在我的评论arraylist中获得我的评论以匹配我的回复arraylist的回复;然后确保回复符合评论。此外,评论总是由特定游戏对象添加。但是回复是由它之前的特定注释对象添加的。我希望这就像一个链接。然而,它们都是评论对象。有没有像实例链接或对象继承对象等...(听起来很傻,我不能搜索它,但你永远不知道?)

我玩了很多东西,但过了一段时间后,它变得令人沮丧。我一直在寻找年龄。

内容

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);

}

评论 - 即使我在这个课程中有一个打印方法,我也不想在那里有它。此外它并没有真正起作用。

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();
}
}

1 个答案:

答案 0 :(得分:1)

我不确定我理解你想做什么,但是如果你想显示像

那样的东西
comment1
reply to comment1
reply to comment1
comment2
reply to comment2

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

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

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

并将游戏中的方法更改为

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