Java Reference
In-Depth Information
}
Note that the
Address
class is a nested static class inside the
Person
class. The reason
is that the address field applies only to people. We use a standard trick to generate the IDs
of the people. We created a
static
variable that holds the next available ID. This variable
is initially zero. Every time a new person is created, the current value of the variable is
assigned for the ID and then the static variable is incremented by one.
Below is the code for the
Customer
class.
public class
Customer
extends
Person
{
private
ArrayList
<
BankAccount
>
accounts =
new
ArrayList
<>
() ;
public
Customer(String name, Person.Address address ,
long
phoneNumber
)
{
super
(name , address , phoneNumber) ;
}
public void
addBankAccount(BankAccount account )
{
accounts .add(account) ;
}
public
String toString()
{
return
"Customer: "
+
super
. toString()+
" Accounts: "
+accounts ;
}
}
A customer can have multiple bank accounts. Therefore, we associated an
ArrayList
of
bank accounts with every customer. Note that the
Person
class inherits from the interface
Serializable
because it inherits from the
Person
class and the
Person
class inherits from
the interface
Serializable
. Next, we show the code for the
Employee
class.
public class
Employee
extends
Person
{
private double
salary ;
public
Employee(String name, Person.Address address ,
long
phoneNumber
,
double
salary)
{
super
(name , address , phoneNumber) ;
this
. salary = salary ;
}
public
String toString()
{
return
"Employee: "
+
super
. toString()+
" salary: "
+salary ;
}
}
Similar to the
Customer
class, the
Employee
class inherits from the interface
Serializable
because inheritance is transitive. It remains to show the code for the
BankAccount
and
Transaction
classes.
public class
BankAccount
implements
Serializable
{
private static long
accountCounter = 0;
private long
accountNumber ;
private double
balance ;
private
ArrayList
<
Transaction
>
transactions =
new
ArrayList
<>
() ;
private
Customer customer ;
public
BankAccount(Customer customer )
{
this
.customer = customer;
accountNumber = accountCounter ;
accountCounter++;
}
public
BankAccount(
double
initialDeposit , Customer customer)
{