-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaExample.java
More file actions
47 lines (39 loc) · 1.19 KB
/
Copy pathJavaExample.java
File metadata and controls
47 lines (39 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package java17;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JavaExample {
static class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 15),
new Person("Charlie", 40),
new Person("David", 18)
);
List<Person> processedPeople = new ArrayList<>();
for (Person person : people) {
if (person.age >= 18) {
processedPeople.add(new Person(person.name.toUpperCase(), person.age));
}
}
for (Person person : processedPeople) {
System.out.println(personInfo(person));
}
}
static String personInfo(Person person) {
if (person.age < 20) {
return person.name + " is a teenager.";
} else if (person.age >= 20 && person.age < 40) {
return person.name + " is an adult.";
}
return person.name + " is middle-aged.";
}
}