-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuestions
57 lines (45 loc) · 2.92 KB
/
Questions
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
1) what is the difference between sout and serr in java?
What i want:
- I have 2 classes: CSVManager and CityManager
CSVManager should read from .csv file and CityManager should construct City collection from value, returned from readFromFile method in CSVManager.
requirements:
1) in CSVManager i should ise InputStreamReader to read from file
my first idea was to read lines of .csv file using csvParser and write them to ArrayList of Strings, or to new file, named 'readyForConstructingCollection' (don't know what to do yet)
Then, return that arraylist and using csvParser, construct collection from it in CityManager:
1) get class Class object, get all the fields from City class, than knowing what fields there are in class City, i can retrieve them from String lines in ArrayList and construct CityManager
like this:
TreeSet<City> cities = new TreeSet<>(new CityComparator());
for (CSVRecord fields : csvParser) {
long id = cities.hashCode();
String name = fields.get("name");
Integer x = Integer.parseInt(fields.get("x"));
double y = Double.parseDouble(fields.get("y"));
Coordinates coordinates = new Coordinates(x, y);
java.util.Date creationDate = java.sql.Date.valueOf(LocalDate.now());
Integer area = Integer.parseInt(fields.get("area"));
int population = Integer.parseInt(fields.get("population"));
Double metersAboveSeaLevel = null;
if (fields.get("metersAboveSeaLevel") != null && !fields.get("metersAboveSeaLevel").isEmpty()) {
metersAboveSeaLevel = Double.parseDouble(fields.get("metersAboveSeaLevel"));
}
Climate climate = Climate.valueOf(fields.get("climate"));
Government government = Government.valueOf(fields.get("government"));
StandardOfLiving standardOfLiving = StandardOfLiving.valueOf(fields.get("standardOfLiving"));
Human governor = new Human(fields.get("governor"));
cities.add(new City(id, name, coordinates, creationDate, area, population, metersAboveSeaLevel, climate, government, standardOfLiving, governor));
- my current load method in CityManager:
public void loadCollection(String envKey) {
String pathToDataFile = System.getenv(envKey);
CityManager.setPathToDataFile(pathToDataFile);
if (pathToDataFile == null) {
System.out.println("Переменной окружения lab5 не существует!");
} else if (pathToDataFile.trim().split(" ").length != 1) {
System.out.println("Некорректно задана переменная окружения lab5! " +
"\nЗадайте переменную окружения с именем \"lab5\", поместив туда полный путь к .csv файлу.");
}
CSVManager csvManager = new CSVManager();
TreeSet<City> cities = new TreeSet<>(new CityComparator());
ArrayList<String> parsedCSVFile = csvManager.readFromFile(pathToDataFile);
CSVParser csvParser = new CSVParser();
}
help me to create proper java code for this, please)