- Refactoring
- Reformatting - Improved unit
This commit is contained in:
parent
7aeee2e593
commit
07dbc7d494
@ -46,6 +46,7 @@ public class Main {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
|
* Parse commandline parameters
|
||||||
*
|
*
|
||||||
* @param args String that contains program arguments
|
* @param args String that contains program arguments
|
||||||
*/
|
*/
|
||||||
@ -53,28 +54,7 @@ public class Main {
|
|||||||
Parser.parse("/de/dhbw/contactdb/db/contacts2021.db");
|
Parser.parse("/de/dhbw/contactdb/db/contacts2021.db");
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
if (args[0].contains("--personensuche")) {
|
parseArguments(args[0]);
|
||||||
System.out.println(searchForPerson(getSearchString(args[0])));
|
|
||||||
} else if (args[0].contains("--ortssuche")) {
|
|
||||||
System.out.println(searchForPlace(getSearchString(args[0])));
|
|
||||||
} else if (args[0].contains("--kontaktpersonen")) {
|
|
||||||
int id = Integer.parseInt(getSearchString(args[0]));
|
|
||||||
|
|
||||||
System.out.println(getContactPersons(id).stream()
|
|
||||||
.filter(visit -> visit.getPerson().getId() != id)
|
|
||||||
.map(visit -> visit.getPerson().getName())
|
|
||||||
.sorted()
|
|
||||||
.distinct()
|
|
||||||
.collect(Collectors.joining(", ")));
|
|
||||||
} else if (args[0].contains("--besucher")) {
|
|
||||||
System.out.println(getVisitors(args[0]).stream()
|
|
||||||
.map(visit -> visit.getPerson().getName())
|
|
||||||
.sorted()
|
|
||||||
.distinct()
|
|
||||||
.collect(Collectors.joining(", ")));
|
|
||||||
} else {
|
|
||||||
argument_error();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
argument_error();
|
argument_error();
|
||||||
}
|
}
|
||||||
@ -87,14 +67,14 @@ public class Main {
|
|||||||
* @author florian kaiser
|
* @author florian kaiser
|
||||||
*/
|
*/
|
||||||
private static void argument_error() {
|
private static void argument_error() {
|
||||||
System.out.println("Invalid parameter! \nUsage [Option]=\"[Value]\"\n--personensuche\n--ortssuche\n--kontaktpersonen\n--besucher");
|
System.err.println("Invalid parameter! \nUsage [Option]=\"[Value]\"\n--personensuche\n--ortssuche\n--kontaktpersonen\n--besucher");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the HashMap places for a specific string
|
* Searches the HashMap places for a specific string
|
||||||
* @param search String that contains search value
|
|
||||||
*
|
*
|
||||||
|
* @param search String that contains search value
|
||||||
* @author florian kaiser
|
* @author florian kaiser
|
||||||
*/
|
*/
|
||||||
public static String searchForPlace(String search) {
|
public static String searchForPlace(String search) {
|
||||||
@ -110,8 +90,8 @@ public class Main {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the HashMap persons for a specific string
|
* Searches the HashMap persons for a specific string
|
||||||
* @param search String that contains search value
|
|
||||||
*
|
*
|
||||||
|
* @param search String that contains search value
|
||||||
* @author florian kaiser
|
* @author florian kaiser
|
||||||
*/
|
*/
|
||||||
public static String searchForPerson(String search) {
|
public static String searchForPerson(String search) {
|
||||||
@ -131,26 +111,25 @@ public class Main {
|
|||||||
*
|
*
|
||||||
* @param id Contains the id of a person
|
* @param id Contains the id of a person
|
||||||
* @return List that contains visits
|
* @return List that contains visits
|
||||||
*
|
|
||||||
* @author florian kaiser
|
* @author florian kaiser
|
||||||
*/
|
*/
|
||||||
public static List<Visit> getContactPersons(int id) {
|
public static List<Visit> getContactPersons(int id) {
|
||||||
List<Visit> places_where_id_was_indoor;
|
List<Visit> filtered_visits;
|
||||||
places_where_id_was_indoor = visits.stream()
|
filtered_visits = visits.stream()
|
||||||
.filter(visit -> visit.getPlace().isIndoor())
|
.filter(visit -> visit.getPlace().isIndoor())
|
||||||
.filter(visit -> visit.getPerson().getId() == id)
|
.filter(visit -> visit.getPerson().getId() == id)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
List<Visit> contacts_of_id = new ArrayList<>();
|
List<Visit> contacts = new ArrayList<>();
|
||||||
for (Visit filtered_b : places_where_id_was_indoor) {
|
for (Visit visit_1 : filtered_visits) {
|
||||||
for (Visit b : visits) {
|
for (Visit visit_2 : visits) {
|
||||||
if (b.isOverlapping(filtered_b.getStart(), filtered_b.getEnd()) && filtered_b.getPlace().getId() == b.getPlace().getId()) {
|
if (visit_2.isOverlapping(visit_1.getStart(), visit_1.getEnd()) && visit_1.getPlace().getId() == visit_2.getPlace().getId()) {
|
||||||
contacts_of_id.add(b);
|
contacts.add(visit_2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return contacts_of_id;
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -160,20 +139,19 @@ public class Main {
|
|||||||
*
|
*
|
||||||
* @param s contains program argument (id of place and date string, separated by comma)
|
* @param s contains program argument (id of place and date string, separated by comma)
|
||||||
* @return List that contains visits
|
* @return List that contains visits
|
||||||
*
|
|
||||||
* @author florian kaiser
|
* @author florian kaiser
|
||||||
*/
|
*/
|
||||||
public static List<Visit> getVisitors(String s) {
|
public static List<Visit> getVisitors(String s) {
|
||||||
int id = Integer.parseInt(s.split("=")[1].split(",")[0].replaceAll("\"", ""));
|
int id = Integer.parseInt(s.split(",")[0].replaceAll("\"", ""));
|
||||||
LocalDateTime d;
|
LocalDateTime d;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH:mm:ss");
|
||||||
d = LocalDateTime.parse(s.split("=")[1].split(",")[1].replaceAll("\"", ""), formatter);
|
d = LocalDateTime.parse(s.split(",")[1].replaceAll("\"", ""), formatter);
|
||||||
} catch (DateTimeParseException e1) {
|
} catch (DateTimeParseException e1) {
|
||||||
try {
|
try {
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH:mm");
|
||||||
d = LocalDateTime.parse(s.split("=")[1].split(",")[1].replaceAll("\"", ""), formatter);
|
d = LocalDateTime.parse(s.split(",")[1].replaceAll("\"", ""), formatter);
|
||||||
} catch (DateTimeParseException e2) {
|
} catch (DateTimeParseException e2) {
|
||||||
argument_error();
|
argument_error();
|
||||||
System.exit(-2);
|
System.exit(-2);
|
||||||
@ -182,21 +160,21 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LocalDateTime finalD = d;
|
LocalDateTime finalD = d;
|
||||||
List<Visit> place_date_visits = visits.stream()
|
List<Visit> filtered_visits = visits.stream()
|
||||||
.filter(visit -> visit.getPlace().getId() == id)
|
.filter(visit -> visit.getPlace().getId() == id)
|
||||||
.filter(visit -> visit.isInTimePeriod(finalD))
|
.filter(visit -> visit.isInTimePeriod(finalD))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
List<Visit> all = new ArrayList<>();
|
List<Visit> visitors = new ArrayList<>();
|
||||||
for (Visit visit : place_date_visits) {
|
for (Visit visit : filtered_visits) {
|
||||||
if (visit.getPlace().isIndoor()) {
|
if (visit.getPlace().isIndoor()) {
|
||||||
all.addAll(getContactPersons(visit.getPerson().getId()));
|
visitors.addAll(getContactPersons(visit.getPerson().getId()));
|
||||||
} else {
|
} else {
|
||||||
all.add(visit);
|
visitors.add(visit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return all;
|
return visitors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -222,4 +200,35 @@ public class Main {
|
|||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse commandline arguments and run specific functions after
|
||||||
|
*
|
||||||
|
* @param args Commandline arguments
|
||||||
|
*/
|
||||||
|
public static void parseArguments(String args) {
|
||||||
|
if (args.contains("--personensuche")) {
|
||||||
|
System.out.println(searchForPerson(getSearchString(args)));
|
||||||
|
} else if (args.contains("--ortssuche")) {
|
||||||
|
System.out.println(searchForPlace(getSearchString(args)));
|
||||||
|
} else if (args.contains("--kontaktpersonen")) {
|
||||||
|
int id = Integer.parseInt(getSearchString(args));
|
||||||
|
|
||||||
|
System.out.println(getContactPersons(id).stream()
|
||||||
|
.filter(visit -> visit.getPerson().getId() != id)
|
||||||
|
.map(visit -> visit.getPerson().getName())
|
||||||
|
.sorted()
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.joining(", ")));
|
||||||
|
} else if (args.contains("--besucher")) {
|
||||||
|
System.out.println(getVisitors(getSearchString(args)).stream()
|
||||||
|
.map(visit -> visit.getPerson().getName())
|
||||||
|
.sorted()
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.joining(", ")));
|
||||||
|
} else {
|
||||||
|
argument_error();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -23,6 +23,7 @@ public class Person {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
*
|
||||||
* @param id ID of person
|
* @param id ID of person
|
||||||
* @param name Name of person
|
* @param name Name of person
|
||||||
*/
|
*/
|
||||||
|
@ -9,26 +9,23 @@ package de.dhbw.contactdb;
|
|||||||
* @since 21.06.2021
|
* @since 21.06.2021
|
||||||
*/
|
*/
|
||||||
public class Place {
|
public class Place {
|
||||||
|
/**
|
||||||
|
* is place indoor
|
||||||
|
*/
|
||||||
|
private final boolean indoor;
|
||||||
/**
|
/**
|
||||||
* ID of place
|
* ID of place
|
||||||
*/
|
*/
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* name of place
|
* name of place
|
||||||
*/
|
*/
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* is place indoor
|
|
||||||
*/
|
|
||||||
private final boolean indoor;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
*
|
||||||
* @param id ID of place
|
* @param id ID of place
|
||||||
* @param name Name of place
|
* @param name Name of place
|
||||||
* @param indoor is place indoor
|
* @param indoor is place indoor
|
||||||
|
@ -11,32 +11,27 @@ import java.time.LocalDateTime;
|
|||||||
* @since 21.06.2021
|
* @since 21.06.2021
|
||||||
*/
|
*/
|
||||||
public class Visit {
|
public class Visit {
|
||||||
|
/**
|
||||||
|
* place where person is
|
||||||
|
*/
|
||||||
|
private final Place place;
|
||||||
/**
|
/**
|
||||||
* start date of visit
|
* start date of visit
|
||||||
*/
|
*/
|
||||||
private LocalDateTime start;
|
private LocalDateTime start;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* end date of visit
|
* end date of visit
|
||||||
*/
|
*/
|
||||||
private LocalDateTime end;
|
private LocalDateTime end;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* person who visits
|
* person who visits
|
||||||
*/
|
*/
|
||||||
private Person person;
|
private Person person;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* place where person is
|
|
||||||
*/
|
|
||||||
private final Place place;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
*
|
||||||
* @param start start date of visit
|
* @param start start date of visit
|
||||||
* @param end end date of visit
|
* @param end end date of visit
|
||||||
* @param person person who visits
|
* @param person person who visits
|
||||||
@ -109,7 +104,6 @@ public class Visit {
|
|||||||
/**
|
/**
|
||||||
* @param start Start of timespan
|
* @param start Start of timespan
|
||||||
* @param end End of timespan
|
* @param end End of timespan
|
||||||
*
|
|
||||||
* @return boolean if timespan between start and end is overlapping timespan between this visits start and end
|
* @return boolean if timespan between start and end is overlapping timespan between this visits start and end
|
||||||
*/
|
*/
|
||||||
public boolean isOverlapping(LocalDateTime start, LocalDateTime end) {
|
public boolean isOverlapping(LocalDateTime start, LocalDateTime end) {
|
||||||
@ -119,7 +113,6 @@ public class Visit {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param inputTime time
|
* @param inputTime time
|
||||||
*
|
|
||||||
* @return boolean if time is between this visits start and end
|
* @return boolean if time is between this visits start and end
|
||||||
*/
|
*/
|
||||||
public boolean isInTimePeriod(LocalDateTime inputTime) {
|
public boolean isInTimePeriod(LocalDateTime inputTime) {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package de.dhbw.contactdb.utils;
|
package de.dhbw.contactdb.utils;
|
||||||
|
|
||||||
import de.dhbw.contactdb.Place;
|
|
||||||
import de.dhbw.contactdb.Visit;
|
|
||||||
import de.dhbw.contactdb.Main;
|
import de.dhbw.contactdb.Main;
|
||||||
import de.dhbw.contactdb.Person;
|
import de.dhbw.contactdb.Person;
|
||||||
|
import de.dhbw.contactdb.Place;
|
||||||
|
import de.dhbw.contactdb.Visit;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -36,7 +36,7 @@ public class Parser {
|
|||||||
public static void parse(String filename) {
|
public static void parse(String filename) {
|
||||||
InputStream db = Main.class.getResourceAsStream(filename);
|
InputStream db = Main.class.getResourceAsStream(filename);
|
||||||
|
|
||||||
if(db == null) {
|
if (db == null) {
|
||||||
System.err.println("Cannot find database!");
|
System.err.println("Cannot find database!");
|
||||||
System.exit(-3);
|
System.exit(-3);
|
||||||
}
|
}
|
||||||
@ -48,7 +48,6 @@ public class Parser {
|
|||||||
if (line.toLowerCase().contains("start_date")) new_entity = db_new_entity.VISIT;
|
if (line.toLowerCase().contains("start_date")) new_entity = db_new_entity.VISIT;
|
||||||
else if (line.toLowerCase().contains("location_id")) new_entity = db_new_entity.PLACE;
|
else if (line.toLowerCase().contains("location_id")) new_entity = db_new_entity.PLACE;
|
||||||
else if (line.toLowerCase().contains("person_id")) new_entity = db_new_entity.PERSON;
|
else if (line.toLowerCase().contains("person_id")) new_entity = db_new_entity.PERSON;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ package de.dhbw.contactdb.utils;
|
|||||||
*
|
*
|
||||||
* @author florian kaiser
|
* @author florian kaiser
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @since 21.06.2021
|
|
||||||
* @see de.dhbw.contactdb.utils.Parser
|
* @see de.dhbw.contactdb.utils.Parser
|
||||||
|
* @since 21.06.2021
|
||||||
*/
|
*/
|
||||||
public enum db_new_entity {
|
public enum db_new_entity {
|
||||||
/**
|
/**
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package de.dhbw.unit;
|
package de.dhbw.unit;
|
||||||
|
|
||||||
import de.dhbw.contactdb.Main;
|
import de.dhbw.contactdb.Main;
|
||||||
import de.dhbw.contactdb.ProjektTester;
|
|
||||||
import de.dhbw.contactdb.utils.Parser;
|
import de.dhbw.contactdb.utils.Parser;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
public class ProjectTest {
|
public class ProjectTest {
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
@ -38,7 +38,7 @@ public class ProjectTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitors() {
|
public void testVisitors() {
|
||||||
assertEquals("Anna, Charlotte, Emilia, Leonie, Marie, Mia", Main.getVisitors("=3,\"2021-05-15T11:00:00\"").stream()
|
assertEquals("Anna, Charlotte, Emilia, Leonie, Marie, Mia", Main.getVisitors("3,\"2021-05-15t11:00:00\"").stream()
|
||||||
.map(visit -> visit.getPerson().getName())
|
.map(visit -> visit.getPerson().getName())
|
||||||
.sorted()
|
.sorted()
|
||||||
.distinct()
|
.distinct()
|
||||||
@ -49,4 +49,13 @@ public class ProjectTest {
|
|||||||
public void testImport() {
|
public void testImport() {
|
||||||
assertNotNull(Main.class.getResourceAsStream("/de/dhbw/contactdb/db/contacts2021.db"));
|
assertNotNull(Main.class.getResourceAsStream("/de/dhbw/contactdb/db/contacts2021.db"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSearchString() {
|
||||||
|
assertEquals("3,2021-05-15t11:00:00", Main.getSearchString("=3,\"2021-05-15T11:00:00\""));
|
||||||
|
assertEquals("3", Main.getSearchString("=3"));
|
||||||
|
assertEquals("3", Main.getSearchString("=\"3\""));
|
||||||
|
assertEquals("ila", Main.getSearchString("=ila"));
|
||||||
|
assertEquals("ila", Main.getSearchString("=\"ila\""));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user