-3

I'm new to Java and object oriented programming. I've tried to find the solution myself, but couldn't manage to solve this problem.

I have two duplicated pieces of code which I need to remove. As I understand it, I have to create a super class for it. How can I create a super class or method in order to remove the duplicated code?:

public class PartnerOneService {

 public static void updatePartner(Handle handle, Long mandant, Long rID, Integer roleCd, Long lebId, String user) throws ResourceNotFoundException {
     // 1: Check if partner already exists
            Role roleInDb = roleSql.readActive(mandant, rID, roleCd);
    
            if (roleInDb == null) {
                // 2: Insert new partner 
                Process process = processCheck.get(0);
                Role role = createRole(mandant, roleCd, rID, process.getProcessId(), process.getProcessSubId(), lebId);
                roleVersioning.insert(role, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
            } else {
                // 3: Update partner 
                roleInDb.setPartnerId(lebId);
                roleVersioning.insert(roleInDb, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
            }
}
}

public class PartnerTwoService {
 public static void updatePartner(Handle handle, Long mandant, Long rID, Integer roleCd, Long lebId, String user) throws ResourceNotFoundException {
     // 1: Check if partner already exists
            Role roleInDb = roleSql.readActive(mandant, rID, roleCd);
    
            if (roleInDb == null) {
                // 2: Insert new partner 
                Process process = processCheck.get(0);
                Role role = createRole(mandant, roleCd, rID, process.getProcessId(), process.getProcessSubId(), lebId);
                roleVersioning.insert(role, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
            } else {
                // 3: Update partner 
                roleInDb.setPartnerId(lebId);
                roleVersioning.insert(roleInDb, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
            }
}
}
dgr379
  • 353
  • 3
  • 13
  • 1
    What is your question? – QBrute Oct 06 '22 at 09:42
  • 1
    the code above will never compile, since you have statements outside of a block, method or constructor. First try to get compilable code before trying to improve it – Stultuske Oct 06 '22 at 09:43
  • Since `updatePartner` is a public, static method, you can simply call it like so: `PartnerOneService.updatePartner( ... )`. (Replace `...` with relevant method arguments.) Therefore you can safely remove the `updatePartner` method from class `PartnerTwoService`. – Abra Oct 06 '22 at 09:57
  • So you are new to Java but are directly working with complex topics that are indicated here, e.g. db access, versioning, services etc.? Why don't you grab a Java tutorial first and work your way through at least the polymorphism/inheritance portion? – Thomas Oct 06 '22 at 10:02
  • @Thomas I am a frontend person but have to do some backend stuff. – dgr379 Oct 06 '22 at 12:47

1 Answers1

1

You can create third service like RolePartnerService and put this code in where. Then you need to set roles partner you can call it like

rolePartnerService.setRole(mandant, rID, roleCd); //and whatever arguments you need
Ekaterina
  • 26
  • 1