Search code examples
javaconstructorundefineddecoratorimplements

Decorator Pattern - undefined constructor


I have a liitle problem with my code. Everything looks good but when I trying to start program I see this: "Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor Pakiety(Samochod) is undefined The constructor Pakiet2(Samochod) is undefined The constructor Pakiet3(Samochod) is undefined

at SamochodInfo.main(SamochodInfo.java:16)"

There is code, comments and classes are in Polish cuz it's my homework. Help me please. :)

First class:

//Definiujemy klasę abstrakcyjną
abstract class Samochod {
protected String opis;

public String getOpis() {
    return opis;
}

public abstract double cena(); 

@Override //Nadpisanie metody
public String toString() {
    return cena() + "(" + getOpis() + ")";


}
}

Second class:

class Audi extends Samochod {

public Audi() {
    opis = "Podstawowa wersja samochodu z wyposażeniem standarowym";
}
@Override
public double cena() {
    return 75.000;
    }
}

Third class:

abstract class DekoratorSamochodu extends Samochod {
    protected Samochod car;

public DekoratorSamochodu(Samochod car) {
    this.car = car;
}

@Override
public abstract String getOpis();
}

Fourth class:

class Pakiety extends DekoratorSamochodu {

public Pakiety(DekoratorSamochodu car) {
    super(car);
}

@Override // Nadpisanie metody Opis (analogicznie w innych pakietach)
public String getOpis() {
    return car.getOpis() + ", Pakiet Smart";
}

@Override // Nadpisanie metody cena - cena zwiększy się o 10tyś. zł 
(analogicznie w innych
            // pakietach)
public double cena() {
    return car.cena() + 10.000;
}
}

class Pakiet2 extends DekoratorSamochodu {

public Pakiet2(DekoratorSamochodu car) {
    super(car);
}

@Override
public String getOpis() {
    return car.getOpis() + ", Pakiet Wygoda";
}

@Override
public double cena() {
    return car.cena() + 25.000;
}
}

class Pakiet3 extends DekoratorSamochodu {

public Pakiet3(DekoratorSamochodu car) {
    super(car);
}

@Override
public String getOpis() {
    return car.getOpis() + ", Pakiet Exclusive";
}

@Override
public double cena() {
    return car.cena() + 37.000;
}
}

Fifth class:

import java.util.List;

import java.util.*;

public class SamochodInfo {
public static void main(String[] args) {
    Samochod car;
    List<Samochod> info = new ArrayList<>();

//Wyświetlenie podstawowej wersi Audi
    car = new Audi();
    info.add(car);

//Wyświetlenie pierwszego pakietu Audi - smart
    car = new Audi();
    car = new Pakiety(car);
    info.add(car);

//Wyświetlenie druiego pakietu Audi - wygoda
    car = new Audi();
    car = new Pakiet2(car);
    info.add(car);

//Wyświetlenie trzecieko pakietu Audi - exclusive
    car = new Audi();
    car = new Pakiet3(car);
    info.add(car);

    for (int i = 0; i < info.size(); i++) {
        int infoNr = i+1;
        System.out.println("Informacje o samochodzie " + infoNr + ":");
        System.out.println(" * " + info.get(i));
    } 


}
}

Solution

  • car is of type Samohod and class Pakety doesn't have constructor that accepts Samohod it's only constructor accepts DekoratorSamochodu. You need to either add another constructor or change constructors of Pakiety, Pakiet2 and Packiet3 to accept Samohod instead of DekoratorSamochodu