Create a java class for Bank Accounts with these public String attributes: ownerName, acctNbr and these private double attributes: debits, credits. Create a constructor that accepts an owner name and bank account number. It should set debits and credits to 0. Create 3 public methods: addDebit() that accepts a double and adds that amount to the debits addCredit() that accepts a double and adds that amount to the credits getBalance() that returns the credits minus the debits

Respuesta :

Answer:

public class BankAccounts

 {

   public String ownerName;

   public String acctNbr;

   private double debits;

   private double credits;

   public BankAccounts(String ownerNm, String acNum)

  {

    debits = 0;

    credits = 0;

    ownerName = ownerNm;

    acctNbr = acNum;

  }

public void addDebit(double debitAmt)

  {

    if(credits > (debits + debitAmt))

      debits = debits + debitAmt;

    else

      System.out.println("Insufficient balance");

}

   public void addCredit(double creditAmt)

  {

    credits = credits + creditAmt;

  }

public double getBalance()

  {

    double balance = 0;

    if((credits - debits) > 0)   // return the value if there is actually some balance left.

      balance = credits - debits;

    return balance;   //returns 0 if credits are lesser or equal to debits

  }

 }

Explanation:

Here class name is BankAccounts which can be used to create a new bank account whenever any user wants to open a bank account. This class will help to track any new account opening, debits and credits operations carried out on that account.

We have a constructor here, which accepts 2 arguments i.e. Bank account Owner Name and Account Number. The constructor also sets the values of debits and credits as 0.

The methods addDebit(), addCredit() and getBalance() help to perform Debit and Credit operations on the account.

NOTE: The method getBalance() returns a positive value only when credits are greater than debits.

The method addDebits() can add debits only as the condition that the credits are greater than the resultant debits.

The code for class BankAccounts is hereby attached.

Ver imagen isyllus