mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 16:43:12 +02:00
tune up transaction generator
This commit is contained in:
parent
06f79923bb
commit
41abb20094
@ -8,7 +8,7 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
public class TransactionGenerator {
|
public class TransactionGenerator {
|
||||||
private static final int NUM_CARDS = 10000;
|
private static final int NUM_CARDS = 10000;
|
||||||
private static final int NUM_USERS = 5000; // Assuming each user has ~2 cards on average
|
private static final int NUM_USERS = 5000; // Assuming each user has ~2 cards on average
|
||||||
private static final Map<String, Set<Double[]>> cardLocations = new HashMap<>();
|
private static final Map<String, Set<LocationPoint>> cardLocations = new HashMap<>();
|
||||||
private static final Map<String, Double> cardLimits = new HashMap<>();
|
private static final Map<String, Double> cardLimits = new HashMap<>();
|
||||||
private static final Map<String, String> cardToUser = new HashMap<>();
|
private static final Map<String, String> cardToUser = new HashMap<>();
|
||||||
private static final List<String> cardIds = new ArrayList<>();
|
private static final List<String> cardIds = new ArrayList<>();
|
||||||
@ -57,13 +57,20 @@ public class TransactionGenerator {
|
|||||||
actualAnomalyType = (anomalyType >= 0 && anomalyType <= 3) ?
|
actualAnomalyType = (anomalyType >= 0 && anomalyType <= 3) ?
|
||||||
anomalyType : ThreadLocalRandom.current().nextInt(1, 4);
|
anomalyType : ThreadLocalRandom.current().nextInt(1, 4);
|
||||||
} else if (ThreadLocalRandom.current().nextDouble() < ANOMALY_PROBABILITY) {
|
} else if (ThreadLocalRandom.current().nextDouble() < ANOMALY_PROBABILITY) {
|
||||||
actualAnomalyType = ThreadLocalRandom.current().nextInt(1, 4);
|
double roll = ThreadLocalRandom.current().nextDouble();
|
||||||
|
if (roll < 0.3) {
|
||||||
|
actualAnomalyType = ANOMALY_LOCATION;
|
||||||
|
} else if (roll < 0.8) {
|
||||||
|
actualAnomalyType = ANOMALY_AMOUNT;
|
||||||
|
} else {
|
||||||
|
actualAnomalyType = ANOMALY_FREQUENCY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get or generate location
|
// Get or generate location
|
||||||
Double[] location = getLocationForCard(cardId, actualAnomalyType == ANOMALY_LOCATION);
|
LocationPoint location = getLocationForCard(cardId, actualAnomalyType == ANOMALY_LOCATION);
|
||||||
double latitude = location[0];
|
double latitude = location.latitude;
|
||||||
double longitude = location[1];
|
double longitude = location.longitude;
|
||||||
|
|
||||||
// Generate transaction amount
|
// Generate transaction amount
|
||||||
double amount;
|
double amount;
|
||||||
@ -93,14 +100,14 @@ public class TransactionGenerator {
|
|||||||
return transaction;
|
return transaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Double[] getLocationForCard(String cardId, boolean generateAnomaly) {
|
private LocationPoint getLocationForCard(String cardId, boolean generateAnomaly) {
|
||||||
Set<Double[]> locations = cardLocations.get(cardId);
|
Set<LocationPoint> locations = cardLocations.get(cardId);
|
||||||
|
|
||||||
if (locations.isEmpty() || generateAnomaly) {
|
if (locations.isEmpty() || generateAnomaly) {
|
||||||
// Generate a random worldwide location
|
// Generate a random worldwide location
|
||||||
double latitude = ThreadLocalRandom.current().nextDouble(-90, 90);
|
double latitude = ThreadLocalRandom.current().nextDouble(-90, 90);
|
||||||
double longitude = ThreadLocalRandom.current().nextDouble(-180, 180);
|
double longitude = ThreadLocalRandom.current().nextDouble(-180, 180);
|
||||||
Double[] newLocation = {latitude, longitude};
|
LocationPoint newLocation = new LocationPoint(latitude, longitude);
|
||||||
|
|
||||||
// Store this location for future use unless it's an anomaly
|
// Store this location for future use unless it's an anomaly
|
||||||
if (!generateAnomaly) {
|
if (!generateAnomaly) {
|
||||||
@ -110,7 +117,7 @@ public class TransactionGenerator {
|
|||||||
return newLocation;
|
return newLocation;
|
||||||
} else {
|
} else {
|
||||||
// Pick a random location from the card's history
|
// Pick a random location from the card's history
|
||||||
Double[][] locArray = locations.toArray(new Double[0][]);
|
LocationPoint[] locArray = locations.toArray(new LocationPoint[0]);
|
||||||
return locArray[ThreadLocalRandom.current().nextInt(locArray.length)];
|
return locArray[ThreadLocalRandom.current().nextInt(locArray.length)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -135,9 +142,9 @@ public class TransactionGenerator {
|
|||||||
double availableLimit = cardLimits.get(cardId);
|
double availableLimit = cardLimits.get(cardId);
|
||||||
|
|
||||||
// Get location
|
// Get location
|
||||||
Double[] location = getLocationForCard(cardId, forceAnomaly && anomalyType == ANOMALY_LOCATION);
|
LocationPoint location = getLocationForCard(cardId, forceAnomaly && anomalyType == ANOMALY_LOCATION);
|
||||||
double latitude = location[0];
|
double latitude = location.latitude;
|
||||||
double longitude = location[1];
|
double longitude = location.longitude;
|
||||||
|
|
||||||
// Generate amount
|
// Generate amount
|
||||||
double amount;
|
double amount;
|
||||||
@ -161,4 +168,28 @@ public class TransactionGenerator {
|
|||||||
Instant.now()
|
Instant.now()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class LocationPoint {
|
||||||
|
final double latitude;
|
||||||
|
final double longitude;
|
||||||
|
|
||||||
|
LocationPoint(double latitude, double longitude) {
|
||||||
|
this.latitude = latitude;
|
||||||
|
this.longitude = longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
LocationPoint that = (LocationPoint) o;
|
||||||
|
return Double.compare(that.latitude, latitude) == 0 &&
|
||||||
|
Double.compare(that.longitude, longitude) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(latitude, longitude);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user