Rytov flightApp
unknown
java
a year ago
4.5 kB
6
Indexable
import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; public class App { // parameters private static String filePath = "src/flights_and_forecast.json"; private static double windMax = 30; private static double visibilityMin = 200; private static Map<String, Integer> timeZones = new HashMap<>(); private static List<Map<String, Object>> flightsList; private static Map<String, Object> forecastMap; public static void main(String[] args) { init(); } private static void init() { timeZones.put("moscow", 3); timeZones.put("omsk", 6); timeZones.put("novosibirsk", 7); try { String json = new String(Files.readAllBytes(Paths.get(filePath))); JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject(); Gson gson = new Gson(); // Get flights from JSON flightsList = gson.fromJson(jsonObject.getAsJsonArray("flights"), new TypeToken<List<Map<String, Object>>>() { }.getType()); // Get forecast from JSON JsonObject forecastObject = jsonObject.getAsJsonObject("forecast"); forecastMap = gson.fromJson(forecastObject, Map.class); for (Map<String, Object> flight : flightsList) { String no = (String) flight.get("no"); String from = (String) flight.get("from"); String to = (String) flight.get("to"); Double departureDouble = (Double)flight.get("departure"); int departure = departureDouble.intValue(); Double durationDouble = (Double)flight.get("duration"); int duration = durationDouble.intValue(); boolean canDeparture = calculatePossibility(departure, from); if (!canDeparture) { printFlightStatus(no, from, to, canDeparture); continue; } int arrivalTime = calculateArrivalTime(from, to, departure, duration); boolean canArrive = calculatePossibility(arrivalTime, to); printFlightStatus(no, from, to, canArrive); } } catch (IOException e) { e.printStackTrace(); } } // method for calculating arrival time private static int calculateArrivalTime(String from, String to, int departure, int duration) { // calculate diff between timezones int timeDiff = (int) timeZones.get(to) - (int) timeZones.get(from); int time = departure + duration + timeDiff; //System.out.println("City time will be: "+time); int result = time; // parse time to 24 format if (time > 24) { result = time % 24; } else if (time < 0) { result = 24 - time; } // return calculated time return result; } // method for calculating possibility of flight private static boolean calculatePossibility(int time, String city) { List<Map<String, Double>> cityList = (List<Map<String, Double>>) forecastMap.get(city); Map<String, Double> timeForecast = cityList.get(time); if (timeForecast.get("wind") > windMax) { //System.out.println("В "+time+" в городе "+city+" слишком сильный ветер - "+timeForecast.get("wind")); return false; } else if (timeForecast.get("visibility") < visibilityMin) { //System.out.println("В "+time+" в городе "+city+" слишком слабая видимость - "+timeForecast.get("visibility")); return false; } else { return true; } } // method for printing status of flight to console private static void printFlightStatus(String no, String from, String to, boolean willFlight) { String flightStatus; if (willFlight) { flightStatus = "по расписанию"; } else { flightStatus = "отменен"; } System.out.println(no + " | " + from + " -> " + to + " | " + flightStatus); //System.out.println(); } }
Editor is loading...