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
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions src/main/java/ApartmentLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import java.util.Objects;

@XmlType(propOrder={"city", "street" , "house", "floor"})
public class ApartmentLocation {
private String city;
private String street;
private int house;
private int floor;

public ApartmentLocation () {
}

public ApartmentLocation(String city, String street, int house, int floor) {
this.city = city;
this.street = street;
this.house = house;
this.floor = floor;
}

public String getCity() {
return city;
}

@XmlAttribute
public void setCity(String city) {
this.city = city;
}

public String getStreet() {
return street;
}

@XmlAttribute
public void setStreet(String street) {
this.street = street;
}

public int getHouse() {
return house;
}

@XmlAttribute
public void setHouse(int house) {
this.house = house;
}

public int getFloor() {
return floor;
}

@XmlAttribute
public void setFloor(int floor) {
this.floor = floor;
}

@Override
public String toString() {
return "item" +
" city=" + city +
" street=" + street +
" house=" + house +
" floor=" + floor;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ApartmentLocation that = (ApartmentLocation) o;
return house == that.house &&
floor == that.floor &&
Objects.equals(city, that.city) &&
Objects.equals(street, that.street);
}

@Override
public int hashCode() {
return Objects.hash(city, street, house, floor);
}
}
58 changes: 58 additions & 0 deletions src/main/java/ApartmentLocationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.*;

public class ApartmentLocationHandler {
public static final int MAX_FLOOR = 5;
public static void showRepeats(List<ApartmentLocation> apartmentLocations) {
Map<ApartmentLocation, Integer> apartmentLocationsWithRepeats = new HashMap<>();
for (ApartmentLocation apartmentLocation : apartmentLocations) {
if (apartmentLocationsWithRepeats.containsKey(apartmentLocation)) {
apartmentLocationsWithRepeats.put(apartmentLocation, apartmentLocationsWithRepeats.get(apartmentLocation) + 1);
} else {
apartmentLocationsWithRepeats.put(apartmentLocation, 1);
}
}

for (Map.Entry<ApartmentLocation, Integer> pairOfLocationAndCount: apartmentLocationsWithRepeats.entrySet()) {
if (pairOfLocationAndCount.getValue() > 1) {
System.out.println(pairOfLocationAndCount.getKey() + " - repeats = " + pairOfLocationAndCount.getValue());
}
}
}

public static void showCitiesWithFloors(List<ApartmentLocation> apartmentLocations) {
Map<String, List<Integer>> citiesWithFloors = new HashMap<>();

for (ApartmentLocation apartmentLocation : apartmentLocations) {

String city = apartmentLocation.getCity();
int floor = apartmentLocation.getFloor();

if (citiesWithFloors.containsKey(city)) {
citiesWithFloors.get(city).set(
floor, citiesWithFloors.get(city).get(floor) + 1
);
} else {
List<Integer> listWithFloors = new ArrayList<>(MAX_FLOOR + 1);
for (int i = 0; i <= MAX_FLOOR; i++) {
listWithFloors.add(i, 0);
}
listWithFloors.set(floor, 1);
citiesWithFloors.put(city, listWithFloors);
}
}

for (Map.Entry<String, List<Integer>> cityWithFloors: citiesWithFloors.entrySet()) {
System.out.print(cityWithFloors.getKey() + " - ");
for (int i = 1; i <= MAX_FLOOR; i++) {
System.out.print(i + " floor - " + cityWithFloors.getValue().get(i) + " ");
}
System.out.println();
}
}

public static ArrayList<ApartmentLocation> removeDuplicates(List<ApartmentLocation> apartmentLocations) {
Set<ApartmentLocation> set = new HashSet<>(apartmentLocations);
return new ArrayList<>(set);
}

}
11 changes: 11 additions & 0 deletions src/main/java/Root.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@XmlType(name = "root")
@XmlRootElement
public class Root {
@XmlElement(name = "item")
public static List<ApartmentLocation> apartmentLocations = new ArrayList<>();

}
18 changes: 18 additions & 0 deletions src/main/java/XMLParser.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

public class XMLParser {

public static void parse(String filename) {
try {
File file = new File(filename);
JAXBContext context = JAXBContext.newInstance(ApartmentLocation.class, Root.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Root root = (Root) unmarshaller.unmarshal(file);
} catch (JAXBException exception) {
exception.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/XMLParserTestDrive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.io.IOException;

public class XMLParserTestDrive {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
XMLParser.parse("/home/gregory/Downloads/address.xml");
ApartmentLocationHandler.showRepeats(Root.apartmentLocations);
ApartmentLocationHandler.showCitiesWithFloors(ApartmentLocationHandler.removeDuplicates(Root.apartmentLocations));
long finishTime = System.currentTimeMillis();
System.out.println(finishTime - startTime + " ms");
}
}