java - 油漆转换 (JAVA NETBEANS)
问题描述
我正在尝试做一个油漆转换项目。最终我会制作一个 GUI,但我想先充实它。我在下面有代码。有没有人可以简化这一点,而不仅仅是拥有一堆不同的方法?下面是主类和方法类,它显示了我如何计算一切。原谅我第一次在这里发布问题的任何乱七八糟的格式。
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!");
}
}
}
解决方案
我会将所有数量转换为幕后的通用单位,然后根据需要将它们转换回来。比如说,在一个名为 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();
推荐阅读
- c# - 为什么本地 Service Fabric 无权访问我的证书的私钥?键集不存在
- flutter - 颤振将PDF数据发送到打印机
- sql - SSRS SUM() 组外的分组值
- sql - SQL 连接和并发 SQL 查询哪个更高效?
- javascript - 找不到错误 - 异步函数 - 返回太快
- javascript - 当我选择两个或两个以上选项时,discord.js 多选菜单无法运行多个值代码并且无法回复多个交互
- flutter - 为什么拉刷新不使用颤振刷新我的列表
- excel - Excel - vlookup 值 - 来自另一张工作表的 2 列(无连接)的组合
- assembly - 汇编中的变量
- flutter - Flutter相机包,如何旋转图像?