From 92fe7dcbedffda775c85597b6c50358e8ff5ff31 Mon Sep 17 00:00:00 2001 From: Newarin Date: Mon, 20 Nov 2023 09:19:03 +0300 Subject: [PATCH] Homework 5 --- module05/pom.xml | 46 ++++++++++++++-- .../src/main/java/ru/sberbank/edu/App.java | 22 ++++++-- .../main/java/ru/sberbank/edu/CityInfo.java | 8 +++ .../java/ru/sberbank/edu/GeoPosition.java | 53 +++++++++++++++++++ .../java/ru/sberbank/edu/TravelService.java | 43 +++++++++++++-- .../test/java/ru/sberbank/edu/AppTest.java | 45 +++++++--------- 6 files changed, 178 insertions(+), 39 deletions(-) diff --git a/module05/pom.xml b/module05/pom.xml index e64564f4..524fb82c 100644 --- a/module05/pom.xml +++ b/module05/pom.xml @@ -8,12 +8,50 @@ 4.0.0 module05 - jar - - module05 - UTF-8 + + 16 + 16 + 5.9.2 + 5.5.0 + 5.3.1 + 3.24.2 + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + + org.mockito + mockito-junit-jupiter + ${mockito.junit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + + + junit + junit + RELEASE + test + + + + + diff --git a/module05/src/main/java/ru/sberbank/edu/App.java b/module05/src/main/java/ru/sberbank/edu/App.java index 5419c026..4ed5920c 100644 --- a/module05/src/main/java/ru/sberbank/edu/App.java +++ b/module05/src/main/java/ru/sberbank/edu/App.java @@ -4,10 +4,22 @@ * Hello world! * */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); +public class App { + public static void main(String[] args) { + System.out.println("Hello World!"); + + + CityInfo moscow = new CityInfo("Moscow", new GeoPosition("55(45'4.4784'')", "37(37'6.3228'')")); + //53° 12′ 10″ N, 50° 8′ 27″ E + CityInfo samara = new CityInfo("Samara", new GeoPosition("53(12' 10'')", "50(8'27'')")); + + TravelService ts = new TravelService(); + + ts.add(moscow); + ts.add(samara); + + System.out.println(ts.getDistance("Moscow","Samara")); + System.out.println(ts.getCitiesNear("Moscow",1000000).get(1).getName()); + } } diff --git a/module05/src/main/java/ru/sberbank/edu/CityInfo.java b/module05/src/main/java/ru/sberbank/edu/CityInfo.java index f37134e2..a8004247 100644 --- a/module05/src/main/java/ru/sberbank/edu/CityInfo.java +++ b/module05/src/main/java/ru/sberbank/edu/CityInfo.java @@ -8,6 +8,14 @@ public class CityInfo { private String name; private GeoPosition position; + public String getName() { + return name; + } + + public GeoPosition getPosition() { + return position; + } + /** * Ctor. * diff --git a/module05/src/main/java/ru/sberbank/edu/GeoPosition.java b/module05/src/main/java/ru/sberbank/edu/GeoPosition.java index 2dc4e21b..575c96e9 100644 --- a/module05/src/main/java/ru/sberbank/edu/GeoPosition.java +++ b/module05/src/main/java/ru/sberbank/edu/GeoPosition.java @@ -15,6 +15,14 @@ public class GeoPosition { */ private double longitude; + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + /** * Ctor. * @@ -24,6 +32,51 @@ public class GeoPosition { */ public GeoPosition(String latitudeGradus, String longitudeGradus) { // parse and set latitude and longitude in radian + + int degres = 0; + int minutes = 0; + double seconds = 0; + + String[] splitLatitude = latitudeGradus.split("\\(|\\'{1,2}\\)*"); + String[] splitLongitude = longitudeGradus.split("\\(|\\'{1,2}\\)*"); +/** + Считаем широту + */ + for(int i =0; i <= splitLongitude.length; i++){ + switch (i) { + case (0): + degres = Integer.parseInt(splitLongitude[i]); + break; + case (1): + minutes = Integer.parseInt(splitLongitude[i]); + break; + case (2): + seconds = Double.parseDouble(splitLongitude[i]); + break; + default: + break; + } + } + longitude = ( degres + (minutes + seconds /60)/60) * Math.PI/180; + /** + Считаем долготу + */ + for(int i =0; i <= splitLatitude.length; i++){ + switch (i) { + case (0): + degres = Integer.parseInt(splitLatitude[i]); + break; + case (1): + minutes = Integer.parseInt(splitLatitude[i]); + break; + case (2): + seconds = Double.parseDouble(splitLongitude[i]); + break; + default: + break; + } + } + latitude = ( degres + (minutes + seconds /60)/60) * Math.PI/180; } // getters and toString diff --git a/module05/src/main/java/ru/sberbank/edu/TravelService.java b/module05/src/main/java/ru/sberbank/edu/TravelService.java index 830d188c..756dd749 100644 --- a/module05/src/main/java/ru/sberbank/edu/TravelService.java +++ b/module05/src/main/java/ru/sberbank/edu/TravelService.java @@ -8,9 +8,12 @@ */ public class TravelService { + final double RADIUS = 6372795; + // do not change type private final List cities = new ArrayList<>(); + /** * Append city info. * @@ -19,6 +22,7 @@ public class TravelService { */ public void add(CityInfo cityInfo) { // do something + cities.add(cityInfo); } /** @@ -29,13 +33,14 @@ public void add(CityInfo cityInfo) { */ public void remove(String cityName) { // do something + cities.removeIf(f -> cityName.equalsIgnoreCase(f.getName())); } /** * Get cities names. */ public List citiesNames() { - return null; + return cities.stream().map(CityInfo::getName).toList(); } /** @@ -46,8 +51,37 @@ public List citiesNames() { * @param destCityName - destination city * @throws IllegalArgumentException if source or destination city doesn't exist. */ + + /** + * + * Много долгих скучных вычислений + * + */ public int getDistance(String srcCityName, String destCityName) { - return 0; + /** + * подготовка данных для расчёта + */ + double srcLongitude = cities.stream().filter(f -> f.getName().equals(srcCityName)).toList().get(0).getPosition().getLongitude(); + double srcLatitude = cities.stream().filter(f -> f.getName().equals(srcCityName)).toList().get(0).getPosition().getLatitude(); + + double destLongitude = cities.stream().filter(f -> f.getName().equals(destCityName)).toList().get(0).getPosition().getLongitude(); + double destLatitude = cities.stream().filter(f -> f.getName().equals(destCityName)).toList().get(0).getPosition().getLatitude(); + + double cosLatitudeSrc = Math.cos(srcLatitude); + double cosLatitudeDest = Math.cos(destLatitude); + double sinLatitudeSrc = Math.sin(srcLatitude); + double sinLatitudeDest = Math.sin(destLatitude); + double delta = destLongitude - srcLongitude; + double cosDelta = Math.cos(delta); + double sinDelta = Math.sin(delta); + + double y = Math.sqrt(Math.pow(cosLatitudeDest * sinDelta, 2) + Math.pow(cosLatitudeSrc * sinLatitudeDest - sinLatitudeSrc * cosLatitudeDest * cosDelta, 2)); + double x = sinLatitudeSrc * sinLatitudeDest + cosLatitudeSrc * cosLatitudeDest * cosDelta; + + double dist = Math.atan2(y, x) * RADIUS; + + return (int)dist; + } /** @@ -57,7 +91,8 @@ public int getDistance(String srcCityName, String destCityName) { * @param radius - radius in kilometers for search * @throws IllegalArgumentException if city with cityName city doesn't exist. */ - public List getCitiesNear(String cityName, int radius) { - return null; + public List getCitiesNear(String cityName, int radius) { + return cities.stream().filter(f -> ((this.getDistance(cityName, f.getName()) <= radius))).toList(); } + } diff --git a/module05/src/test/java/ru/sberbank/edu/AppTest.java b/module05/src/test/java/ru/sberbank/edu/AppTest.java index 895d735c..a1593ecf 100644 --- a/module05/src/test/java/ru/sberbank/edu/AppTest.java +++ b/module05/src/test/java/ru/sberbank/edu/AppTest.java @@ -1,38 +1,31 @@ package ru.sberbank.edu; -import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } +public class AppTest + extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } + @Test + void main(){ + CityInfo moscow = new CityInfo("Moscow", new GeoPosition("55(45'4.4784'')", "37(37'6.3228'')")); + //53° 12′ 10″ N, 50° 8′ 27″ E + CityInfo samara = new CityInfo("Samara", new GeoPosition("53(12' 10'')", "50(8'27'')")); + + TravelService ts = new TravelService(); + + ts.add(moscow); + ts.add(samara); - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); + Assertions.assertThat(ts.getDistance("Moscow","Samara")).isEqualTo(855832); + + Assertions.assertThat(ts.getCitiesNear("Moscow",1000000).get(1).getName()).isEqualTo("Samara"); } } + +