diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/ApartmentLocation.java b/src/main/java/ApartmentLocation.java new file mode 100644 index 0000000..e6ec345 --- /dev/null +++ b/src/main/java/ApartmentLocation.java @@ -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); + } +} diff --git a/src/main/java/ApartmentLocationHandler.java b/src/main/java/ApartmentLocationHandler.java new file mode 100644 index 0000000..6407865 --- /dev/null +++ b/src/main/java/ApartmentLocationHandler.java @@ -0,0 +1,58 @@ +import java.util.*; + +public class ApartmentLocationHandler { + public static final int MAX_FLOOR = 5; + public static void showRepeats(List apartmentLocations) { + Map 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 pairOfLocationAndCount: apartmentLocationsWithRepeats.entrySet()) { + if (pairOfLocationAndCount.getValue() > 1) { + System.out.println(pairOfLocationAndCount.getKey() + " - repeats = " + pairOfLocationAndCount.getValue()); + } + } + } + + public static void showCitiesWithFloors(List apartmentLocations) { + Map> 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 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> 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 removeDuplicates(List apartmentLocations) { + Set set = new HashSet<>(apartmentLocations); + return new ArrayList<>(set); + } + +} \ No newline at end of file diff --git a/src/main/java/Root.java b/src/main/java/Root.java new file mode 100644 index 0000000..74b0ce4 --- /dev/null +++ b/src/main/java/Root.java @@ -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 apartmentLocations = new ArrayList<>(); + +} diff --git a/src/main/java/XMLParser.java b/src/main/java/XMLParser.java index 06d5bd2..4cecc8e 100644 --- a/src/main/java/XMLParser.java +++ b/src/main/java/XMLParser.java @@ -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(); + } + } } diff --git a/src/main/java/XMLParserTestDrive.java b/src/main/java/XMLParserTestDrive.java new file mode 100644 index 0000000..bbfeb65 --- /dev/null +++ b/src/main/java/XMLParserTestDrive.java @@ -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"); + } +} \ No newline at end of file