Search code examples
javadictionarycomparableiterablebag

Custom bag class using a map doesn't allow me to call methods from the instance of a class which is the key


I'm trying to code a Bag custom class in java. For this I use a Map where Car is an instance of a class.

In my code, I'm not able to call function from the Car class, but I can't figure out why.

Here is the code for a better understanding.

BagTest.java

public class BagTest {
    public static void main(String[] args) {
         Car c1 = new Car("Model1", 15000);
        Car c2 = new Car("Model2", 20000);
        Car c3 = new Car("Model3", 12000);

        Bag bag = new Bag<Car>();
        bag.add(c1);
        bag.add(c2, 2);
        bag.add(c3);
    }
}

Car.java

final class Car implements Comparable<Car> {
    private final String _name;
    private final int _price;

    public Car(String name, int price) {
        this._name = name;
        this._price = price;
    }

    public String getName() {
        return (_name);
    }

    public int getPrice() {
        return (_price);
    }

    public boolean equals(Object car) {
        if (!(car instanceof Car))
           return (false);
        Car c = (Car)car;
        return (this._name.equals(c._name) && this._price == c._price);
    }

    public String toString() {
        return (this._name + " " + this._price);
    }

    public int hashCode() {
        return (31 * this._name.hashCode() * this._price);
    }

    @Override
    public int compareTo(Car car) {
        if (car == null)
            return (-1);
        if (this._price < car._price)
            return (-1);
        if (this._price == car._price)
            return (0);
        else
            return (1);
    }
}

Bag.java

class Bag <Car extends Comparable<Car>> implements Iterable<Car> {
    private Map<Car, Integer> _bag;

    public Bag() {
        this._bag = new TreeMap<>();
    }

    public void add(Car car) {
        if (car == null)
            return;
        if (this._bag.containsKey(car))
            this._bag.put(car, this._bag.get(car) + 1);
        else
            this._bag.put(car, 1);
    }

    public void add(Car car, int qty) {
        if (car == null || qty == 0)
            return;
        if (this._bag.containsKey(car))
            this._bag.put(car, this._bag.get(car) + qty);
        else
            this._bag.put(car, qty);
    }

    public void getCarsName() {
        for (Car car : this) {
            car.getName();  // HERE IS THE PROBLEM
        }
    }

    public Iterator<Car> iterator() {
        List<Car> carsList = new ArrayList<>();
        for (Car car: _bag.keySet())
        {
            int fr = _bag.get(car);
            for (int j = 0; j < fr; j++)
                carsList.add(car);
        }
        return (carsList.iterator());
    }
}

Problem

The problem is that I can't reach any method / variable of Car. In the code above, I can't reach the method .getName() (in the function getCarsName(), in the file Bag.java), it's like the method doesn't exist.

Also I can't create a new Car in the Bag class, I got the following error: "Type parameter 'Car' cannot be instantiated directly". It seems linked to the problem and will sugest that the problem come from the Car class, but I can't figure out where it is.

Thanks in advance.


Solution

  • As you can see below, the type of a car variable is not Car but Object (even though the instance may be of type Car) and type Object does not have any such a method.

    ...
    
    for (Object car : this) {
        car.getName();
    }
    
    ...
    

    You have to change it as follows.

    ...
    
    for (Car car : this) {
        car.getName();
    }
    
    ...
    

    And also change Bag class declaration as follows.

    class Bag implements Iterable<Car> {
    
        ...
    
    }