System.java now saves all distances after one calculation to fasten random_destinations methods up

random_destinations_fix
Selim Eser 2024-06-09 21:51:37 +02:00
parent 1aed134131
commit e2923ae838
2 changed files with 36 additions and 43 deletions

View File

@ -20,6 +20,7 @@ public class System {
private User current_user = new User(); private User current_user = new User();
private String api_key; private String api_key;
private ArrayList<String> distances = new ArrayList<>();
public System(String api_key) { public System(String api_key) {
this.api_key = api_key; this.api_key = api_key;
@ -82,23 +83,15 @@ public class System {
public ArrayList<String> random_destinations_car(){ public ArrayList<String> random_destinations_car(){
ArrayList<String> mem = new ArrayList<>(); calc_all_distances();
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { ArrayList<String> mem = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
line = line.replace("\"", "");
if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) > 150)
mem.add(line);
}
} catch (Exception e) {}
ArrayList<String> result = new ArrayList<>(); ArrayList<String> result = new ArrayList<>();
for(int i = 0; i<this.distances.size(); i++){
if(Double.parseDouble(this.distances.get(i).split(";")[2])>150)
mem.add(this.distances.get(i));
}
for(int i = 0; i<3; i++) for(int i = 0; i<3; i++)
result.add(mem.get((int) (Math.random()*mem.size()))); result.add(mem.get((int) (Math.random()*mem.size())));
@ -107,23 +100,16 @@ public class System {
} }
public ArrayList<String> random_destinations_bike(){ public ArrayList<String> random_destinations_bike(){
ArrayList<String> mem = new ArrayList<>();
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); calc_all_distances();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { ArrayList<String> mem = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
line = line.replace("\"", "");
if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) < 100)
mem.add(line);
}
} catch (Exception e) {}
ArrayList<String> result = new ArrayList<>(); ArrayList<String> result = new ArrayList<>();
for(int i = 0; i<this.distances.size(); i++){
if(Double.parseDouble(this.distances.get(i).split(";")[2])<100)
mem.add(this.distances.get(i));
}
for(int i = 0; i<3; i++) for(int i = 0; i<3; i++)
result.add(mem.get((int) (Math.random()*mem.size()))); result.add(mem.get((int) (Math.random()*mem.size())));
@ -308,6 +294,25 @@ public class System {
} }
public void calc_all_distances(){
if(!this.distances.isEmpty())
return;
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.replace("\"", "");
this.distances.add(line.split(";")[0] + ";" + line.split(";")[1] + ";" + Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")));
}
} catch (Exception e) {}
}
public String[] travel_time(String destination_zip){ public String[] travel_time(String destination_zip){
String[] result = new String[2]; String[] result = new String[2];

View File

@ -9,19 +9,17 @@ import org.junit.jupiter.api.Test;
public class SystemTest { public class SystemTest {
System current_system = new System("35a75437476f12302f72e55d368485db");
@Test @Test
public void weather_forecast(){ public void weather_forecast(){
System current_system = new System("35a75437476f12302f72e55d368485db");
assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.weather_forecast("68161")); assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.weather_forecast("68161"));
} }
@Test @Test
public void current_weather(){ public void current_weather(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.current_weather()); assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.current_weather());
@ -30,8 +28,6 @@ public class SystemTest {
@Test @Test
public void search(){ public void search(){
System current_system = new System("35a75437476f12302f72e55d368485db");
ArrayList<String> list = current_system.search("Mannheim"); ArrayList<String> list = current_system.search("Mannheim");
assertEquals("68159", list.get(0).split(";")[0]); assertEquals("68159", list.get(0).split(";")[0]);
@ -42,8 +38,6 @@ public class SystemTest {
@Test @Test
public void distance(){ public void distance(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
assertEquals("88.46 km", current_system.distance("60306")); // Frankfurt assertEquals("88.46 km", current_system.distance("60306")); // Frankfurt
@ -55,8 +49,6 @@ public class SystemTest {
@Test @Test
public void travel_time(){ public void travel_time(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
current_system.set_current_user_car_avg_kmh(100); current_system.set_current_user_car_avg_kmh(100);
current_system.set_current_user_bike_avg_kmh(20); current_system.set_current_user_bike_avg_kmh(20);
@ -71,8 +63,6 @@ public class SystemTest {
@Test @Test
public void calc_l_consumption(){ public void calc_l_consumption(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
current_system.set_current_user_car_avg_kmh(100); current_system.set_current_user_car_avg_kmh(100);
current_system.set_current_user_car_l_100km(10); current_system.set_current_user_car_l_100km(10);
@ -85,8 +75,6 @@ public class SystemTest {
@Test @Test
public void random_destinations(){ public void random_destinations(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
assertEquals(3, current_system.random_destinations_car().size()); // random_destinations_car gibt genau 3 destinations zurück assertEquals(3, current_system.random_destinations_car().size()); // random_destinations_car gibt genau 3 destinations zurück