dimanche 19 juillet 2020

I have an issue while implementing builder design pattern

I am learning design patterns in java from online video tutorial i have learned builder design pattern i am trying to implement what he does but for me its showing some errors. help me to resolve this.

public class Phone {

private String model;
private String os;
private int ram;
private double screensize;
private int battery;

public Phone(String model, String os, int ram, double screensize, int battery) {
    super();
    this.model = model;
    this.os = os;
    this.ram = ram;
    this.screensize = screensize;
    this.battery = battery;
}

@Override
public String toString() {
    return "Phone [model=" + model + ", os=" + os + ", ram=" + ram + ", screensize=" + screensize + ", battery="
            + battery + "]";
}           

}

public class Phonebuilder {

private String model;
private String os;
private int ram;
private double screensize;
private int battery;


public Phonebuilder setModel(String model) {
    this.model = model;
    return this;
}
public Phonebuilder setOs(String os) {
    this.os = os;
    return this;
}
public Phonebuilder setRam(int ram) {
    this.ram = ram;
    return this;
}
public Phonebuilder setScreensize(double screensize) {
    this.screensize = screensize;
    return this;
}
public Phonebuilder setBattery(int battery) {
    this.battery = battery;
    return this;
}

public Phone getphone () {
    return new Phone(model, os, ram, screensize, battery);
}

}

public class Test {

public static void main(String[] args) {
    //It showing error in the below line like type mismatch.

    Phone p = new Phonebuilder().setBattery(9000).setModel("M31").setOs("Android");
    System.out.println(p);
}

}

Aucun commentaire:

Enregistrer un commentaire