Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions module05/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,50 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>module05</artifactId>
<packaging>jar</packaging>

<name>module05</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<junit.version>5.9.2</junit.version>
<mockito.version>5.5.0</mockito.version>
<mockito.junit.version>5.3.1</mockito.junit.version>
<assertj.version>3.24.2</assertj.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<!-- ExtendWith -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>



</project>
22 changes: 17 additions & 5 deletions module05/src/main/java/ru/sberbank/edu/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
8 changes: 8 additions & 0 deletions module05/src/main/java/ru/sberbank/edu/CityInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ public class CityInfo {
private String name;
private GeoPosition position;

public String getName() {
return name;
}

public GeoPosition getPosition() {
return position;
}

/**
* Ctor.
*
Expand Down
53 changes: 53 additions & 0 deletions module05/src/main/java/ru/sberbank/edu/GeoPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class GeoPosition {
*/
private double longitude;

public double getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

/**
* Ctor.
*
Expand All @@ -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
Expand Down
43 changes: 39 additions & 4 deletions module05/src/main/java/ru/sberbank/edu/TravelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
*/
public class TravelService {

final double RADIUS = 6372795;

// do not change type
private final List<CityInfo> cities = new ArrayList<>();


/**
* Append city info.
*
Expand All @@ -19,6 +22,7 @@ public class TravelService {
*/
public void add(CityInfo cityInfo) {
// do something
cities.add(cityInfo);
}

/**
Expand All @@ -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<String> citiesNames() {
return null;
return cities.stream().map(CityInfo::getName).toList();
}

/**
Expand All @@ -46,8 +51,37 @@ public List<String> 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;

}

/**
Expand All @@ -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<String> getCitiesNear(String cityName, int radius) {
return null;
public List<CityInfo> getCitiesNear(String cityName, int radius) {
return cities.stream().filter(f -> ((this.getDistance(cityName, f.getName()) <= radius))).toList();
}

}
45 changes: 19 additions & 26 deletions module05/src/test/java/ru/sberbank/edu/AppTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}


12 changes: 12 additions & 0 deletions module06/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,17 @@
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
25 changes: 25 additions & 0 deletions module06/src/main/java/ru/sberbank/edu/CarBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public static void main(String[] args) throws Exception {
CarService carService = new CarServiceImpl(carRepository);

carService.addCar("777", "Lada");
carService.addCar("7", "Gazel");
carService.addCar("77", "VAZ");
carService.addCar("7777", "UAZ");

// Test check start
String readAllCarsSql = "SELECT * FROM car";
Expand All @@ -33,6 +36,28 @@ public static void main(String[] args) throws Exception {
System.out.println("id=" + id + "; model=" + model);
}
// Test end
carService.deleteCar("77");

readAllCarsSql = "SELECT * FROM car";
statement = H2DbEmbedded.getConnection().createStatement();
resultSet = statement.executeQuery(readAllCarsSql);

while (resultSet.next()) {
String id = resultSet.getString(1);
String model = resultSet.getString(2);
System.out.println("id=" + id + "; model=" + model);
}

System.out.println(carService.findAll());

System.out.println(carService.findByModel("UAZ"));

System.out.println(carService.findAll());

System.out.println(carService.deleteAll());

System.out.println(carService.findAll());

}
server.stop();
}
Expand Down
Loading