SHARE
TWEET

Untitled

a guest Nov 28th, 2018 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package model;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Comparator;
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author mga
  16.  */
  17.  
  18. public class City implements Comparable<City> {
  19.     private final int id;
  20.     private String cityName;
  21.     private String country;
  22.     private List<YearData> yearDataCollection;
  23.    
  24.      
  25.     private static int lastIdAllocated = 0;
  26.      
  27.     static final char EOLN='\n';      
  28.     static final String QUOTE="\"";
  29.    
  30.  
  31.     public City() {
  32.         this.id = ++lastIdAllocated;
  33.         this.cityName = "TBC";
  34.         this.country = "TBC";
  35.         this.yearDataCollection = new ArrayList<>();  
  36.        
  37.     }
  38.  
  39.     public City(String cityName, String country) {
  40.         this.id = ++lastIdAllocated;
  41.         this.cityName = cityName;
  42.         this.country = country;
  43.         this.yearDataCollection = new ArrayList<>();
  44.     }
  45.  
  46.     public City(String cityName, String country, List<YearData> yearDataCollection) {
  47.         this.id = ++lastIdAllocated;        
  48.         this.cityName = cityName;
  49.         this.country = country;
  50.         this.yearDataCollection = yearDataCollection;
  51.     }
  52.  
  53.     public City(int id, String cityName, String country, List<YearData> yearDataCollection) {
  54.         this.id = id;
  55.         this.cityName = cityName;
  56.         this.country = country;
  57.         this.yearDataCollection = yearDataCollection;
  58.         if (id > City.lastIdAllocated)
  59.             City.lastIdAllocated = id;            
  60.     }
  61.  
  62.    public City (int id, String cityName, String country)
  63.    {
  64.        this.id=id;
  65.        this.cityName=cityName;
  66.        this.country=country;
  67.        this.yearDataCollection = new ArrayList<>();
  68.    }
  69.    
  70.    
  71.    
  72.    
  73.     /**
  74.      * @return the id
  75.      */
  76.     public int getId() {
  77.         return this.id;
  78.     }    
  79.      public String getCityName()
  80.     {
  81.         return this.cityName;
  82.     }
  83.      
  84.      public void setCityName (String cityName){
  85.        this.cityName=cityName;
  86.    }
  87.      
  88.     public String getCountry(){
  89.         return this.country;
  90.     }
  91.      
  92.    
  93.    public void setCountry (String country)
  94.    {
  95.        this.country=country;
  96.    }
  97.    
  98.      public List<YearData> getyearDataCollection() {
  99.         return this.yearDataCollection;
  100.     }
  101.    
  102.    public void setYearDataCollection (List<YearData> yearDataCollection)
  103.            {
  104.                this.yearDataCollection=yearDataCollection;
  105.            }
  106.    
  107.    public void addYearData(YearData yearData){
  108.        this.yearDataCollection.add(yearData);
  109.    }
  110.    
  111.     @Override
  112.     public String toString() {
  113.      
  114.          String str="";
  115.         int i=1;
  116.         for (YearData yeardata:yearDataCollection) {
  117.             str += Integer.toString(i++) + ": " +yeardata + "\n";
  118.         }        
  119.        
  120.         return "\nCity Id: " +Integer.toString(this.id)+"\n"
  121.                 + " - \nCity Name: " + cityName +            
  122.                 " - \nCountry: " + country +" "+ "\nData: " +" "+ yearDataCollection + "\n"+str;
  123.          
  124.        
  125.     }
  126.      
  127.      
  128.      
  129.    public String toString(char delimiter) {
  130.          
  131.          
  132.         String output= this.id+delimiter
  133.                       +QUOTE+this.cityName+QUOTE+delimiter
  134.                 +this.country+QUOTE+delimiter+Integer.toString(this.yearDataCollection.hashCode());  
  135.         for (YearData str: this.yearDataCollection)
  136.          
  137.             output+=delimiter+QUOTE+str+QUOTE;
  138.          
  139.                 output+=EOLN;
  140.                
  141.                
  142.         return output;
  143.                  
  144.       }
  145.      
  146.  
  147.    
  148.    
  149.        
  150.     @Override
  151.     public int hashCode()
  152.     {
  153.         return getId()*31+getCityName().hashCode()*31+ getCountry().hashCode()*31+getyearDataCollection().hashCode()*31;
  154.     }
  155.          
  156.      @Override
  157.     public boolean equals(Object o) {
  158.         if (o instanceof City) {
  159.             City c = (City)o;
  160.             return  c.getId() == getId() &&
  161.                     c.getCityName().equals(getCityName()) &&
  162.                     c.getCountry().equals(getCountry()) &&
  163.                     c.getyearDataCollection().equals(getyearDataCollection());
  164.                    
  165.         } else {
  166.             return false;
  167.         }
  168.     }
  169.    
  170.     public static Comparator<City> CityNameComparator
  171.                       = new Comparator<City>() {
  172.  
  173.         @Override
  174.         public int compare(City cust1, City cust2) {
  175.  
  176.           String cityName1 = cust1.getCityName();
  177.           String cityName2 = cust2.getCityName();
  178.  
  179.           //ascending order
  180.           return cityName1.compareTo(cityName2);
  181.  
  182.           //descending order
  183.           //return custName2.compareTo(custName1);
  184.         }
  185.  
  186.     };    
  187.  
  188. public static Comparator<City> CountryComparator
  189.                       = new Comparator<City>() {
  190.  
  191.         @Override
  192.         public int compare(City country1, City country2) {
  193.  
  194.           String countryName1 = country1.getCountry();
  195.           String countryName2 = country2.getCountry();
  196.  
  197.           //ascending order
  198.           return countryName1.compareTo(countryName2);
  199.  
  200.           //descending order
  201.           //return custName2.compareTo(custName1);
  202.         }
  203.  
  204.     };      
  205.  
  206.  
  207.    
  208.       @Override
  209.     public int compareTo(City compareCity) {
  210.    
  211.         int cityId = ((City) compareCity).getId();
  212.        
  213.         //ascending order
  214.        
  215.                 return this.id-cityId;
  216.        
  217.         //descending order
  218.         //return customerId - this.custId;
  219.        
  220.     }
  221.  
  222. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top