Копирование имутабельного класса

Я начал изучать Java программирование.

Получил задание: Создайте иммутабельный класс для хранения информации о грузах, передаваемых в курьерскую службу. Реализуйте в классе методы, дающие возможность изменять адрес доставки, габариты и массу груза без изменения исходного объекта путём создания его копии. Код вроде бы написал, но поменять данные с помощью сеттера копирования не выходит,что я вбивая в классе Main в StorageOfCargoInformation, то и выводит в консоль. Что в коде не так?

public class StorageOfCargoInformation {
    private final double weight;
    private final Dimensions dimensions;
    private final String address;
    private final boolean turnOver;
    private final String registrationNumber;
    private final boolean fragile;
    private final double length;
    private final double width;
    private final double height;

    public StorageOfCargoInformation(double weight, String address,
                                     Dimensions dimensions, String registrationNumber,
                                     boolean turnOver, boolean fragile, double length,
                                     double width, double height)
    {
        this.weight = weight;
        this.address = address;
        this.registrationNumber = registrationNumber;
        this.length = length;
        this.width = width;
        this.height = height;
        this.dimensions = dimensions;
        this.turnOver = turnOver;
        this.fragile = fragile;
    }

    public StorageOfCargoInformation setWeight(double weight) {
        return new StorageOfCargoInformation(weight, address, dimensions,
                                            registrationNumber, turnOver, fragile, length, width, height);
    }

    public double getWeight() {
        return weight;
    }


    public StorageOfCargoInformation setAddress(String address) {
        return new StorageOfCargoInformation(weight, address, dimensions,
                                            registrationNumber, turnOver, fragile, length, width, height);
    }

    public String getAddress() {
        return address;
    }

    public StorageOfCargoInformation setDimensions(Dimensions dimensions) {
        return new StorageOfCargoInformation(weight, address, dimensions,
                                            registrationNumber, turnOver, fragile, length, width, height);
    }

    public Dimensions getDimensions() {
        return dimensions;
    }

    public StorageOfCargoInformation setRegistrationNumber(String registrationNumber) {
        return new StorageOfCargoInformation(weight, address, dimensions,
                                            registrationNumber, turnOver, fragile, length, width, height);
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public StorageOfCargoInformation setTurnOver(boolean turnOver) {
        return new StorageOfCargoInformation(weight, address, dimensions,
                registrationNumber, turnOver, fragile, length, width, height);
    }

    public boolean canTurnOver() {
        return turnOver;
    }

    public StorageOfCargoInformation setFragile(boolean fragile) {
        return new StorageOfCargoInformation(weight, address, dimensions,
                registrationNumber, turnOver, fragile, length, width, height);
    }

    public boolean isFragile() {
        return fragile;
    }

    public StorageOfCargoInformation setLength(double length){
        return new StorageOfCargoInformation(weight, address, dimensions,
            registrationNumber, turnOver, fragile, length, width, height);}

    public StorageOfCargoInformation setWidth(double width){
        return new StorageOfCargoInformation(weight, address, dimensions,
                registrationNumber, turnOver, fragile, length, width, height);}

    public StorageOfCargoInformation setHeight(double height){
        return new StorageOfCargoInformation(weight, address, dimensions,
                registrationNumber, turnOver, fragile, length, width, height);}

    public String toString(){return "вес " + weight + "\n" + "адрес " + address + "\n" + dimensions +
            "\n" +  "регистрационный номер " + registrationNumber;}
}

Second class Dimensions

public class Dimensions {
    private final double length;
    private final double width;
    private final double height;

    public Dimensions(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    public Dimensions setLength(double length) {
        return new Dimensions(length, width, height);
    }

    public Dimensions setWidth(double width) {
        return new Dimensions(length, width, height);
    }

    public Dimensions setHeight(double height) {
        return new Dimensions(length, width, height);
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }

    public double volume() {
        return (length * width * height);
    }

    public String toString(){return "Длина " + length + "мм.;" + "\n" + "Ширина " + width + "мм.;"
            + "\n" + "Высота " + height + "мм.;";}
}

And Teast class Main:

public class Main {
    public static void main(String[] args) {
        Dimensions dimensions = new Dimensions(1,2,3);
        dimensions.setLength(1900);
        dimensions.setWidth(1900);
        dimensions.setHeight(1900);
        StorageOfCargoInformation cargoInformation = new StorageOfCargoInformation(50,
                "Проспект", dimensions, "Ф100",
                true, true, 1,1,1);
        cargoInformation.setWeight(111);
        System.out.println(cargoInformation);
        }
    }

Ответы (1 шт):

Автор решения: Aziz Umarov

Надо так если имутабельный. Ваши сеттеры возращяют новый объект, может нужно именно с ними работать.

public class Main {
    public static void main(String[] args) {
        Dimensions dimensions = new Dimensions(1,2,3);
        dimensions = dimensions.setLength(1900);
        dimensions = dimensions.setWidth(1900);
        dimensions = dimensions.setHeight(1900);
        StorageOfCargoInformation cargoInformation = new StorageOfCargoInformation(50,
                "Проспект", dimensions, "Ф100",
                true, true, 1,1,1);
        cargoInformation = cargoInformation.setWeight(111);
        System.out.println(cargoInformation);
        }
    }
→ Ссылка