Improved functions, fixed functions, refactoring

This commit is contained in:
Administrator 2021-06-21 17:46:12 +02:00
parent bc8a12661f
commit 0466ca60f6
8 changed files with 534 additions and 205 deletions

View File

@ -1,63 +0,0 @@
package de.dhbw.contactdb;
import java.time.LocalDateTime;
public class Besuch {
private LocalDateTime start;
private LocalDateTime end;
private Person person;
private Ort ort;
public Besuch(String s) {
}
public Besuch(LocalDateTime start, LocalDateTime end, Person person, Ort ort) {
this.start = start;
this.end = end;
this.person = person;
this.ort = ort;
}
public LocalDateTime getStart() {
return start;
}
public void setStart(LocalDateTime start) {
this.start = start;
}
public LocalDateTime getEnd() {
return end;
}
public void setEnd(LocalDateTime end) {
this.end = end;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Ort getOrt() {
return ort;
}
public void setOrt(Ort ort) {
this.ort = ort;
}
@Override
public String toString() {
return "Besuch{" +
"start=" + start +
", end=" + end +
", person=" + person +
", ort=" + ort +
'}';
}
}

View File

@ -4,103 +4,218 @@ import de.dhbw.contactdb.utils.Parser;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Main {
public static HashMap<Integer, Person> persons = new HashMap<>();
public static HashSet<Besuch> besuche = new HashSet<>();
public static HashMap<Integer, Ort> orte = new HashMap<>();
/**
* Main class
*
* @author florian kaiser
* @version 1.0
* @since 21.06.2021
*/
public class Main {
/**
* Contains all persons from contacts2021.db
*
* @see de.dhbw.contactdb.utils.Parser
*/
public static HashMap<Integer, Person> persons = new HashMap<>();
/**
* Contains all places from contacts2021.db
*
* @see de.dhbw.contactdb.utils.Parser
*/
public static HashMap<Integer, Place> places = new HashMap<>();
/**
* Contains all visits from contacts2021.db
*
* @see de.dhbw.contactdb.utils.Parser
*/
public static HashSet<Visit> visits = new HashSet<>();
/**
* Main function
*
* @param args String that contains program arguments
*/
public static void main(String[] args) { public static void main(String[] args) {
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")) { if (args[0].contains("--personensuche")) {
String s = args[0].split("=")[1].replaceAll("\"", "").toLowerCase(); searchForPerson(getSearchString(args[0]));
System.out.println("Personensuche: " + s);
persons.forEach((key, value) -> {
if (value.getName().toLowerCase().contains(s)) {
System.out.println(value);
}
});
} else if (args[0].contains("--ortssuche")) { } else if (args[0].contains("--ortssuche")) {
String s = args[0].split("=")[1].replaceAll("\"", "").toLowerCase(); searchForPlace(getSearchString(args[0]));
} else if (args[0].contains("--kontaktpersonen")) {
int id = Integer.parseInt(getSearchString(args[0]));
System.out.println("Ortssuche: " + s); 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 {
argument_error();
}
}
orte.forEach((key, value) -> {
if (value.getName().toLowerCase().contains(s)) { /**
* Prints error message
*
* @author florian kaiser
*/
private static void argument_error() {
System.out.println("Invalid parameter! \nUsage [Option]=\"[Value]\"\n--personensuche\n--ortssuche\n--kontaktpersonen\n--besucher");
}
/**
* Searches the HashMap places for a specific string
* @param search String that contains search value
*
* @author florian kaiser
*/
public static void searchForPlace(String search) {
places.forEach((key, value) -> {
if (value.getName().toLowerCase().contains(search)) {
System.out.println(value); System.out.println(value);
} }
}); });
} else if (args[0].contains("--kontaktpersonen")) {
int id = Integer.parseInt(args[0].split("=")[1].replaceAll("\"", ""));
System.out.println("Kontaktpersonen: " + id);
List<Person> kontaktpersonen = new ArrayList<>();
besuche.forEach(value -> {
if (value.getPerson().getId() == id) {
if(value.getOrt().isIndoor()) {
besuche.forEach(besuch -> {
if(besuch.getOrt().getId() == value.getOrt().getId()) {
if(!(besuch.getStart().isBefore(value.getStart()) && besuch.getEnd().isBefore(value.getStart())) && !besuch.getStart().isAfter(value.getEnd())) {
kontaktpersonen.add(besuch.getPerson());
} }
/**
* Searches the HashMap persons for a specific string
* @param search String that contains search value
*
* @author florian kaiser
*/
public static void searchForPerson(String search) {
persons.forEach((key, value) -> {
if (value.getName().toLowerCase().contains(search)) {
System.out.println(value);
} }
}); });
} }
/**
* Lists all persons that were at the same place at the same time as a specific person.
* Only if the place is indoor
*
* @param id Contains the id of a person
* @return List that contains visits
*
* @author florian kaiser
*/
public static List<Visit> getContactPersons(int id) {
List<Visit> places_where_id_was_indoor;
places_where_id_was_indoor = visits.stream()
.filter(visit -> visit.getPlace().isIndoor())
.filter(visit -> visit.getPerson().getId() == id)
.toList();
List<Visit> contacts_of_id = new ArrayList<>();
for (Visit filtered_b : places_where_id_was_indoor) {
for (Visit b : visits) {
if (b.isOverlapping(filtered_b.getStart(), filtered_b.getEnd()) && filtered_b.getPlace().getId() == b.getPlace().getId()) {
contacts_of_id.add(b);
}
}
} }
});
System.out.println(kontaktpersonen.stream() return contacts_of_id;
.map(Person::getName) }
.sorted()
.collect(Collectors.joining(", ")));
} else if (args[0].contains("--besucher")) {
/**
* Lists all Persons that were at the same place (id) at the same time.
* If the place is indoor, the contact persons of each person is added to the result too.
*
* @param s contains program argument (id of place and date string, separated by comma)
* @return List that contains visits
*
* @author florian kaiser
*/
public static List<Visit> getVisitors(String s) {
int id = Integer.parseInt(s.split("=")[1].split(",")[0].replaceAll("\"", ""));
LocalDateTime d;
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
int id = Integer.parseInt(args[0].split("=")[1].split(",")[0].replaceAll("\"", "")); d = LocalDateTime.parse(s.split("=")[1].split(",")[1].replaceAll("\"", ""), formatter);
LocalDateTime d = LocalDateTime.parse(args[0].split("=")[1].split(",")[1].replaceAll("\"", ""), formatter); } catch (DateTimeParseException e1) {
try {
System.out.println("Besucher: " + id + " " + d); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
d = LocalDateTime.parse(s.split("=")[1].split(",")[1].replaceAll("\"", ""), formatter);
// TODO } catch (DateTimeParseException e2) {
List<Person> kontaktpersonen = new ArrayList<>(); argument_error();
besuche.forEach(value -> { System.exit(-2);
if (value.getOrt().getId() == id) { throw e1;
if(value.getOrt().isIndoor()) {
besuche.forEach(besuch -> {
if(!(besuch.getStart().isBefore(value.getStart()) && besuch.getEnd().isBefore(value.getStart())) && !besuch.getStart().isAfter(value.getEnd())) {
kontaktpersonen.add(besuch.getPerson());
} }
}); }
LocalDateTime finalD = d;
List<Visit> place_date_visits = visits.stream()
.filter(visit -> visit.getPlace().getId() == id)
.filter(visit -> visit.isInTimePeriod(finalD))
.toList();
List<Visit> all = new ArrayList<>();
for (Visit visit : place_date_visits) {
if (visit.getPlace().isIndoor()) {
all.addAll(getContactPersons(visit.getPerson().getId()));
} else { } else {
// Outdoor all.add(visit);
// TODO
} }
} }
});
System.out.println(kontaktpersonen.stream() return all;
.map(Person::getName) }
.sorted()
.collect(Collectors.joining(", ")));
} else {
System.out.println("Invalid paramater! \nUsage [Option]=\"[Value]\"\n--personensuche\n--ortsuche\n--kontaktpersonen\n--besucher"); /**
* Extracts string from String.
* Exits program if string is empty, blank or not available
*
* @param s Contains program argument
* @return extracted string
*/
public static String getSearchString(String s) {
try {
s = s.split("=")[1].replaceAll("\"", "").toLowerCase();
} catch (ArrayIndexOutOfBoundsException e) {
argument_error();
System.exit(-1);
} }
} else {
System.out.println("Invalid paramater! \nUsage [Option]=\"[Value]\"\n--personensuche\n--ortsuche\n--kontaktpersonen\n--besucher"); if (s.isEmpty() || s.isBlank()) {
argument_error();
System.exit(-1);
} }
// persons.forEach((key, value) -> System.out.println(key + " " + value));
// besuche.forEach(System.out::println); return s;
// orte.forEach((key, value) -> System.out.println(key + " " + value));
} }
} }

View File

@ -1,50 +0,0 @@
package de.dhbw.contactdb;
public class Ort {
private int id;
private String name;
private boolean indoor;
public Ort(String s) {
}
public Ort(int id, String name, boolean indoor) {
this.id = id;
this.name = name;
this.indoor = indoor;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isIndoor() {
return indoor;
}
public void setIndoor(boolean indoor) {
this.indoor = indoor;
}
@Override
public String toString() {
return "Ort{" +
"id=" + id +
", name='" + name + '\'' +
", indoor=" + indoor +
'}';
}
}

View File

@ -1,34 +1,69 @@
package de.dhbw.contactdb; package de.dhbw.contactdb;
/**
* Person
*
* @author florian kaiser
* @version 1.0
* @since 21.06.2021
*/
public class Person { public class Person {
/**
* ID of person
*/
private int id; private int id;
/**
* Name of person
*/
private String name; private String name;
public Person(String s) {
}
/**
* Constructor
* @param id ID of person
* @param name Name of person
*/
public Person(int id, String name) { public Person(int id, String name) {
this.id = id; this.id = id;
this.name = name; this.name = name;
} }
/**
* @return {@link #id}
*/
public int getId() { public int getId() {
return id; return id;
} }
/**
* @param id {@link #id}
*/
public void setId(int id) { public void setId(int id) {
this.id = id; this.id = id;
} }
/**
* @return {@link #name}
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* @param name {@link #name}
*/
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@Override @Override
public String toString() { public String toString() {
return "Person{" + return "Person{" +

View File

@ -0,0 +1,90 @@
package de.dhbw.contactdb;
/**
* Main class
*
* @author florian kaiser
* @version 1.0
* @since 21.06.2021
*/
public class Place {
/**
* ID of place
*/
private int id;
/**
* name of place
*/
private String name;
/**
* is place indoor
*/
private final boolean indoor;
/**
* Constructor
* @param id ID of place
* @param name Name of place
* @param indoor is place indoor
*/
public Place(int id, String name, boolean indoor) {
this.id = id;
this.name = name;
this.indoor = indoor;
}
/**
* @return {@link #id}
*/
public int getId() {
return id;
}
/**
* @param id {@link #id}
*/
public void setId(int id) {
this.id = id;
}
/**
* @return {@link #name}
*/
public String getName() {
return name;
}
/**
* @param name {@link #name}
*/
public void setName(String name) {
this.name = name;
}
/**
* @return {@link #indoor}
*/
public boolean isIndoor() {
return indoor;
}
@Override
public String toString() {
return "Ort{" +
"id=" + id +
", name='" + name + '\'' +
", indoor=" + indoor +
'}';
}
}

View File

@ -0,0 +1,138 @@
package de.dhbw.contactdb;
import java.time.LocalDateTime;
/**
* Main class
*
* @author florian kaiser
* @version 1.0
* @since 21.06.2021
*/
public class Visit {
/**
* start date of visit
*/
private LocalDateTime start;
/**
* end date of visit
*/
private LocalDateTime end;
/**
* person who visits
*/
private Person person;
/**
* place where person is
*/
private final Place place;
/**
* Constructor
* @param start start date of visit
* @param end end date of visit
* @param person person who visits
* @param place place where person is
*/
public Visit(LocalDateTime start, LocalDateTime end, Person person, Place place) {
this.start = start;
this.end = end;
this.person = person;
this.place = place;
}
/**
* @return {@link #start}
*/
public LocalDateTime getStart() {
return start;
}
/**
* @param start {@link #start}
*/
public void setStart(LocalDateTime start) {
this.start = start;
}
/**
* @return {@link #end}
*/
public LocalDateTime getEnd() {
return end;
}
/**
* @param end {@link #end}
*/
public void setEnd(LocalDateTime end) {
this.end = end;
}
/**
* @return {@link #person}
*/
public Person getPerson() {
return person;
}
/**
* @param person {@link #person}
*/
public void setPerson(Person person) {
this.person = person;
}
/**
* @return {@link #place}
*/
public Place getPlace() {
return place;
}
/**
* @param start Start of timespan
* @param end End of timespan
*
* @return boolean if timespan between start and end is overlapping timespan between this visits start and end
*/
public boolean isOverlapping(LocalDateTime start, LocalDateTime end) {
return ((null == end) || this.start.isBefore(end)) && ((null == this.end) || start.isBefore(this.end));
}
/**
* @param inputTime time
*
* @return boolean if time is between this visits start and end
*/
public boolean isInTimePeriod(LocalDateTime inputTime) {
return this.start.isBefore(inputTime) && this.end.isAfter(inputTime) || this.end.equals(inputTime) || this.start.equals(inputTime);
}
@Override
public String toString() {
return "Besuch{" +
"start=" + start +
", end=" + end +
", person=" + person +
", ort=" + place +
'}';
}
}

View File

@ -1,8 +1,8 @@
package de.dhbw.contactdb.utils; package de.dhbw.contactdb.utils;
import de.dhbw.contactdb.Besuch; import de.dhbw.contactdb.Place;
import de.dhbw.contactdb.Visit;
import de.dhbw.contactdb.Main; import de.dhbw.contactdb.Main;
import de.dhbw.contactdb.Ort;
import de.dhbw.contactdb.Person; import de.dhbw.contactdb.Person;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -11,41 +11,81 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
/**
* Parser for parsing contacts2021.db
*
* @author florian kaiser
* @version 1.0
* @since 21.06.2021
*/
public class Parser { public class Parser {
/**
* Saves type of current line in database
*/
private static db_new_entity new_entity = null; private static db_new_entity new_entity = null;
/**
* Parses contacts.db, creates objects of content and saves these objects in HashMaps
*
* @param filename Filename and path of contacts.db
*/
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(-1); System.exit(-3);
} }
try (BufferedReader bf = new BufferedReader(new InputStreamReader(db))) { try (BufferedReader bf = new BufferedReader(new InputStreamReader(db))) {
String line; String line;
while ((line = bf.readLine()) != null) { while ((line = bf.readLine()) != null) {
if (line.toLowerCase().contains("new_entity")) { if (line.toLowerCase().contains("new_entity")) {
if (line.toLowerCase().contains("start_date")) new_entity = db_new_entity.BESUCH; if (line.toLowerCase().contains("start_date")) new_entity = db_new_entity.VISIT;
else if (line.toLowerCase().contains("location_id")) new_entity = db_new_entity.ORT; 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;
} }
if (new_entity == db_new_entity.BESUCH) { if (new_entity == db_new_entity.VISIT) {
LocalDateTime start = null;
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
LocalDateTime start = LocalDateTime.parse(line.split(",")[0].replaceAll("\"", ""), formatter); start = LocalDateTime.parse(line.split(",")[0].replaceAll("\"", ""), formatter);
LocalDateTime end = LocalDateTime.parse(line.split(",")[1].replaceAll("\"", ""), formatter); } catch (DateTimeParseException e1) {
Person p = Main.persons.get(Integer.parseInt(line.split(",")[2].replaceAll("\"", ""))); try {
Ort o = Main.orte.get(Integer.parseInt(line.split(",")[3].replaceAll("\"", ""))); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
Main.besuche.add(new Besuch(start, end, p, o)); start = LocalDateTime.parse(line.split(",")[0].replaceAll("\"", ""), formatter);
} else if (new_entity == db_new_entity.ORT) { } catch (DateTimeParseException e2) {
System.exit(-1);
}
}
LocalDateTime end = null;
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
end = LocalDateTime.parse(line.split(",")[1].replaceAll("\"", ""), formatter);
} catch (DateTimeParseException e1) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
end = LocalDateTime.parse(line.split(",")[1].replaceAll("\"", ""), formatter);
} catch (DateTimeParseException e2) {
System.exit(-1);
}
}
Person person = Main.persons.get(Integer.parseInt(line.split(",")[2].replaceAll("\"", "")));
Place place = Main.places.get(Integer.parseInt(line.split(",")[3].replaceAll("\"", "")));
Main.visits.add(new Visit(start, end, person, place));
} else if (new_entity == db_new_entity.PLACE) {
int id = Integer.parseInt(line.split(",")[0].replaceAll("\"", "")); int id = Integer.parseInt(line.split(",")[0].replaceAll("\"", ""));
String name = line.split(",")[1].replaceAll("\"", ""); String name = line.split(",")[1].replaceAll("\"", "");
boolean indoor = line.split(",")[2].replaceAll("\"", "").equals("in_door"); boolean indoor = line.split(",")[2].replaceAll("\"", "").equals("in_door");
Main.orte.put(id, new Ort(id, name, indoor)); Main.places.put(id, new Place(id, name, indoor));
} else if (new_entity == db_new_entity.PERSON) { } else if (new_entity == db_new_entity.PERSON) {
int id = Integer.parseInt(line.split(",")[0].replaceAll("\"", "")); int id = Integer.parseInt(line.split(",")[0].replaceAll("\"", ""));
String name = line.split(",")[1].replaceAll("\"", ""); String name = line.split(",")[1].replaceAll("\"", "");

View File

@ -1,5 +1,29 @@
package de.dhbw.contactdb.utils; package de.dhbw.contactdb.utils;
/**
* Used by Parser
*
* @author florian kaiser
* @version 1.0
* @since 21.06.2021
* @see de.dhbw.contactdb.utils.Parser
*/
public enum db_new_entity { public enum db_new_entity {
PERSON, ORT, BESUCH /**
* Person
*/
PERSON,
/**
* Place
*/
PLACE,
/**
* Visit
*/
VISIT
} }