CIS 265
Summer 2011 – V. Matos
Review: Multiclass Application, Inheritance, Overriding, Overloading, Associations, Class Diagram.
packagecsu.matos;
publicclass Driver {
/**
* Author: Matos
* Date:July 5, 2011
* Goal:Exploring Classes-Subclasses. UML Class Diagram.
* Person-Agent-Client Mapping
*/
publicstaticvoid main(String[] args) {
//creating a Person object
Person p1 = newPerson("Maria Macarena", "01/05/1976");
System.out.println("PERSON \n"+ p1.showData() );
//creating Client objects (Client is a subclass of Person)
Client c1 = newClient("Charlie Brown", "1/1/1950", 111, 1000000);
System.out.println(c1.showData());
Client c2 = newClient("Linus van Pelt", "2/2/1950", 222, 2000000);
System.out.println(c2.showData());
Client c3 = newClient("Patricia Reichardt", "3/3/1950", 333, 3000000);
System.out.println(c3.showData());
//creating an Agent object (Agent is a subclass of Person)
Agent ag1 = newAgent("Charles Schulz", "11/26/1922", "216-123-4567");
//threading [1..many] association Agent-Client
ag1.addClient(c1);
ag1.addClient(c2);
ag1.addClient(c3);
//showing results
System.out.println(ag1.showData());
}//main
}
Note:
//Reminder: Formatting strings for 'nice' output
//String st = String.format("Name: %s Salary %6.2f", "Jose Cuervo", 1234.555);
//System.out.println(st);
//NumberFormatnf = NumberFormat.getCurrencyInstance();
//System.out.println(nf.format(123456.789));
PERSON CLASS
packagecsu.matos;
publicclass Person {
private String mPersonName;
private String mBdate;
public Person(String mPersonName, String mDdate) {
super();
this.mPersonName = mPersonName;
this.mBdate = mDdate;
}
public String getmPersonName() {
returnmPersonName;
}
publicvoidsetmPersonName(String mPersonName) {
this.mPersonName = mPersonName;
}
public String getmDdate() {
returnmBdate;
}
publicvoidsetmDdate(String mDdate) {
this.mBdate = mDdate;
}
public String showData() {
String result = "Name: " + this.getmPersonName()
+ " Birthdate: " + this.getmDdate();
return result;
}
}
AGENT CLASS
packagecsu.matos;
importjava.util.ArrayList;
publicclass Agent extends Person{
private String mAgentPhone;
privateArrayListclients;
public Agent(String mPersonName, String mDdate, String mAgentPhone ) {
super(mPersonName, mDdate);
this.mAgentPhone = mAgentPhone;
clients = newArrayList();
}
public String getmAgentPhone() {
returnmAgentPhone;
}
publicvoidsetmAgentPhone(String mAgentPhone) {
this.mAgentPhone = mAgentPhone;
}
publicArrayListgetClients() {
returnclients;
}
publicvoidsetClients(ArrayList clients) {
this.clients = clients;
}
publicvoidaddClient(Client mClient){
this.clients.add(mClient);
mClient.setmAgent(this);
}
public String showData(){
Client aClient;
String result = "\nAGENT " + super.showData();
for (inti=0; ithis.clients.size(); i++){
aClient = (Client) this.clients.get(i);
result += "\n" + aClient.showData();
}
return result;
}
}
CLIENT CLASS
packagecsu.matos;
publicclass Client extends Person{
privatelongmLifeInsurancePolicyNo;
privatedoublemLifeInsuranceCoverage;
private Agent mAgent;
public Client(String mPersonName, String mDdate, longpolicyNo, double coverage) {
super(mPersonName, mDdate);
this.mLifeInsurancePolicyNo = policyNo;
this.mLifeInsuranceCoverage = coverage;
}
public Agent getmAgent() {
returnmAgent;
}
publicvoidsetmAgent(Agent mAgent) {
this.mAgent = mAgent;
}
publiclonggetmLifeInsurancePolicyNo() {
returnmLifeInsurancePolicyNo;
}
publicvoidsetmLifeInsurancePolicyNo(longmLifeInsurancePolicyNo) {
this.mLifeInsurancePolicyNo = mLifeInsurancePolicyNo;
}
publicdoublegetmLifeInsuranceCoverage() {
returnmLifeInsuranceCoverage;
}
publicvoidsetmLifeInsuranceCoverage(doublemLifeInsuranceCoverage) {
this.mLifeInsuranceCoverage = mLifeInsuranceCoverage;
}
public String showData() {
String result = "\tCLIENT: " + super.showData()
+ " \tPolicy#: " + this.getmLifeInsurancePolicyNo()
+ " Coverage $" + this.getmLifeInsuranceCoverage();
if (this.mAgent != null){
result += " Agent: " + mAgent.getmPersonName();
}
return result;
}
}
CONSOLE - SCREEN SHOT
AGENT Name: Charles Schulz Birthdate: 11/26/1922
CLIENT: Name: Charlie Brown Birthdate: 1/1/1950 Policy#: 111 Coverage $1000000.0 Agent: Charles Schulz
CLIENT: Name: Linus van Pelt Birthdate: 2/2/1950 Policy#: 222 Coverage $2000000.0 Agent: Charles Schulz
CLIENT: Name: Patricia ReichardtBirthdate: 3/3/1950 Policy#: 333 Coverage $3000000.0 Agent: Charles Schulz
1