diff --git a/src/City.java b/src/City.java index 977bd5e..cfc2036 100644 --- a/src/City.java +++ b/src/City.java @@ -2,10 +2,65 @@ public class City { private String name; private Station trainStation; private Station busStation; + private int row; + private int col; - City(String name, Station train, Station bus) { + City(String name, String train, String bus, int row, int col) { + this.name = name; + this.trainStation = new Station(TransportType.TRAIN, train); + this.busStation = new Station(TransportType.BUS, bus); + } + + City(String name, Station train, Station bus, int row, int col) { this.name = name; this.trainStation = train; this.busStation = bus; + this.row = row; + this.col = col; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Station getTrainStation() { + return trainStation; + } + + public void setTrainStation(Station trainStation) { + this.trainStation = trainStation; + } + + public Station getBusStation() { + return busStation; + } + + public void setBusStation(Station busStation) { + this.busStation = busStation; + } + + public int getRow() { + return row; + } + + public void setRow(int row) { + this.row = row; + } + + public int getCol() { + return col; + } + + public void setCol(int col) { + this.col = col; + } + + @Override + public String toString() { + return name + "\n\tTrain Station: " + trainStation + "\n\tBus Station: " + busStation; } } diff --git a/src/Departure.java b/src/Departure.java index b960d0f..4871177 100644 --- a/src/Departure.java +++ b/src/Departure.java @@ -1,16 +1,27 @@ +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + public class Departure { private TransportType type; private String from; private String to; private int duration; + private LocalTime departureTime; private double price; private int minTransferTime; public Departure( - TransportType type, String from, String to, int duration, double price, int minTransferTime) { + TransportType type, + String from, + String to, + String departureTime, + int duration, + double price, + int minTransferTime) { this.type = type; this.from = from; this.to = to; + this.departureTime = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm")); this.duration = duration; this.price = price; this.minTransferTime = minTransferTime; @@ -39,4 +50,26 @@ public class Departure { public int getMinTransferTime() { return minTransferTime; } + + @Override + public String toString() { + return "Departure{" + + "type=" + + type + + ", from='" + + from + + '\'' + + ", to='" + + to + + '\'' + + ", departureTime=" + + departureTime.format(DateTimeFormatter.ofPattern("HH:mm")) + + ", duration=" + + duration + + ", price=" + + price + + ", minTransferTime=" + + minTransferTime + + '}'; + } } diff --git a/src/JsonParser.java b/src/JsonParser.java index 7088f90..e73be1b 100644 --- a/src/JsonParser.java +++ b/src/JsonParser.java @@ -1,13 +1,18 @@ import java.io.BufferedReader; import java.io.FileReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class JsonParser { public static void main(String[] args) { - JsonParser.getValue("transport_data.json", "countryMap"); + // String[][] map = JsonParser.parseCountryMap("transport_data.json", "countryMap"); + // JsonParser.parseCities("transport_data.json", "stations"); + JsonParser.getDeparturesList("transport_data.json", "departures"); } - public static Object getValue(String fileName, String key) { + public static String getValue(String fileName, String key) { StringBuilder json = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new FileReader(fileName)); @@ -20,25 +25,147 @@ public class JsonParser { } String searchKey = "\"" + key + "\": ["; - System.out.println(searchKey); int keyIndex = json.indexOf(searchKey); - if (keyIndex == -1) - return null; + 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; + if (endIndex == -1) return null; - System.out.println(jsonString.substring(startIndex, endIndex)); return jsonString.substring(startIndex, endIndex); } + public static String[][] parseCountryMap(String fileName, String key) { + + String mapData = getValue(fileName, key); + + mapData = mapData.replaceAll("[\\[\\]]+", "|"); + String[] rows = mapData.split("\\|"); + + List matrixList = new ArrayList<>(); + + for (String row : rows) { + if (row.trim().isEmpty()) continue; + String[] cities = + Arrays.stream(row.split(",")) + .map(s -> s.replaceAll("\"", "").trim()) + .filter(s -> !s.isEmpty()) + .toArray(String[]::new); + for (String city : cities) { + System.out.println(city); + } + if (cities.length > 0) matrixList.add(cities); + } + String[][] result = new String[matrixList.size()][]; + for (int i = 0; i < matrixList.size(); i++) { + + result[i] = matrixList.get(i); + } + + for (int i = 0; i < result.length; i++) { + for (int j = 0; j < result[i].length; j++) { + System.out.println("result[" + i + "][" + j + "] = " + result[i][j]); + } + } + + return result; + } + + public static List getDeparturesList(String fileName, String key) { + List departures = new ArrayList<>(); + + String departuresJson = getValue(fileName, key); + System.out.println(departuresJson); + + String res = + departuresJson + .replaceAll("[\\{\\[\\]]", "") + .replaceAll("\\},", "\\|") + .replaceAll("\\}", "") + .replaceAll("\\\"", "") + .replaceAll( + "type:\\s|\\sfrom:\\s|\\sto:\\s|\\sdepartureTime:\\s|\\sduration:\\s|\\sprice:\\s|\\sminTransferTime:\\s", + ""); + + System.out.println(res); + + String[] arr = res.split("\\|"); + for (String a : arr) { + String[] temp = a.split(","); + departures.add( + new Departure( + parseTransportType(temp[0]), + temp[1], + temp[2], + temp[3], + Integer.parseInt(temp[4]), + Double.parseDouble(temp[5]), + Integer.parseInt(temp[6]))); + System.out.println(a); + } + + for (Departure dep : departures) System.out.println(dep); + return departures; + } + + public static List parseCities(String fileName, String key) { + String cityData = getValue(fileName, key); + System.out.println(cityData); + String res = + cityData + .replaceAll("[\\[\\]]+", "") + .replaceAll("\\},\\{", "\n") + .replaceAll("[\\{\\}]", "") + .replaceAll("\\n", "|"); + + String[] arr = res.split("\\|"); + + List cities = new ArrayList<>(); + List list = new ArrayList<>(); + for (int i = 0; i < arr.length; i++) { + String[] temp = arr[i].split(","); + list.add(temp); + } + int i = 0; + List temp = new ArrayList<>(); + List formatedList = new ArrayList<>(); + for (String[] a : list) { + temp.clear(); + formatedList.clear(); + System.out.println(i); + i++; + for (String b : a) { + temp.add(b); + } + for (String item : temp) { + item = (item.replaceAll("\\\"[a-zA-Z]+\\\":\\s", "").trim().replaceAll("\\\"", "")); + System.out.println(item); + } + + for (int j = 0; j < temp.size(); j++) { + formatedList.add( + temp.get(j).replaceAll("\\\"[a-zA-Z]+\\\":\\s", "").trim().replaceAll("\\\"", "")); + System.out.println(formatedList.get(j)); + } + + cities.add( + new City( + formatedList.get(0), + formatedList.get(1), + formatedList.get(2), + Integer.parseInt(String.valueOf(formatedList.get(0).charAt(2))), + Integer.parseInt(String.valueOf(formatedList.get(0).charAt(4))))); + } + for (City city : cities) { + System.out.println(city); + } + return cities; + } + private static int findPair(String str, int startIndex) { char open = str.charAt(startIndex); @@ -59,4 +186,13 @@ public class JsonParser { } return -1; } + + public static TransportType parseTransportType(String str) { + for (TransportType t : TransportType.values()) { + if (t.toString().equalsIgnoreCase(str)) { + return t; + } + } + return TransportType.NOT_ASSIGNED; + } } diff --git a/src/Station.java b/src/Station.java index ded1709..95ed8a5 100644 --- a/src/Station.java +++ b/src/Station.java @@ -7,14 +7,10 @@ public class Station { private String name; private List departures = new ArrayList(); - Station() { + Station() {} - } - - Station(TransportType type, String name, List departures) { + Station(TransportType type, String name) { this.type = type; this.name = name; - this.departures = departures; } - } diff --git a/src/TransportData.java b/src/TransportData.java deleted file mode 100644 index 5c3399b..0000000 --- a/src/TransportData.java +++ /dev/null @@ -1,32 +0,0 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.util.ArrayList; -import java.util.List; - -public class TransportData { - private List busStations = new ArrayList<>(); - private List 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(); - } -} diff --git a/src/TransportDataGenerator.java b/src/TransportDataGenerator.java index 78830bb..651649f 100644 --- a/src/TransportDataGenerator.java +++ b/src/TransportDataGenerator.java @@ -11,7 +11,7 @@ public class TransportDataGenerator { private static final Random random = new Random(); public static void main(String[] args) { - TransportDataGenerator generator = new TransportDataGenerator(3, 3); + TransportDataGenerator generator = new TransportDataGenerator(3, 4); TransportData data = generator.generateData(); generator.saveToJson(data, "transport_data.json"); System.out.println("Podaci su generisani i sacuvani kao transport_data.json"); diff --git a/src/TransportDataLoader.java b/src/TransportDataLoader.java deleted file mode 100644 index 7d1e81c..0000000 --- a/src/TransportDataLoader.java +++ /dev/null @@ -1,13 +0,0 @@ -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(); - } - } -} diff --git a/src/TransportType.java b/src/TransportType.java index e0887e5..7ceac07 100644 --- a/src/TransportType.java +++ b/src/TransportType.java @@ -1,16 +1,16 @@ public enum TransportType { - NOT_ASSIGNED("null"), - BUS("Bus"), - TRAIN("Train"); + NOT_ASSIGNED("null"), + BUS("autobus"), + TRAIN("voz"); - private final String type; + private final String type; - TransportType(String type) { - this.type = type; - } + TransportType(String type) { + this.type = type; + } - @Override - public String toString() { - return this.type; - } + @Override + public String toString() { + return this.type; + } }