Skip to content

Commit

Permalink
Create Footballer Service
Browse files Browse the repository at this point in the history
  • Loading branch information
tzi-ipt committed Aug 8, 2024
1 parent 91611aa commit 8924a27
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 129 deletions.
64 changes: 64 additions & 0 deletions src/main/java/ch/ipt/see/git/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ch.ipt.see.git;

import java.util.Arrays;

public class QuickSort {

// *----------------------------------------------------------------*
// | All exercises have to be done without the use of the git cli! |
// | Do not push any of your results to origin master |
// *----------------------------------------------------------------*

// 1. Add a piece of code and commit the results into your local master. Use the IntelliJ Commit UI for this task.
// 2. Create two git branches from your local master. Modify the same lines of code on both branches. Merge your branches into your local master branch and use the merge dialog from intelliJ to resolve the conflict.
// 3. Reset your local master to the commit on the remote
// 4. Create a local branch (name: feat/see_yyyymmdd_ipt-kürzel) and add the patch file
// 5. Push the branch to the repo and create a merge request
// 6. Optional: Change some lines of Code without committing them. Stash those changes and explore the git stash menu.

public static void main(String[] args) {

int[] numberArray = new int[]{1, 43, 5, 7, 2};
System.out.println("Initial list of numbers: ");
printArray(numberArray);
System.out.println("Sorting numbers...");
quickSort(numberArray, 0, numberArray.length - 1);
System.out.println("Result: ");

printArray(numberArray);
}

public static void printArray(int[] arr) {
Arrays.stream(arr).forEach(System.out::println);
}

public static void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);

quickSort(arr, begin, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, end);
}
}

private static int partition(int[] arr, int begin, int end) {
int pivot = arr[end];
int i = (begin - 1);

for (int j = begin; j < end; j++) {
if (arr[j] <= pivot) {
i++;

int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}

int swapTemp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = swapTemp;

return i + 1;
}
}
4 changes: 0 additions & 4 deletions src/main/java/ch/ipt/see/playground/Greeting.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/ch/ipt/see/playground/HelloController.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/ch/ipt/see/playground/PlaygroundApplication.java

This file was deleted.

68 changes: 68 additions & 0 deletions src/main/java/ch/ipt/see/refactoring/Footballer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch.ipt.see.refactoring;

import java.security.MessageDigest;

public class Footballer {

// *----------------------------------------------------------------*
// | All exercises have to be done using the shortcut on the |
// | IntelliJ cheat sheet (see generate code). |
// *----------------------------------------------------------------*
//
// https://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf
//
// 1. Generate Getters for the given attributes of this class.
// 2. Generate Setters for the given attributes of this class.
// 3. Generate two different constructors for this class.
// 4. Generate the hashcode and the equals method for this class.
// 5. Generate a unit test for this class and let it run.

private String lastname;
private String firstname;
private boolean isBlockedByLaw;
private Integer age;
private String healthyStatus; // might be HEALTHY or NOT_HEALTHY

public Footballer(String firstname, String lastname, int age) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}

public int getAge() {
return age;
}

public String getFirstname() {
return firstname;
}

public String getLastname() {
return lastname;
}

@Override
public String toString() {
return "Footballer{" +
"lastname='" + lastname + '\'' +
", firstname='" + firstname + '\'' +
", isBlockedByLaw=" + isBlockedByLaw +
", age=" + age +
", healthyStatus='" + healthyStatus + '\'' +
'}';
}

//most advanced hash ever
@Override
public int hashCode() {
try {
MessageDigest md1 = MessageDigest.getInstance("SHA");
return md1.digest(this.toString().getBytes(), 1, 100);
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
System.out.println(e);
}
return 0;
}
}
68 changes: 68 additions & 0 deletions src/main/java/ch/ipt/see/refactoring/FootballerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch.ipt.see.refactoring;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class FootballerService {

// *----------------------------------------------------------------*
// | All exercises have to be done using the shortcut on the |
// | IntelliJ cheat sheet (see generate code). |
// *----------------------------------------------------------------*
//
// https://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf
//
// 1. Rename this file to another name.
// 2. Extract a variable.
// 3. Extract at least one parameter from a method in this class.
// 4. Extract a field.
// 5. Extract a method.
// 6. Extract a constant.

List<Footballer> allFootballers = new ArrayList<Footballer>() {{
add(new Footballer("Hansi", "Hinterseeeeher", 64));
add(new Footballer("Guido", "Maria Kretschmar", 51));
add(new Footballer("Wendy", "Holdi", 27));
add(new Footballer("Dark", "Forster", 38));
}};

private String formatName(String firstName, String lastName) {
return firstName + " " + lastName;
}

public List<Footballer> getAllFullAgedFootballer() {
return allFootballers.stream().filter(f -> f.getAge() >= 18).collect(Collectors.toList());
}

public List<String> getNames() {

return allFootballers.stream()
.filter(f -> f.getAge() >= 18)
.collect(Collectors.toList())
.stream()
.map(footballer -> formatName(footballer.getFirstname(), footballer.getLastname()))
.collect(Collectors.toList());
}

public Double calculateAverageAge(List<Footballer> footballers) {
return footballers
.stream()
.mapToDouble(Footballer::getAge)
.average()
.orElse(Double.NaN);
}

// Does some advanced searching nobody will ever understand
public List<String> searchNames(String name) {
Pattern pattern = Pattern.compile("(" + name + "+)+");

return allFootballers.stream()
.filter(f -> pattern.matcher(f.getLastname()).matches())
.collect(Collectors.toList())
.stream()
.map(footballer -> formatName(footballer.getFirstname(), footballer.getLastname()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ch.ipt.see.refactoring;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FootballerServiceSolution {

public static final int AGE_REQUIREMENT = 18;
List<Footballer> allFootballers = new ArrayList<Footballer>() {{
add(new Footballer("Hansi", "Hinterseeeeher", 64));
add(new Footballer("Guido", "Maria Kretschmar", 51));
add(new Footballer("Wendy", "Holdi", 27));
add(new Footballer("Dark", "Forster", 38));
}};

private String formatName(String firstName, String lastName) {
return firstName + " " + lastName;
}

public List<Footballer> getAllFullAgedFootballer() {
return allFootballers.stream().filter(f -> isAdult(f)).collect(Collectors.toList());
}

// using extract method
// using extract constant
private boolean isAdult(Footballer f) {
return f.getAge() >= AGE_REQUIREMENT;
}

public List<String> getNames() {

// using extract variable
List<Footballer> collect = allFootballers.stream()
.filter(f -> isAdult(f))
.collect(Collectors.toList());
return collect
.stream()
.map(footballer -> formatName(footballer.getFirstname(), footballer.getLastname()))
.collect(Collectors.toList());
}
}
41 changes: 41 additions & 0 deletions src/main/java/ch/ipt/see/testing/Footballer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ch.ipt.see.testing;

public class Footballer {

private String lastname;
private String firstname;
private boolean isBlockedByLaw;
private Integer age;
private String healthyStatus; // might be HEALTHY or NOT_HEALTHY

public Footballer(String firstname, String lastname, Integer age) {
this.firstname = firstname;
this.lastname = lastname;
age = age;
}

public boolean isBlockedByLaw() {
return isBlockedByLaw;
}

public Integer getAge() {
return age;
}

public String getHealthyStatus() {
return healthyStatus;
}

public boolean isInjured() {
return !"HEALTHY".equals(healthyStatus);
}

public String getFirstname() {
return firstname;
}

public String getLastname() {
return lastname;
}

}
12 changes: 12 additions & 0 deletions src/main/java/ch/ipt/see/testing/FootballerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ch.ipt.see.testing;

import java.util.List;

public class FootballerController {
private FootballerService footballerServiceInterface = new FootballerService();

public List<String> getNames() {
return footballerServiceInterface.getNames();
}

}
33 changes: 33 additions & 0 deletions src/main/java/ch/ipt/see/testing/FootballerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ch.ipt.see.testing;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FootballerService {

List<Footballer> allFootballers = new ArrayList<Footballer>() {{
add(new Footballer("Hansi", "Hinterseeeeher", 64));
add(new Footballer("Guido", "Maria Kretschmar", 51));
add(new Footballer("Wendy", "Holdi", 27));
add(new Footballer("Dark", "Forster", 38));
}};

private String formatName(String firstName, String lastName) {
return firstName + " " + lastName;
}

public List<Footballer> getAllFullAgedFootballer() {
return allFootballers.stream().filter(f -> f.getAge() >= 18).collect(Collectors.toList());
}

public List<String> getNames() {
return allFootballers.stream()
.filter(f -> f.getAge() >= 18)
.collect(Collectors.toList())
.stream()
.map(footballer -> formatName(footballer.getFirstname(), footballer.getLastname()))
.collect(Collectors.toList());
}

}
12 changes: 12 additions & 0 deletions src/main/java/ch/ipt/see/testing/FootballerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ch.ipt.see.testing;

public class FootballerUtil {

public static boolean canPlay(Footballer footballer) {
return isHealthy(footballer) && !footballer.isBlockedByLaw();
}

public static boolean isHealthy(Footballer footballer) {
return footballer.getHealthyStatus().equals("HEALTHY");
}
}
Loading

0 comments on commit 8924a27

Please sign in to comment.