oopbanking

I could not understand what my homework says


It is expected that the Account class, in which User object, balance, minimum balance limit, account number, iban number information is kept, and all accessor and mutator methods of this class are created. In addition, you are expected to develop a method that performs eft transactions within this class and takes the receiver account object as a parameter. You are also expected to create a method that performs the balance checks required to perform the Eft transaction.

A part of my homework wants from me to create two methods as I left above. I created methods but I don't know whether it is enough to work. Would you please do check? You please don't understand as I want from you to write actual code, please say me where I am wrong. Thanks for now

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;
}

Solution

  • 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)
      }
    }
    

    You can then reference the object as you're attempting to:

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