Преобразование краски (JAVA NETBEANS) - PullRequest
1 голос
/ 21 апреля 2020

Я пытаюсь сделать проект преобразования краски. В конце концов, я сделаю GUI, но сначала хочу просто набросать sh. У меня есть код здесь ниже. Есть ли кто-нибудь, кого я мог бы упростить, а не просто иметь кучу разных методов? Ниже будет основной класс и метод класса, который показывает, как я все вычисляю. Прошу прощения за любое неаккуратное форматирование здесь, это мой первый раз, когда я задаю вопрос.

package paintconversion;

/** @author JoshS */
public class Paint2Quart {

  private double Quart;
  private double Gallon;
  private double Pint;

  public Paint2Quart() {
    Quart = 0;
    Gallon = 0;
    Pint = 0;
  }
  // Method to convert Quart to Gallon
  public void setQuartToGallon(double conQuart) {
    if (conQuart > 0) {
      Quart = conQuart / 4;
      System.out.println(conQuart + " quart equals " + Quart + " Gallon(s)");
    } else {
      System.out.println("Numbers Must Be Positive!");
    }
  }
  // Method to convert Gallon To Quart
  public void setGallonToQuart(double conGallon) {
    if (conGallon > 0) {
      Gallon = conGallon * 4;
      System.out.println(conGallon + " gallon equals " + Gallon + " Quart(s)");
    } else {
      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setPintToGallon(double conPint) {
    if (conPint > 0) {
      Pint = conPint / 8;
      System.out.println(conPint + " pint equals " + Pint + " Gallon(s)");
    } else {
      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setPintToQuart(double conPint) {
    if (conPint > 0) {
      Pint = conPint / 2;
      System.out.println(conPint + " pint equals " + Pint + " Quart(s)");
    } else {

      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setGallonToPint(double conGallon) {
    if (conGallon > 0) {
      Gallon = conGallon * 8;
      System.out.println(conGallon + " Gallon equals " + Gallon + " Pint(s)");
    } else {

      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setQuartToPint(double conQuart) {
    if (conQuart > 0) {
      Pint = conQuart * (2);
      System.out.println(conQuart + " Quart equals " + Pint + " Pint(s)");
    } else {

      System.out.println("Numbers Must Be Positive!");
    }
  }
}

1 Ответ

0 голосов
/ 21 апреля 2020

Я бы преобразовал все величины в общую единицу за кулисами, а затем преобразовал бы их обратно по мере необходимости. Скажем, в классе Volume:

public class Volume {
    private double gallons;

    private Volume(double gallons) {
        if (gallons < 0) {
            throw new IllegalArgumentException("value must be positive");
        }
        this.gallons = gallons;
    }

    public static Volume ofGallons(double gallons) {
        return new Volume(gallons);
    }

    public static Volume ofQuarts(double quarts) {
        return new Volume(quarts / 4);
    }

    public static Volume ofPints(double pints) {
        return new Volume(pints / 8);
    }

    public static Volume ofLiters(double liters) {
        return new Volume(0.264172 * liters);
    }

    public double asGallons() {
        return gallons;
    }

    public double asQuarts() {
        return gallons * 4;
    }

    public double asPints() {
        return gallons * 8;
    }

    public double asLiters() {
        return gallons / 0.264172;
    }

    @Override
    public String toString() {
        return gallons + " gallon" + (gallons == 1 ? "" : "s");
    }
}

Затем, чтобы преобразовать объем, скажем, из пинты в кварты:

quarts = Volume.ofPints(pints).asQuarts();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...