We are the wild!

This commit is contained in:
2025-04-15 03:25:12 +03:00
parent 08b3029899
commit 10672d5540
3 changed files with 105 additions and 34 deletions

View File

@ -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;
}
}

View File

@ -10,6 +10,7 @@ import java.util.Scanner;
import java.util.stream.Collectors;
import itmo.lab5.models.enums.*;
import itmo.lab5.cli.helpers.FlatComparatorFactory;
import itmo.lab5.models.*;
public class Reader {
@ -42,17 +43,13 @@ public class Reader {
collection.put(parsedFlat.getId(), parsedFlat);
}
collection = collection.entrySet()
.stream()
.sorted(HashMap.Entry.comparingByKey())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue,
HashMap::new));
var sortedById = FlatComparatorFactory.sortFlats(
collection,
FlatComparatorFactory.SortField.ID
);
scanner.close();
return collection;
return sortedById;
}
private static Flat parseFlat(String lineToParse) throws IllegalArgumentException, ParseException {

View File

@ -1,5 +1,6 @@
package itmo.lab5.parser;
import itmo.lab5.cli.helpers.FlatComparatorFactory;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
@ -14,33 +15,38 @@ public class Writer {
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter writer = new BufferedWriter(osw)) {
for (Flat flat : flats.values()) {
String date = "";
flats = FlatComparatorFactory.sortFlats(
flats,
FlatComparatorFactory.SortField.ID
);
try {
date = dateFormat.format(flat.getCreationDate()).toString();
} catch (Exception e) {
System.out.println(e.getMessage());
for (Flat flat : flats.values()) {
String date = "";
try {
date = dateFormat.format(flat.getCreationDate()).toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}
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);
}
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.";
} catch (IOException e) {
e.printStackTrace();