We are the wild!
This commit is contained in:
@ -0,0 +1,68 @@
|
|||||||
|
package itmo.lab5.cli.helpers;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import itmo.lab5.models.Flat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The FlatComparatorFactory class provides functionality to create comparators
|
||||||
|
* for sorting Flat objects based on various fields such as ID, name, area,
|
||||||
|
* number of rooms, and creation date.
|
||||||
|
*/
|
||||||
|
public class FlatComparatorFactory {
|
||||||
|
public enum SortField {
|
||||||
|
ID,
|
||||||
|
NAME,
|
||||||
|
AREA,
|
||||||
|
NUMBER_OF_ROOMS,
|
||||||
|
CREATION_DATE
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Comparator for Flat objects based on the specified sort field.
|
||||||
|
*
|
||||||
|
* @param field the field by which to sort the Flat objects
|
||||||
|
* @return a Comparator for Flat objects
|
||||||
|
* @throws IllegalArgumentException if the specified sort field is unknown
|
||||||
|
*/
|
||||||
|
public static Comparator<Flat> getComparator(SortField field) {
|
||||||
|
switch (field) {
|
||||||
|
case ID:
|
||||||
|
return Comparator.comparingInt(Flat::getId);
|
||||||
|
case NAME:
|
||||||
|
return Comparator.comparing(Flat::getName);
|
||||||
|
case AREA:
|
||||||
|
return Comparator.comparingDouble(Flat::getArea);
|
||||||
|
case NUMBER_OF_ROOMS:
|
||||||
|
return Comparator.comparingInt(Flat::getNumberOfRooms);
|
||||||
|
case CREATION_DATE:
|
||||||
|
return Comparator.comparing(Flat::getCreationDate);
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unknown sort field: " + field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts a HashMap of Flat objects based on the specified sort field and
|
||||||
|
* returns a new sorted HashMap.
|
||||||
|
*
|
||||||
|
* @param flatsMap the HashMap of Flat objects to be sorted
|
||||||
|
* @param field the field by which to sort the Flat objects
|
||||||
|
* @return a new HashMap containing the sorted Flat objects
|
||||||
|
*/
|
||||||
|
public static HashMap<Integer, Flat> sortFlats(HashMap<Integer, Flat> flatsMap, SortField field) {
|
||||||
|
List<Flat> flatList = new ArrayList<>(flatsMap.values());
|
||||||
|
Collections.sort(flatList, getComparator(field));
|
||||||
|
|
||||||
|
|
||||||
|
var sortedMap = new HashMap<Integer, Flat>();
|
||||||
|
for (Flat flat : flatList) {
|
||||||
|
sortedMap.put(flat.getId(), flat);
|
||||||
|
}
|
||||||
|
return sortedMap;
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ import java.util.Scanner;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import itmo.lab5.models.enums.*;
|
import itmo.lab5.models.enums.*;
|
||||||
|
import itmo.lab5.cli.helpers.FlatComparatorFactory;
|
||||||
import itmo.lab5.models.*;
|
import itmo.lab5.models.*;
|
||||||
|
|
||||||
public class Reader {
|
public class Reader {
|
||||||
@ -42,17 +43,13 @@ public class Reader {
|
|||||||
collection.put(parsedFlat.getId(), parsedFlat);
|
collection.put(parsedFlat.getId(), parsedFlat);
|
||||||
}
|
}
|
||||||
|
|
||||||
collection = collection.entrySet()
|
var sortedById = FlatComparatorFactory.sortFlats(
|
||||||
.stream()
|
collection,
|
||||||
.sorted(HashMap.Entry.comparingByKey())
|
FlatComparatorFactory.SortField.ID
|
||||||
.collect(Collectors.toMap(
|
);
|
||||||
Map.Entry::getKey,
|
|
||||||
Map.Entry::getValue,
|
|
||||||
(oldValue, newValue) -> oldValue,
|
|
||||||
HashMap::new));
|
|
||||||
|
|
||||||
scanner.close();
|
scanner.close();
|
||||||
return collection;
|
return sortedById;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Flat parseFlat(String lineToParse) throws IllegalArgumentException, ParseException {
|
private static Flat parseFlat(String lineToParse) throws IllegalArgumentException, ParseException {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package itmo.lab5.parser;
|
package itmo.lab5.parser;
|
||||||
|
|
||||||
|
import itmo.lab5.cli.helpers.FlatComparatorFactory;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -13,34 +14,39 @@ public class Writer {
|
|||||||
try (FileOutputStream fos = new FileOutputStream(filename);
|
try (FileOutputStream fos = new FileOutputStream(filename);
|
||||||
OutputStreamWriter osw = new OutputStreamWriter(fos);
|
OutputStreamWriter osw = new OutputStreamWriter(fos);
|
||||||
BufferedWriter writer = new BufferedWriter(osw)) {
|
BufferedWriter writer = new BufferedWriter(osw)) {
|
||||||
|
|
||||||
|
flats = FlatComparatorFactory.sortFlats(
|
||||||
|
flats,
|
||||||
|
FlatComparatorFactory.SortField.ID
|
||||||
|
);
|
||||||
|
|
||||||
|
for (Flat flat : flats.values()) {
|
||||||
|
String date = "";
|
||||||
|
|
||||||
for (Flat flat : flats.values()) {
|
try {
|
||||||
String date = "";
|
date = dateFormat.format(flat.getCreationDate()).toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
String line = String.format("%d,%s,%d,%d,%s,%.2f,%d,%s,%s,%s,%s,%d,%d\n",
|
||||||
date = dateFormat.format(flat.getCreationDate()).toString();
|
flat.getId(),
|
||||||
} catch (Exception e) {
|
flat.getName(),
|
||||||
System.out.println(e.getMessage());
|
flat.getCoordinates().getX(),
|
||||||
|
flat.getCoordinates().getY(),
|
||||||
|
date,
|
||||||
|
flat.getArea(),
|
||||||
|
flat.getNumberOfRooms(),
|
||||||
|
flat.getFurnish(),
|
||||||
|
flat.getView() != null ? flat.getView() : "",
|
||||||
|
flat.getTransport(),
|
||||||
|
flat.getHouse() != null ? flat.getHouse().getName() : "",
|
||||||
|
flat.getHouse() != null ? flat.getHouse().getYear() : 0,
|
||||||
|
flat.getHouse() != null ? flat.getHouse().getNumberOfFloors() : 0);
|
||||||
|
|
||||||
|
writer.write(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
String line = String.format("%d,%s,%d,%d,%s,%.2f,%d,%s,%s,%s,%s,%d,%d\n",
|
|
||||||
flat.getId(),
|
|
||||||
flat.getName(),
|
|
||||||
flat.getCoordinates().getX(),
|
|
||||||
flat.getCoordinates().getY(),
|
|
||||||
date,
|
|
||||||
flat.getArea(),
|
|
||||||
flat.getNumberOfRooms(),
|
|
||||||
flat.getFurnish(),
|
|
||||||
flat.getView() != null ? flat.getView() : "",
|
|
||||||
flat.getTransport(),
|
|
||||||
flat.getHouse() != null ? flat.getHouse().getName() : "",
|
|
||||||
flat.getHouse() != null ? flat.getHouse().getYear() : 0,
|
|
||||||
flat.getHouse() != null ? flat.getHouse().getNumberOfFloors() : 0);
|
|
||||||
|
|
||||||
writer.write(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
return "Collection has been saved to the file successfully.";
|
return "Collection has been saved to the file successfully.";
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Reference in New Issue
Block a user