title: How can i outsource the code of 2 classes into a new one, so that both classes can call the method
body: <p>I have 2 classes which are doing basically the same. They both generate a list with Person opjects that are filled with data. DKKRPerson inherits from GKVPerson and contains even more data than a GKVPerson. My goal is to outsource the code of the classes to a new one, so that both classes can call the method without writing the code twice.</p>
  <p>Here is class Nr. 1:</p>
  <pre><code>public class GenerateGkvPerson {
  
  Random randomItem = new Random();
  
  public List&lt;GKVPerson&gt; generatedGkvPersons(List&lt;PersonFirstName&gt; personFirstNames, List&lt;Street&gt; streets, List&lt;PersonLastName&gt; personLastNames, List&lt;City&gt; cities) {
  
      List&lt;GKVPerson&gt; gkvPersons = new ArrayList&lt;&gt;();
  
      Scanner in = new Scanner(System.in);
      System.out.printf(&quot;Number of GKVPersons, who will be generated: &quot;);
      int numberOfRecords = in.nextInt();
  
      for (int i = 0; i &lt; numberOfRecords; i++) {
          int matching = randomItem.nextInt(personFirstNames.size());
          int matchingBirthData = randomItem.nextInt(cities.size());
          int matchingCityZipCode = randomItem.nextInt(cities.size());
          int birthYear = generateRandomNumber(1973, 2021);
  
          GKVPerson person = new GKVPerson();
  
          person.setId(i + 1);
          person.setFirstName(personFirstNames.get(matching).getFirstName());
          person.setGender(personFirstNames.get(matching).getGender());
          person.setLastName(personLastNames.get(randomItem.nextInt(personLastNames.size())).getLastName());
          person.setFirstBirthName(personFirstNames.get(matching).getFirstName());
          person.setLastBirthName(personLastNames.get(randomItem.nextInt(personLastNames.size())).getLastName());
          person.setBirthDate(generateBirthDate() + birthYear);
          person.setBirthYear(birthYear);
          person.setLivingPlaceCityNow(cities.get(matchingCityZipCode).getCity());
          person.setLivingPlaceStreetNameNow(streets.get(randomItem.nextInt(streets.size())).getStreetName());
          person.setLivingPlaceHouseNumberNow(generateRandomNumber(1, 200));
          person.setLivingPlaceZipCodeNow(cities.get(matchingCityZipCode).getZipCode());
          person.setBirthPlaceCityName(cities.get(matchingBirthData).getCity());
          person.setBirthPlaceZipCode(cities.get(matchingBirthData).getZipCode());
  
          gkvPersons.add(person);
      }
      return gkvPersons;
  }
  
  public static int generateRandomNumber(int min, int max) {
      return (int) (Math.random() * (max - min)) + min;
  }
  
  public String generateBirthDate() {
      String birthDate = &quot;&quot;;
      int month = generateRandomNumber(1, 12);
      int day;
  
          if (month % 2 == 0) {
              day = generateRandomNumber(1, 30);
          } else if (month == 2) {
              day = generateRandomNumber(1, 28);
          } else {
              day = generateRandomNumber(1, 31);
          }
  
          if (month &gt;= 10 &amp;&amp; day &gt;= 10) {
              birthDate = day + &quot;.&quot; + month + &quot;.&quot;;
          } else if (month &lt; 10 &amp;&amp; day &lt; 10) {
              birthDate = &quot;0&quot; + day + &quot;.&quot; + &quot;0&quot; + month + &quot;.&quot;;
          } else if (month &lt; 10 &amp;&amp; day &gt;= 10) {
              birthDate = day + &quot;.&quot; + &quot;0&quot; + month + &quot;.&quot; ;
          } else if (month &gt;= 10 &amp;&amp; day &lt; 10) {
              birthDate = &quot;0&quot; + day + &quot;.&quot; + month + &quot;.&quot;;
          }
  
      return birthDate;
  }
  </code></pre>
  <p>and here is class Nr. 2:</p>
  <pre><code>public class GenerateDkkrPerson {
  
  Random randomItem = new Random();
  
  public List&lt;DKKRPerson&gt; generateDkkrPerson(List&lt;PersonFirstName&gt; personFirstNames, List&lt;Street&gt; streets, List&lt;PersonLastName&gt; personLastNames, List&lt;City&gt; cities) {
  
      List&lt;DKKRPerson&gt; dkkrPersons = new ArrayList&lt;&gt;();
  
      Scanner in = new Scanner(System.in);
      System.out.printf(&quot;Number of DKKRPersons, who will be generated: &quot;);
      int numberOfRecords = in.nextInt();
  
      for(int i = 0; i &lt; numberOfRecords; i++) {
          int matching = randomItem.nextInt(personFirstNames.size());
          int matchingBirthData = randomItem.nextInt(cities.size());
          int matchingCityZipCode = randomItem.nextInt(cities.size());
          int birthYear = generateRandomNumber(1973, 2021);
          int matchingDiagnoseCity = randomItem.nextInt(cities.size());
          int matchingParentsCityData = randomItem.nextInt(cities.size());
          int matchingParentsStreetData = randomItem.nextInt(streets.size());
          int matchingParentsHouseNumber = generateRandomNumber(1, 200);
  
          DKKRPerson dkkrPerson = new DKKRPerson();
  
          dkkrPerson.setId(i + 1);
          dkkrPerson.setFirstName(personFirstNames.get(matching).getFirstName());
          dkkrPerson.setGender(personFirstNames.get(matching).getGender());
          dkkrPerson.setLastName(personLastNames.get(randomItem.nextInt(personLastNames.size())).getLastName());
          dkkrPerson.setFirstBirthName(personFirstNames.get(matching).getFirstName());
          dkkrPerson.setLastBirthName(personLastNames.get(randomItem.nextInt(personLastNames.size())).getLastName());
          dkkrPerson.setBirthDate(generateBirthDate() + birthYear);
          dkkrPerson.setBirthYear(birthYear);
          dkkrPerson.setLivingPlaceCityNow(cities.get(matchingCityZipCode).getCity());
          dkkrPerson.setLivingPlaceStreetNameNow(streets.get(randomItem.nextInt(streets.size())).getStreetName());
          dkkrPerson.setLivingPlaceHouseNumberNow(generateRandomNumber(1, 200));
          dkkrPerson.setLivingPlaceZipCodeNow(cities.get(matchingCityZipCode).getZipCode());
          dkkrPerson.setBirthPlaceCityName(cities.get(matchingBirthData).getCity());
          dkkrPerson.setBirthPlaceZipCode(cities.get(matchingBirthData).getZipCode());
          dkkrPerson.setDiagnoseStreetName(streets.get(randomItem.nextInt(streets.size())).getStreetName());
          dkkrPerson.setDiagnoseHouseNumber(generateRandomNumber(1, 200));
          dkkrPerson.setDiagnoseCityName(cities.get(matchingDiagnoseCity).getCity());
          dkkrPerson.setDiagnoseZipCode(cities.get(matchingDiagnoseCity).getZipCode());
          dkkrPerson.setParentsStreetName(streets.get(matchingParentsStreetData).getStreetName());
          dkkrPerson.setParentsHouseNumber(matchingParentsHouseNumber);
          dkkrPerson.setParentsCityName(cities.get(matchingParentsCityData).getCity());
          dkkrPerson.setParentsZipCode(cities.get(matchingParentsCityData).getZipCode());
          dkkrPerson.setMotherStreetName(streets.get(matchingParentsStreetData).getStreetName());
          dkkrPerson.setMotherHouseNumber(matchingParentsHouseNumber);
          dkkrPerson.setMotherCityName(cities.get(matchingParentsCityData).getCity());
          dkkrPerson.setMotherZipCode(cities.get(matchingParentsCityData).getZipCode());
          dkkrPerson.setFatherStreetName(streets.get(matchingParentsStreetData).getStreetName());
          dkkrPerson.setFatherHouseNumber(matchingParentsHouseNumber);
          dkkrPerson.setFatherCityName(cities.get(matchingParentsCityData).getCity());
          dkkrPerson.setFatherZipCode(cities.get(matchingParentsCityData).getZipCode());
  
          dkkrPersons.add(dkkrPerson);
      }
      return dkkrPersons;
  }
  
  public static int generateRandomNumber(int min, int max) {
      return (int) (Math.random() * (max - min)) + min;
  }
  
  public String generateBirthDate() {
      String birthDate = &quot;&quot;;
      int month = generateRandomNumber(1, 12);
      int day;
  
      if (month % 2 == 0) {
          day = generateRandomNumber(1, 30);
      } else if (month == 2) {
          day = generateRandomNumber(1, 28);
      } else {
          day = generateRandomNumber(1, 31);
      }
  
      if (month &gt;= 10 &amp;&amp; day &gt;= 10) {
          birthDate = day + &quot;.&quot; + month + &quot;.&quot;;
      } else if (month &lt; 10 &amp;&amp; day &lt; 10) {
          birthDate = &quot;0&quot; + day + &quot;.&quot; + &quot;0&quot; + month + &quot;.&quot;;
      } else if (month &lt; 10 &amp;&amp; day &gt;= 10) {
          birthDate = day + &quot;.&quot; + &quot;0&quot; + month + &quot;.&quot; ;
      } else if (month &gt;= 10 &amp;&amp; day &lt; 10) {
          birthDate = &quot;0&quot; + day + &quot;.&quot; + month + &quot;.&quot;;
      }
  
      return birthDate;
  }
  </code></pre>
  <p>Maybe someone of you can help me</p>