简体   繁体   中英

How do I automatically store user defined objects into a map?

I'm creating a little program that allows you to insert your own multiple choice questions and which can ask you these questions by another method. So I set up my "Question Class" with the constructor, a toString() method and a method that can ask me these questions. My problem now is that I have to store the questions in a certain way, because one of the questions parameters is the integer "priority" which changes if you answered it right or wrong.

I thought about a map as you can see below but I don't know how I have to set it up properly, so it stores a new created question automatically into this map. Maybe I have to create another method that does that, but I would like to find a way that excludes calling an extra method. The code below shows how I create a new question in the main method and the data fields and the constructor of the Question class.

So in this example I'd like to save the question number1 into the Map database. I don't want to do that manually.

public static void main(String[] args) {

Question number1 = new Question("What is the right answer?",
            new String[] { "1", "2", "3", "4" }, 3, 1.0);
}

public class Question {

public String question;
public String[] answers;
public int solution;
public double priority;
public static Map<Integer, Question> Database = new TreeMap<Integer,Question>();

public Question(String que, String[] ans, int sol, double prio){
this.question = que;
this.answers = ans;
this.solution = sol;
this.priority = prio;

}

From your comments, to automatically insert it into the map use :

public class Question {

   static int keyCount; 

   public Question(String que, String[] ans, int sol, double prio){
      this.question = que;
      this.answers = ans;
      this.solution = sol;
      this.priority = prio;
      Database.put(++keyCount, this);
   }

}

Here, we insert an entry in the map everytime you create a new object.

  1. The this refers to the current object created.
  2. The static variable keyCount is used to increment the value of the key everytime an object is created.

I would suggest against adding the object to Map inside the constructor. One reason would be the Single Responsibility principle. Your constructor would do 2 things (initialize an object and add it to the Map). This is a bad practice especially because the name of the method (the constructor in your case) does not state clearly what it does. Another reason is the use of "this" inside a constructor. You should be very careful doing so because it can lead to issues really difficult to debug. The reason is: while you are still in the constructor, your object (so "this") is not yet fully initialized. So you are passing an object that is not fully initialized as a parameter to a method. As I said, that can lead to big problems.

If you really need to do all in one you can create a static method in your class like this:

   public static addQuestion(String que, String[] ans, int sol, double prio){
      int key = Database.size();
      Database.put(key, new Question (que, ans, sol, prio));
   }

You can then call it like so:

   Question.addQuestion("What is the right answer?",
            new String[] { "1", "2", "3", "4" }, 3, 1.0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM