├── .gitignore
├── pom.xml
└── src
├── main
├── java
│ ├── Main.java
│ ├── collections
│ │ ├── ArraysExample.java
│ │ ├── MapsExamples.java
│ │ └── SetsExamples.java
│ ├── enums
│ │ ├── CountryEnum.java
│ │ ├── GenderEnum.java
│ │ └── RoleEnum.java
│ ├── functional
│ │ └── FunctionalExamples.java
│ ├── model
│ │ ├── Secretary.java
│ │ ├── Student.java
│ │ ├── Subject.java
│ │ ├── Teacher.java
│ │ └── common
│ │ │ ├── Citizen.java
│ │ │ └── Human.java
│ └── streams
│ │ └── StreamsExamples.java
└── resources
│ └── subjects.dat
└── test
└── java
├── ArraysTest.java
├── EnumTest.java
├── OopTest.java
├── StreamsTest.java
└── util
└── AccessModifierEnum.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 | #Intellij idea files
10 | *.iml
11 | .idea/
12 | # Compiled files
13 | target/
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | OOPExample-MIP
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 18
13 | 18
14 |
15 |
16 |
17 |
18 |
19 | org.junit.jupiter
20 | junit-jupiter-engine
21 | 5.8.2
22 | test
23 |
24 |
25 |
26 | org.projectlombok
27 | lombok
28 | 1.18.24
29 | provided
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/main/java/Main.java:
--------------------------------------------------------------------------------
1 | import streams.StreamsExamples;
2 |
3 | import java.io.*;
4 | import java.net.URISyntaxException;
5 |
6 | public class Main {
7 | public static void main(String[] args) throws IOException, URISyntaxException {
8 | /* Collections */
9 | // ArraysExample.examplePrimitives();
10 | // MapsExamples.exampleObjects();
11 | // SetsExamples.examplesHashSetPrimitives();
12 | // SetsExamples.examplesTreeSetPrimitives();
13 |
14 | /* Streams */
15 | StreamsExamples.streamCreation();
16 | StreamsExamples.intStreams();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/collections/ArraysExample.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.Comparator;
6 | import java.util.List;
7 |
8 | public class ArraysExample {
9 | /* Used for testes */
10 | private List integerArrayList;
11 |
12 | public ArraysExample() {
13 | integerArrayList = Arrays.asList(1,8,3,9,4,10,5);
14 | }
15 |
16 | public static void examplePrimitives() {
17 | System.out.println("\n========================= Arrays Primitives =========================");
18 | //Init the array via constructor
19 | List arrayList = Arrays.asList(2, 3, 1, 4, 10, 0);
20 |
21 | //CRUD operations
22 | //Create
23 | arrayList.add(25);
24 | arrayList.add(30);
25 | arrayList.add(20);
26 |
27 | //Read all elements
28 | System.out.println("Initial array :" + arrayList);
29 | //Initial array :[2, 3, 1, 4, 10, 0, 25, 30, 20]
30 | //As you can see the insert order is maintained
31 |
32 | //Read an element
33 | System.out.println("Element from index 1: " + arrayList.get(1));
34 | //Element from index 1: 3
35 | //First index is always 0, so index 1 is the second element
36 |
37 | //Update
38 | arrayList.set(0, 0); // update the element from first position to value zero
39 |
40 | //Delete
41 | arrayList.remove(8);
42 |
43 | System.out.println("Array Updated :" + arrayList);
44 | //Array Updated :[0, 3, 1, 4, 10, 0, 25, 30]
45 |
46 | //Arrays class functions
47 | arrayList.sort(Integer::compareTo);
48 |
49 | System.out.println("Array sorted asc:" + arrayList);
50 | //Array sorted asc:[0, 0, 1, 3, 4, 10, 25, 30]
51 |
52 | //Reverse the array using Collections and lambda expression
53 | arrayList.sort((first, second) -> {
54 | if (first > second)
55 | return -1;
56 | else return 0;
57 | }); // More examples in functional package
58 |
59 | System.out.println("Array sorted desc:" + arrayList);
60 | //Array sorted desc:[30, 25, 10, 4, 3, 1, 0, 0]
61 | }
62 |
63 | public Integer getFirst(){
64 | // your code here
65 | return null;
66 | }
67 |
68 | public Integer getLast(){
69 | // your code here
70 | return null;
71 | }
72 |
73 | public Integer getTheHighestNumber(){
74 | // your code here
75 | return null;
76 | }
77 |
78 | public Integer getTheLowestNumber(){
79 | // your code here
80 | return null;
81 | }
82 |
83 | /* There are many solutions for last ones, please watch for: Streams, Comparable, Comparator, Collections - with and without lambdas*/
84 | public List sortAsc(){
85 | // your code here
86 | return null;
87 | }
88 |
89 | public List sortDesc(){
90 | // your code here
91 | return null;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/java/collections/MapsExamples.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import model.Student;
4 | import enums.RoleEnum;
5 |
6 | import java.util.HashMap;
7 | import java.util.Iterator;
8 | import java.util.Map;
9 |
10 | public class MapsExamples {
11 | public static void exampleObjects() {
12 | System.out.println("\n========================= Maps Objects =========================");
13 |
14 | Student andrei = new Student();
15 | andrei.setForename("Andrei");
16 |
17 | Student marian = new Student();
18 | marian.setForename("Marian");
19 |
20 | Student ana = new Student();
21 | ana.setForename("Ana");
22 |
23 | HashMap roles = new HashMap* Student, RoleEnum*/>();
24 | roles.put(andrei, RoleEnum.USER);
25 | roles.put(marian, RoleEnum.ADMIN);
26 | roles.put(ana, RoleEnum.MODERATOR);
27 | roles.put(andrei, RoleEnum.MODERATOR);
28 |
29 | Student nullescu = new Student();
30 | nullescu.setForename("Nullescu");
31 |
32 | System.out.println("This is my map " + roles);
33 |
34 | System.out.println("GET SOMETHING NULL " + roles.get(nullescu));
35 |
36 | System.out.println("AGAIN - This is my map " + roles);
37 |
38 | //Iterate with an Iterator
39 | System.out.println("\nIterate with Iterator:");
40 | Iterator it = roles.entrySet().iterator();
41 | while (it.hasNext()){
42 | Map.Entry pair = (Map.Entry) it.next();
43 | System.out.println(pair);
44 | }
45 |
46 | //Iterate with a for
47 | System.out.println("\nIterate with for:");
48 | for (Map.Entry studentRoleEnumEntry : roles.entrySet()) {
49 | System.out.println(studentRoleEnumEntry);
50 | }
51 |
52 | //Iterate the map - lambda
53 | System.out.println("\nIterate with lambda function:");
54 | roles.forEach((key, value) -> {
55 | System.out.println(key + "=" + value);
56 | });
57 |
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/collections/SetsExamples.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import model.Student;
4 |
5 | import java.util.HashSet;
6 | import java.util.Set;
7 | import java.util.TreeSet;
8 |
9 | public class SetsExamples {
10 | public static void examplesHashSetPrimitives() {
11 | System.out.println("========================= HashSets with PRIMITIVES =========================");
12 | Set set1 = new HashSet<>();
13 | set1.add("Apple");
14 | set1.add("Orange");
15 | set1.add("Mango");
16 | set1.add("Pineapple");
17 | set1.add("Pear");
18 | set1.add("Mango");
19 | set1.add("Orange");
20 |
21 | System.out.println("An initial list of elements: " + set1);
22 |
23 | // Remove on specific element
24 | set1.remove("Pear");
25 | System.out.println("The new list of elements: " + set1);
26 |
27 | Set set2 = new HashSet<>();
28 | set2.add("Plum");
29 | set2.add("Dragon Fruit");
30 | set2.add("Pear");
31 | set1.add("Orange");
32 |
33 | set1.addAll(set2);
34 |
35 | //Removing elements on the basis of specified condition
36 | set1.removeIf(str -> str.contains("Mango"));
37 | System.out.println("After invoking removeIf() method: " + set1);
38 | }
39 |
40 | public static void examplesTreeSetPrimitives() {
41 | /* Auto sorted but slower */
42 | System.out.println("========================= TreeSets with PRIMITIVES =========================");
43 | Set set1 = new TreeSet<>();
44 | set1.add(5);
45 | set1.add(3);
46 | set1.add(9);
47 | set1.add(2);
48 | set1.add(6);
49 | set1.add(4);
50 | set1.add(3);
51 |
52 | System.out.println("Sorted elements with TreeSet " + set1);
53 |
54 | Set set2 = new HashSet<>();
55 | set2.add(3);
56 | set2.add(20);
57 | set2.add(8);
58 | set2.add(1);
59 | set2.add(3);
60 |
61 | System.out.println("Unordered elements with HashSet " + set2);
62 | }
63 |
64 | public static void examplesObjects() {
65 | Student andrei = new Student();
66 | andrei.setForename("Andrei");
67 |
68 | Student marian = new Student();
69 | marian.setForename("Marian");
70 |
71 | Student ana = new Student();
72 | ana.setForename("Ana");
73 |
74 | Set set = new HashSet<>();
75 | set.add(andrei);
76 | set.add(marian);
77 | set.add(ana);
78 |
79 | System.out.println("Print unique students list: " + set);
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/enums/CountryEnum.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | import javax.swing.text.StyledEditorKit;
4 |
5 | /* A less advanced example in GenderEnum */
6 | public enum CountryEnum {
7 | ROMANIA("RO"),
8 | UNITED_KINGDOM("UK"),
9 | UNITED_STATES_OF_AMERICA("USA"),
10 | ITALY("IT"),
11 | FRANCE("FR"),
12 | /* DEMACIA and NOXUS constant override the isFictionalPlace method */
13 | DEMACIA("DEMACIA") {
14 | @Override
15 | public Boolean isFictionalPlace() {
16 | return true;
17 | }
18 | },
19 | NOXUS("NOXUS"){
20 | @Override
21 | public Boolean isFictionalPlace() {
22 | return true;
23 | }
24 | },
25 | OTHER("O") {
26 | @Override
27 | public Boolean isFictionalPlace() {
28 | return true;
29 | }
30 | };
31 |
32 | /* In Java enums accept attributes, constructor and methods */
33 | private String value;
34 |
35 | CountryEnum(String value) {
36 | this.value = value;
37 | }
38 |
39 | public String getValue() {
40 | return value;
41 | }
42 |
43 | public Boolean isFictionalPlace() {
44 | return false; /* default */
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/enums/GenderEnum.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | /* Basic enum in Java */
4 | /* A more advanced example in CountryEnum */
5 | public enum GenderEnum {
6 | /* As in other programming languages, enums have a default index starting from 0 for each constant */
7 | /* GenderEnum[ ] */
8 | OTHER("O"), /* [0] */
9 | MALE("M"), /* [1] */
10 | FEMALE("F") /* [2] */;
11 |
12 | private String value;
13 | GenderEnum(String value) {
14 | this.value = value;
15 | }
16 |
17 | public String getValue() {
18 | return value;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/enums/RoleEnum.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum RoleEnum {
4 | USER,ADMIN,MODERATOR;
5 | }
6 |
--------------------------------------------------------------------------------
/src/main/java/functional/FunctionalExamples.java:
--------------------------------------------------------------------------------
1 | package functional;
2 |
3 | public class FunctionalExamples {
4 | public static void exmaples() {
5 |
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/model/Secretary.java:
--------------------------------------------------------------------------------
1 | package model;
2 |
3 | import model.common.Human;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Builder;
6 | import lombok.NoArgsConstructor;
7 |
8 | import java.util.HashMap;
9 | import java.util.List;
10 |
11 | @Builder
12 | @NoArgsConstructor
13 | @AllArgsConstructor
14 | /* final keyword means that this class can not have anymore children */
15 | public final class Secretary {
16 | private HashMap> documents = new HashMap<>();
17 |
18 | public HashMap> requestDocuments(Human human) throws Exception {
19 | if(human instanceof Student)
20 | return new HashMap<>(this.documents);
21 | throw new Exception("Access denied");
22 | }
23 |
24 | public void setDocuments(HashMap> documents) {
25 | this.documents = documents;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/model/Student.java:
--------------------------------------------------------------------------------
1 | package model;
2 |
3 | import model.common.Citizen;
4 | import lombok.*;
5 | import lombok.experimental.SuperBuilder;
6 |
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | @Data /* @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor except @NoArgsConstructor */
11 | @SuperBuilder
12 | @NoArgsConstructor
13 | @EqualsAndHashCode(callSuper=true)
14 | @ToString(callSuper=true)
15 | public class Student extends Citizen {
16 | private List subjects;
17 | private Map finalGrades;
18 |
19 | @Override
20 | public void speak() {
21 | System.out.println("I am student");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/model/Subject.java:
--------------------------------------------------------------------------------
1 | package model;
2 |
3 | import lombok.*;
4 | import java.io.Serializable;
5 |
6 | @Data
7 | @ToString
8 | @Builder
9 | @AllArgsConstructor
10 | @NoArgsConstructor
11 | public class Subject implements Serializable {
12 | private String abbreviation;
13 | private String name;
14 | private String description;
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/model/Teacher.java:
--------------------------------------------------------------------------------
1 | package model;
2 |
3 | import model.common.Citizen;
4 |
5 | public class Teacher extends Citizen{
6 | @Override
7 | public void speak() {
8 | /* EMPTY */
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/model/common/Citizen.java:
--------------------------------------------------------------------------------
1 | package model.common;
2 |
3 | import enums.CountryEnum;
4 | import enums.GenderEnum;
5 | import lombok.Data;
6 | import lombok.RequiredArgsConstructor;
7 | import lombok.experimental.SuperBuilder;
8 |
9 | @Data
10 | @SuperBuilder
11 | @RequiredArgsConstructor
12 | public abstract class Citizen implements Human {
13 | protected String forename;
14 | protected String surname;
15 | protected String tin;
16 | protected Integer age;
17 | protected GenderEnum gender;
18 | protected CountryEnum country;
19 |
20 | @Override
21 | public abstract void speak();
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/model/common/Human.java:
--------------------------------------------------------------------------------
1 | package model.common;
2 |
3 | public interface Human {
4 | void speak();
5 | }
6 |
--------------------------------------------------------------------------------
/src/main/java/streams/StreamsExamples.java:
--------------------------------------------------------------------------------
1 | package streams;
2 |
3 | import model.Student;
4 |
5 | import java.util.*;
6 | import java.util.stream.Collectors;
7 | import java.util.stream.IntStream;
8 | import java.util.stream.Stream;
9 |
10 | public class StreamsExamples {
11 |
12 | public static void streamCreation(){
13 | /* Empty stream trough empty() Stream method */
14 | Stream emptyStream = Stream.empty();
15 | emptyStream.forEach(System.out::println);
16 |
17 | /* Create a stream from an existing instantiated Collection */
18 | System.out.println("Stream from collection");
19 | List collection = Arrays.asList(1, 2, 3, 4);
20 | Stream streamFromCollection = collection.stream();
21 | streamFromCollection.forEach(System.out::println);
22 | System.out.println();
23 |
24 | System.out.println("Stream using of()");
25 | /* of() function from Stream. This function accepts a var. nr. of args. of any given type */
26 | Stream streamOf = Stream.of("a", "b", "c");
27 | streamOf.forEach(System.out::println);
28 | System.out.println();
29 |
30 | System.out.println("Stream using util classes");
31 | /* Through some util classes */
32 | String[] primitiveList = new String[]{"a", "b", "c"};
33 | Stream streamOfArray = Arrays.stream(primitiveList);
34 | streamOfArray.forEach(System.out::println);
35 | System.out.println();
36 |
37 | System.out.println("Stream using builder");
38 | /* Builder */
39 | Stream streamBuilder = Stream.builder()
40 | .add(new Student())
41 | .add(new Student())
42 | .add(new Student()).build();
43 | streamBuilder.forEach(System.out::println);
44 | System.out.println();
45 |
46 | System.out.println("Stream creation using generator");
47 | /* Generate */
48 | System.out.println("Generate a random list of numbers");
49 | Stream randomNumberList = Stream.generate(new Random()::nextInt /* We call it like this because nextInt is not static */)
50 | .limit(5);
51 | /* We must set a limit, because the streams accept infinity, and without a limit constraint the print below will print forever */
52 | randomNumberList.forEach(System.out::println);
53 | System.out.println();
54 |
55 | /* Another example, with a lambda function as a Supplier */
56 | Stream stringsGenerated = Stream.generate(()-> ":)").limit(5);
57 | System.out.println("Generate a list of five ':)'");
58 | stringsGenerated.forEach(System.out::println);
59 | System.out.println();
60 |
61 | System.out.println("Stream creation using iterate. Prints numbers from 1 to 10");
62 | Stream streamIterated = Stream.iterate(1,n->n+1).limit(10);
63 | streamIterated.forEach(System.out::println);
64 | System.out.println();
65 |
66 | /* Primitive Streams */
67 | IntStream rangeIntStream = IntStream.range(1 /* inclusive */,5 /* exclusive */); // => [1,2,3,4]
68 | IntStream closedRangeIntStream = IntStream.rangeClosed(1 /* inclusive */, 5/* inclusive */); // => [1,2,3,4,5]
69 |
70 | System.out.println("Primitives streams creations ");
71 | rangeIntStream.forEach(System.out::println); System.out.println();
72 | closedRangeIntStream.forEach(System.out::println); System.out.println();
73 | }
74 |
75 | public static void intStreams() {
76 | /* The difference between IntStream and Stream is that
77 | * IntStream is a stream of primitives int values
78 | * Stream is a stream of Integer objects */
79 | IntStream intPrimitiveStream = IntStream.iterate(1, n -> n + 1).limit(10);
80 | Stream intObjectStream = Stream.iterate(1, n -> n + 1).limit(10);
81 |
82 | /* Furthermore, InStream has built-in functions as range(), sum(), average() and
83 | Stream uses reduce() and collect()
84 | */
85 |
86 | System.out.println("Sum of 1+2+..+10");
87 |
88 | int sum = IntStream.iterate(1, n -> n + 1).limit(10).sum();
89 | int sum2 = Stream.iterate(1, n -> n + 1).limit(10) .reduce(0, Integer::sum);
90 | int sum3 = Stream.iterate(1, n -> n + 1).limit(10) .collect(Collectors.summingInt(Integer::intValue));
91 |
92 | /* Reduce helps us to produce a single result from a sequence */
93 | System.out.println("Sum resulted from IntStream sum() function " + sum );
94 | System.out.println("Sum resulted from Stream reduce() function " + sum2 );
95 | System.out.println("Sum resulted from Stream collect() function " + sum3 );
96 | System.out.println();
97 |
98 | /* Collect help us to pack/fold stream elements in a new response */
99 | System.out.println("Average of 1+2+..+10");
100 | Double average = IntStream.iterate(1, n -> n + 1).limit(10).average().getAsDouble();
101 | /* In this case collect applies arithmetic operations */
102 | Double average2 = Stream.iterate(1, n -> n + 1).limit(10) .collect(Collectors.averagingInt(Integer::intValue));
103 |
104 | /* It is possible to use reduce to do the average, but we should use a custom class in order to do this
105 | * Basically, collect is an enhanced version of reduce because allows Mutable objects in parallel.
106 | */
107 | System.out.println("Average resulted from IntStream average() function " + average);
108 | System.out.println("Average resulted from Stream collect() function " + average2);
109 | }
110 |
111 | public static IntStream oddStreamOfTen () {
112 | /* Return a stream of ten even numbers starting from 0 */
113 | return null;
114 | }
115 |
116 | /* *
117 | * The goal is to return any set of numbers that starts from 0 and ends in n without the number 5 in it.
118 | * Examples:
119 | * anySetOfNumbersWithoutFive(2) => 0, 1, 2
120 | * anySetOfNumbersWithoutFive(5) => 0, 1, 2, 3, 4
121 | * anySetOfNumbersWithoutFive(10) => 0, 1, 2, 3, 4, 6, 7, 8, 9, 10
122 | * */
123 | public static Stream anySetOfNumbersWithoutNrFive (Integer n) {
124 | /* Hint: There is a filter function */
125 | return null;
126 | }
127 |
128 | public static IntStream anySetOfNumbersWithoutNrFour (Integer n) {
129 | /* Hint: There is a filter function */
130 | return null;
131 | }
132 |
133 | public static Integer minNumber(List list) {
134 | /* there are multiple solutions, you can try to reduce, collect and direct call on min */
135 | return null;
136 | }
137 |
138 | public static Integer maxNumber(List list) {
139 | return null;
140 | }
141 |
142 | public static Integer secondMaxNr(List list) {
143 | return null;
144 | }
145 |
146 | public static Integer sortIntegerAsc(List list){
147 | return null;
148 | }
149 |
150 | public static Integer sortIntegerDesc(List list){
151 | return null;
152 | }
153 |
154 | public static Integer oldestStudentAge(List students) {
155 | return null;
156 | }
157 |
158 | public static Integer youngestStudentAge(List students) {
159 | return null;
160 | }
161 |
162 | public static List sortStudentsByAgeAsc(List students){
163 | return null;
164 | }
165 |
166 | public static List sortStudentsByAgeDesc(List students){
167 | return null;
168 | }
169 |
170 | public static double studentAverageSubjects(Student mockStudent) {
171 | return 0;
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/src/main/resources/subjects.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RitanMihai/Java-Walkthrough/7e46d71d8294f9b4b823e61d5dc85e6822ff6a2d/src/main/resources/subjects.dat
--------------------------------------------------------------------------------
/src/test/java/ArraysTest.java:
--------------------------------------------------------------------------------
1 | import collections.ArraysExample;
2 | import org.junit.jupiter.api.Assertions;
3 | import org.junit.jupiter.api.BeforeEach;
4 | import org.junit.jupiter.api.Test;
5 |
6 | import java.util.ArrayList;
7 | import java.util.Arrays;
8 | import java.util.List;
9 |
10 | public class ArraysTest {
11 | private ArraysExample dataSource;
12 |
13 | @BeforeEach
14 | public void setup(){
15 | dataSource = new ArraysExample(); /* new ArrayList<>(Arrays.asList(1,8,3,9,4,10,5)) */
16 | }
17 |
18 | @Test
19 | public void getFirst() {
20 | Integer expected = 1;
21 | Assertions.assertEquals(dataSource.getFirst(), expected);
22 | }
23 |
24 | @Test
25 | public void getLast() {
26 | Integer expected = 5;
27 | Assertions.assertEquals(dataSource.getLast(), expected);
28 | }
29 |
30 | @Test
31 | public void highest() {
32 | Integer expected = 10;
33 | Assertions.assertEquals(dataSource.getTheHighestNumber(), expected);
34 | }
35 |
36 | @Test
37 | public void lowest() {
38 | Integer expected = 1;
39 | Assertions.assertEquals(dataSource.getTheLowestNumber(), expected);
40 | }
41 |
42 | @Test
43 | public void sortAsc(){
44 | List expected = new ArrayList<>(Arrays.asList(1,3,4,5,8,9,10));
45 | Assertions.assertEquals(dataSource.sortAsc(), expected);
46 | }
47 |
48 | @Test
49 | public void sortDesc(){
50 | List expected = new ArrayList<>(Arrays.asList(10,9,8,5,4,3,1));
51 | Assertions.assertEquals(dataSource.sortDesc(), expected);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/test/java/EnumTest.java:
--------------------------------------------------------------------------------
1 | import enums.CountryEnum;
2 | import enums.GenderEnum;
3 | import org.junit.jupiter.api.Test;
4 |
5 | import static org.junit.jupiter.api.Assertions.*;
6 |
7 | public class EnumTest {
8 | @Test
9 | public void testEnum() {
10 | assertEquals(GenderEnum.values().length, 3);
11 | assertEquals(GenderEnum.values()[0], GenderEnum.OTHER);
12 | assertEquals(GenderEnum.values()[1], GenderEnum.MALE);
13 | assertEquals(GenderEnum.values()[2], GenderEnum.FEMALE);
14 |
15 | /* TODO: Uncomment the lines below and make the test pass */
16 | assertEquals(GenderEnum.MALE.getValue(), "M");
17 | assertEquals(GenderEnum.FEMALE.getValue(), "F");
18 | assertEquals(GenderEnum.OTHER.getValue(), "O");
19 |
20 | assertSame(CountryEnum.NOXUS.isFictionalPlace(), Boolean.TRUE);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/OopTest.java:
--------------------------------------------------------------------------------
1 | import model.common.Citizen;
2 | import model.common.Human;
3 | import model.*; /* You can import the whole package, not commanded because, usually, you do not use the whole package */
4 | import org.junit.jupiter.api.Test;
5 | import java.lang.reflect.Field;
6 | import java.util.HashMap;
7 | import java.util.List;
8 |
9 | import static org.junit.jupiter.api.Assertions.*;
10 | import static util.AccessModifierEnum.PRIVATE;
11 |
12 | public class OopTest {
13 |
14 | /* This test pass by default */
15 | @Test
16 | public void testInheritanceStudent() {
17 | Human citizen = new Student();
18 | assertInstanceOf(Citizen.class, citizen);
19 | assertInstanceOf(Human.class, citizen);
20 | }
21 |
22 | @Test
23 | public void testInheritanceTeacher() {
24 | Teacher teacher = new Teacher();
25 | assertInstanceOf(Citizen.class, teacher);
26 | assertInstanceOf(Human.class, teacher);
27 | }
28 |
29 | /* *
30 | * My article about OOP might help you
31 | * https://medium.com/@lucian.ritan/programare-orientat%C4%83-pe-obiect-java-83dbfd222c63
32 | * */
33 | @Test
34 | public void testEncapsulation() throws Exception {
35 | /* Setup */
36 | Teacher teacher = new Teacher();
37 | Student student = new Student();
38 | Secretary secretary = new Secretary();
39 |
40 | HashMap> studentDocuments = new HashMap<>();
41 | studentDocuments.put(student, List.of("BAC", "CI", "CN"));
42 | secretary.setDocuments(studentDocuments);
43 |
44 | Field documentField = Secretary.class.getDeclaredField("documents");
45 | int documentAccessModifier = documentField.getModifiers();
46 |
47 | /* Tests */
48 | assertEquals(documentAccessModifier, PRIVATE.getValue());
49 | assertThrows(Exception.class, () -> secretary.requestDocuments((Human) teacher));
50 | /* Check the difference between == and equals in Java */
51 | assertEquals(studentDocuments, secretary.requestDocuments(student)); /* equivalent with a .equals verification */
52 | assertNotSame(studentDocuments, secretary.requestDocuments(student)); /* equivalent with a != verification */
53 | }
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/test/java/StreamsTest.java:
--------------------------------------------------------------------------------
1 | import collections.ArraysExample;
2 | import enums.GenderEnum;
3 | import model.Student;
4 | import model.Subject;
5 | import org.junit.jupiter.api.BeforeEach;
6 | import org.junit.jupiter.api.Test;
7 | import streams.StreamsExamples;
8 |
9 | import java.util.*;
10 | import java.util.stream.IntStream;
11 | import java.util.stream.Stream;
12 |
13 | import static org.junit.jupiter.api.Assertions.assertEquals;
14 | import static org.junit.jupiter.api.Assertions.assertInstanceOf;
15 |
16 | public class StreamsTest {
17 | private List students;
18 | private Student mockStudent;
19 |
20 | @BeforeEach
21 | public void setup(){
22 | students = Arrays.asList(
23 | Student.builder().forename("Andrei").gender(GenderEnum.MALE).age(19).build(),
24 | Student.builder().forename("Mihai").gender(GenderEnum.MALE).age(20).build(),
25 | Student.builder().forename("Ritan").gender(GenderEnum.MALE).age(23).build(),
26 | Student.builder().forename("Ana").gender(GenderEnum.FEMALE).age(45).build(),
27 | Student.builder().forename("Ina").gender(GenderEnum.FEMALE).age(21).build()
28 | );
29 |
30 | mockStudent = Student.builder().forename("Mock").finalGrades(
31 | Map.of(Subject.builder().abbreviation("SD").build(), 9,
32 | Subject.builder().abbreviation("MIP").build(), 10,
33 | Subject.builder().abbreviation("SO").build(), 10,
34 | Subject.builder().abbreviation("ALGRAF").build(), 10)).build();
35 | }
36 |
37 | @Test
38 | public void testOddStreamLimitTen() {
39 | assertInstanceOf(IntStream.class, StreamsExamples.oddStreamOfTen());
40 |
41 | List expected = Arrays.asList(0,2,4,6,8,10,12,14,16,18);
42 | List integerList = StreamsExamples.oddStreamOfTen().boxed().toList();
43 | assertEquals(expected, integerList);
44 | }
45 |
46 | @Test
47 | public void testAnySetOfNumbersWithoutNrFive() {
48 | assertInstanceOf(Stream.class, StreamsExamples.anySetOfNumbersWithoutNrFive(0));
49 |
50 | assertEquals(Arrays.asList(0,1,2,3,4), StreamsExamples.anySetOfNumbersWithoutNrFive(5).toList());
51 | assertEquals(Arrays.asList(0,1,2,3,4,6,7), StreamsExamples.anySetOfNumbersWithoutNrFive(7).toList());
52 | assertEquals(Arrays.asList(0,1,2), StreamsExamples.anySetOfNumbersWithoutNrFive(2).toList());
53 | }
54 |
55 | @Test
56 | public void testAnySetOfNumbersWithoutNrFour() {
57 | assertInstanceOf(IntStream.class, StreamsExamples.anySetOfNumbersWithoutNrFour(0));
58 |
59 | assertEquals(Arrays.asList(0,1,2,3,5), StreamsExamples.anySetOfNumbersWithoutNrFour(5).boxed().toList());
60 | assertEquals(Arrays.asList(0,1,2,3,5,6,7), StreamsExamples.anySetOfNumbersWithoutNrFour(7).boxed().toList());
61 | assertEquals(Arrays.asList(0,1,2), StreamsExamples.anySetOfNumbersWithoutNrFour(2).boxed().toList());
62 | }
63 |
64 | @Test
65 | public void testMinMaxNumber() {
66 | /* Min nr. */
67 | assertEquals(1, StreamsExamples.minNumber(Arrays.asList(5,6,1,2,4,10,2)));
68 | assertEquals(3, StreamsExamples.minNumber(Arrays.asList(10,8,9,7,6,5,4,3)));
69 |
70 | /* Max nr. */
71 | assertEquals(10, StreamsExamples.maxNumber(Arrays.asList(5,6,1,2,4,10,2)));
72 | assertEquals(10, StreamsExamples.maxNumber(Arrays.asList(10,8,9,7,6,5,4,3)));
73 | }
74 |
75 | @Test
76 | public void testSecondMaxNumber() {
77 | assertEquals(6, StreamsExamples.secondMaxNr(Arrays.asList(5,6,1,2,4,10,2)));
78 | assertEquals(9, StreamsExamples.secondMaxNr(Arrays.asList(10,8,9,7,6,5,4,3)));
79 | }
80 |
81 | @Test
82 | public void testSortIntegers() {
83 | assertEquals(Arrays.asList(1,2,2,4,5,6,10), StreamsExamples.sortIntegerAsc(Arrays.asList(5,6,1,2,4,10,2)));
84 | assertEquals(Arrays.asList(10,6,5,4,2,2,1), StreamsExamples.sortIntegerDesc(Arrays.asList(5,6,1,2,4,10,2)));
85 | }
86 |
87 | @Test
88 | public void testStudentsAge() {
89 | assertEquals(45, StreamsExamples.oldestStudentAge(students));
90 | assertEquals(19, StreamsExamples.youngestStudentAge(students));
91 | }
92 |
93 | @Test
94 | public void testStudentAverageSubjects() {
95 | assertEquals(9.75, StreamsExamples.studentAverageSubjects(mockStudent));
96 | }
97 |
98 | @Test
99 | public void testStudentSort() {
100 | List studentsDesc = StreamsExamples.sortStudentsByAgeDesc(new ArrayList<>(this.students));
101 | List studentsAsc = StreamsExamples.sortStudentsByAgeAsc(new ArrayList<>(this.students));
102 |
103 | assert studentsAsc != null;
104 | assertEquals(19, studentsAsc.get(0).getAge());
105 | assertEquals(45, studentsAsc.get(studentsAsc.size()-1).getAge());
106 |
107 | assert studentsDesc != null;
108 | assertEquals(45, studentsDesc.get(0).getAge());
109 | assertEquals(19, studentsDesc.get(studentsDesc.size()-1).getAge());
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/test/java/util/AccessModifierEnum.java:
--------------------------------------------------------------------------------
1 | package util;
2 |
3 | /* *
4 | * Check java.lang.reflect.Modifier from Constant Values documentation for more details
5 | * https://docs.oracle.com/javase/7/docs/api/constant-values.html
6 | * */
7 | public enum AccessModifierEnum {
8 | DEFAULT(0),
9 | PUBLIC(1),
10 | PRIVATE(2),
11 | PROTECTED(4);
12 |
13 | private int value;
14 |
15 | AccessModifierEnum(int value) {
16 | this.value = value;
17 | }
18 |
19 | public int getValue() {
20 | return value;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------