First American Bank provides both normal bank services like checking accounts an
ID: 3920409 • Letter: F
Question
First American Bank provides both normal bank services like checking accounts and mortgages to its customers. Each customer who uses checking services has a checking account. Each customer who gets a mortgage loan from the bank has a mortgage account.
The bank executives ask a team of software analysts to develop a system to manage its customers' bank accounts.
Section A:
Define an abstract Java class named “BankAccount” in a package named “BANKACCOUNTS”. This class has three attributes:
(1) Acount number: an integer of 10 digits, defined as protected
(2) Account type: String (e.g.: "CHECKING" versus "MORTGAGE"), defined as protected
(3) Customer's full name: String, defined as protected
Class BankAccount definition provides
a default constructor
get-methods and set-methods for the variable attributes,
a public method, named "displayAccountData()", that prints out the following pieces of data: account number, account type, and customer's full name in only one line.
Section B:
Define a Java class named “CheckingAccount” in the same package, BANKACCOUNTS, that extends the class BankAccount.
Class CheckingAccount has only one attribute:
checking balance, that is defined as private.
Class CheckingAccount definition provides:
a default constructor,
another constructor that accepts account number and customer's full name as two different parameters
get-method and set-method for the attribute checking balance,
a public method (displayAccountData()) that overrides the method of the same name of the superclass BankAccount and prints out the following pieces of data in only one line:
account number,
account type,
customer's full name,
checking balance (only 2 digits after decimal point)
Section C:
Define a Java class named “MortgageAccount” in the same package, BANKACCOUNTS, that extends the above class BankAccount.
Class MortgageAccount has two attributes:
principal balance that is defined as private.
interest rate that is defined as private.
Class MortgageAccount definition provides:
a default constructor,
another constructor that accepts account number and customer's full name as two different parameters
get-method and set-method for eachvariable attribute
a public method (displayAccountData()) that overrides the method of the same name of the superclass BankAccount and prints out the following pieces of data in only one line:
account number,
account type,
customer's full name,
the principal balance (only 2 digits after decimal point),
the interest rate (only 2 digits after decimal point)
Section D:
Write a Java program that can read inputs of bank accounts and print out the account data. The account can be either a checking account or a mortgage account.
First, the program reads an input of a numeric account code that identifies which type of account that the user wants to work with. The values of account code are:
1 for checking account
2 for mortgage account
The program should check to be sure that the user enters correct account code. Otherwise, the program asks the user to re-enter the code until it is correctly done.
Then, for the checking account, the user enters the following pieces of data from the console:
account number,
customer's full name,
checking balance (only 2 digits after decimal point)
For the mortgage account, the user enters the following pieces of data from the console:
account number,
customer's full name,
principal balance
interest rate
The program should check to be sure that checking balance, principal balance, and interest rate, are not negative.
The Java program is another Java class named “AccountDisplayer” in the same package, i.e. BANKACCOUNTS. To provide a solution to the problem, it is expected that inheritance and polymorphism are used in the coding.
Explanation / Answer
BankAccount.java
public class BankAccount{
protected int accountNumber;
protected String accountType;
protected String customerName;
public BankAccount() {
}
public BankAccount(int accountNumber, String accountType, String customerName) {
super();
this.accountNumber = accountNumber;
this.accountType = accountType;
this.customerName = customerName;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String displayAccountData() {
return "BankAccount [accountNumber=" + accountNumber + ", accountType=" + accountType + ", customerName="
+ customerName + "]";
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount{
private double checkingBalance;
public CheckingAccount(int an, String cn) {
super.setAccountNumber(an);
super.setCustomerName(cn);
}
public CheckingAccount(int accountNumber, String accountType, String customerName) {
super(accountNumber, accountType, customerName);
// TODO Auto-generated constructor stub
}
public double getCheckingBalance() {
return checkingBalance;
}
public void setCheckingBalance(double checkingBalance) {
this.checkingBalance = checkingBalance;
}
public String displayAccountData() {
return "CheckingAccount [checkingBalance=" + checkingBalance + ", accountNumber=" + accountNumber
+ ", accountType=" + accountType + ", customerName=" + customerName + "]";
}
}
MortgageAccount.java
public class MortgageAccount extends BankAccount{
private double principalAmount, interestRate;
public MortgageAccount(int an, String cn) {
super.setAccountNumber(an);
super.setCustomerName(cn);
}
public MortgageAccount(int accountNumber, String accountType, String customerName) {
super(accountNumber, accountType, customerName);
// TODO Auto-generated constructor stub
}
public double getPrincipalAmount() {
return principalAmount;
}
public void setPrincipalAmount(double principalAmount) {
this.principalAmount = principalAmount;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public String displayAccountData() {
return "MortgageAccount [principalAmount=" + principalAmount + ", interestRate=" + interestRate
+ ", accountNumber=" + accountNumber + ", accountType=" + accountType + ", customerName=" + customerName
+ "]";
}
}
AccountDisplayer.java
import java.util.Scanner;
public class AccountDisplayer {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int accountCode;
BankAccount bc;
while(true) {
System.out.println("Enter the account code: ");
accountCode = in.nextInt();
if(accountCode == 1 || accountCode == 2)
break;
else
System.out.println("Enter correct Code:");
}
System.out.println("Enter the account number:");
int accountNumber = in.nextInt();
in.nextLine();
System.out.println("Enter the customer's full name:");
String customerName = in.nextLine();
int interestRate = 0;
double balance;
if(accountCode == 1) {
while(true) {
System.out.println("Enter the customer's checking balance:");
balance = in.nextDouble();
if(balance < 0)
System.out.println("Checking balance cannot be negative");
else
break;
}
CheckingAccount ca = new CheckingAccount(accountNumber, "CHECKING", customerName);
ca.setCheckingBalance(balance);
System.out.println(ca.displayAccountData());
}else {
while(true) {
System.out.println("Enter the principal balance:");
balance = in.nextDouble();
if(balance < 0)
System.out.println("Principal balance cannot be negative");
else
break;
}
while(true) {
System.out.println("Enter the interest:");
interestRate = in.nextInt();
if(interestRate < 0)
System.out.println("Interest Rate cannot be negative");
else
break;
}
MortgageAccount mc = new MortgageAccount(accountNumber, "MORTGAGE", customerName);
mc.setPrincipalAmount(balance);
mc.setInterestRate(interestRate);
System.out.println(mc.displayAccountData());
}
}
}
**Comment for any doubts
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.