Thursday 18 February 2016

Java: Class Diagram & OneToOne Association Relationship.

Q: Create two classes and define one-to-one association relationship between them. Demonstrate it from "main" method.

//

Consider a person, in our case its Vai (Don) "Kopa Samsu".

( ͡° ͜ʖ ͡°)


// Profile
Name     : " Kopa Samsu "
Address : " D3, 22A Road, Dhaka, 1100 "

Mr. Kopa Samsu has only one Den that is the address specified in his profile.
Now, Create a Person Class and Address Class in our java project.


  • Person Class will contain firstName, lastName and personAddress attributes.
  • Address Class will contain houseNo, roadNo, postCode, district attributes.
  • Person Class will also use Calculator Class to find out the amount of Salami(money) to given to Vai.

  Class Diagram: One-to-One Association Relationship.


Address ClassPerson Class_1____________________>1



// Person Class CODE:


// ADLabs

public class Person {
 private String firstName;
 private String middleName;
 private String lastName;
 private Address personAddress;

 public void setPersonAddress(Address personAddress) {
  this.personAddress = personAddress;
 }

 public Address getPersonAddress() {
  return personAddress;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public Person(String firstName, String middleName, String lastName) {
  this(firstName, lastName);
  setMiddleName(middleName);
 }

 public Person(String firstName, String lastName) {
  this();
  setFirstName(firstName);
  setLastName(lastName);
 }

 public Person() {

 }

 public double getSalami() {
  double basic = 10000;
  double specialCase = 5000;
  Calculator aCalc = new Calculator();
  double total = aCalc.add(basic, specialCase);
  return total;
 }

 public String getMiddleName() {
  return middleName;
 }

 public void setMiddleName(String middleName) {
  this.middleName = middleName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getFullName() {
  String fullName = firstName + " " + lastName;
  return fullName;
 }
}



// Address Class CODE
:

// ADLabs
public class Address {
 private String houseNo;
 private String roadNo;
 private String district;
 private int postCode;

 public Address(String houseNo, String roadNo, String district, int postCode) {
  this.houseNo = houseNo;
  this.roadNo = roadNo;
  this.postCode = postCode;
  this.district = district;
 }

 public String getHouseNo() {
  return houseNo;
 }

 public void setHouseNo(String houseNo) {
  this.houseNo = houseNo;
 }

 public String getRoadNo() {
  return roadNo;
 }

 public void setRoadNo(String roadNo) {
  this.roadNo = roadNo;
 }

 public int getPostCode() {
  return postCode;
 }

 public void setPostCode(int postCode) {
  this.postCode = postCode;
 }

 public String getDistrict() {
  return district;
 }

 public void setDistrict(String district) {
  this.district = district;
 }

}



// Calculator Class:

// ADLabs

public class Calculator {
 public double add(double n1, double n2) {
  return n1 + n2;
 }

 public double subtract(double n1, double n2) {
  return n1 - n2;
 }
}


// main Class Code:

// ADLabs

public class StartPoint {

 public static void main(String[] args) {

  Person aPerson = new Person();
  aPerson.setFirstName("Kopa");
  aPerson.setLastName("Samsu");

  Address address1 = new Address("D3", "22A", "Dhaka", 1100);

  aPerson.setPersonAddress(address1);

  System.out.println("Go to this Address :\n");
  System.out.println("Name: " + aPerson.getFullName());
  System.out.println("House Number: " + aPerson.getPersonAddress().getHouseNo());
  System.out.println("Road No: " + aPerson.getPersonAddress().getRoadNo());
  System.out.println("District :" + aPerson.getPersonAddress().getDistrict() + "\n");
  System.out.println("Give Salami :");
  System.out.println("Salami : " + aPerson.getSalami());
 }

}


Happy Coding :)
-@D