0

好的,所以我遇到了很多麻烦,我还在学习 Java,我的书为我设置了一项我在网上发现的常见任务,我坚持的部分是......

我必须创建一个银行账户程序,给账户持有人一个储蓄账户(有利率,没有透支)和一个支票账户(有 100 英镑的透支,没有利息)。

我还没有实施透支,只是准备好提款和存款功能的一半,但我的问题是利息,我在我的超类中定义了储蓄账户余额和支票账户余额,所以在计算我的兴趣时储蓄账户类我无法引用 savebalance,因为我已将其设为私有。我正在尝试使用 set.name 方法,但显然我做错了....

一个灿烂的笑容,感谢任何可以帮助或提供建议的人!

超类如下:

public class BankDetails
    {
        private String customer;
        private String accountno;
        private double savebalance;
        private double checkbalance;


//Constructor Methods

 public BankDetails(String customerIn, String accountnoIn, double savebalanceIn, double checkbalanceIn)
        {
            customer = customerIn;
            accountno = accountnoIn;
            savebalance = savebalanceIn;
            checkbalance = checkbalanceIn;
        }

      // Get  name
      public String getcustomername()
        {
            return (customer);
        }

      // Get account number
      public String getaccountnumber()
        {
            return (accountno);
        }

       public double getcheckbalanceamount()
        {
            return (checkbalance);
        }

       public double getsavebalanceamount()
        {
            return (savebalance);
        }


public void savewithdraw(double savewithdrawAmountIn)

  {
        savebalance = savebalance - savewithdrawAmountIn;

  }

public void checkwithdraw(double checkwithdrawAmountIn)

  {
        checkbalance = checkbalance - checkwithdrawAmountIn;

  }


 public void savedeposit(double savedepositAmountIn)

  {
        savebalance = savebalance - savedepositAmountIn;

  }

public void checkdeposit(double checkdepositAmountIn)

  {
        checkbalance = checkbalance - checkdepositAmountIn;

  }




    } // End Class BankDetails

子类如下:

    import java.util.*; 

public class Savings extends BankDetails
  {

      private String saveaccount;
      private double interest;


      public Savings(String customerIn, String accountnoIn, float interestIn, 
      String saveaccountIn, double savebalanceIn)
        {


            super (customerIn, accountnoIn, savebalanceIn, interestIn);


            saveaccount = saveaccountIn;
            interest = interestIn;

        }


      public String getsaveaccountno()
       {
           return (saveaccount);
       }

      public double getinterestamount()
       {
           return (interest);
       }


      public void interestamount(String[] args)

       {
           BankDetails.getsavebalanceamount(savebalance);
           interest = (savebalance / 100) * 1.75;


       }


       }
4

2 回答 2

3

使用超类的getSaveBalance()方法来访问余额(这个名字很可疑,因为你有一个储蓄账户类,但是把余额放在别处)。

(目前是getsavebalanceamount(),我假设重命名以符合 Java 约定。)


我建议在命名 getter 和 setter 时使用一致的 CamelCase,例如 ,getInterestAmount()getSaveAccountNo()

我建议不要评论简单的 getter/setter,但如果这样做,请使用 Javadoc 约定,例如:

/** Returns current savings account balance. */
public double getSaveBalance() { ... etc ... }

我还建议避免不必要的括号,就像目前在你的吸气剂中一样,例如:

public double getSaveBalance() {
    return saveBalance; // No parens required.
}
于 2012-01-14T19:19:56.897 回答
2

我建议你做这样的事情,

interface Account{
  int getAccountNumber();
  float getBalance();
}

public class SavingAccount implements Account, Interest{
  int accountNumber;
  public int getAccountNumber(){
    return accountNumber;
  }
  float balance;
  public float getBalance(){
    return balance;
  }
  float savingInterestRate;
  public float getInterestRate(){
     return savingInterestRate;
  }
}

public class CheckingAccount implements Account, OverDraft{
  int accountNumber;
  public int getAccountNumber(){
  return accountNumber;
  }
  float balance;
  public float getBalance(){
    return balance;
  }
}

interface Interest{
  float getInterestRate();
}

interface OverDraft{
....
}
于 2012-01-14T19:38:18.547 回答