【问题标题】:I could not understand what my homework says我不明白我的作业说什么
【发布时间】:2020-08-27 14:46:16
【问题描述】:

预计Account类,其中User对象,余额,最小余额 限制、帐号、iban 号码信息被保留,以及所有访问者和变更者 创建此类的方法。此外,您应该开发一种方法, 在此类中执行 eft 交易,并将接收方帐户对象作为 范围。您还应该创建一个执行余额检查的方法 需要执行 Eft 交易。

我的一部分作业希望我创建上面留下的两种方法。我创建了方法,但我不知道它是否足以工作。请你检查一下好吗? 你不明白,因为我希望你写实际的代码,请告诉我我哪里错了。谢谢你现在

public class Account {
private User user;
private int balance;
private int minBalanceLim;
private String accNum;
private String IBAN;

public User getUser() {
    return user;
}

public int getBalance() {
    return balance;
}

public int getMinBalanceLim() {
    return minBalanceLim;
}

public String getAccNum() {
    return accNum;
}

public String getIBAN() {
    return IBAN;
}

public void setUser(User user) {
    this.user = user;
}

public void setBalance(int balance) {
    this.balance = balance;
}

public void setMinBalanceLim(int minBalanceLim) {
    this.minBalanceLim = minBalanceLim;
}

public void setAccNum(String accNum) {
    this.accNum = accNum;
}

public void setIBAN(String IBAN) {
    this.IBAN = IBAN;
}



public void eft(Account receiver, Account sender, int amount)
{
   if(sender.balanceCheck(sender, amount))
   {
       sender.setBalance(sender.getBalance()-amount);
       receiver.setBalance(receiver.getBalance()+amount);
   }
   else
   {
       System.out.println(sender.getUser().getName() + " has not enough money");
   }
}
public boolean balanceCheck(Account sender, int amount)
{
    return sender.getBalance()>= amount;
}

【问题讨论】:

  • eft的作用是什么?还有balanceCheck,它似乎比检查余额更多?在我看来,balanceCheck 应该接受两个参数,一个是 Account 类型,一个是 Integer 类型,并且应该返回 Boolean 类型,表明帐户是否有正确的资金。

标签: oop banking


【解决方案1】:
public boolean balanceCheck(int amount) {
  // so we changed it here, as you have the getMinBalanceLim it should be used
  return this.getBalance() - amount >= this.getMinBalanceLim();
}

public void etf(Account receiver, int amount) {
  if (this.balanceCheck(amount)) 
  {
    // set the balance of the receiver
    receiver.setBalance(receiver.getBalance() + amount);

    // set the balance of the user
    this.setBalance(this.getBalance() - amount);
  }
  else 
  {
    // handle your error (user has no money, he broke)
  }
}

然后您可以在尝试时引用该对象:

// ... inside your main method
a1.etf(a2, 23);

【讨论】:

  • 这就是我真正想要的。我不知道你提到的事情,例如 this.set ... 。我想了解更多关于这件事的信息,所以我如何谷歌呢?我很可能错过了一些关于“方法”的研究。谢谢大家的关注。
  • docs.oracle.com/javase/tutorial/java/javaOO/classvars.html 这可能会有所帮助,您正在寻找java 的类成员和属性。不过祝你好运!如果我的回答有效,您可以接受它作为可行的解决方案,这让其他人知道!一切顺利。
  • 发现这个网站需要一些时间。我接受了,谢谢你的问候:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
相关资源
最近更新 更多