对于我的任务,我必须为一家煤气公司创建一个煤气表系统,以允许员工创建新的客户帐户并修改数据,例如姓名和单位成本以及从帐户中提取(存入)钱。
我已经创建了构造函数,甚至添加了重载方法,尽管在启动我的一个名为 deposit的 方法时当前遇到问题,这应该从用户帐户中赚钱,而其他方法(如 recordUnits 允许)员工可以输入燃气表来读取客户使用了多少个单位,并更新该客户帐户的余额,这基本上就是客户欠公司的钱。
当尝试启动存款方法时仅使用预设信息测试程序时,我得到了
Account.deposit(Double.MAX_VALUE);
我不太确定这意味着什么,而且似乎也找不到通过它的方法!
测试数据和代码如下所示:
public class TestGasAccount { public static void main (String [] args) { GasAccount Account = new GasAccount (223,"Havana","TQ",1000); Account.getAccNo(); Account.getName(); Account.getAddress(); Account.getUnits(); Account.getBalance(); Account.recordUnits(1000); Account.getUnits(); Account.getBalance(); Account.deposit(Double.MAX_VALUE); } }
打破
public class GasAccount { private int intAccNo; private String strName; private String strAddress; private double dblBalance; private double dblUnits; protected double dblUnitCost = 0.02; public GasAccount(int intNewAccNo,String strNewName,String strNewAddress,double dblNewUnits) { intAccNo = intNewAccNo; strName = strNewName; strAddress = strNewAddress; dblUnits = dblNewUnits; dblBalance = dblNewUnits * dblUnitCost; } public GasAccount (int intNewAccNo, String strNewName, String strNewAddress) { intAccNo = intNewAccNo; strName = strNewName; strAddress = strNewAddress; } public double deposit (Double dblDepositAmount) { dblBalance = dblBalance - dblDepositAmount; return dblBalance; } public String recordUnits (double dblUnitsUsed) { double dblTempBalance; dblTempBalance = dblUnitsUsed * dblUnitCost; dblBalance = dblBalance + dblTempBalance; dblUnits = dblUnits + dblUnitsUsed; return "Transaction Successful"; } public int getAccNo () { System.out.println(intAccNo); return intAccNo; } public String getName() { System.out.println(strName); return strName; } public String getAddress() { System.out.println(strAddress); return strName; } public double getBalance() { System.out.println("£"+dblBalance); return dblBalance; } public double getUnitCost() { return dblUnitCost; } public double getUnits () { System.out.println(dblUnits); return dblUnits; } public void updateUnitCost (double dblNewUnitCost) { dblUnitCost = dblNewUnitCost; } }
Double.MAX_VALUE是double可以表示的最大值(大约1.7 * 10 ^ 308)。
如果您尝试减去数据类型的最大可能值,则这将导致一些计算问题。
即使在处理金钱时,也不要使用浮点值,尤其是在四舍五入时,这可能会引起问题(然后您的系统中必须有多少钱)。