"Porozumiewanie" się metod

0

Witam,
piszę program, który: użytkownik wybiera kontener(szklanka, kufel, butelka) i na lewa do niej pewnej cieczy(Woda, Cola itp), program powinien obsłużyć tę rzeczy czyli tworzenie obiektu, "nalewanie" do obiektu, sprawdzanie czy nasz obiekt np(szklanka)po nalaniu nie jest już pełna. Powinien również, drukować informacje o tym co już się znajduję w kontenerze. Czyli jeśli lejemy do kontenera 300 ml wody, to wyświetla informacje o tym, że znajduje się tam woda, jej gęstość i ilość. (Do kontenera można wlewać różne substancję/ciecze). Poniżej znajduję się kod napisany w javie, nie jestem pewny czy dobrze to robię dlatego prosiłbym o sprawdzenie kodu. Następnie w klasie Faucet metodzie pour jak sprawdzić, czy kontener jest już pełny, dać tam kolejnego if'a ?

public class Main {
    
    public static void main(String[] args) {

        // Tworze obiekiekt kasy Glass, która posiada swoją mase i swoją objetość tzn ile może zmieścić cieczy w sobie
        Container glass = new Glass(0.3,0.5);

        // Leje wode do szklanki ilosc podana w argumentcie
        glass = Faucet.pour(glass,new Water(0.2));
        glass.printContent();
        System.out.println();

        // Leje Cole do szklanki ilosc podana w argumentcie
        glass = Faucet.pour(glass,new Coke(0.2));
        glass.printContent();
        System.out.println();

        // Leje wode do szklanki ilosc podana w argumentcie
        glass = Faucet.pour(glass,new Water(0.4));
        glass.printContent();
        System.out.println();

        // Leje Cole do szklanki ilosc podana w argumentcie
        glass = Faucet.pour(glass,new Coke(0.7));
        glass.printContent();
        System.out.println();

    }
}
import java.util.LinkedList;

public class Container {

    private double mass;
    private double volume;
    private LinkedList<Liquid> liquids = new LinkedList<>(); // lista cieczy która zawiera pojemnik

    // sprawdza czy szklanka jest pusta (czy jakaś ciecz(liquid) sie w niej znajduje)
    public boolean isEmpty() {
        if (getLiquids().size() == 0)
            return true;
        else
            return false;
    }
    //drukuje informacje o tym co pojemnik zawiera
    public void printContent()
    {
        for (Liquid l: liquids) {
            System.out.println(l);
        }
    }
    public Container() {
    }
    public Container(double mass, double volume){
        this.mass = mass;
        this.volume = volume;
    }
    public LinkedList<Liquid> getLiquids() {
        return liquids;
    }
    public double getVolume() {
        return volume;
    }
    public double getMass() {
        return mass;
    }
}
public class Glass extends Container {

    public Glass(double mass, double volume) {
        super(mass, volume);
    }
}
public class Faucet {
    // Pierwszo sprawdzam czy w szklance znajduje sie jakaś ciecz, jeśli nie to nalej odrazu.
    // Jeśli jakaś się już tam znajduje to sprawdz czy nie lejemy tego samego, tzn jeśli w szklance znajduje sie
    // woda i chcemy teraz nalac wody to nalewamy tej wody zmienajac ilosc wody ktora już się w tej szklance
    // znajdowała,
    public static Container pour(Container container, Liquid liquid)
    {
       if(container.getLiquids().size()!=0)
       {
           for(Liquid l : container.getLiquids()) {
               if (l.getName().equals(liquid.getName())) {
                   l.setQuantity(l.getQuantity() + liquid.getQuantity());
                   return container;
               }
           }
           container.getLiquids().add(liquid);
           return container;
       }else {
           container.getLiquids().add(liquid);
           return container;
       }
    }
}
public class Coke extends Liquid {

    // Czy taka deklaracja jest prawidłowa ?
    {
        setName("Cola");
        setDensity(1.015);
    }
    public Coke() {
    }
    public Coke(double quantity) {
        setQuantity(quantity);
    }
}
public class Liquid {

    private String name;
    private double density;
    private double quantity;

    public String getName() {
        return name;
    }

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

    public double getDensity() {
        return density;
    }

    public void setDensity(double density) {
        this.density = density;
    }

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Liquid{" +
                "name='" + name + '\'' +
                ", density=" + density +
                ", quantity=" + quantity +
                '}';
    }
}
public class Water extends Liquid {

    // Czy taka deklaracja jest prawidłowa ?
    {
        setName("Water");
        setDensity(1);
    }
    public Water() {
    }
    public Water(double quantity) {
        setQuantity(quantity);
    }

}

0
  1. Użyj tam Map na te twoje liquidy a nie List, skoro chcesz je wygodnie wyciągać
  2. Zamiast setterów, szczególnie w konstruktorze, zrób parametry konstruktora!

1 użytkowników online, w tym zalogowanych: 0, gości: 1