Inheritance The Customer class is the superclass. The CheckingAccount and LoanAc
ID: 3920519 • Letter: I
Question
Inheritance
The Customer class is the superclass. The CheckingAccount and LoanAccount are the subclasses. They inherit all the properties and methods of the Customer super class by using the extends keyword. For example:
class CheckingAccount extends Customer
{
How to Declare an ArrayList of Objects
In your main program, declare two ArrayLists - one for CheckingAccount and one for LoanAccount. You can use the data below to get started.
ArrayList<CheckingAccount> Check = new ArrayList<CheckingAccount>();
Check.add(new CheckingAccount("Kirk","David","dkirk@dcccd.edu",10000.0));
Check.add(new CheckingAccount("Kirk","James","tribbles@starfleet.gov",2500.0));
Check.add(new CheckingAccount("Scott","Hulu","hulu@aol.com",2500.0));
ArrayList<LoanAccount> Loan = new ArrayList<LoanAccount>();
Loan.add(new LoanAccount("Zeus","Moses","moses@love.com",5000));
Loan.add(new LoanAccount("Einstein","Amy","aeinstein@starfleet.gov",1000));
Loan.add(new LoanAccount("Galileo","Julius","aeinstein@starfleet.gov",500));
Printing all Accounts
To print all accounts you can use a for loop to step through each ArrayList element. The following example prints outs the last name of each checking account:
for (int x=0; x<Check.size(); x++)
{
System.out.println( Check.get(x).GetLName() + " " + Check.get(x).GetFName());
}
Retrieving (static) Bank Information
For the static variables, remember that you can use any of the array elements and either the Check class or Loan class - they all point to the same variable. The following two lines give you the same answer:
System.out.println( Check.get(0).GetBankBalance() );
System.out.println( Loan.get(0).GetBankBalance() );
if you can code it in textpad and answer that will be awesome.
Customer Class Variable Names protected String EName, IName protected String Email protected int CuscomexTransactions Variable Description Customer's first and last name Customer's e-mail address The total number of transactions (deposits and withdrawals) made by the customer protected static double EankBalance The bank's total balance (static). You should change this variable when customers make deposits, withdrawals, take loans, and make loan payments The bank's total number of transactions (static). Increment this variable every time a customer makes a deposit, withdrawal, takes a loan, or makes a loan protected static int BankTransactions yment protected static int NumberCustomers The total number of customers at the bank (static Methods Names GetFName (SetEName ) GetLName), SetLName() GetEmail (), SetEmail() Methods Description Get and Set methods GetCustomerTransactions () GetBankBalance () GetBankTransactions () GetNumberCustomers () CheckingAccount Class Variable Names private double CheckingBalance private boolean OverdraftHistory Methods Names CheckingAccount (String theLName, String theFName, String theEmail, double OpeningDeposit) Variable Description The customer's checking account balance Set to true if the customer has ever overdrafted Methods Description The constructor should (1) initialize the name and email variables, (2) set CheckingBalance to OpeningDeposit, (3) add the OpeningDeposit to the BankBalance, (4) increment BankTransactions, (5) increment NumherCustomersExplanation / Answer
import java.util.ArrayList;
//Customer class
class Customer{
protected String Fname,Lname;
protected String Email;
protected int CustomerTransactios;
protected static double BankBalance;
protected static int BankTransactions;
protected static int NumberCustomers;
public void SetFname(String name) {
Fname=name;
}
public void SetLname(String name) {
Lname=name;
}
public void SetEmail(String mail) {
Email=mail;
}
public String GetFname() {
return Fname;
}
public String GetLname() {
return Lname;
}
public String GetEmail() {
return Email;
}
public int GetCustomerTransactios() {
return CustomerTransactios;
}
public double GetBankBalance() {
return BankBalance;
}
public int GetNumberCustomers() {
return NumberCustomers;
}
public int GetBankTransactions() {
return BankTransactions;
}
}
//Checking Account class
class CheckingAccount extends Customer{
private double CheckingBalance;
private boolean OverdraftHistory;
public CheckingAccount(String theLName,String theFName,String theEmail,double OpeningDeposit) {
Fname=theFName;
Lname=theLName;
Email=theEmail;
CheckingBalance=OpeningDeposit;
BankBalance+=OpeningDeposit;
BankTransactions++;
NumberCustomers++;
}
public double GetCheckingBalance() {
return CheckingBalance;
}
public boolean GetOverdraftHistory() {
return OverdraftHistory;
}
public void Deposit(double Amount) {
BankBalance+=Amount;
}
public void withdraw(double Amount) {
if(Amount>BankBalance) {
BankBalance-=25;
OverdraftHistory=true;
}
else BankBalance-=Amount;
BankTransactions++;
}
}
class LoanAccount extends Customer{
private double LoanBalance;
public LoanAccount(String theLName,String theFName,String theEmail,double OpeningLoan) {
Fname=theFName;
Lname=theLName;
Email=theEmail;
LoanBalance=OpeningLoan*(0.25);
BankBalance-=OpeningLoan;
BankTransactions++;
NumberCustomers++;
}
public double GetLoanBalance() {
return LoanBalance;
}
public void MakeLoan(double Amount) {
LoanBalance+=Amount*(1.25);
BankBalance-=Amount;
BankTransactions++;
}
public void MakePayment(double Amount) {
LoanBalance-=Amount;
if(LoanBalance==0)System.out.println(Fname+" "+Lname+" just payed off Loan !");
BankBalance+=Amount;
BankTransactions++;
}
}
public class Account {
public static void main(String args[] ) {
ArrayList<CheckingAccount> Check = new ArrayList<CheckingAccount>();
Check.add(new CheckingAccount("Kirk","David","dkirk@dcccd.edu",10000.0));
Check.add(new CheckingAccount("Kirk","James","tribbles@starfleet.gov",2500.0));
Check.add(new CheckingAccount("Scott","Hulu","hulu@aol.com",2500.0));
ArrayList<LoanAccount> Loan = new ArrayList<LoanAccount>();
Loan.add(new LoanAccount("Zeus","Moses","moses@love.com",5000));
Loan.add(new LoanAccount("Einstein","Amy","aeinstein@starfleet.gov",1000));
Loan.add(new LoanAccount("Galileo","Julius","aeinstein@starfleet.gov",500));
for (int x=0; x<Check.size(); x++)
{
System.out.println( Check.get(x).GetLname() + " " + Check.get(x).GetFname());
}
for (int x=0; x<Loan.size(); x++)
{
System.out.println( Loan.get(x).GetLname() + " " + Loan.get(x).GetFname());
}
System.out.println( Check.get(0).GetBankBalance() );
System.out.println( Loan.get(0).GetBankBalance() );
}
}