Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package model;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Comparator;
- /**
- *
- * @author mga
- */
- public class City implements Comparable <City> {
- private final int id;
- private String cityName;
- private String country;
- private List<YearData> yearDataCollection;
- private static int lastIdAllocated = 0;
- static final char EOLN='\n';
- static final String QUOTE="\"";
- private ArrayList<Object> CityNameComparator;
- public City() {
- this.id = ++lastIdAllocated;
- this.cityName = "TBC";
- this.country = "TBC";
- this.yearDataCollection = new ArrayList<>();
- this.CityNameComparator=new ArrayList<>();
- }
- public City(String cityName, String country) {
- this.id = ++lastIdAllocated;
- this.cityName = cityName;
- this.country = country;
- this.yearDataCollection = new ArrayList<>();
- }
- public City(String cityName, String country, List<YearData> yearDataCollection) {
- this.id = ++lastIdAllocated;
- this.cityName = cityName;
- this.country = country;
- this.yearDataCollection = yearDataCollection;
- }
- public City(int id, String cityName, String country, List<YearData> yearDataCollection) {
- this.id = id;
- this.cityName = cityName;
- this.country = country;
- this.yearDataCollection = yearDataCollection;
- if (id > City.lastIdAllocated)
- City.lastIdAllocated = id;
- }
- public City (int id, String cityName, String country)
- {
- this.id=id;
- this.cityName=cityName;
- this.country=country;
- }
- /**
- * @return the id
- */
- public int getId() {
- return this.id;
- }
- public String getCityName()
- {
- return this.cityName;
- }
- public String getCountry(){
- return this.country;
- }
- public void setCityName (String cityName){
- this.cityName=cityName;
- }
- public void setCountry (String country)
- {
- this.country=country;
- }
- public List<YearData> getyearDataCollection() {
- return this.yearDataCollection;
- }
- public void setYearDataCollection (List<YearData> yearDataCollection)
- {
- this.yearDataCollection=yearDataCollection;
- }
- public boolean addYearData( YearData yearData ) {
- yearDataCollection.add( yearData ); //yearData is added to the end of the YearDataCollection list.
- System.out.println("contents:"+yearData);
- System.out.println("contents2:"+yearDataCollection);
- return true;
- }
- @Override
- public String toString() {
- String str="";
- int i=1;
- for (YearData yeardata:yearDataCollection) {
- str += Integer.toString(i++) + ": " +yeardata + "\n";
- }
- return "\nCity Id: " +Integer.toString(this.id)
- + " - City Name: " + cityName +
- " - Country: " + country + "\nData: " + yearDataCollection + "\n"+str;
- }
- public String toString(char delimiter) {
- String output= this.id+delimiter
- +QUOTE+this.cityName+QUOTE+delimiter
- +this.country+QUOTE+delimiter+Integer.toString(this.yearDataCollection.hashCode());
- for (YearData str: this.yearDataCollection)
- output+=delimiter+QUOTE+str+QUOTE;
- output+=EOLN;
- return output;
- }
- @Override
- public int hashCode()
- {
- return getId()*31+getCityName().hashCode()*31+ getCountry().hashCode()*31+getyearDataCollection().hashCode()*31;
- }
- @Override
- public boolean equals(Object o) {
- if (o instanceof City) {
- City c = (City)o;
- return c.getId() == getId() &&
- c.getCityName().equals(getCityName()) &&
- c.getCountry().equals(getCountry()) &&
- c.getyearDataCollection().equals(getyearDataCollection());
- } else {
- return false;
- }
- }
- @Override
- public int compareTo(City compareCity) {
- int Id = ((City) compareCity).getId();
- //ascending order
- return this.id - Id;
- //descending order
- //return - this.id;
- }
- // Methods required: getters (done), setters(done), add(done), hashCode (done), equals (done), compareTo (done), comparator (done)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement