Search code examples
javahashmapsubclasssuperclass

How do I implement attributes from a hashMap when creating an object for a subclass?


What im trying to do is basically iterate my genres and use my hashMap as a lookup table and then choose the genre when creating the object. I don't know what I need to do next as it keeps giving me errors about creating a new constructor when I already have one.

import java.util.HashMap;
import java.util.Scanner;

public class disc {
   private String title;
   private String releaseDate;
   HashMap<Integer, String> genre = new HashMap<Integer, String>();
   Scanner mainInput = new Scanner(System.in);

    public disc(String releaseDate, String title, HashMap<Integer, String> genre) {
        this.title = title;
        this.releaseDate = releaseDate;
        this.genre = genre;
        this.genre.put(1, "Horror");
        this.genre.put(2, "Action");
        this.genre.put(3, "Hip-Hop");
        this.genre.put(4, "Pop");
        this.genre.put(5, "Jazz");
        this.genre.put(6, "Thriller");
    }

subclass:

import java.util.HashMap;

public class game extends disc{
    private int PEGIRating;
    private String Platform;
   public game(int PEGIRating, String Platform, String title, String releaseDate, HashMap<Integer, String> genre){
        super(releaseDate, title, genre);
        this.PEGIRating = PEGIRating;
        this.Platform = Platform;

        game g = new game(18,"PS5","Evil Within","Feb 2018",1);

    }
}

Solution

  • As mentioned above you are initializating your game object in the game constuctor which will compile but cause possible run time issues.

    To add to this you are passing an int data type into the last parameter of the game constuctor instead of passing a HashMap<Integer, String> as you have specific in your constructor signature. Either create a constructor that takes the exact parameter list but with int as the last data type or pass a HashMap as the last parameter instead.