Advertisement
irmantas_radavicius

Untitled

Mar 27th, 2022
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Route implements Comparable<Route> {
  5.     private String start;
  6.     private String end;
  7.     private int length;
  8.    
  9.     public Route(String start, String end, int length){
  10.         this.start = start;
  11.         this.end = end;
  12.         this.length = length;
  13.     }
  14.    
  15.     public static Route add(Route a, Route b){
  16.         if(a != null && b != null && (a.end.equalsIgnoreCase(b.start))){
  17.             return new Route(a.start, b.end, a.length + b.length);         
  18.         }
  19.         return null;
  20.     }
  21.    
  22.     public Route add(Route b){
  23.         return Route.add(this, b);
  24.     }  
  25.    
  26.     public int getLength(){
  27.         return length;
  28.     }
  29.    
  30.     public int compareTo(Route r){
  31.         return Integer.compare(this.length, r.length);
  32.     }
  33.    
  34.     public boolean hasCity(String city){
  35.         return city.equalsIgnoreCase(start) || city.equalsIgnoreCase(end);
  36.     }
  37.    
  38.     public boolean equals(Route route){
  39.         boolean eqStart = this.start.equalsIgnoreCase(route.start);
  40.         boolean eqEnd = this.end.equalsIgnoreCase(route.end);
  41.         boolean eqLength = this.length == route.length;
  42.         return (eqStart && eqEnd && eqLength);
  43.     }
  44.    
  45.     public String toString(){
  46.         return start + "-" + end + ", " + length + "km.";
  47.     }
  48. }
  49.  
  50. class RouteSorterByLength implements Comparator<Route> {
  51.     public int compare(Route r1, Route r2){
  52.         return Integer.compare(r1.getLength(), r2.getLength());
  53.     }
  54. }
  55.  
  56. class Utils {
  57.     public static ArrayList<Route> filterByCity(ArrayList<Route> data, String city){
  58.         ArrayList<Route> arr = new ArrayList<>();
  59.         for(int i = 0; i < data.size(); ++i){
  60.             if (data.get(i).hasCity(city)){
  61.                 arr.add(data.get(i));
  62.             }
  63.         }
  64.         return arr;
  65.     }
  66.    
  67.     public static void print(ArrayList<Route> data){       
  68.         for(int i = 0; i < data.size(); ++i){
  69.             System.out.println(data.get(i));
  70.         }      
  71.     }
  72.      
  73.    
  74.     public static boolean isSorted(ArrayList<Route> data){
  75.         for(int i = 0; i < data.size()-1; ++i){
  76.             if(data.get(i).getLength() < data.get(i+1).getLength()){               
  77.             } else {
  78.                 return false;
  79.             }
  80.         }
  81.         return true;
  82.     }
  83.  
  84.     public static void sort(ArrayList<Route> data){
  85.         while(!isSorted(data)){        
  86.             for(int i = 0; i < data.size()-1; ++i){
  87.                 if(data.get(i+1).getLength() < data.get(i).getLength()){
  88.                     Collections.swap(data, i, i+1);
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. public class Sandbox {  
  96.    
  97.     public static void main(String args[]) {       
  98.         try {
  99.             ArrayList<Route> arr = new ArrayList<>();
  100.             arr.add(new Route("Vilnius", "Kaunas", 100));
  101.             arr.add(new Route("Kaunas", "Klaipeda", 200));         
  102.             arr.add(new Route("Vilnius", "Klaipeda", 300));
  103.             arr.add(new Route("Kaunas", "Marijampole", 50));
  104.             arr.add(new Route("Klaipeda", "Siauliai", 150));
  105.  
  106.            
  107.             ArrayList<Route> hasKaunas = Utils.filterByCity(arr, "Kaunas");
  108.             Collections.sort(hasKaunas, new RouteSorterByLength());        
  109.            
  110.             Utils.print(hasKaunas);
  111.             System.out.println(Utils.isSorted(hasKaunas));
  112.            
  113.             ArrayList<Route> hasKlaipeda = Utils.filterByCity(arr, "Klaipeda");
  114.             Collections.sort(hasKlaipeda, new RouteSorterByLength());
  115.            
  116.             Utils.print(hasKlaipeda);
  117.             System.out.println(Utils.isSorted(hasKlaipeda));
  118.            
  119.            
  120.            
  121.            
  122.         } catch(Exception e){
  123.             e.printStackTrace();           
  124.             System.out.println("Unexpected error, sorry!");
  125.         }          
  126.     }  
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement