jeudi 17 juin 2021

State design pattern, need advice

I have been trying all day to get to the bottom of this problem, but I just cannot see how to implement it, hence the reason for applying here seeking for help. I am looking for any type of advice someone could give.

I have the following class:

public class Client {
    private String name;
    private String surname;
    private String address;

    public Client(String name, String surname, String address){
        this.name = name;
        this.surname = surname;
        this.address = address;
    }

    public String getNume() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


}

I wanted to implement the state design pattern for something like a delivery-type of exercise.

I have an interface with the showStatus() and nextStatus() method, and two classes that implement it - InDelivery and Delivered.

public interface Delivery {
    void showStatus();
    void nextStatus(Courier courier)
}


public class Courier {
    int phoneNumber;
    String destination;
    Client client;
    Delivery statusDelivery = new InDelivery();


    Courier(int phoneNumber, String destination){
        this.destination  = destination;
        this.phoneNumber = phoneNumber;


    }

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public void setStatus(Delivery statusDelivery){
        this.statusDelivery = statusDelivery;
    }

    public void printStatus(){
        statusDelivery.printStatusStatus();
        statusDelivery.statusUrmator(this);
    }

}

public class InDelivery implements Delivery{
    Client client;

    @Override
    public void showStatus() {
        System.out.println("In delivery");
    }

    @Override
    public void nextStatus(Courier courier) {
        if(courier.getDestination() == client.getAddress())
            courier.setStatus(new Delivered());

    }
}


public class Delivered implements Delivery{
    @Override
    public void showStatus() {
        System.out.println("Delivered");
    }

    @Override
     public void nextStatus(Courier courier) {
    System.out.println("No status, already delivered");
    }


}


What I am trying to do is only when the Courier address matches the Client address it moves to the next status (the Delivered status).

This piece of code is the problem:

public void nextStatus(Courier courier) {
        if(courier.getDestination() == client.getAddress())
            courier.setStatus(new Delivered());

    }

Any kind of advice I'd greatly appreciate..

Aucun commentaire:

Enregistrer un commentaire