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
14 changes: 14 additions & 0 deletions locations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<root>
<row>
<UniqueID>12</UniqueID>
<Name>CHURCH VIEW BP</Name>
<Latitude>51.14</Latitude>
<Longitude>0.2617</Longitude>
</row>
<row>
<UniqueID>14</UniqueID>
<Name>CLARENDON BP</Name>
<Latitude>51.6581</Latitude>
<Longitude>0.2511</Longitude>
</row>
</root>
1 change: 1 addition & 0 deletions randomized_locations.xml

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions src/main/java/cluster_manager/Cluster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cluster_manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Cluster {
private LatLng center;
private List<Location> items;

Cluster(List<Location> locations) {
items = new ArrayList<>(locations);
findCenter();
}

public LatLng getCenter() {
return center;
}

private void findCenter() {
double latitude = 0;
double longitude = 0;

for (Location item : items) {
latitude += item.getLatitude();
longitude += item.getLongitude();
}
center = new LatLng(latitude / items.size(), longitude / items.size());
}

public List<Location> getLocations() {
return Collections.unmodifiableList(items);
}

public void addItem(Location item) {
items.add(item);
findCenter();
}

static List<Cluster> manageLocations(List<Location> locations, int gridSize) {
List<Cluster> clusters = new ArrayList<>();
//... TO DO
return clusters;
}
}
43 changes: 43 additions & 0 deletions src/main/java/cluster_manager/ClusterManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cluster_manager;

import cluster_manager.xml_logic.LocationGenerator;
import cluster_manager.xml_logic.LocationParser;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ClusterManager {
private List<Location> locations;
private List<Cluster> clusters;
private final int gridSize = 50;

ClusterManager(File locationsxml) {
if (locationsxml != null) {
locations = LocationParser.parse(locationsxml);
clusters = Cluster.manageLocations(locations, gridSize);
} else {
System.out.println("File doesn't exist!");
}
}

ClusterManager(int count) {
this(LocationGenerator.generate(count));
}

List<Location> getLocations() {
return Collections.unmodifiableList(locations);
}

public List<Cluster> getInBounds(double minLat, double minLng, double maxLat, double maxLng) {
List<Cluster> clustersInBounds = new ArrayList<>();

for (Cluster current : clusters) {
if (current.getCenter().inBounds(minLat, minLng, maxLat, maxLng)) {
clustersInBounds.add(current);
}
}
return clustersInBounds;
}
}
23 changes: 23 additions & 0 deletions src/main/java/cluster_manager/LatLng.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cluster_manager;

class LatLng {
private final double latitude;
private final double longitude;

LatLng(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}

double getLatitude() {
return latitude;
}

double getLongitude() {
return longitude;
}

boolean inBounds(double minLat, double minLng, double maxLat, double maxLng) {
return (latitude >= minLat && latitude <= maxLat) && (longitude >= minLng && longitude <= maxLng);
}
}
28 changes: 28 additions & 0 deletions src/main/java/cluster_manager/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cluster_manager;

public class Location extends LatLng {
private final int id;
private final String name;

public Location(int id, String name, double latitude, double longitude) {
super(latitude, longitude);
this.id = id;
this.name = name;
}

int getId() {
return id;
}

String getName() {
return name;
}

LatLng getLocation() {
return this;
}

public String toString() {
return String.format("Location ID: %d - %s (lat: %f, lng: %f)", id, name, getLatitude(), getLongitude());
}
}
66 changes: 66 additions & 0 deletions src/main/java/cluster_manager/xml_logic/LocationGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cluster_manager.xml_logic;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class LocationGenerator {

public static File generate(int count) {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document locations = builder.newDocument();

Element rootElement = locations.createElement("root");
locations.appendChild(rootElement);

for (int i = 0; i < count; i++) {
Element row = locations.createElement("row");
rootElement.appendChild(row);

Element uniqueId = locations.createElement("UniqueID");
uniqueId.appendChild(locations.createTextNode(String.format("%d", i)));
row.appendChild(uniqueId);

Element name = locations.createElement("Name");
name.appendChild(locations.createTextNode(Randomizer.getRandomName()));
row.appendChild(name);

Element latitude = locations.createElement("Latitude");
latitude.appendChild(locations.createTextNode(Randomizer.getRandomLatitude()));
row.appendChild(latitude);

Element longitude = locations.createElement("Longitude");
longitude.appendChild(locations.createTextNode(Randomizer.getRandomLongitude()));
row.appendChild(longitude);
}

File randomizedLocations = new File("randomized_locations.xml");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(locations);
StreamResult result = new StreamResult(randomizedLocations);

transformer.transform(source, result);

return randomizedLocations;
} catch (ParserConfigurationException | TransformerException ex) {
ex.printStackTrace();
return null;
}
}


}
66 changes: 66 additions & 0 deletions src/main/java/cluster_manager/xml_logic/LocationParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cluster_manager.xml_logic;

import cluster_manager.Location;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class LocationParser {

public static List<Location> parse(File locationsXML) {
List<Location> items = new ArrayList<>();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(locationsXML);

NodeList locationList = document.getElementsByTagName("row");

for (int i = 0; i < locationList.getLength(); i++) {
Node location = locationList.item(i);
if (location.getNodeType() == Node.ELEMENT_NODE) {
NodeList parameters = location.getChildNodes();
int id = 0;
String name = null;
double latitude = 0;
double longitude = 0;

for (int j = 0; j < parameters.getLength(); j++) {
Node parameter = parameters.item(j);
if (parameter.getNodeType() == Node.ELEMENT_NODE) {
if (parameter.getNodeName().equals("UniqueID")) {
id = Integer.parseInt(parameter.getTextContent());
continue;
}
if (parameter.getNodeName().equals("Name")) {
name = parameter.getTextContent();
continue;
}
if (parameter.getNodeName().equals("Latitude")) {
latitude = Double.parseDouble(parameter.getTextContent());
continue;
}
if (parameter.getNodeName().equals("Longitude")) {
longitude = Double.parseDouble(parameter.getTextContent());
}
}
}
items.add(new Location(id, name, latitude, longitude));
}
}
} catch (ParserConfigurationException | SAXException | IOException ex) {
ex.printStackTrace();
}
return items;
}
}
34 changes: 34 additions & 0 deletions src/main/java/cluster_manager/xml_logic/Randomizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cluster_manager.xml_logic;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

class Randomizer {

static String getRandomName(int minLength, int maxLength) {
Random random = new Random();
int length = random.nextInt(maxLength - minLength) + minLength;
StringBuilder result = new StringBuilder();
for (int i = 0; i < length; i++) {
//[65 - 90] - latin alphabet in unicode(uppercase)
int code = random.nextInt(90 - 65) + 65;
char current = (char) code;
result.append(current);
}
return result.toString();
}

static String getRandomName() {
return getRandomName(5, 15);
}

static String getRandomLatitude() {
double random = ThreadLocalRandom.current().nextDouble(-90, 90);
return String.format("%.4f", random);
}

static String getRandomLongitude() {
double random = ThreadLocalRandom.current().nextDouble(-180, 180);
return String.format("%.4f", random);
}
}
27 changes: 27 additions & 0 deletions src/main/java/products/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package products;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Category {
private final CategoryType category;
private final List<Product> products = new ArrayList<>();

public CategoryType getCategoryType() {
return category;
}

public List<Product> getProducts() {
return Collections.unmodifiableList(products);
}

Category(CategoryType category) {
this.category = category;
}

void addProductToCategory(Product product) {
products.add(product);
}
}

5 changes: 5 additions & 0 deletions src/main/java/products/CategoryType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package products;

enum CategoryType {
Healthy, Sweet, Natural, Fruit, Meat, Children, Chocolate
}
Loading