Design Q&A as you see in Amazon, Walmart website for an item.
How about this?
public class QnA {
ArrayList<Question> questions;
ArrayList<Answers> answers;
ArrayList<Items> items;
QnA() {
this.questions = new ArrayList<Question>();
this.answers = new ArrayList<Answers>();
this.items = new ArrayList<Answers>();
}
boolean PostQuestion(int itemId, String ques) {
try {
Item item = items.get(itemId);
Question q = new Question(itemId, ques);
this.questions.add(q);
} catch (IndexOutOfBoundException) {
return false;
}
return true;
}
boolean PostAnswer(int qid, int itemId, String ques) {
try {
Question q = questions.get(qid);
Answer a = new Answer(qid, itemId, ques);
this.questions.add(q);
q.ans.add(a);
} catch (IndexOutOfBoundException) {
return false;
}
return true;
}
// upvoteQuestion()
// upvoteAnswer()
}
class Question {
int itemId;
string ques;
static int qctr = 0;
int id;
ArrayList<Answers> ans;
Question(int itemId, String ques) {
this.id = qctr++;
this.ques = ques;
this.itemId = itemId;
this.ans = new ArrayList<Answers>();
}
}
class Answer {
int qid;
int id;
String ans;
static int actr = 0;
int itemId;
Answer(int qid, int itemId, String ans) {
this.id = actr++;
this.itemId = itemId;
this.ans = ans;
}
}
class Item {
ArrayList<Question> qs;
}
main() {
QnA qna = new QnA();
if(!qna.postQuestion(1, "what is the product price")) {
System.out.println("Invalid Item Id");
}
if (!qna.postAnswer(1, 1, "its 19.4")) {
System.out.println("Invalid Question Id");
}
}Inputs will be greatly appreciated.
Thanks in advance.