从两个 ArrayLists 中提取特定连接的对象,组合这些相关对象并从子类中打印
Extracting specifically connected objects from two ArrayLists, combining those related objects and printing from within a subclass
根据下面的信息和代码,我想知道当我在游戏中调用 printAllReview() 时如何显示 'Review' 以及所有与该评论相关的 'Replies' .
我有两个 ArrayList,一个用于评论,一个用于回复。 ArrayList Review 在 class 内容中并持有对象。 class 评论有 ArrayList 回复和引用本身。
addReviews() 方法在扩展内容的 class 游戏中。 addReplies() 方法在 class 评论中。 printAllReview() 方法在游戏 class 中,是我想用来打印评论及其相应回复的方法。 Comments 中的那个只是一个测试人员,但是当它被调用时回复不会全部打印出来(我想我知道为什么,我相信这是因为 addReply 方法是如何被调用的,但是必须有一种方法可以解决这个问题而不改变调用方法).我在评论 class.
时遇到了一些麻烦
基本上,我不知道如何让我的评论数组列表中的评论与我的回复数组列表中的回复相匹配;然后确保回复与评论相符。此外,评论总是由特定游戏对象添加。但回复是由其前面的特定评论对象添加的。我希望这就像 link 一样。然而它们都是 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);
}
评论 - 尽管我在此 class 中有打印方法,但我不想将其放在那里。此外它并没有真正发挥作用。
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);
}
}
}
游戏 - 我想打印游戏 class 中的所有评论和回复,因为它们显然与特定对象相关。我可以打印所有评论,但我不知道如何将评论 link 回复,反之亦然。我一直在尝试一些事情,但后来我把我的代码弄得一团糟。
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
那么最简单的解决方案就是使用递归。当然,您可能想为其添加一些格式或更多逻辑,但基本思想是:
在评论中创建一个方法,例如
public void display() {
System.out.println(usrComment);
for (Comment c : reply) {
c.display();
}
}
并将游戏中的方法更改为
public void printAllReview() {
for (Comment a : this.Review) {
a.display();
}
}
根据下面的信息和代码,我想知道当我在游戏中调用 printAllReview() 时如何显示 'Review' 以及所有与该评论相关的 'Replies' .
我有两个 ArrayList,一个用于评论,一个用于回复。 ArrayList Review 在 class 内容中并持有对象。 class 评论有 ArrayList 回复和引用本身。
addReviews() 方法在扩展内容的 class 游戏中。 addReplies() 方法在 class 评论中。 printAllReview() 方法在游戏 class 中,是我想用来打印评论及其相应回复的方法。 Comments 中的那个只是一个测试人员,但是当它被调用时回复不会全部打印出来(我想我知道为什么,我相信这是因为 addReply 方法是如何被调用的,但是必须有一种方法可以解决这个问题而不改变调用方法).我在评论 class.
时遇到了一些麻烦基本上,我不知道如何让我的评论数组列表中的评论与我的回复数组列表中的回复相匹配;然后确保回复与评论相符。此外,评论总是由特定游戏对象添加。但回复是由其前面的特定评论对象添加的。我希望这就像 link 一样。然而它们都是 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);
}
评论 - 尽管我在此 class 中有打印方法,但我不想将其放在那里。此外它并没有真正发挥作用。
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);
}
}
}
游戏 - 我想打印游戏 class 中的所有评论和回复,因为它们显然与特定对象相关。我可以打印所有评论,但我不知道如何将评论 link 回复,反之亦然。我一直在尝试一些事情,但后来我把我的代码弄得一团糟。
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
那么最简单的解决方案就是使用递归。当然,您可能想为其添加一些格式或更多逻辑,但基本思想是:
在评论中创建一个方法,例如
public void display() {
System.out.println(usrComment);
for (Comment c : reply) {
c.display();
}
}
并将游戏中的方法更改为
public void printAllReview() {
for (Comment a : this.Review) {
a.display();
}
}