Merge branch 'sign_in_up_out'
commit
1a8f2bfa74
5
pom.xml
5
pom.xml
|
@ -28,6 +28,11 @@
|
||||||
<artifactId>json</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>20210307</version>
|
<version>20210307</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>1.17.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<resources>
|
<resources>
|
||||||
|
|
|
@ -1,68 +1,246 @@
|
||||||
package de.hs_mannheim.domain;
|
package de.hs_mannheim.domain;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.net.http.HttpResponse.BodyHandlers;
|
import java.net.http.HttpResponse.BodyHandlers;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class System {
|
public class System {
|
||||||
|
|
||||||
private User current_user = new User();
|
private User current_user = new User();
|
||||||
|
private ArrayList<User> all_user = new ArrayList<>();
|
||||||
private String api_key;
|
private String api_key;
|
||||||
private ArrayList<String> distances = new ArrayList<>();
|
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;
|
||||||
|
get_all_user();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set_current_user_zip(String zip){
|
public void set_current_user_zip(String zip) {
|
||||||
this.current_user.setZip(Integer.parseInt(zip));
|
this.current_user.setZip(Integer.parseInt(zip));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set_current_user_car_l_100km(double car_l_100km){
|
public void set_current_user_car_l_100km(double car_l_100km) {
|
||||||
this.current_user.setCar_l_100km(car_l_100km);
|
this.current_user.setCar_l_100km(car_l_100km);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set_current_user_car_avg_kmh(double car_avg_kmh){
|
public void set_current_user_car_avg_kmh(double car_avg_kmh) {
|
||||||
this.current_user.setCar_avg_kmh(car_avg_kmh);
|
this.current_user.setCar_avg_kmh(car_avg_kmh);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set_current_user_bike_avg_kmh(double bike_avg_kmh){
|
public void set_current_user_bike_avg_kmh(double bike_avg_kmh) {
|
||||||
this.current_user.setBike_avg_kmh(bike_avg_kmh);
|
this.current_user.setBike_avg_kmh(bike_avg_kmh);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<User> get_all_user(){
|
public static String decoding(String string) {
|
||||||
return new HashSet<User>();
|
byte[] binary_data = new byte[string.length()];
|
||||||
|
for(int i = 0; i < string.length(); i++) {
|
||||||
|
binary_data[i] = (byte)string.charAt(i);
|
||||||
|
}
|
||||||
|
return new String(Base64.decodeBase64(binary_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sign_in_user(String username, String password){
|
public static String encoding(String string) {
|
||||||
|
byte[] binary_data = new byte[string.length()];
|
||||||
|
for (int i = 0; i < string.length(); i++) {
|
||||||
|
binary_data[i] = (byte) string.charAt(i);
|
||||||
|
}
|
||||||
|
return Base64.encodeBase64String(binary_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int parseInt(String s) throws NumberFormatException{
|
||||||
|
if(s.equals(""))
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return Integer.parseInt(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parseDouble(String s) throws NumberFormatException {
|
||||||
|
if(s.equals(""))
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return Double.parseDouble(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void get_all_user() {
|
||||||
|
String[] fileString = new String[8];
|
||||||
|
|
||||||
|
InputStream inputStream = System.class.getResourceAsStream("/user_data.csv");
|
||||||
|
String path;
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||||
|
while ((path = reader.readLine()) != null) {
|
||||||
|
fileString = path.split(";");
|
||||||
|
fileString[1] = decoding(fileString[1]);
|
||||||
|
this.all_user.add(new User(fileString[0], fileString[1], fileString[2],
|
||||||
|
Integer.parseInt(fileString[3]), fileString[4],
|
||||||
|
Double.parseDouble(fileString[5]),
|
||||||
|
Double.parseDouble(fileString[6]),
|
||||||
|
Double.parseDouble(fileString[7])));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> all_user_toString(){
|
||||||
|
|
||||||
|
ArrayList<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
for(User user : this.all_user){
|
||||||
|
result.add(user.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean sign_in_user(String username, String password) {
|
||||||
|
|
||||||
|
for (User user : this.all_user) {
|
||||||
|
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
|
||||||
|
current_user = new User(user.getUsername(), user.getPassword(),
|
||||||
|
user.getHometown(), user.getZip(), user.getCar_name(),
|
||||||
|
user.getCar_l_100km(), user.getCar_avg_kmh(), user.getBike_avg_kmh());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean sign_up_user(String username, String password, String hometown, String zipS,
|
||||||
|
String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){
|
||||||
|
|
||||||
|
int zip;
|
||||||
|
double car_l_100km;
|
||||||
|
double car_avg_kmh;
|
||||||
|
double bike_avg_kmh;
|
||||||
|
|
||||||
|
try{
|
||||||
|
zip = parseInt(zipS);
|
||||||
|
car_l_100km = parseDouble(car_l_100kmS);
|
||||||
|
car_avg_kmh = parseDouble(car_avg_kmhS);
|
||||||
|
bike_avg_kmh = parseDouble(bike_avg_kmhS);
|
||||||
|
} catch (NumberFormatException n){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals(""))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for(User user: this.all_user)
|
||||||
|
if(user.getUsername().equals(username))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
ArrayList<String> mem = search(zipS);
|
||||||
|
boolean bool = false;
|
||||||
|
|
||||||
|
for (String line: mem)
|
||||||
|
if(line.split(";")[1].equals(hometown)) {
|
||||||
|
bool = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!bool)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh);
|
||||||
|
|
||||||
|
this.all_user.add(current_user);
|
||||||
|
|
||||||
|
write_to_file(all_user_toString(), "src/main/resources/user_data.csv");
|
||||||
|
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sign_up_user(String username, String password, String hometown, int zip,
|
public boolean change_user_details(String username, String password, String hometown, String zipS,
|
||||||
String car_name, double car_co2_km, double car_avg_kmh, double bike_avg_kmh){
|
String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){
|
||||||
|
|
||||||
|
int zip;
|
||||||
|
double car_l_100km;
|
||||||
|
double car_avg_kmh;
|
||||||
|
double bike_avg_kmh;
|
||||||
|
|
||||||
|
try{
|
||||||
|
zip = parseInt(zipS);
|
||||||
|
car_l_100km = parseDouble(car_l_100kmS);
|
||||||
|
car_avg_kmh = parseDouble(car_avg_kmhS);
|
||||||
|
bike_avg_kmh = parseDouble(bike_avg_kmhS);
|
||||||
|
} catch (NumberFormatException n){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals(""))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for(User user: this.all_user)
|
||||||
|
if(user.getUsername().equals(username))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
ArrayList<String> mem = search(zipS);
|
||||||
|
boolean bool = false;
|
||||||
|
|
||||||
|
for (String line: mem)
|
||||||
|
if(line.split(";")[1].equals(hometown)) {
|
||||||
|
bool = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!bool)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
|
||||||
|
for(int i = 0; i< this.all_user.size(); i++)
|
||||||
|
if(this.all_user.get(i).getUsername().equals(current_user.getUsername()))
|
||||||
|
this.all_user.remove(i);
|
||||||
|
|
||||||
|
write_to_file(all_user_toString(), "src/main/resources/user_data.csv");
|
||||||
|
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
|
||||||
|
|
||||||
|
this.current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh);
|
||||||
|
this.all_user.add(current_user);
|
||||||
|
|
||||||
|
write_to_file(all_user_toString(), "src/main/resources/user_data.csv");
|
||||||
|
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sign_out_user(){
|
public void write_to_file(ArrayList<String> lines, String file) {
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
|
||||||
|
for (int i = 0; i < lines.size() - 1; i++) {
|
||||||
|
writer.write(lines.get(i));
|
||||||
|
writer.newLine();
|
||||||
|
}
|
||||||
|
writer.write(lines.getLast());
|
||||||
|
} catch (IOException e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sign_out_user() {
|
||||||
this.distances = new ArrayList<>();
|
this.distances = new ArrayList<>();
|
||||||
|
|
||||||
|
current_user = new User();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean change_user_details(String username, String password, String hometown, int zip,
|
public String[] getDetails(){
|
||||||
String car_name, double car_co2_km, double car_avg_kmh, double bike_avg_kmh){
|
return new String[]{current_user.getUsername(), current_user.getPassword(),
|
||||||
return true;
|
current_user.getHometown(),
|
||||||
|
current_user.getZip()==0?"":String.valueOf(current_user.getZip()),
|
||||||
|
current_user.getCar_name(),
|
||||||
|
current_user.getCar_l_100km()==0?"":String.valueOf(current_user.getCar_l_100km()),
|
||||||
|
current_user.getCar_avg_kmh()==0?"":String.valueOf(current_user.getCar_avg_kmh()),
|
||||||
|
current_user.getBike_avg_kmh()==0?"":String.valueOf(current_user.getBike_avg_kmh())};
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> search(String hometown_or_zip){
|
public ArrayList<String> search(String hometown_or_zip) {
|
||||||
|
|
||||||
TreeSet<String> zip_set = new TreeSet<>();
|
TreeSet<String> zip_set = new TreeSet<>();
|
||||||
|
|
||||||
|
@ -70,54 +248,55 @@ public class System {
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null && zip_set.size()<200) {
|
while ((line = reader.readLine()) != null && zip_set.size() < 200) {
|
||||||
if(line.contains(hometown_or_zip)){
|
if (line.contains(hometown_or_zip)) {
|
||||||
line = line.replace("\"", "");
|
line = line.replace("\"", "");
|
||||||
zip_set.add(line);
|
zip_set.add(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {}
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
return new ArrayList<>(zip_set);
|
return new ArrayList<>(zip_set);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> random_destinations_car(){
|
public ArrayList<String> random_destinations_car() {
|
||||||
|
|
||||||
calc_all_distances();
|
calc_all_distances();
|
||||||
|
|
||||||
ArrayList<String> mem = new ArrayList<>();
|
ArrayList<String> mem = new ArrayList<>();
|
||||||
ArrayList<String> result = new ArrayList<>();
|
ArrayList<String> result = new ArrayList<>();
|
||||||
|
|
||||||
for(int i = 0; i<this.distances.size(); i++){
|
for (int i = 0; i < this.distances.size(); i++) {
|
||||||
if(Double.parseDouble(this.distances.get(i).split(";")[2])>150)
|
if (Double.parseDouble(this.distances.get(i).split(";")[2]) > 150)
|
||||||
mem.add(this.distances.get(i));
|
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())));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> random_destinations_bike(){
|
public ArrayList<String> random_destinations_bike() {
|
||||||
|
|
||||||
calc_all_distances();
|
calc_all_distances();
|
||||||
|
|
||||||
ArrayList<String> mem = new ArrayList<>();
|
ArrayList<String> mem = new ArrayList<>();
|
||||||
ArrayList<String> result = new ArrayList<>();
|
ArrayList<String> result = new ArrayList<>();
|
||||||
|
|
||||||
for(int i = 0; i<this.distances.size(); i++){
|
for (int i = 0; i < this.distances.size(); i++) {
|
||||||
if(Double.parseDouble(this.distances.get(i).split(";")[2])<100)
|
if (Double.parseDouble(this.distances.get(i).split(";")[2]) < 100)
|
||||||
mem.add(this.distances.get(i));
|
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())));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] destination_details(String destination_zip){
|
public String[] destination_details(String destination_zip) {
|
||||||
String[] result = new String[5];
|
String[] result = new String[5];
|
||||||
|
|
||||||
result[0] = distance(destination_zip); // Entfernung
|
result[0] = distance(destination_zip); // Entfernung
|
||||||
|
@ -129,34 +308,35 @@ public class System {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String current_weather(){
|
public String current_weather() {
|
||||||
|
|
||||||
String weather = "";
|
String weather = "";
|
||||||
double temperature = 1;
|
double temperature = 1;
|
||||||
|
|
||||||
try{
|
try {
|
||||||
HttpClient http_client = HttpClient.newHttpClient();
|
HttpClient http_client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
HttpRequest get_request = HttpRequest.newBuilder()
|
HttpRequest get_request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip="+current_user.getZip()+",de&appid="+api_key+"&units=metric&lang=de"))
|
.uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip=" + current_user.getZip() + ",de&appid=" + api_key + "&units=metric&lang=de"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
|
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
|
||||||
|
|
||||||
JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
|
JSONObject json = new JSONObject(((String) get_response.body()).substring(0, ((String) get_response.body()).length()));
|
||||||
weather = json.getJSONArray("weather").getJSONObject(0).getString("description");
|
weather = json.getJSONArray("weather").getJSONObject(0).getString("description");
|
||||||
temperature = json.getJSONObject("main").getDouble("temp");
|
temperature = json.getJSONObject("main").getDouble("temp");
|
||||||
} catch (Exception e){}
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
if(weather.equals(""))
|
if (weather.equals(""))
|
||||||
return "Es ist ein Fehler aufgetreten!";
|
return "Es ist ein Fehler aufgetreten!";
|
||||||
else
|
else
|
||||||
return weather + ": " + temperature + " °C";
|
return weather + ": " + temperature + " °C";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String weather_forecast(String destination_zip){
|
public String weather_forecast(String destination_zip) {
|
||||||
|
|
||||||
String weather_day_1 = "";
|
String weather_day_1 = "";
|
||||||
String weather_day_2 = "";
|
String weather_day_2 = "";
|
||||||
|
@ -190,17 +370,17 @@ public class System {
|
||||||
double temperature_day_3_low = 1;
|
double temperature_day_3_low = 1;
|
||||||
|
|
||||||
|
|
||||||
try{
|
try {
|
||||||
HttpClient http_client = HttpClient.newHttpClient();
|
HttpClient http_client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
HttpRequest get_request = HttpRequest.newBuilder()
|
HttpRequest get_request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip="+destination_zip+",de&appid="+api_key+"&units=metric&lang=de"))
|
.uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip=" + destination_zip + ",de&appid=" + api_key + "&units=metric&lang=de"))
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
|
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
|
||||||
|
|
||||||
JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
|
JSONObject json = new JSONObject(((String) get_response.body()).substring(0, ((String) get_response.body()).length()));
|
||||||
weather_day_1 = json.getJSONArray("list").getJSONObject(11).getJSONArray("weather").getJSONObject(0).getString("description");
|
weather_day_1 = json.getJSONArray("list").getJSONObject(11).getJSONArray("weather").getJSONObject(0).getString("description");
|
||||||
weather_day_2 = json.getJSONArray("list").getJSONObject(19).getJSONArray("weather").getJSONObject(0).getString("description");
|
weather_day_2 = json.getJSONArray("list").getJSONObject(19).getJSONArray("weather").getJSONObject(0).getString("description");
|
||||||
weather_day_3 = json.getJSONArray("list").getJSONObject(27).getJSONArray("weather").getJSONObject(0).getString("description");
|
weather_day_3 = json.getJSONArray("list").getJSONObject(27).getJSONArray("weather").getJSONObject(0).getString("description");
|
||||||
|
@ -220,14 +400,15 @@ public class System {
|
||||||
temperature_day_3_3 = json.getJSONArray("list").getJSONObject(28).getJSONObject("main").getDouble("temp");
|
temperature_day_3_3 = json.getJSONArray("list").getJSONObject(28).getJSONObject("main").getDouble("temp");
|
||||||
temperature_day_3_4 = json.getJSONArray("list").getJSONObject(30).getJSONObject("main").getDouble("temp");
|
temperature_day_3_4 = json.getJSONArray("list").getJSONObject(30).getJSONObject("main").getDouble("temp");
|
||||||
|
|
||||||
} catch (Exception e){}
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
temperature_day_1.add(temperature_day_1_1);
|
temperature_day_1.add(temperature_day_1_1);
|
||||||
temperature_day_1.add(temperature_day_1_2);
|
temperature_day_1.add(temperature_day_1_2);
|
||||||
temperature_day_1.add(temperature_day_1_3);
|
temperature_day_1.add(temperature_day_1_3);
|
||||||
temperature_day_1.add(temperature_day_1_4);
|
temperature_day_1.add(temperature_day_1_4);
|
||||||
|
|
||||||
temperature_day_1_high = (double) temperature_day_1.toArray()[temperature_day_1.size()-1];
|
temperature_day_1_high = (double) temperature_day_1.toArray()[temperature_day_1.size() - 1];
|
||||||
temperature_day_1_low = (double) temperature_day_1.toArray()[0];
|
temperature_day_1_low = (double) temperature_day_1.toArray()[0];
|
||||||
|
|
||||||
temperature_day_2.add(temperature_day_2_1);
|
temperature_day_2.add(temperature_day_2_1);
|
||||||
|
@ -235,7 +416,7 @@ public class System {
|
||||||
temperature_day_2.add(temperature_day_2_3);
|
temperature_day_2.add(temperature_day_2_3);
|
||||||
temperature_day_2.add(temperature_day_2_4);
|
temperature_day_2.add(temperature_day_2_4);
|
||||||
|
|
||||||
temperature_day_2_high = (double) temperature_day_2.toArray()[temperature_day_1.size()-1];
|
temperature_day_2_high = (double) temperature_day_2.toArray()[temperature_day_1.size() - 1];
|
||||||
temperature_day_2_low = (double) temperature_day_2.toArray()[0];
|
temperature_day_2_low = (double) temperature_day_2.toArray()[0];
|
||||||
|
|
||||||
temperature_day_3.add(temperature_day_3_1);
|
temperature_day_3.add(temperature_day_3_1);
|
||||||
|
@ -243,18 +424,18 @@ public class System {
|
||||||
temperature_day_3.add(temperature_day_3_3);
|
temperature_day_3.add(temperature_day_3_3);
|
||||||
temperature_day_3.add(temperature_day_3_4);
|
temperature_day_3.add(temperature_day_3_4);
|
||||||
|
|
||||||
temperature_day_3_high = (double) temperature_day_3.toArray()[temperature_day_1.size()-1];
|
temperature_day_3_high = (double) temperature_day_3.toArray()[temperature_day_1.size() - 1];
|
||||||
temperature_day_3_low = (double) temperature_day_3.toArray()[0];
|
temperature_day_3_low = (double) temperature_day_3.toArray()[0];
|
||||||
|
|
||||||
if(weather_day_1.equals("")||weather_day_2.equals("")||weather_day_3.equals(""))
|
if (weather_day_1.equals("") || weather_day_2.equals("") || weather_day_3.equals(""))
|
||||||
return "Es ist ein Fehler aufgetreten!";
|
return "Es ist ein Fehler aufgetreten!";
|
||||||
else
|
else
|
||||||
return "Morgen: " + weather_day_1 + ": Minimum: " + temperature_day_1_low + " °C" + "; Maximum: " + temperature_day_1_high + " °C\n"
|
return "Morgen: " + weather_day_1 + ": Minimum: " + temperature_day_1_low + " °C" + "; Maximum: " + temperature_day_1_high + " °C\n"
|
||||||
+ "Übermorgen: " + weather_day_2 + ": Minimum: " + temperature_day_2_low + " °C" + "; Maximum: " + temperature_day_2_high + " °C\n"
|
+ "Übermorgen: " + weather_day_2 + ": Minimum: " + temperature_day_2_low + " °C" + "; Maximum: " + temperature_day_2_high + " °C\n"
|
||||||
+ "Überübermorgen: " + weather_day_3 + ": Minimum: " + temperature_day_3_low + " °C" + "; Maximum: " + temperature_day_3_high + " °C";
|
+ "Überübermorgen: " + weather_day_3 + ": Minimum: " + temperature_day_3_low + " °C" + "; Maximum: " + temperature_day_3_high + " °C";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String distance(String destination_zip){
|
public String distance(String destination_zip) {
|
||||||
|
|
||||||
double lon1 = -1;
|
double lon1 = -1;
|
||||||
double lon2 = -1;
|
double lon2 = -1;
|
||||||
|
@ -268,35 +449,36 @@ public class System {
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
line = line.replace("\"", "");
|
line = line.replace("\"", "");
|
||||||
|
|
||||||
if(line.split(";")[0].equals(destination_zip)){
|
if (line.split(";")[0].equals(destination_zip)) {
|
||||||
lon2 = Double.parseDouble(line.split(";")[2]);
|
lon2 = Double.parseDouble(line.split(";")[2]);
|
||||||
lat2 = Double.parseDouble(line.split(";")[3]);
|
lat2 = Double.parseDouble(line.split(";")[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(line.split(";")[0].equals(""+current_user.getZip())){
|
if (line.split(";")[0].equals("" + current_user.getZip())) {
|
||||||
lon1 = Double.parseDouble(line.split(";")[2]);
|
lon1 = Double.parseDouble(line.split(";")[2]);
|
||||||
lat1 = Double.parseDouble(line.split(";")[3]);
|
lat1 = Double.parseDouble(line.split(";")[3]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {}
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
if(lon1==-1||lon2==-1||lat1==-1||lat2==-1)
|
if (lon1 == -1 || lon2 == -1 || lat1 == -1 || lat2 == -1)
|
||||||
return "Es ist ein Fehler aufgetreten!";
|
return "Es ist ein Fehler aufgetreten!";
|
||||||
|
|
||||||
double dLat = lat2-lat1;
|
double dLat = lat2 - lat1;
|
||||||
double dLon = lon2-lon1;
|
double dLon = lon2 - lon1;
|
||||||
|
|
||||||
double a = Math.pow(Math.sin(Math.toRadians(dLat/2.0)), 2) + Math.pow(Math.sin(Math.toRadians(dLon/2.0)), 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
|
double a = Math.pow(Math.sin(Math.toRadians(dLat / 2.0)), 2) + Math.pow(Math.sin(Math.toRadians(dLon / 2.0)), 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
|
||||||
|
|
||||||
double distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a));
|
double distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
|
||||||
|
|
||||||
return "" + (Math.round((distance * 1.25)*1000)/1000.0) + " km";
|
return "" + (Math.round((distance * 1.25) * 1000) / 1000.0) + " km";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void calc_all_distances(){
|
public void calc_all_distances() {
|
||||||
|
|
||||||
if(!this.distances.isEmpty())
|
if (!this.distances.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
InputStream inputStream = System.class.getResourceAsStream("/zip.csv");
|
InputStream inputStream = System.class.getResourceAsStream("/zip.csv");
|
||||||
|
@ -310,31 +492,32 @@ public class System {
|
||||||
this.distances.add(line.split(";")[0] + ";" + line.split(";")[1] + ";" + Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")));
|
this.distances.add(line.split(";")[0] + ";" + line.split(";")[1] + ";" + Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")));
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {}
|
} 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];
|
||||||
|
|
||||||
if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")){
|
if (distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")) {
|
||||||
result[0] = "Es ist ein Fehler aufgetreten!";
|
result[0] = "Es ist ein Fehler aufgetreten!";
|
||||||
result[1] = "Es ist ein Fehler aufgetreten!";
|
result[1] = "Es ist ein Fehler aufgetreten!";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
result[0] = "" + (Math.round(((Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getCar_avg_kmh()))*1000) / 1000.0) + " h";
|
result[0] = "" + (Math.round(((Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getCar_avg_kmh())) * 1000) / 1000.0) + " h";
|
||||||
result[1] = "" + (Math.round(((Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getBike_avg_kmh()))*1000) / 1000.0) + " h";
|
result[1] = "" + (Math.round(((Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getBike_avg_kmh())) * 1000) / 1000.0) + " h";
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String calc_l_consumption(String destination_zip){
|
public String calc_l_consumption(String destination_zip) {
|
||||||
|
|
||||||
if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!"))
|
if (distance(destination_zip).equals("Es ist ein Fehler aufgetreten!"))
|
||||||
return "Es ist ein Fehler aufgetreten!";
|
return "Es ist ein Fehler aufgetreten!";
|
||||||
|
|
||||||
return "" + (Math.round((Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100.0))*1000)/1000.0) + " l";
|
return "" + (Math.round((Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100.0)) * 1000) / 1000.0) + " l";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,11 +5,25 @@ public class User {
|
||||||
private String username = "";
|
private String username = "";
|
||||||
private String password = "";
|
private String password = "";
|
||||||
private String hometown = "";
|
private String hometown = "";
|
||||||
private int zip;
|
private int zip = 0;
|
||||||
private String car_name = "";
|
private String car_name = "";
|
||||||
private double car_l_100km;
|
private double car_l_100km = 0;
|
||||||
private double car_avg_kmh;
|
private double car_avg_kmh = 0;
|
||||||
private double bike_avg_kmh;
|
private double bike_avg_kmh = 0;
|
||||||
|
|
||||||
|
public User(){}
|
||||||
|
|
||||||
|
public User(String username, String password, String hometown, int zip
|
||||||
|
, String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh){
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.hometown = hometown;
|
||||||
|
this.zip = zip;
|
||||||
|
this.car_name = car_name;
|
||||||
|
this.car_l_100km = car_l_100km;
|
||||||
|
this.car_avg_kmh = car_avg_kmh;
|
||||||
|
this.bike_avg_kmh = bike_avg_kmh;
|
||||||
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
|
@ -74,4 +88,9 @@ public class User {
|
||||||
public void setBike_avg_kmh(double bike_avg_kmh) {
|
public void setBike_avg_kmh(double bike_avg_kmh) {
|
||||||
this.bike_avg_kmh = bike_avg_kmh;
|
this.bike_avg_kmh = bike_avg_kmh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
return this.username + ";" + System.encoding(this.password) + ";" + this.hometown + ";" + this.zip + ";" + this.car_name + ";"
|
||||||
|
+ this.car_l_100km + ";" + this.car_avg_kmh + ";" + this.bike_avg_kmh;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2
|
||||||
|
David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0
|
|
|
@ -86,4 +86,68 @@ public class SystemTest {
|
||||||
assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100);
|
assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void encoding(){
|
||||||
|
|
||||||
|
String test_password = "123Esel";
|
||||||
|
|
||||||
|
assertEquals("MTIzRXNlbA==",System.encoding(test_password));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void decoding(){
|
||||||
|
|
||||||
|
String test_password = "MTIzRXNlbA==";
|
||||||
|
|
||||||
|
assertEquals("123Esel",System.decoding(test_password));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sign_in_user() {
|
||||||
|
|
||||||
|
assertEquals(true, current_system.sign_in_user("Daniel", "1401Daniel"));
|
||||||
|
assertEquals("Daniel",current_system.getDetails()[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sign_out_user() {
|
||||||
|
|
||||||
|
assertEquals(true, current_system.sign_in_user("David", "123Esel"));
|
||||||
|
assertEquals("David",current_system.getDetails()[0]);
|
||||||
|
current_system.sign_out_user();
|
||||||
|
assertEquals("",current_system.getDetails()[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sign_up_user(){
|
||||||
|
// Username darf nicht doppelt vorkommen!
|
||||||
|
assertEquals(false, current_system.sign_up_user("David","123Esel","Mannheim","68161","AMG","10","300","20"));
|
||||||
|
assertEquals(true, current_system.sign_up_user("Selim","Penis69","Mannheim","68161","AMG","10","300","20"));
|
||||||
|
// PLZ muss mit Stadt übereinstimmen
|
||||||
|
assertEquals(false, current_system.sign_up_user("Lukas","123Esel","Mannheim","11105","AMG","10","300","20"));
|
||||||
|
assertEquals(true, current_system.sign_up_user("Lukas","123Esel","Mannheim","68305","AMG","10","300","20"));
|
||||||
|
|
||||||
|
assertEquals("Lukas",current_system.getDetails()[0]);
|
||||||
|
current_system.sign_out_user();
|
||||||
|
assertEquals("",current_system.getDetails()[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void change_user_details(){
|
||||||
|
|
||||||
|
current_system.sign_in_user("David", "123Esel");
|
||||||
|
current_system.change_user_details("Enes", "Penis123", "Mannheim", "68161", "", "", "", "");
|
||||||
|
assertEquals("Enes", current_system.getDetails()[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Tests auf Basis von user_data.csv: Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2
|
||||||
|
David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2
|
||||||
|
David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0
|
|
Loading…
Reference in New Issue