-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (40 loc) · 1.89 KB
/
Main.java
File metadata and controls
49 lines (40 loc) · 1.89 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
48
package com.ironhack;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[10];
employees[0] = new Employee("John Doe", "john.doe@email.com", 19, 20000);
employees[1] = new Employee("Jane Smith", "jane.smith@email.com", 22, 15000);
employees[2] = new Employee("Mike Brown", "mike.brown@email.com", 30, 5000);
employees[3] = new Employee("Anna White", "anna.white@email.com", 28, 10000);
employees[4] = new Employee("David Green", "david.green@email.com", 35, 19000);
employees[5] = new Employee("Emily Black", "emily.black@email.com", 26, 12000);
employees[6] = new Employee("Chris Blue", "chris.blue@email.com", 40, 8000);
employees[7] = new Intern("Sarah Yellow", "sarah.yellow@email.com", 24, 1000);
employees[8] = new Intern("Tom Orange", "tom.orange@email.com", 31, 18000);
employees[9] = new Employee("Lisa Purple", "lisa.purple@email.com", 29, 13000);
logEmployees(employees);
}
public static void logEmployees(Employee[] employees) {
String filePath = "employees.txt";
try(FileWriter writer = new FileWriter(filePath)) {
for (Employee employee : employees) {
String content = getContent(employee);
// Write content of employee to file
writer.write(content);
}
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
public static String getContent(Employee employee) {
return String.format("""
Name: %s
Email: %s
Age: %d
Salary: %.2f
================
""", employee.getName(), employee.getEmail(), employee.getAge(), employee.getSalary());
}
}