This commit is contained in:
Ksan 2025-07-17 16:08:15 +02:00
commit cb1201c781
11 changed files with 347 additions and 0 deletions

31
.gitignore vendored Normal file
View File

@ -0,0 +1,31 @@
### IntelliJ IDEA ###
transport_data.json
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/PJ2.iml" filepath="$PROJECT_DIR$/PJ2.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
PJ2.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

42
src/Departure.java Normal file
View File

@ -0,0 +1,42 @@
public class Departure {
private TransportType type;
private String from;
private String to;
private int duration;
private int price;
private int minTransferTime;
public Departure(
TransportType type, String from, String to, int duration, int price, int minTransferTime) {
this.type = type;
this.from = from;
this.to = to;
this.duration = duration;
this.price = price;
this.minTransferTime = minTransferTime;
}
public TransportType getType() {
return type;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public int getDuration() {
return duration;
}
public int getPrice() {
return price;
}
public int getMinTransferTime() {
return minTransferTime;
}
}

6
src/Main.java Normal file
View File

@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

6
src/Station.java Normal file
View File

@ -0,0 +1,6 @@
import java.util.*;
public class Station {
private List<Departure> departures = new ArrayList();
}

View File

@ -0,0 +1,219 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class TransportDataGenerator {
private static final int SIZE = 10;
private int n;
private int m;
private static final int DEPARTURES_PER_STATION = 15;
private static final Random random = new Random();
public static void main(String[] args) {
TransportDataGenerator generator = new TransportDataGenerator(15, 10);
TransportData data = generator.generateData();
generator.saveToJson(data, "transport_data.json");
System.out.println("Podaci su generisani i sacuvani kao transport_data.json");
}
TransportDataGenerator() {
this.n = SIZE;
this.m = SIZE;
}
TransportDataGenerator(int n) {
this.n = n;
this.m = n;
}
TransportDataGenerator(int n, int m) {
this.n = n;
this.m = m;
}
// struktura podataka koja sadrzi sve trazene ulazne podatke
public static class TransportData {
public String[][] countryMap;
public List<Station> stations;
public List<Departure> departures;
}
public static class Station {
public String city;
public String busStation;
public String trainStation;
}
public static class Departure {
public String type; // "autobus" ili "voz"
public String from;
public String to;
public String departureTime;
public int duration; // u minutama
public int price;
public int minTransferTime; // vrijeme potrebno za transfer (u minutama)
}
public TransportData generateData() {
TransportData data = new TransportData();
data.countryMap = generateCountryMap();
data.stations = generateStations();
data.departures = generateDepartures(data.stations);
return data;
}
// generisanje gradova (G_X_Y)
private String[][] generateCountryMap() {
String[][] countryMap = new String[n][m];
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
countryMap[x][y] = "G_" + x + "_" + y;
}
}
return countryMap;
}
// generisanje autobuskih i zeljeznickih stanica
private List<Station> generateStations() {
List<Station> stations = new ArrayList<>();
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
Station station = new Station();
station.city = "G_" + x + "_" + y;
station.busStation = "A_" + x + "_" + y;
station.trainStation = "Z_" + x + "_" + y;
stations.add(station);
}
}
return stations;
}
// generisanje vremena polazaka
private List<Departure> generateDepartures(List<Station> stations) {
List<Departure> departures = new ArrayList<>();
for (Station station : stations) {
int x = Integer.parseInt(station.city.split("_")[1]);
int y = Integer.parseInt(station.city.split("_")[2]);
// generisanje polazaka autobusa
for (int i = 0; i < DEPARTURES_PER_STATION; i++) {
departures.add(generateDeparture("autobus", station.busStation, x, y));
}
// generisanje polazaka vozova
for (int i = 0; i < DEPARTURES_PER_STATION; i++) {
departures.add(generateDeparture("voz", station.trainStation, x, y));
}
}
return departures;
}
private Departure generateDeparture(String type, String from, int x, int y) {
Departure departure = new Departure();
departure.type = type;
departure.from = from;
// generisanje susjeda
List<String> neighbors = getNeighbors(x, y);
departure.to = neighbors.isEmpty() ? from : neighbors.get(random.nextInt(neighbors.size()));
// generisanje vremena
int hour = random.nextInt(24);
int minute = random.nextInt(4) * 15; // 0, 15, 30, 45
departure.departureTime = String.format("%02d:%02d", hour, minute);
// geneirsanje cijene
departure.duration = 30 + random.nextInt(151);
departure.price = 100 + random.nextInt(901);
// generisanje vremena transfera
departure.minTransferTime = 5 + random.nextInt(26);
return departure;
}
// pronalazak susjednih gradova
private List<String> getNeighbors(int x, int y) {
List<String> neighbors = new ArrayList<>();
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int[] dir : directions) {
int nx = x + dir[0];
int ny = y + dir[1];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
neighbors.add("G_" + nx + "_" + ny);
}
}
return neighbors;
}
// cuvanje podataka u JSON mapu
private void saveToJson(TransportData data, String filename) {
try (FileWriter file = new FileWriter(filename)) {
StringBuilder json = new StringBuilder();
json.append("{\n");
// mapa drzave
json.append(" \"countryMap\": [\n");
for (int i = 0; i < n; i++) {
json.append(" [");
for (int j = 0; j < m; j++) {
json.append("\"").append(data.countryMap[i][j]).append("\"");
if (j < m - 1) json.append(", ");
}
json.append("]");
if (i < n - 1) json.append(",");
json.append("\n");
}
json.append(" ],\n");
// stanice
json.append(" \"stations\": [\n");
for (int i = 0; i < data.stations.size(); i++) {
Station s = data.stations.get(i);
json.append(" {\"city\": \"")
.append(s.city)
.append("\", \"busStation\": \"")
.append(s.busStation)
.append("\", \"trainStation\": \"")
.append(s.trainStation)
.append("\"}");
if (i < data.stations.size() - 1) json.append(",");
json.append("\n");
}
json.append(" ],\n");
// vremena polazaka
json.append(" \"departures\": [\n");
for (int i = 0; i < data.departures.size(); i++) {
Departure d = data.departures.get(i);
json.append(" {\"type\": \"")
.append(d.type)
.append("\", \"from\": \"")
.append(d.from)
.append("\", \"to\": \"")
.append(d.to)
.append("\", \"departureTime\": \"")
.append(d.departureTime)
.append("\", \"duration\": ")
.append(d.duration)
.append(", \"price\": ")
.append(d.price)
.append(", \"minTransferTime\": ")
.append(d.minTransferTime)
.append("}");
if (i < data.departures.size() - 1) json.append(",");
json.append("\n");
}
json.append(" ]\n");
json.append("}");
file.write(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}

4
src/TransportType.java Normal file
View File

@ -0,0 +1,4 @@
public enum TransportType {
BUS,
TRAIN
}