This commit is contained in:
ksan 2025-07-17 19:51:10 +02:00
parent cb1201c781
commit 6bd9f754f5
9 changed files with 164 additions and 16 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.class
### IntelliJ IDEA ###
transport_data.json
out/
@ -28,4 +29,4 @@ bin/
.vscode/
### Mac OS ###
.DS_Store
.DS_Store

11
src/City.java Normal file
View File

@ -0,0 +1,11 @@
public class City {
private String name;
private Station trainStation;
private Station busStation;
City(String name, Station train, Station bus) {
this.name = name;
this.trainStation = train;
this.busStation = bus;
}
}

View File

@ -3,11 +3,11 @@ public class Departure {
private String from;
private String to;
private int duration;
private int price;
private double price;
private int minTransferTime;
public Departure(
TransportType type, String from, String to, int duration, int price, int minTransferTime) {
TransportType type, String from, String to, int duration, double price, int minTransferTime) {
this.type = type;
this.from = from;
this.to = to;
@ -32,7 +32,7 @@ public class Departure {
return duration;
}
public int getPrice() {
public double getPrice() {
return price;
}

62
src/JsonParser.java Normal file
View File

@ -0,0 +1,62 @@
import java.io.BufferedReader;
import java.io.FileReader;
public class JsonParser {
public static void main(String[] args) {
JsonParser.getValue("transport_data.json", "countryMap");
}
public static Object getValue(String fileName, String key) {
StringBuilder json = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
json.append(line.trim());
}
} catch (Exception e) {
e.printStackTrace();
}
String searchKey = "\"" + key + "\": [";
System.out.println(searchKey);
int keyIndex = json.indexOf(searchKey);
if (keyIndex == -1)
return null;
String jsonString = json.toString();
int startIndex = keyIndex + searchKey.length() - 1;
int endIndex = findPair(jsonString, startIndex);
System.out.println("end index");
if (endIndex == -1)
return null;
System.out.println(jsonString.substring(startIndex, endIndex));
return jsonString.substring(startIndex, endIndex);
}
private static int findPair(String str, int startIndex) {
char open = str.charAt(startIndex);
System.out.println(open);
char closed = (open == '[') ? ']' : '}';
int depth = 0;
for (int i = startIndex; i < str.length(); i++) {
char c = str.charAt(i);
if (c == open) {
depth++;
} else if (c == closed) {
depth--;
if (depth == 0) {
return i;
}
}
}
return -1;
}
}

View File

@ -1,6 +1,20 @@
import java.util.*;
import java.util.ArrayList;
import java.util.List;
public class Station {
private List<Departure> departures = new ArrayList();
private TransportType type = TransportType.NOT_ASSIGNED;
private String name;
private List<Departure> departures = new ArrayList<Departure>();
Station() {
}
Station(TransportType type, String name, List<Departure> departures) {
this.type = type;
this.name = name;
this.departures = departures;
}
}

32
src/TransportData.java Normal file
View File

@ -0,0 +1,32 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class TransportData {
private List<Station> busStations = new ArrayList<>();
private List<Station> trainStations = new ArrayList<>();
public static void main(String[] args) {
System.out.println(TransportData.readFileAsString("transport_data.json"));
}
public TransportData loadData(String path) {
TransportData data = new TransportData();
return data;
}
public static String readFileAsString(String path) {
StringBuilder string = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String line;
while ((line = reader.readLine()) != null)
string.append(line).append("\n");
} catch (Exception e) {
e.printStackTrace();
}
return string.toString();
}
}

View File

@ -7,11 +7,11 @@ public class TransportDataGenerator {
private int n;
private int m;
private static final int DEPARTURES_PER_STATION = 15;
private static final int DEPARTURES_PER_STATION = 3;
private static final Random random = new Random();
public static void main(String[] args) {
TransportDataGenerator generator = new TransportDataGenerator(15, 10);
TransportDataGenerator generator = new TransportDataGenerator(3, 3);
TransportData data = generator.generateData();
generator.saveToJson(data, "transport_data.json");
System.out.println("Podaci su generisani i sacuvani kao transport_data.json");
@ -137,7 +137,7 @@ public class TransportDataGenerator {
// 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}};
int[][] directions = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
for (int[] dir : directions) {
int nx = x + dir[0];
@ -161,10 +161,12 @@ public class TransportDataGenerator {
json.append(" [");
for (int j = 0; j < m; j++) {
json.append("\"").append(data.countryMap[i][j]).append("\"");
if (j < m - 1) json.append(", ");
if (j < m - 1)
json.append(", ");
}
json.append("]");
if (i < n - 1) json.append(",");
if (i < n - 1)
json.append(",");
json.append("\n");
}
json.append(" ],\n");
@ -180,7 +182,8 @@ public class TransportDataGenerator {
.append("\", \"trainStation\": \"")
.append(s.trainStation)
.append("\"}");
if (i < data.stations.size() - 1) json.append(",");
if (i < data.stations.size() - 1)
json.append(",");
json.append("\n");
}
json.append(" ],\n");
@ -204,7 +207,8 @@ public class TransportDataGenerator {
.append(", \"minTransferTime\": ")
.append(d.minTransferTime)
.append("}");
if (i < data.departures.size() - 1) json.append(",");
if (i < data.departures.size() - 1)
json.append(",");
json.append("\n");
}
json.append(" ]\n");
@ -216,4 +220,3 @@ public class TransportDataGenerator {
}
}
}

View File

@ -0,0 +1,13 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
public class TransportDataLoader {
public static void loadData(String path) {
try {
FileReader file = new FileReader(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

View File

@ -1,4 +1,16 @@
public enum TransportType {
BUS,
TRAIN
NOT_ASSIGNED("null"),
BUS("Bus"),
TRAIN("Train");
private final String type;
TransportType(String type) {
this.type = type;
}
@Override
public String toString() {
return this.type;
}
}