Search code examples
javaconstructorcompiler-errors

The constructor Game(String, int, int, double, String) is undefined


So I am making a store in java and for some reason when doing class inheritance it says my constructor is undefined.

Below is my code

public class Store {
    private String name;
    private ArrayList<Item> itemArray;
    private int discount;

    public Store(String initName, int initDiscount) {
        name = initName;
        discount = initDiscount;
        itemArray = new ArrayList<Item>();
        Game item1 = new Game("Pokemon", 2000, 200, 4.5, "Nintendo");
        itemArray.add(item1);
    }
}

public class Item {
    public String name;
    public int price;
    public boolean taxable;
    public String category;
    public int inventory;

    public Item(String initName, int initPrice, boolean initTaxable, String initCategory, int initInventory) {
        name = initName;
        price = initPrice;
        taxable = initTaxable;
        category = initCategory;
        inventory = initInventory;
    }

    public void description() {
        System.out.println("This is a " + name);
        System.out.println("The base price is " + displayPrice());
        if (taxable) {
            System.out.println("This item is taxed");
        } else {
            System.out.println("This item is not taxed");
        }
        System.out.println("This item is a " + category);
        System.out.println("There are " + inventory + " of this item left");
    }
    //Gets the price of the item before discounts(The price is in cents)
    public int getBasePrice() {
        return price;
    }
    //Gets the price of the item after discounts are applied the percentages should be in decimal form ex. .3 is 30% off
    public int getCurrentPrice(int discount) {
        int percentOfFullPrice = 100 - discount;
        double percentOfFullPriceDouble = percentOfFullPrice / 100.0;
        double finalPrice = percentOfFullPriceDouble * price;
        int finalIntPrice = (int) finalPrice;
        return finalIntPrice/100;
    }
    //Sets the new price of the item
    public void setPrice(int newPrice) {
        price = newPrice;
    }
    //Displays the price as a string
    public String displayPrice() {
        return "$" + (float) price / 100;
    }
    //Gets the inventory of the item
    public int getInventory() {
        return inventory;
    }

    //Adds to the inventory of the item
    public int addInventory(int additionalInventory) {
        inventory += additionalInventory;
        return inventory;
    }
}

public class Game extends Item {
    public float reviews;
    public String companyBy;

    public Game(String initGameName, int initPrice, int initInventory, float initReviews, String initCompanyBy) {
        super(initGameName, initPrice, true, "Video Game", initInventory);
        reviews = initReviews;
        companyBy = initCompanyBy;
    }

    public void getReviews() {
        System.out.println("This video game has " + reviews + " out of 5 stars");
    }
    
}

I tried to initialize an object with parameters that would go to the superclass however, it seemed to get an undefined. If I initialize something simply using the Item class it works but I want different subsets.


Solution

  • Your actual constructor is Game(String, int, int, float, String), notice float, while you provide it with double - new Game("Pokemon", 2000, 200, 4.5, "Nintendo");. The value 4.5 as literal is of type double. To make it a float, you need to use it like this - 4.5f or 4.5F.

    new Game("Pokemon", 2000, 200, 4.5F, "Nintendo");
    

    Check Floating point literal, floating literal, double Literal for more information.