Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.Mapper;

import com.example.entity.Customer;
import com.example.entity.Employee;

public class PersonFactory {

public Employee createEmployee() { return new Employee(); }
public Customer createCustomer() { return new Customer(); }
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.Mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import com.example.dto.PersonDTO;
import com.example.entity.Customer;
import com.example.entity.Employee;

@Mapper(componentModel = "default", uses = PersonFactory.class)
public interface PersonMapperFactory {
PersonMapperFactory INSTANCE = Mappers.getMapper(PersonMapperFactory.class);

@Mapping(target = "department", source = "department")
@Mapping(target = "salary", source = "salary")
Employee toEmployee(PersonDTO dto);

@Mapping(target = "customerId", source = "customerId")
@Mapping(target = "tier", source = "tier")
Customer toCustomer(PersonDTO dto);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.Mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ObjectFactory;
import org.mapstruct.factory.Mappers;

import com.example.dto.PersonDTO;
import com.example.entity.Customer;
import com.example.entity.Employee;
import com.example.entity.Person;

@Mapper
public interface PersonMapperInlineFactory {
PersonMapperInlineFactory INSTANCE = Mappers.getMapper(PersonMapperInlineFactory.class);

Person toPerson(PersonDTO dto);

@ObjectFactory
default Person createPerson(PersonDTO dto) {
if ("employee".equalsIgnoreCase(dto.getType())) return new Employee();
else if ("customer".equalsIgnoreCase(dto.getType())) return new Customer();
throw new IllegalArgumentException("Unknown type: " + dto.getType());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.dto;

public class PersonDTO {
private String name;
private int age;

// Employee-specific
private String department;
private double salary;

// Customer-specific
private String customerId;
private String tier;

// Type to decide subclass: "employee" or "customer"
private String type;

// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }

public int getAge() { return age; }
public void setAge(int age) { this.age = age; }

public String getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }

public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }

public String getCustomerId() { return customerId; }
public void setCustomerId(String customerId) { this.customerId = customerId; }

public String getTier() { return tier; }
public void setTier(String tier) { this.tier = tier; }

public String getType() { return type; }
public void setType(String type) { this.type = type; }
}




Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.entity;

public class Customer extends Person {
private String customerId;
private String tier;

public Customer() {}
public Customer(String name, int age, String customerId, String tier) {
super(name, age);
this.customerId = customerId;
this.tier = tier;
}

public String getCustomerId() { return customerId; }
public void setCustomerId(String customerId) { this.customerId = customerId; }
public String getTier() { return tier; }
public void setTier(String tier) { this.tier = tier; }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.entity;

public class Employee extends Person {
private String department;
private double salary;

public Employee() {}
public Employee(String name, int age, String department, double salary) {
super(name, age);
this.department = department;
this.salary = salary;
}

public String getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.entity;

public abstract class Person {

private String name;
private int age;

public Person() {}

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }


}