-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTester1.java
89 lines (70 loc) · 1.74 KB
/
Tester1.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Tester1 {
public static void main(String[] args) {
List<User> users = readUsersFromCSV("users.txt");
for(User b : users) {
System.out.println(b);
}
}
private static List<User> readUsersFromCSV(String fileName){
List<User> users = new ArrayList<>();
Path pathToFile = Paths.get(fileName);
try(BufferedReader br = Files.newBufferedReader(pathToFile,StandardCharsets.US_ASCII)){
String line = br.readLine();
while(line != null) {
String[] attributes = line.split(",");
User user = createUser(attributes);
users.add(user);
line = br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return users;
}
private static User createUser(String[] metadata) {
String name = metadata[0];
int age = Integer.parseInt(metadata[1]);
String username = metadata[2];
return new User(name, age, username);
}
}
class User{
private String name;
private int age;
private String username;
public User(String name, int age, String username) {
this.name = name;
this.age = age;
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge() {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername() {
this.username = username;
}
@Override
public String toString() {
return "User: nume= " + name +", varsta= " + age + ", username= " + username;
}
}