a) Create a class named BankAccount with fields that hold an account number, the
ID: 642385 • Letter: A
Question
a) Create a class named BankAccount with fields that hold an account number, the owner's name, and the account balance. Include a constructor that initializes each field to appropriate default values. Also include methods to get and set each of the fields. Include a method named deductMonthlyFee() that reduces the balance by $4.00. Include a static method named explainAccountPolicy() that explains that the $4 service fee will be deducted each month.
**Note--(provide your best shot in explaining why you would be taking away the money from them
Explanation / Answer
public class BankAccount {
int accountNumber;
String ownerName;
int accountBalance;
public BankAccount(int accountNumber, String ownerName, int accountBalance) {
super();
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.accountBalance = accountBalance;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public int getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(int accountBalance) {
this.accountBalance = accountBalance;
}
public void deductMonthlyFee()
{
this.setAccountBalance(this.getAccountBalance()-4);
}
public static void explainAccountPolicy()
{
System.out.println("According to polocy the service fee of $4 will be deducted each month");
}
@Override
public String toString() {
return "BankAccount [accountNumber=" + accountNumber + ", ownerName="
+ ownerName + ", accountBalance=" + accountBalance + "]";
}
public BankAccount() {
super();
}
}
import java.io.ObjectInputStream.GetField;
import java.util.Scanner;
public class TestBankAccount {
Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
TestBankAccount testBankAccount=new TestBankAccount();
BankAccount bankAccount1=testBankAccount.getData();
BankAccount bankAccount2=testBankAccount.getData();
BankAccount bankAccount3=testBankAccount.getData();
BankAccount bankAccount4=new BankAccount();
BankAccount.explainAccountPolicy();
testBankAccount.showValues(bankAccount1);
testBankAccount.showValues(bankAccount2);
testBankAccount.showValues(bankAccount3);
testBankAccount.showValues(bankAccount4);
}
public BankAccount getData()
{
System.out.println("enter Account number Owner's Name Account Balance");
int accountNumber=sc.nextInt();
String ownerName=sc.next();
int accountBalance=sc.nextInt();
return new BankAccount(accountNumber, ownerName, accountBalance);
}
public void showValues(BankAccount bankAccount)
{
System.out.println(bankAccount);
//or u can format as below
/*System.out.println("Account Number "+bankAccount.getAccountNumber()+"'s owner is "+bankAccount.getOwnerName()+" having balance "+bankAccount.getAccountBalance());*/
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.