【问题标题】:Placing interest on a bank transaction. Varying interests for different accounts not working?对银行交易产生利息。不同帐户的不同兴趣不起作用?
【发布时间】:2016-07-14 16:16:35
【问题描述】:

我正在尝试对银行交易设置利息。主要目标是尝试在 2000 年以上的交易中获得 0.05 或 5%。储蓄账户为 0.02 或 2%,支票账户为 0%。

所以我有一个支票利息方法,它放在任何存款之前,以便给出正确的利息。如果不压倒其他利益,我就无法让 0% 的人工作。我的代码默认是这样的。

public void checkInterest(double amount){

 if(balance + amount <= 2000){

     interest = 0.02;
   }
    else{
        interest = 0.05; 
    }

我最后一次尝试是这样的。

 public void checkInterest(double amount){

 if(balance + amount <= 2000){

     interest = 0.02;
   }
    else{
        interest = 0.05; 
    }
  if(Checking.accountType == "checking"){

     interest = 0.00;
 }
}

如果我这样做,在运行时它会覆盖并给出一个固定的 0%。我也尝试使用 Checking.accountType != null,但这也没有用。

有什么建议吗?这只是为了学术工作。 (另外,我只是一个初学者,所以放轻松)

【问题讨论】:

  • @Henry 刚刚将其换成 .equals() 并且没有运气。也许我的代码中的其他地方出了问题,但很难说,因为通过每种方法,它应该是正确的。
  • 用调试器单步调试代码,看看哪里出错了。

标签: java object banking


【解决方案1】:

如果帐户类型是String,您应该像这样比较它们:

if(Checking.accountType.equals("checking")){

此外,您可能希望更改您的 checkInterest 方法以返回 interest 的值。

 public double checkInterest(double amount){
   double interest;
   // Calculate interest rate here
   return interest;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 2022-01-13
    • 1970-01-01
    • 2016-02-04
    • 2015-01-23
    • 1970-01-01
    • 2020-07-06
    • 2017-06-05
    相关资源
    最近更新 更多